PDA

View Full Version : vertex custom attributes



nico_ad
10-18-2014, 08:55 AM
Hello,

I m working on some 2D project and would love to add a custom attributes in a GLSL vertex shader, but I m a bit confused about usage of glEnableVertexAttribArray, glVertexAttribPointer and glBindAttribLocation

basically, i m drawing 2D quads and what to have to get an index (in the range [0,1,2,3]) for each vertex. ex : bottom left will get index 0 , bottom right vertex will get index 1.

my code :



Vertex2df vertices[] = {
{-half_x , -half_y ,0},
{ half_x , -half_y ,0},
{ half_x , half_y ,0},
{-half_x , half_y ,0}};

float text []=
{ BotLeftTex.x , BotLeftTex.y ,
TopRightTex.x , BotLeftTex.y ,
TopRightTex.x , TopRightTex.y ,
BotLeftTex.x , TopRightTex.y
};

short attributes[] =
{
0,1,2,3
};



glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, vertices);

glTexCoordPointer(2,GL_FLOAT,0,text);

if (pShader)
{
glBindAttribLocation(pShader->getProgramObj(), 10, "vertexIndex");
glEnableVertexAttribArray(10);
//
// (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer);
glVertexAttribPointer(10,1,GL_SHORT,false,0,attrib utes);
}

glDrawArrays(GL_QUADS,0,4);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

if (pShader)
{
glDisableVertexAttribArray(10);
}




GLSL



attribute short vertexIndex;

void main(void)
{
if (vertexIndex==1)
{
// do something
}

gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}



the code works fine for vertex position/texture, but something is wrong in my usage of custom attributes


Any ideas ?

Thanks in advance,

Nicolas

mbentrup
10-18-2014, 09:27 AM
attribute short vertexIndex;



There is no type "short" in GLSL. Use "int".

nico_ad
10-18-2014, 09:53 AM
I changed to




GLint attributes[] =
{
0,1,2,3
};


glVertexAttribPointer(10,1,GL_INT,false,0,attribut es);





attribute int vertexIndex;


but look like the vertexIndex dont have the correct value in the vertex shader.

GClements
10-18-2014, 11:37 AM
You need to use glVertexAttribIPointer (note the "I") for values where the shader variables are integers. glVertexAttribPointer is for values which are accessed via a float variable in the shader.

You can have arrays of byte or short values; its just GLSL which only supports int or float.

mhagain
10-19-2014, 07:12 AM
You can achieve the same result with gl_VertexID (https://www.opengl.org/sdk/docs/man/html/gl_VertexID.xhtml).