1
$\begingroup$

I'm trying to render a randomly generated terrain with vertex buffers and OpenGL. I want to use a shader to color the terrain, but it seemms like my shader isn't used. I have used shaders before when drawing triangles manually on the CPU and didn't have any problems there.

activate and deactivate is just glUseProgram(m_ShaderProgram) and glUseProgram(0);

This is my draw method:

void Terrain::drawTest() {

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    // setup position & normal pointers
    glVertexPointer(3, GL_FLOAT, sizeof(TerrainVertex), BUFFER_OFFSET(0));
    glNormalPointer(GL_FLOAT, sizeof(TerrainVertex), BUFFER_OFFSET(12));

    m_ShaderProgram.activate();
    glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
    m_ShaderProgram.deactivate();

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
}

And my shaders look like this:

Vertex shader:

varying vec3 Normal;
varying vec3 Position;
varying vec2 Texcoord;

void main(void) {
    Position = (gl_ModelViewMatrix * gl_Vertex).xyz;
    Normal = gl_NormalMatrix * gl_Normal;
    Texcoord = gl_MultiTexCoord0.xy;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment shader:

varying vec3 Normal;
varying vec3 Position;
varying vec2 Texcoord;

void main(void) {
   gl_FragColor = vec4(1.0,1.0,0,0);
}

Result: Terrain

As you can see the fragment shader doesn't seem to do anything and I can't figure out what the problem is.

$\endgroup$
  • 1
    $\begingroup$ Have you called glGetError() to see if there are any errors? $\endgroup$ – SurvivalMachine Aug 16 '16 at 14:40
  • $\begingroup$ Gives me 1281 - Invalid Value $\endgroup$ – lega Aug 16 '16 at 14:56
  • $\begingroup$ So I get GL_INVALID_ENUM on the first run of the render loop after glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer); and GL_INVALID_VALUE after the first shaderProgram.activate() and everytime thereafter. $\endgroup$ – lega Aug 16 '16 at 15:06
-1
$\begingroup$

Apparently there was just a problem with the loading of the shader. A bug that crept into the code from an old project...

EDIT: The GLuint of the compiled shader was never saved in the class and only local to the load function. Just a stupid mistake really.

$\endgroup$
  • 3
    $\begingroup$ As it stands this is more of a comment than an answer. Could you expand to explain the bug and how it affected your code? $\endgroup$ – trichoplax Aug 17 '16 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.