From 207094b0c810fe974943e11c221432191ad171b4 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 27 Feb 2018 23:15:07 +0100 Subject: [PATCH] Trying to draw a colored coordinate system ... --- data/shaders/fs_simple.glsl | 17 ++++++++++---- data/shaders/vs_simple.glsl | 10 ++++++--- src/main.cc | 7 ------ src/modules/RenderModule.cc | 44 ++++++++++++++++++++++++++++++++++++- src/modules/RenderModule.h | 6 +---- src/modules/RenderUtils.cc | 31 ++++++++++++++++++++++++++ src/modules/RenderUtils.h | 40 +++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 20 deletions(-) diff --git a/data/shaders/fs_simple.glsl b/data/shaders/fs_simple.glsl index 2e637f8..b83f17d 100644 --- a/data/shaders/fs_simple.glsl +++ b/data/shaders/fs_simple.glsl @@ -1,9 +1,18 @@ -#version 330 core - -layout(location = 0) out vec3 outColor; +#version 150 core uniform vec4 uColor; +in vec3 fragmentColor; + +out vec3 outColor; + void main() { - outColor = uColor.rgb; + outColor = vec3( + uColor.r * fragmentColor.r, + uColor.g * fragmentColor.g, + uColor.b * fragmentColor.b + ); + + outColor = max(uColor.rgb, fragmentColor); +//outColor = fragmentColor + uColor.rgb - uColor.rgb; } diff --git a/data/shaders/vs_simple.glsl b/data/shaders/vs_simple.glsl index 74b1de0..5051b16 100644 --- a/data/shaders/vs_simple.glsl +++ b/data/shaders/vs_simple.glsl @@ -1,10 +1,14 @@ #version 150 core -#extension GL_ARB_explicit_uniform_location : require +#extension GL_ARB_explicit_attrib_location : require -in vec3 inVertex; +layout(location = 0) in vec3 inCoord; +layout(location = 1) in vec3 inColor; uniform mat4 uModelViewProj; +out vec3 fragmentColor; + void main() { - gl_Position = uModelViewProj * vec4(inVertex, 1); + gl_Position = uModelViewProj * vec4(inCoord, 1); + fragmentColor = inColor; } diff --git a/src/main.cc b/src/main.cc index 9016598..c41ee95 100644 --- a/src/main.cc +++ b/src/main.cc @@ -161,13 +161,6 @@ int main(void) glfwPollEvents(); ImGui_ImplGlfwGL3_NewFrame(); -// imguiBeginFrame (gGuiInputState->mouseX, -// gGuiInputState->mouseY, -// gGuiInputState->mouseButton, -// gGuiInputState->mouseScroll, -// width, -// height); - if (module_manager.CheckModulesChanged()) { gLog("Detected module update at frame %d. Unloading all modules.", frame_counter); module_manager.UnloadModules(); diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 2399ca5..cbecd92 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -41,6 +41,15 @@ static const GLfloat g_textured_quad_vertex_buffer_data[] = { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, }; +static const GLfloat g_coordinate_system_vertex_buffer_data[] = { + 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f +}; + // // Module // @@ -186,6 +195,13 @@ void Renderer::Initialize(int width, int height) { glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); + // Coordinate System + glGenVertexArrays(1, &mCoordinateSystem.mVertexArrayId); + glBindVertexArray(mCoordinateSystem.mVertexArrayId); + glGenBuffers(1, &mCoordinateSystem.mVertexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW); + // Simple Shader mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); bool load_result = mDefaultProgram.Load(); @@ -230,6 +246,8 @@ void Renderer::Shutdown() { glDeleteBuffers(1, &mMesh.mVertexBuffer); glDeleteVertexArrays(1, &mPlane.mVertexArrayId); glDeleteBuffers(1, &mPlane.mVertexBuffer); + glDeleteVertexArrays(1, &mCoordinateSystem.mVertexArrayId); + glDeleteBuffers(1, &mCoordinateSystem.mVertexBuffer); } @@ -275,7 +293,31 @@ void Renderer::RenderGl() { (void*)0 // offset ); - glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total +// glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total + glDisableVertexAttribArray(0); + + // Coordinate system + glBindVertexArray(mCoordinateSystem.mVertexArrayId); + glEnableVertexAttribArray(0); + glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 1.0f, 1.0f).data()); + glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer); + glVertexAttribPointer( + 0, + 3, + GL_FLOAT, + GL_FALSE, + (sizeof(float[6])), + (void*)0 + ); + glVertexAttribPointer( + 1, + 3, + GL_FLOAT, + GL_FALSE, + (sizeof(float[6])), + (void*) (sizeof(float[3])) + ); + glDrawArrays(GL_LINES, 0, 6); if (mSettings->DrawDepth) { mRenderTarget.RenderToLinearizedDepth(true); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index f037d03..24eb3b7 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -102,11 +102,6 @@ struct Light { } }; -struct Mesh { - GLuint mVertexArrayId = -1; - GLuint mVertexBuffer = -1; -}; - struct RendererSettings; struct Renderer { @@ -119,6 +114,7 @@ struct Renderer { Camera mCamera; Mesh mMesh; Mesh mPlane; + Mesh mCoordinateSystem; Texture mDefaultTexture; diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index f28f643..aa9005a 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -323,3 +323,34 @@ bool Texture::Load(const char* filename, int num_components) { // unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components); assert(false); } + +void VertexArray::Initialize(const int& size, GLenum usage) { + mSize = size; + mUsed = 0; + + glGenVertexArrays (1, &mVertexArrayId); + glBindVertexArray(mVertexArrayId); + + glGenBuffers(1, &mVertexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * size, NULL, usage); +} + +GLuint VertexArray::AllocateMesh(const int& size) { + GLuint mesh_data_size = size * sizeof(VertexData); + if (mUsed + mesh_data_size > mSize) { + gLog("Cannot allocate mesh in VertexArray: not enough vertices available"); + assert(false); + return -1; + } + + GLuint offset = mUsed; + mUsed += mesh_data_size; + + return offset; +} + +void VertexArrayMesh::Initialize(VertexArray &array, const int& size) { + mOffset = array.AllocateMesh(size); + assert(mOffset != -1); +} diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 2a044df..bcd8d4b 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -186,4 +186,44 @@ struct Texture { bool Load(const char* path, int num_components = 3); }; +struct Mesh { + GLuint mVertexArrayId = -1; + GLuint mVertexBuffer = -1; +}; + +/** + * Multiple VertexArrayMeshes can be stored in a single VertexArray. + * Storage order is: + * (VVVV)(NNNN)(UV)(CCCC) + */ +struct VertexArray { + GLuint mVertexBuffer = -1; + GLuint mVertexArrayId = -1; + GLuint mSize = -1; + GLuint mUsed = -1; + + struct VertexData { + float mCoords[4]; + float mNormals[4]; + float mTexCoords[2]; + GLubyte mColor[4]; + }; + + void Initialize(const int& size, GLenum usage); + GLuint AllocateMesh(const int& size); +}; + +struct VertexArrayMesh { + GLuint mOffset; + + void Initialize(VertexArray &array, const int& size); + void SetData( + const std::vector &coords, + const std::vector &normals, + const std::vector &uvs, + const std::vector &colors + ); +}; + + #endif