more glsl troubles...

Started by
5 comments, last by _the_phantom_ 19 years, 3 months ago
Hi, this time its some problems with sending vertex attributes using VBO's. I've created a test shader to keep things simple unitl I find whats wrong. Heres what I have. Heres the extension initialization:

GLUX_LOAD(GL_ARB_vertex_program);
GLUX_LOAD(GL_ARB_vertex_buffer_object);
GLUX_LOAD(GL_EXT_draw_range_elements);
GLUX_LOAD(GL_ARB_shader_objects);
GLUX_LOAD(GL_ARB_shading_language_100);
GLUX_LOAD(GL_ARB_vertex_shader);
GLUX_LOAD(GL_ARB_fragment_shader);
The variables:

int ShaderColor[4],ShaderIndex,ShaderVertex;
unsigned ShaderVBO,ShaderVertexVBO;
The initialization:

glShaderSourceARB(ProgramObject, 1, (const GLcharARB **)&ShaderSource, &length);
//i know it compiles ok, cos if i dont use the VBOs everything runs fine
glAttachObjectARB(ShaderObject, ProgramObject);
glLinkProgramARB(ShaderObject);

for(int i=0;i<4;i++)
{
char txt[256];
sprintf(txt,"Color[%i]",i);
ShaderColor=glGetUniformLocationARB(ShaderObject, txt);
if(ShaderColor==-1)
MessageBox(NULL,txt,"Cant Get Uniform",MB_OK);
}

glGenBuffersARB(1,&ShaderVBO);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, ShaderVBO);
float TempData[4];
TempData[0]=0.0f;
TempData[1]=1.0f;
TempData[2]=2.0f;
TempData[3]=3.0f;
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 4*sizeof(float),TempData, GL_STATIC_DRAW_ARB);
if( glGetError() != GL_NO_ERROR )
{
MessageBox(NULL,"Error!","VBO",MB_OK);
}

glGenBuffersARB(1,&ShaderVertexVBO);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, ShaderVertexVBO);
float VertData[16];
VertData[0]=0.0f;
VertData[1]=0.0f;
VertData[2]=0.0f;
VertData[3]=1.0f;

VertData[4]=800.0f;
VertData[5]=0.0f;
VertData[6]=0.0f;
VertData[7]=1.0f;

VertData[8]=800.0f;
VertData[9]=600.0f;
VertData[10]=0.0f;
VertData[11]=1.0f;

VertData[12]=0.0f;
VertData[13]=600.0f;
VertData[14]=0.0f;
VertData[15]=1.0f;

glBufferDataARB(GL_ARRAY_BUFFER_ARB, 4*4*sizeof(float),VertData, GL_STATIC_DRAW_ARB);
if( glGetError() != GL_NO_ERROR )
{
MessageBox(NULL,"Error!","VBO",MB_OK);
}

ShaderIndex=myWeaponShader->GetAttribIndexByName("Index");
//allways returns 1, as it should be?
if(ShaderIndex==-1)
{
MessageBox(NULL,"Index","Cant Find Attribute",MB_OK);
}

ShaderVertex=myWeaponShader->GetAttribIndexByName("Vertex");
//allways returns 2, as it should be?
if(ShaderVertex==-1)
{
MessageBox(NULL,"Vertex","Cant Find Attribute",MB_OK);
}
The glsl shader:

uniform vec4 Color[4];
attribute float Index;
attribute vec4 Vertex;

void main(void)
{
	gl_FrontColor = Color[int(Index)];
	gl_Position = gl_ModelViewProjectionMatrix * Vertex;
}
And heres the rendering:

glUseProgramObjectARB(ShaderObject);
glEnableVertexAttribArrayARB(ShaderIndex);
glEnableVertexAttribArrayARB(ShaderVertex);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, ShaderVBO);
glVertexAttribPointerARB(ShaderIndex, 1 , GL_FLOAT, false, 0, NULL);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, ShaderVertexVBO);
glVertexAttribPointerARB(ShaderVertex, 4 , GL_FLOAT, false, 0, NULL);

float TempColor[4];
TempColor[0]=0.0f;
TempColor[1]=0.0f;
TempColor[2]=0.0f;
glUniform4fvARB(ShaderColor[0], 4, TempColor);
TempColor[0]=1.0f;
TempColor[1]=1.0f;
TempColor[2]=0.0f;
glUniform4fvARB(ShaderColor[1], 4, TempColor);
TempColor[0]=0.0f;
TempColor[1]=1.0f;
TempColor[2]=0.0f;
glUniform4fvARB(ShaderColor[2], 4, TempColor);
TempColor[0]=0.0f;
TempColor[1]=0.0f;
TempColor[2]=1.0f;
glUniform4fvARB(ShaderColor[3], 4, TempColor);

glDrawArrays( GL_QUADS, 0, 4 );

glDisableVertexAttribArrayARB(ShaderIndex);
glDisableVertexAttribArrayARB(ShaderVertex);
glUseProgramObjectARB(0);
As you see what the shader should do is get the vertex color from the array. I cant seem to spot the error, any ideas? [Edited by - Bouga on January 3, 2005 7:35:47 AM]
"A screen buffer is worth a thousand char's" - me
Advertisement
errm, you're putting the data into a VBO, enabling an array and then not drawing using an array accessing system, why do you think its not working? [smile]
Ok, lol, cant believe I posted that :) That was only wrong in the test app I had written wrong this time, in the original I did it correct. I changed it to how it should be in the first post. But that still doesnt work... besides, the Attribute indexes i get seem to be correct, so the VBO binding must be incorrect somewhere... any ideas?
"A screen buffer is worth a thousand char's" - me
Ok, got an answer on opengl.org, just in case if anyone else runs into this problem, ill post the solution here.

It seems you must have a vertex atrribute with index(location) ID 0 for arrays to work with shaders. So what I needed was to remove the "Vertex" attribute and use the predefined "gl_Vertex" instead in the shader, and just send the vertex arrays to the location ID 0. The test shader now looks like this:

uniform vec4 Color[4];attribute float Index;void main(void){	gl_FrontColor = Color[int(Index)];	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}


and for the above code to work I had to set the ShaderVertex=0; instead of getting an ID from the shader.
"A screen buffer is worth a thousand char's" - me
ah, ofcourse, I missed that line of code and if I'd payed attension to the Orange book is says that 0 ==> gl_vertex/myVertex

Just out of intrest, why are you using your own named stream? I find it easy enuff just to stick with the standard Vertex Array pointer functions and let OGL sort it out...
Hmm, no reason at all really... just the shader which I originally tried to translate was doing it that way so I decided to stick to it, until I get it fully functional & then make the modifications I had planned. So maybe then Ill change it. Whats would be the difference between the two methods?
"A screen buffer is worth a thousand char's" - me
well, there only difference is instead of having to declare your own var to hold the vertex position, set it to attrib. 0 and bind the vertex data to it you instead just bind it like a normal vertex array and access it via gl_Vertex.

This topic is closed to new replies.

Advertisement