From c86f5355715c13345b5e07a603e9e64b6e5946a3 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Wed, 28 Feb 2018 13:37:33 +0100 Subject: [PATCH] Properly drawing VertexArrayMesh plane... yay --- src/modules/RenderModule.cc | 52 +++++++++++++++++++++++++++++++-- src/modules/RenderUtils.h | 57 +++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 8010949..7867f4c 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -52,6 +52,7 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = { VertexArray gVertexArray; VertexArrayMesh gVertexArrayMesh; +VertexArrayMesh gXZPlaneMesh; // // Module @@ -171,7 +172,7 @@ void Camera::DrawGui() { ImGui::Checkbox("Orthographic", &mIsOrthographic); ImGui::SliderFloat("Fov", &mFov, 5, 160); ImGui::SliderFloat("Near", &mNear, -10, 10); - ImGui::SliderFloat("Far", &mFar, -10, 10); + ImGui::SliderFloat("Far", &mFar, -20, 20); if (ImGui::Button("Reset")) { *this = Camera(); } @@ -200,6 +201,38 @@ void Renderer::Initialize(int width, int height) { gVertexArrayMesh.SetData(vertex_data, 6); + // Plane + const int plane_grid_size = 20; + gXZPlaneMesh.Initialize(gVertexArray, (plane_grid_size + 1) * 4); + + std::vector plane_data((plane_grid_size + 1) * 4); + for (int i = 0, n = plane_grid_size + 1; i < n; ++i) { + // lines along the x axis + plane_data[2 * i] = VertexArray::VertexData( + - plane_grid_size * 0.5f, 0.0f, -plane_grid_size * 0.5f + 1.0f * i, 1.0f, + 0.0f, 1.0f, 0.0f, + - plane_grid_size * 0.5f, -1.0f * i, + 255, 255, 255, 255); + plane_data[2 * i + 1] = VertexArray::VertexData( + plane_grid_size * 0.5f, 0.0f, -plane_grid_size * 0.5 + 1.0f * i, 1.0f, + 0.0f, 1.0f, 0.0f, + plane_grid_size * 0.5f, -1.0f * i, + 255, 255, 255, 255); + + // lines along the z axis + plane_data[n * 2 + 2 * i] = VertexArray::VertexData( + -plane_grid_size * 0.5f + 1.0f * i, 0.0f, - plane_grid_size * 0.5f, 1.0f, + 0.0f, 1.0f, 0.0f, + -1.0f * i, - plane_grid_size * 0.5f, + 255, 255, 255, 255); + plane_data[n * 2 + 2 * i + 1] = VertexArray::VertexData( + -plane_grid_size * 0.5f + 1.0f * i, 0.0f, plane_grid_size * 0.5f, 1.0f, + 0.0f, 1.0f, 0.0f, + -1.0f * i, plane_grid_size * 0.5f, + 255, 255, 255, 255); + } + gXZPlaneMesh.SetData(plane_data.data(), plane_data.size()); + // Mesh glGenVertexArrays(1, &mMesh.mVertexArrayId); glBindVertexArray(mMesh.mVertexArrayId); @@ -258,6 +291,7 @@ void Renderer::Initialize(int width, int height) { muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar"); + } void Renderer::Shutdown() { @@ -278,6 +312,10 @@ void Renderer::RenderGl() { mCamera.UpdateMatrices(); + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + glEnable(GL_MULTISAMPLE); + Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f); Matrix44f model_view_projection = model_matrix @@ -345,12 +383,22 @@ void Renderer::RenderGl() { * mCamera.mProjectionMatrix; glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); + gVertexArray.Bind(); + gVertexArrayMesh.Draw(GL_LINES); + + // Plane + model_view_projection = + TranslateMat44(0.0f, 0.0f, 0.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(); - gVertexArrayMesh.Draw(GL_LINES); + gXZPlaneMesh.Draw(GL_LINES); if (mSettings->DrawDepth) { mRenderTarget.RenderToLinearizedDepth(true); diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 051f5da..2e900a1 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -214,8 +214,8 @@ struct VertexArray { float nx; float ny; float nz; - float u; - float v; + float s; + float t; GLubyte r; GLubyte g; GLubyte b; @@ -228,6 +228,59 @@ struct VertexArray { GLubyte mColor[4]; }; }; + + VertexData() : + x(0.0f), + y(0.0f), + z(0.0f), + w(0.0f), + nx(0.0f), + ny(0.0f), + nz(0.0f), + s(0.0f), + t(0.0f), + r(255), + g(255), + b(255), + a(255) {} + + + + VertexData( + float x, + float y, + float z, + float w, + float nx, + float ny, + float nz, + float s, + float t, + GLubyte r, + GLubyte g, + GLubyte b, + GLubyte a + ) : + x(x), y(y), z(z), w(w), + nx(nx), ny(ny), nz(nz), + s(s), t(t), + r(r), g(g), b(b), a(a) {} + + VertexData& operator= (const VertexData& data) { + x = data.x; + y = data.y; + z = data.z; + w = data.w; + nx = data.nx; + ny = data.ny; + nz = data.nz; + r = data.r; + g = data.g; + b = data.b; + a = data.a; + + return *this; + } }; ~VertexArray();