Simplified querying of uniforms

simple_math_single_header
Martin Felis 2018-02-16 21:22:34 +01:00
parent cf59f6b6ae
commit 7bf244be37
8 changed files with 66 additions and 28 deletions

View File

@ -2,6 +2,8 @@
layout(location = 0) out vec3 outColor; layout(location = 0) out vec3 outColor;
uniform vec4 uColor;
void main() { void main() {
outColor = vec3(1, 0, 0); outColor = uColor.rgb;
} }

View File

@ -8,8 +8,9 @@ uniform sampler2D uTexture;
uniform float uTime; uniform float uTime;
void main() { void main() {
outColor = texture(uTexture, ioUV outColor = texture(uTexture,
+ 0.00 * vec2( ioUV
+ 0.01 * vec2(
sin(uTime + 1024.0 * ioUV.x), sin(uTime + 1024.0 * ioUV.x),
cos(uTime + 768.0 * ioUV.y) cos(uTime + 768.0 * ioUV.y)
) )

View File

@ -2,6 +2,7 @@
in vec3 inVertex; in vec3 inVertex;
uniform mat4 uModelViewProj; uniform mat4 uModelViewProj;
uniform float uTime;
out vec2 ioUV; out vec2 ioUV;

View File

@ -1,9 +1,10 @@
#version 150 core #version 150 core
#extension GL_ARB_explicit_uniform_location : require #extension GL_ARB_explicit_uniform_location : require
in vec3 vertexPosition_modelspace; in vec3 inVertex;
uniform mat4 uModelViewProj;
void main() { void main() {
gl_Position.xyz = vertexPosition_modelspace; gl_Position = uModelViewProj * vec4(inVertex, 1);
gl_Position.w = 1.0;
} }

View File

@ -131,10 +131,12 @@ void Renderer::Initialize(int width, int height) {
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Shaders // Simple Shader
mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mProgram.Load(); bool load_result = mDefaultProgram.Load();
assert(load_result); assert(load_result);
muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj");
muDefaultColor = mDefaultProgram.GetUniformLocation("uColor");
// Render Target // Render Target
mRenderTarget = RenderTarget (width, height, RenderTarget::EnableColor | RenderTarget::EnableDepthTexture); 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"); mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
load_result = mRenderQuadProgramColor.Load(); load_result = mRenderQuadProgramColor.Load();
assert(load_result); assert(load_result);
muRenderQuadModelViewProj = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uModelViewProj"); muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj");
gLog("muRenderQuadModelViewProj %d", muRenderQuadModelViewProj);
muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTexture"); muRenderQuadTexture = mRenderQuadProgramColor.GetUniformLocation("uTexture");
gLog("muRenderQuadTexture %d", muRenderQuadTexture ); muRenderQuadTime = mRenderQuadProgramColor.GetUniformLocation("uTime");
muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTime");
gLog("muRenderQuadTime %d", muRenderQuadTime);
// Program for depth texture rendering // Program for depth texture rendering
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
load_result = mRenderQuadProgramDepth.Load(); load_result = mRenderQuadProgramDepth.Load();
assert(load_result); assert(load_result);
muRenderQuadDepthModelViewProj = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uModelViewProj"); muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj");
gLog("muModelViewProj %d", muRenderQuadDepthModelViewProj); muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear");
muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uNear"); muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar");
gLog("muRenderQuadDepthNear %d", muRenderQuadDepthNear);
muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uFar");
gLog("muRenderQuadDepthFar %d", muRenderQuadDepthFar);
} }
void Renderer::Shutdown() { void Renderer::Shutdown() {
@ -181,6 +178,10 @@ void Renderer::RenderGl() {
if (width != mWidth || height != mHeight) if (width != mWidth || height != mHeight)
Resize(width, height); 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 // enable the render target
glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId); glBindFramebuffer(GL_FRAMEBUFFER, mRenderTarget.mFrameBufferId);
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 }; GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
@ -194,7 +195,10 @@ void Renderer::RenderGl() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); 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); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);

View File

@ -113,7 +113,11 @@ struct Renderer {
Camera mCamera; Camera mCamera;
Mesh mMesh; Mesh mMesh;
RenderProgram mProgram;
RenderProgram mDefaultProgram;
GLuint muDefaultModelViewProjection;
GLuint muDefaultColor;
RenderTarget mRenderTarget; RenderTarget mRenderTarget;
GLuint mRenderQuadVertexArrayId; GLuint mRenderQuadVertexArrayId;

View File

@ -102,7 +102,6 @@ bool RenderProgram::Load() {
gLog("%s", &ProgramErrorMessage[0]); gLog("%s", &ProgramErrorMessage[0]);
} }
glDetachShader(ProgramID, VertexShaderID); glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID); glDetachShader(ProgramID, FragmentShaderID);
@ -113,6 +112,30 @@ bool RenderProgram::Load() {
return true; 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 // RenderTarget
// //

View File

@ -139,6 +139,8 @@ struct RenderProgram {
mFragmentShaderFilename(fs) mFragmentShaderFilename(fs)
{} {}
GLuint GetUniformLocation(const std::string& name);
~RenderProgram(); ~RenderProgram();
bool Load(); bool Load();