Can now set index buffers and use them ... and draw a 2-size-unit cube
parent
e2671e8de4
commit
2550bb5407
|
@ -53,6 +53,7 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
|
|||
VertexArray gVertexArray;
|
||||
VertexArrayMesh gVertexArrayMesh;
|
||||
VertexArrayMesh gXZPlaneMesh;
|
||||
VertexArrayMesh gUnitCubeMesh;
|
||||
|
||||
//
|
||||
// Module
|
||||
|
@ -237,6 +238,56 @@ void Renderer::Initialize(int width, int height) {
|
|||
}
|
||||
gXZPlaneMesh.SetData(plane_data.data(), plane_data.size());
|
||||
|
||||
// Unit Cube
|
||||
gUnitCubeMesh.Initialize(gVertexArray, 4 * 6);
|
||||
VertexArray::VertexData unit_cube_data[] = {
|
||||
// front: +x
|
||||
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
|
||||
// back: -x
|
||||
{-1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{-1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
{-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 },
|
||||
|
||||
// side: +z
|
||||
{-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
|
||||
// back side: -z
|
||||
{-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
{1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 255, 255 },
|
||||
|
||||
// top: +y
|
||||
{1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
|
||||
// bottom: -y
|
||||
{1.0f, -1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
{-1.0f, -1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255 },
|
||||
};
|
||||
gUnitCubeMesh.SetData(unit_cube_data, 4 * 6);
|
||||
GLuint unit_cube_index_data[] = {
|
||||
0, 1, 2, 2, 3, 0,
|
||||
4, 7, 6, 6, 5, 4,
|
||||
8, 9, 10, 10, 11, 8,
|
||||
12, 15, 14, 14, 13, 12,
|
||||
16, 17, 18, 18, 19, 16,
|
||||
20, 23, 22, 22, 21, 20
|
||||
};
|
||||
gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36);
|
||||
|
||||
// Mesh
|
||||
glGenVertexArrays(1, &mMesh.mVertexArrayId);
|
||||
glBindVertexArray(mMesh.mVertexArrayId);
|
||||
|
@ -323,6 +374,7 @@ void Renderer::RenderGl() {
|
|||
glEnable(GL_LINE_SMOOTH);
|
||||
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f);
|
||||
Matrix44f model_view_projection =
|
||||
|
@ -408,6 +460,19 @@ void Renderer::RenderGl() {
|
|||
gVertexArray.Bind();
|
||||
gXZPlaneMesh.Draw(GL_LINES);
|
||||
|
||||
model_view_projection =
|
||||
TranslateMat44(3.0f, 0.0f, 1.0f)
|
||||
* mCamera.mViewMatrix
|
||||
* mCamera.mProjectionMatrix;
|
||||
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
|
||||
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data());
|
||||
glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord");
|
||||
glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal");
|
||||
glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV");
|
||||
glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor");
|
||||
gVertexArray.Bind();
|
||||
gUnitCubeMesh.Draw(GL_TRIANGLES);
|
||||
|
||||
if (mSettings->DrawDepth) {
|
||||
mRenderTarget.RenderToLinearizedDepth(true);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
|
|
@ -493,7 +493,31 @@ void VertexArrayMesh::SetData(
|
|||
);
|
||||
}
|
||||
|
||||
void VertexArrayMesh::SetIndexData(const GLuint* indices, const int& count) {
|
||||
assert(mIndexBuffer == -1);
|
||||
|
||||
// copy the indices and increase the indices by mIndexOffset
|
||||
GLuint temp_buffer[count];
|
||||
memcpy (temp_buffer, indices, sizeof(GLuint) * count);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
temp_buffer[i] += mIndexOffset;
|
||||
}
|
||||
|
||||
mIndexCount = count;
|
||||
|
||||
glGenBuffers(1, &mIndexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), temp_buffer, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
void VertexArrayMesh::Draw(GLenum mode) {
|
||||
assert(mVertexArray->IsBound());
|
||||
glDrawArrays(mode, mIndexOffset, mVertexCount);
|
||||
|
||||
if (mIndexBuffer == -1) {
|
||||
glDrawArrays(mode, mIndexOffset, mVertexCount);
|
||||
} else {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
|
||||
glDrawElements(mode, mIndexCount, GL_UNSIGNED_INT, (void*) 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -316,6 +316,8 @@ struct VertexArrayMesh {
|
|||
const std::vector<GLuint> &indices
|
||||
);
|
||||
|
||||
void SetIndexData(const GLuint* indices, const int& count);
|
||||
|
||||
void Draw(GLenum mode);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue