PDA

View Full Version : Shader problems: How to use custom vetex shader attributes



Cornix
11-01-2013, 07:33 AM
Solved
I feel dumb now. 2 minutes after opening the thread I managed to fix the problem after working on this for a whole day.
For anyone who is just as clueless as I was: I had to bind the attribute location. Or at least this solved my problem. :doh:

I was toying around with shaders recently but I got into a problem I just can not fix myself.

I have one very simple shader program:
Vertex-Shader

varying vec4 v_color;

void main()
{
v_color = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}\n
Fragment-Shader

varying vec4 v_color;

void main()
{
gl_FragColor = v_color;
}\n

And I have a VBO with an Array-Buffer like this:

[ 53.0, 353.0, 1.0, 1.0,
1.0, 0.5, 0.0, 1.0,
654.0, 31.0, 1.0, 1.0,
1.0, 0.5, 0.0, 1.0 ]
and an Element-Array-Buffer like this: [0, 1]

I draw with glDrawElements(GL_LINES, 2, GL_INT, 0);

And I have this code to set the attribute pointers:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_FLOAT, 32, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 32, 16);

And everything works. I see a line being drawn on the screen.
But now I wanted to toy around and change the shader slightly:
Vertex-Shader

attribute vec4 test;
varying vec4 v_color;

void main()
{
v_color = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * test;
}\n
Fragment-Shader

varying vec4 v_color;

void main()
{
gl_FragColor = v_color;
}\n

The VBO and the buffers still look the same but I changed the pointers to this:

int id = glGetAttribLocation(pId, "test");
glEnableVertexAttribArray(id);
glVertexAttribPointer(id, 4, GL_FLOAT, GL_FALSE, 32, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 32, 16);
But nothing is drawn anymore.
I know I did something wrong, but I dont know what. I am probably using the custom vertex attribute incorrectly I guess.

Thank you very much for your help.