diff --git a/data/shaders/fs_simple.glsl b/data/shaders/fs_simple.glsl index b28256a..2e637f8 100644 --- a/data/shaders/fs_simple.glsl +++ b/data/shaders/fs_simple.glsl @@ -2,6 +2,8 @@ layout(location = 0) out vec3 outColor; +uniform vec4 uColor; + void main() { - outColor = vec3(1, 0, 0); + outColor = uColor.rgb; } diff --git a/data/shaders/fs_simpletexture.glsl b/data/shaders/fs_simpletexture.glsl index 0d9fce5..70b88e9 100644 --- a/data/shaders/fs_simpletexture.glsl +++ b/data/shaders/fs_simpletexture.glsl @@ -8,10 +8,11 @@ uniform sampler2D uTexture; uniform float uTime; void main() { - outColor = texture(uTexture, ioUV - + 0.00 * vec2( - sin(uTime + 1024.0 * ioUV.x), - cos(uTime + 768.0 * ioUV.y) - ) - ).xyz; + outColor = texture(uTexture, + ioUV + + 0.01 * vec2( + sin(uTime + 1024.0 * ioUV.x), + cos(uTime + 768.0 * ioUV.y) + ) + ).xyz; } diff --git a/data/shaders/vs_passthrough.glsl b/data/shaders/vs_passthrough.glsl index fa8b585..bddaa12 100644 --- a/data/shaders/vs_passthrough.glsl +++ b/data/shaders/vs_passthrough.glsl @@ -2,6 +2,7 @@ in vec3 inVertex; uniform mat4 uModelViewProj; +uniform float uTime; out vec2 ioUV; diff --git a/data/shaders/vs_simple.glsl b/data/shaders/vs_simple.glsl index fbdc8c1..74b1de0 100644 --- a/data/shaders/vs_simple.glsl +++ b/data/shaders/vs_simple.glsl @@ -1,9 +1,10 @@ #version 150 core #extension GL_ARB_explicit_uniform_location : require -in vec3 vertexPosition_modelspace; +in vec3 inVertex; + +uniform mat4 uModelViewProj; void main() { - gl_Position.xyz = vertexPosition_modelspace; - gl_Position.w = 1.0; + gl_Position = uModelViewProj * vec4(inVertex, 1); } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index a234e93..9ceb30c 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -131,10 +131,12 @@ void Renderer::Initialize(int width, int height) { glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - // Shaders - mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); - bool load_result = mProgram.Load(); + // Simple Shader + mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); + bool load_result = mDefaultProgram.Load(); assert(load_result); + muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj"); + muDefaultColor = mDefaultProgram.GetUniformLocation("uColor"); // Render Target mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepthTexture); @@ -151,23 +153,18 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl"); load_result = mRenderQuadProgramColor.Load(); assert(load_result); - muRenderQuadModelViewProj = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uModelViewProj"); - gLog("muRenderQuadModelViewProj %d", muRenderQuadModelViewProj); - muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTexture"); - gLog("muRenderQuadTexture %d", muRenderQuadTexture ); - muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTime"); - gLog("muRenderQuadTime %d", muRenderQuadTime); + muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj"); + + muRenderQuadTexture = mRenderQuadProgramColor.GetUniformLocation("uTexture"); + muRenderQuadTime = mRenderQuadProgramColor.GetUniformLocation("uTime"); // Program for depth texture rendering mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); load_result = mRenderQuadProgramDepth.Load(); assert(load_result); - muRenderQuadDepthModelViewProj = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uModelViewProj"); - gLog("muModelViewProj %d", muRenderQuadDepthModelViewProj); - muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uNear"); - gLog("muRenderQuadDepthNear %d", muRenderQuadDepthNear); - muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uFar"); - gLog("muRenderQuadDepthFar %d", muRenderQuadDepthFar); + muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); + muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); + muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar"); } void Renderer::Shutdown() { @@ -181,6 +178,10 @@ void Renderer::RenderGl() { if (width != mWidth || height != mHeight) Resize(width, height); + mCamera.mtxProj = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f); + + Matrix44f model_view_projection = mCamera.mtxProj * mCamera.mtxView; + // enable the render target glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId); GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 }; @@ -194,7 +195,10 @@ void Renderer::RenderGl() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); - glUseProgram(mProgram.mProgramId); + glUseProgram(mDefaultProgram.mProgramId); + glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); + glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); + glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index 3e34fd5..aabe80b 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -113,7 +113,11 @@ struct Renderer { Camera mCamera; Mesh mMesh; - RenderProgram mProgram; + + RenderProgram mDefaultProgram; + GLuint muDefaultModelViewProjection; + GLuint muDefaultColor; + RenderTarget mRenderTarget; GLuint mRenderQuadVertexArrayId; diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 86fe2e8..0202923 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -102,7 +102,6 @@ bool RenderProgram::Load() { gLog("%s", &ProgramErrorMessage[0]); } - glDetachShader(ProgramID, VertexShaderID); glDetachShader(ProgramID, FragmentShaderID); @@ -113,6 +112,30 @@ bool RenderProgram::Load() { return true; } +GLuint RenderProgram::GetUniformLocation(const std::string& name) { + if (mProgramId == -1) { + gLog("Cannot get uniform '%s' for program '%s' and '%s': shader not valid.", + name.c_str(), + mVertexShaderFilename.c_str(), + mFragmentShaderFilename.c_str() + ); + assert(mProgramId != -1); + } + GLuint result = glGetUniformLocation(mProgramId, name.c_str()); + if (result == -1) { + gLog ("Error loading uniform '%s' from shaders '%s' and '%s': uniform not found.", + name.c_str(), + mVertexShaderFilename.c_str(), + mFragmentShaderFilename.c_str() + ); + assert(false); + } else { + gLog ("Uniform '%s': %d", name.c_str(), result); + } + + return result; +} + // // RenderTarget // diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 17c038d..a81072e 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -139,6 +139,8 @@ struct RenderProgram { mFragmentShaderFilename(fs) {} + GLuint GetUniformLocation(const std::string& name); + ~RenderProgram(); bool Load();