mirror of https://github.com/bjornbytes/lovr.git
Rework shape internals;
This commit is contained in:
parent
78f0c2d947
commit
a6df0c58c7
|
@ -283,48 +283,50 @@ void lovrGraphicsGetDimensions(int* width, int* height) {
|
||||||
glfwGetFramebufferSize(window, width, height);
|
glfwGetFramebufferSize(window, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsSetShapeData(float* vertexData, int vertexLength, unsigned int* indexData, int indexLength) {
|
void lovrGraphicsDrawLinedShape(GLenum mode) {
|
||||||
vec_clear(&state.shapeData);
|
vec_float_t* vertices = &state.shapeData;
|
||||||
vec_pusharr(&state.shapeData, vertexData, vertexLength);
|
vec_uint_t* indices = &state.shapeIndices;
|
||||||
|
|
||||||
|
lovrGraphicsPrepare();
|
||||||
glBindVertexArray(state.shapeArray);
|
glBindVertexArray(state.shapeArray);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, vertexLength * sizeof(float), vertexData, GL_STREAM_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, vertices->length * sizeof(float), vertices->data, GL_STREAM_DRAW);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||||
|
|
||||||
vec_clear(&state.shapeIndices);
|
if (indices->length > 0) {
|
||||||
if (indexData) {
|
|
||||||
vec_pusharr(&state.shapeIndices, indexData, indexLength);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndexBuffer);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndexBuffer);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndices.length * sizeof(unsigned int), state.shapeIndices.data, GL_STREAM_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices->length * sizeof(unsigned int), indices->data, GL_STREAM_DRAW);
|
||||||
|
glDrawElements(mode, indices->length, GL_UNSIGNED_INT, NULL);
|
||||||
|
} else {
|
||||||
|
glDrawArrays(mode, 0, vertices->length / 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsDrawShape(GLenum mode) {
|
void lovrGraphicsDrawFilledShape() {
|
||||||
int usingIbo = state.shapeIndices.length > 0;
|
vec_float_t* vertices = &state.shapeData;
|
||||||
|
int stride = 6;
|
||||||
|
|
||||||
lovrGraphicsPrepare();
|
lovrGraphicsPrepare();
|
||||||
glBindVertexArray(state.shapeArray);
|
glBindVertexArray(state.shapeArray);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertices->length * sizeof(float), vertices->data, GL_STREAM_DRAW);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void*) 0);
|
||||||
if (usingIbo) {
|
glEnableVertexAttribArray(1);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndexBuffer);
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void*) (3 * sizeof(float)));
|
||||||
glDrawElements(mode, state.shapeIndices.length, GL_UNSIGNED_INT, NULL);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices->length / 6);
|
||||||
} else {
|
glBindVertexArray(0);
|
||||||
glDrawArrays(mode, 0, state.shapeData.length / 3);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsLine(float* points, int count) {
|
void lovrGraphicsLine(float* points, int count) {
|
||||||
lovrGraphicsSetShapeData(points, count, NULL, 0);
|
vec_clear(&state.shapeIndices);
|
||||||
lovrGraphicsDrawShape(GL_LINE_STRIP);
|
vec_clear(&state.shapeData);
|
||||||
|
vec_pusharr(&state.shapeData, points, count);
|
||||||
|
lovrGraphicsDrawLinedShape(GL_LINE_STRIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz) {
|
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz) {
|
||||||
float points[] = {
|
|
||||||
-.5, .5, 0,
|
|
||||||
.5, .5, 0,
|
|
||||||
.5, -.5, 0,
|
|
||||||
-.5, -.5, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
// Normalize the normal vector
|
// Normalize the normal vector
|
||||||
float len = sqrt(nx * nx + ny * ny + nz + nz);
|
float len = sqrt(nx * nx + ny * ny + nz + nz);
|
||||||
|
@ -351,32 +353,34 @@ void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, flo
|
||||||
lovrGraphicsTransform(transform);
|
lovrGraphicsTransform(transform);
|
||||||
|
|
||||||
if (mode == DRAW_MODE_LINE) {
|
if (mode == DRAW_MODE_LINE) {
|
||||||
lovrGraphicsSetShapeData(points, 12, NULL, 0);
|
float points[] = {
|
||||||
lovrGraphicsDrawShape(GL_LINE_LOOP);
|
-.5, .5, 0,
|
||||||
|
.5, .5, 0,
|
||||||
|
.5, -.5, 0,
|
||||||
|
-.5, -.5, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
vec_clear(&state.shapeIndices);
|
||||||
|
vec_clear(&state.shapeData);
|
||||||
|
vec_pusharr(&state.shapeData, points, 12);
|
||||||
|
lovrGraphicsDrawLinedShape(GL_LINE_LOOP);
|
||||||
} else if (mode == DRAW_MODE_FILL) {
|
} else if (mode == DRAW_MODE_FILL) {
|
||||||
unsigned int indices[] = { 0, 3, 1, 2 };
|
float data[] = {
|
||||||
lovrGraphicsSetShapeData(points, 12, indices, 4);
|
-.5, .5, 0, 0, 0, -1,
|
||||||
lovrGraphicsDrawShape(GL_TRIANGLE_STRIP);
|
-.5, -.5, 0, 0, 0, -1,
|
||||||
|
.5, .5, 0, 0, 0, -1,
|
||||||
|
.5, -.5, 0, 0, 0, -1
|
||||||
|
};
|
||||||
|
|
||||||
|
vec_clear(&state.shapeData);
|
||||||
|
vec_pusharr(&state.shapeData, data, 24);
|
||||||
|
lovrGraphicsDrawFilledShape();
|
||||||
}
|
}
|
||||||
|
|
||||||
lovrGraphicsPop();
|
lovrGraphicsPop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, float angle, float axisX, float axisY, float axisZ) {
|
void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, float angle, float axisX, float axisY, float axisZ) {
|
||||||
float points[] = {
|
|
||||||
// Front
|
|
||||||
-.5, .5, -.5,
|
|
||||||
.5, .5, -.5,
|
|
||||||
.5, -.5, -.5,
|
|
||||||
-.5, -.5, -.5,
|
|
||||||
|
|
||||||
// Back
|
|
||||||
-.5, .5, .5,
|
|
||||||
.5, .5, .5,
|
|
||||||
.5, -.5, .5,
|
|
||||||
-.5, -.5, .5
|
|
||||||
};
|
|
||||||
|
|
||||||
float cos2 = cos(angle / 2);
|
float cos2 = cos(angle / 2);
|
||||||
float sin2 = sin(angle / 2);
|
float sin2 = sin(angle / 2);
|
||||||
float rw = cos2;
|
float rw = cos2;
|
||||||
|
@ -392,27 +396,77 @@ void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, floa
|
||||||
lovrGraphicsTransform(transform);
|
lovrGraphicsTransform(transform);
|
||||||
|
|
||||||
if (mode == DRAW_MODE_LINE) {
|
if (mode == DRAW_MODE_LINE) {
|
||||||
|
float points[] = {
|
||||||
|
// Front
|
||||||
|
-.5, .5, -.5,
|
||||||
|
.5, .5, -.5,
|
||||||
|
.5, -.5, -.5,
|
||||||
|
-.5, -.5, -.5,
|
||||||
|
|
||||||
|
// Back
|
||||||
|
-.5, .5, .5,
|
||||||
|
.5, .5, .5,
|
||||||
|
.5, -.5, .5,
|
||||||
|
-.5, -.5, .5
|
||||||
|
};
|
||||||
|
|
||||||
unsigned int indices[] = {
|
unsigned int indices[] = {
|
||||||
0, 1, 1, 2, 2, 3, 3, 0, // Front
|
0, 1, 1, 2, 2, 3, 3, 0, // Front
|
||||||
4, 5, 5, 6, 6, 7, 7, 4, // Back
|
4, 5, 5, 6, 6, 7, 7, 4, // Back
|
||||||
0, 4, 1, 5, 2, 6, 3, 7 // Connections
|
0, 4, 1, 5, 2, 6, 3, 7 // Connections
|
||||||
};
|
};
|
||||||
|
|
||||||
lovrGraphicsSetShapeData(points, 24, indices, 24);
|
vec_clear(&state.shapeIndices);
|
||||||
lovrGraphicsDrawShape(GL_LINES);
|
vec_pusharr(&state.shapeIndices, indices, 24);
|
||||||
|
vec_clear(&state.shapeData);
|
||||||
|
vec_pusharr(&state.shapeData, points, 24);
|
||||||
|
lovrGraphicsDrawLinedShape(GL_LINES);
|
||||||
} else {
|
} else {
|
||||||
unsigned int indices[] = {
|
float data[] = {
|
||||||
3, 2, 0, 1, // Front
|
// Front
|
||||||
1, 2, 5, 6, // Right
|
-.5, -.5, -.5, 0, 0, -1,
|
||||||
6, 7, 5, 4, // Back
|
.5, -.5, -.5, 0, 0, -1,
|
||||||
4, 7, 0, 3, // Left
|
-.5, .5, -.5, 0, 0, -1,
|
||||||
3, 7, 2, 6, // Bottom
|
.5, .5, -.5, 0, 0, -1,
|
||||||
6, 0,
|
|
||||||
0, 1, 4, 5 // Top
|
// Right
|
||||||
|
.5, .5, -.5, 1, 0, 0,
|
||||||
|
.5, -.5, -.5, 1, 0, 0,
|
||||||
|
.5, .5, .5, 1, 0, 0,
|
||||||
|
.5, -.5, .5, 1, 0, 0,
|
||||||
|
|
||||||
|
// Back
|
||||||
|
.5, -.5, .5, 0, 0, 1,
|
||||||
|
-.5, -.5, .5, 0, 0, 1,
|
||||||
|
.5, .5, .5, 0, 0, 1,
|
||||||
|
-.5, .5, .5, 0, 0, 1,
|
||||||
|
|
||||||
|
// Left
|
||||||
|
-.5, .5, .5, -1, 0, 0,
|
||||||
|
-.5, -.5, .5, -1, 0, 0,
|
||||||
|
-.5, .5, -.5, -1, 0, 0,
|
||||||
|
-.5, -.5, -.5, -1, 0, 0,
|
||||||
|
|
||||||
|
// Bottom
|
||||||
|
-.5, -.5, -.5, 0, -1, 0,
|
||||||
|
-.5, -.5, .5, 0, -1, 0,
|
||||||
|
.5, -.5, -.5, 0, -1, 0,
|
||||||
|
.5, -.5, .5, 0, -1, 0,
|
||||||
|
|
||||||
|
// Adjust
|
||||||
|
.5, -.5, .5, 0, 1, 0,
|
||||||
|
-.5, .5, -.5, 0, 1, 0,
|
||||||
|
|
||||||
|
// Top
|
||||||
|
-.5, .5, -.5, 0, 1, 0,
|
||||||
|
.5, .5, -.5, 0, 1, 0,
|
||||||
|
-.5, .5, .5, 0, 1, 0,
|
||||||
|
.5, .5, .5, 0, 1, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
lovrGraphicsSetShapeData(points, 24, indices, 26);
|
vec_clear(&state.shapeData);
|
||||||
lovrGraphicsDrawShape(GL_TRIANGLE_STRIP);
|
vec_pusharr(&state.shapeData, data, 156);
|
||||||
|
lovrGraphicsDrawFilledShape();
|
||||||
}
|
}
|
||||||
|
|
||||||
lovrGraphicsPop();
|
lovrGraphicsPop();
|
||||||
|
|
|
@ -87,8 +87,8 @@ void lovrGraphicsRotate(float w, float x, float y, float z);
|
||||||
void lovrGraphicsScale(float x, float y, float z);
|
void lovrGraphicsScale(float x, float y, float z);
|
||||||
void lovrGraphicsTransform(mat4 transform);
|
void lovrGraphicsTransform(mat4 transform);
|
||||||
void lovrGraphicsGetDimensions(int* width, int* height);
|
void lovrGraphicsGetDimensions(int* width, int* height);
|
||||||
void lovrGraphicsSetShapeData(float* data, int count, unsigned int* indices, int indexCount);
|
void lovrGraphicsDrawLinedShape(GLenum mode);
|
||||||
void lovrGraphicsDrawShape();
|
void lovrGraphicsDrawFilledShape();
|
||||||
void lovrGraphicsLine(float* points, int count);
|
void lovrGraphicsLine(float* points, int count);
|
||||||
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz);
|
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz);
|
||||||
void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, float angle, float axisX, float axisY, float axisZ);
|
void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, float angle, float axisX, float axisY, float axisZ);
|
||||||
|
|
|
@ -337,8 +337,7 @@ int l_lovrGraphicsLine(lua_State* L) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lovrGraphicsSetShapeData(points.data, count, NULL, 0);
|
lovrGraphicsLine(points.data, count);
|
||||||
lovrGraphicsDrawShape(DRAW_MODE_LINE);
|
|
||||||
vec_deinit(&points);
|
vec_deinit(&points);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue