I currently have a project that renders OpenGL video using a vertex and fragment shader. The shaders work fine as-is, but in trying to add in texturing, I am running into performance issues and can't figure out why.
Before adding texturing, my program ran just fine and loaded my CPU between 0%-4%. When adding texturing (specifically textures AND color -- noted by comment below), my CPU is 100% loaded. The only code I have added is the relevant texturing code to the shader, and the "glBindTexture()" calls to the rendering code.
Here are my shaders and relevant rending code.
Vertex Shader:
#version 150
uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightPosition;
uniform float diffuseValue;
layout(location = 0) in vec3 vertex;
layout(location = 1) in vec3 color;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec2 texCoord;
smooth out VertData {
vec3 color;
vec3 normal;
vec3 toLight;
float diffuseValue;
vec2 texCoord;
} VertOut;
void main(void)
{
gl_Position = mvpMatrix * vec4(vertex, 1.0);
VertOut.normal = normalize(normalMatrix * normal);
VertOut.toLight = normalize(vec3(mvMatrix * lightPosition - gl_Position));
VertOut.color = color;
VertOut.diffuseValue = diffuseValue;
VertOut.texCoord = texCoord;
}
Fragment Shader:
#version 150
smooth in VertData {
vec3 color;
vec3 normal;
vec3 toLight;
float diffuseValue;
vec2 texCoord;
} VertIn;
uniform sampler2D tex;
layout(location = 0) out vec3 colorOut;
void main(void)
{
float diffuseComp = max( dot(normalize(VertIn.normal), normalize(VertIn.toLight)) ), 0.0);
vec4 color = texture2D(tex, VertIn.texCoord);
colorOut = color.rgb * diffuseComp * VertIn.diffuseValue + color.rgb * (1 - VertIn.diffuseValue);
// FOLLOWING LINE CAUSES PERFORMANCE ISSUES
colorOut *= VertIn.color;
}
Relevant Rendering Code:
// 3 textures have been successfully pre-loaded, and can be used
// texture[0] is a 1x1 white texture to effectively turn off texturing
glUseProgram(program);
// Draw squares
glBindTexture(GL_TEXTURE_2D, texture[1]);
// Set attributes, uniforms, etc
glDrawArrays(GL_QUADS, 0, 6*4);
// Draw triangles
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Set attributes, uniforms, etc
glDrawArrays(GL_TRIANGLES, 0, 3*4);
// Draw reference planes
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Set attributes, uniforms, etc
glDrawArrays(GL_LINES, 0, 4*81*2);
// Draw terrain
glBindTexture(GL_TEXTURE_2D, texture[2]);
// Set attributes, uniforms, etc
glDrawArrays(GL_TRIANGLES, 0, 501*501*6);
// Release
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
Any help is greatly appreciated!