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

<p>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.</p>

CC BY-SA 3.0
2016-08-17T21:53:29.477Z
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?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-08-16T15:14:01.173Z
2016-08-19T14:07:21.030Z
2016-08-19T14:07:21.030Z
lega
<p>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.</p>

<p>activate and deactivate is just glUseProgram(m_ShaderProgram) and glUseProgram(0);</p>

<p>This is my draw method: </p>

<pre><code>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 &amp; 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);
}
</code></pre>

<p>And my shaders look like this:</p>

<p>Vertex shader:</p>

<pre><code>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;
}
</code></pre>

<p>Fragment shader:</p>

<pre><code>varying vec3 Normal;
varying vec3 Position;
varying vec2 Texcoord;

void main(void) {
   gl_FragColor = vec4(1.0,1.0,0,0);
}
</code></pre>

<p>Result:
<a href="https://i.sstatic.net/VzISb.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VzISb.jpg" alt="Terrain"></a></p>

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

CC BY-SA 3.0
2016-08-16T14:40:11.073Z
Have you called `glGetError()` to see if there are any errors?
SurvivalMachine
CC BY-SA 3.0
2016-08-16T14:56:28.573Z
Gives me 1281 - Invalid Value
lega
CC BY-SA 3.0
2016-08-16T15:06:22.370Z
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.
lega
<opengl><c++><vertex-buffer-object>
How to use Shaders with vertex & index buffers?
lega
# How to use Shaders with vertex & index buffers?

(Asked by: username_0 on 2016-08-16)

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](https://i.sstatic.net/VzISb.jpg)](https://i.sstatic.net/VzISb.jpg)

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

**Tags:** opengl, c++, vertex-buffer-object

> **Comments:**
> **username_1**: Have you called `glGetError()` to see if there are any errors?  
> **username_0**: Gives me 1281 - Invalid Value  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_0 on 2016-08-16. Score: -1 | Accepted answer

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.

> **Comments:**
> **username_2**: 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?  

---

