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;
uniform vec4 uColor;
void main() {
outColor = vec3(1, 0, 0);
outColor = uColor.rgb;
}

View File

@ -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;
}

View File

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

View File

@ -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);
}

View File

@ -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);

View File

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

View File

@ -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
//

View File

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