PDA

View Full Version : Moving from immediate to VBO with GLSL 1.1 (Mac)



Name 777
06-19-2011, 06:37 PM
Howdy all,
I've been working to move some of my old code from Immediate draw calls to VBOs using the glVertexAttribPointer to pass in information. I'm using SDL, XCode 4, and Mac OS 10.6.7. I believe I am limited to GLSL 1.1

My code draws fine, until I enable my shader program; at this point nothing draws. The info logs are blank, and glGetError returns no error.

Here is the relevant C code:


static int loadShaderFile(const char* filepath,GLuint shader)
{
ifstream file(filepath,ifstream::in);
if(!file || !file.good())
return 0;

//get length
unsigned long pos=file.tellg();
file.seekg(0,ios::end);
unsigned long len=file.tellg();
file.seekg(pos);
//

if(!len)
return 0;

GLchar** source=new GLchar*;
GLchar* src=new GLchar[len+1];

*source=src;

if (!src)
return 0;

//ensure 0 term
src[len]=0;

//load by character
GLuint i=0;
while(file.good())
{
src[i] = file.get();
// cout<<src[i];
if(!file.eof())
i++;
}

src[i]=0; //0 term real end

GLint* lineCounts=new GLint[1];
lineCounts[0]=i;

file.close();

glShaderSource(shader, 1, (const GLchar**)source, lineCounts);
glCompileShader(shader);
GLint params;
glGetShaderiv(shader, GL_COMPILE_STATUS, &amp;params);
if(params==GL_FALSE)
return 0;
return 1;

}

static void initShaders()
{
vertShader=0;
fragShader=0;
shadProg=0;
vertShader=glCreateShader(GL_VERTEX_SHADER);
fragShader=glCreateShader(GL_FRAGMENT_SHADER);

bool go=true;
if(!vertShader)
{
cout<<"Could not create Vert Shader"<<endl;
go=false;
}
if(!fragShader)
{
cout<<"Could not create Frag Shader"<<endl;
go=false;
}
if(!loadShaderFile(vertPath, vertShader))
{
cout<<"Failed to load Vert file"<<endl;
go=false;
}
if(!loadShaderFile(fragPath, fragShader))
{
cout<<"Failed to load Frag file"<<endl;
go=false;
}

if(go)
{
shadProg=glCreateProgram();
glAttachShader(shadProg, vertShader);
glAttachShader(shadProg, fragShader);

glLinkProgram(shadProg);
cout<<"Shaders loaded"<<endl;

unsigned int len=1024;
GLchar* log=new GLchar[len];
glGetShaderInfoLog(fragShader, len, NULL, log);
cout<<log<<endl;


}

}

static void initGL ()
{

glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1.0f);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

verts[0]=makeVert(-100.0, 0.0,100.0,0.0,1.0);
verts[1]=makeVert(-100.0,0.0,-100.0,0.0,0.0);
verts[2]=makeVert( 100.0, 0.0,100.0,1.0,1.0);
verts[3]=makeVert( 100.0,0.0,-100.0,1.0,0.0);



indices[0]=0;
indices[1]=1;
indices[2]=3;
indices[3]=0;
indices[4]=3;
indices[5]=2;
glGenBuffers(1, &amp;vertbuff);
glBindBuffer(GL_ARRAY_BUFFER, vertbuff);
glBufferData(GL_ARRAY_BUFFER, sizeof(drawVert)*4, verts, GL_STATIC_DRAW);

glGenBuffers(1, &amp;indbuff);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indbuff);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*4, indices, GL_STATIC_DRAW);

glViewport(0, 0, kWindowWidth, kWindowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) kWindowWidth / (GLfloat) kWindowHeight, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

reshape(kWindowWidth,kWindowHeight);

initShaders();

if(shadProg)
glUseProgram(shadProg);
}

static void drawGL ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

setupMatrices(0.0, 2.0, 0.0, 0.0, 1.75, -1.0);

glPushMatrix();

glBindBuffer(GL_ARRAY_BUFFER, vertbuff);
glVertexAttribPointer(0, 3, GL_FLOAT, 0, sizeof(drawVert), BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);


//other attribs here
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indbuff);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

//clear out

glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glPopMatrix();
}


Here are my shaders. These are simple cases that I have yet to get to render properly.

Vert shader:

void main()
{
vec4 a = gl_Vertex;

gl_Position = gl_ModelViewProjectionMatrix * a;
}


Frag Shader:

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


Can anyone see any glaring issues in the code above?

Thanks for your help.

V-man
06-19-2011, 07:44 PM
You have
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*4, indices, GL_STATIC_DRAW);

You have *4 instead of *6

Also, you should check if linking succeeded.



glGetProgramiv(shaderProgram, GL_LINK_STATUS, (GLint *)&amp;IsLinked);
if(IsLinked==FALSE)
{
cout << "Failed to link shader." << endl;

GLint maxLength;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &amp;maxLength);
if(maxLength>0)
{
char *pLinkInfoLog = new char[maxLength];
glGetProgramInfoLog(shaderProgram, maxLength, &amp;maxLength, pLinkInfoLog);
cout << pLinkInfoLog << endl;
delete [] pLinkInfoLog;
}

glDetachShader(shaderProgram, vertexShader);
glDetachShader(shaderProgram, fragmentShader);
glDeleteShader(vertexShader);
vertexShader=0;
glDeleteShader(fragmentShader);
fragmentShader=0;
glDeleteProgram(shaderProgram);
shaderProgram=0;

return -1;
}

Name 777
06-20-2011, 06:05 PM
Thanks V-man for your help. Fixed the index issue, and added a check for program linking based on what you provided. Linking appears to be successful, and nothing is being written to the info log.

Any other thoughts or suggestions?

Thanks again.

Name 777
06-29-2011, 11:51 AM
Just a quick update for anyone following this:
I've spent a few hours analyzing my program with the OpenGL Profiler utility in hopes of gaining a better understanding of what is going on. So far, it looks like the shaders are linking, compiling, and validating properly. Interestingly enough, the depth buffer appears to contain the geometry (a green quad) that I am expecting to see rendered in the scene. All other buffers appear empty.

Could there be an issue somewhere between transferring from the depth buffer to the screen buffer? Am I potentially missing an enable somewhere?

I feel that the solution to this problem is simple and staring me in the face, but I just don't see it.

mhagain
06-29-2011, 02:26 PM
I'm not seeing your setupMatrices function anywhere in the code you posted - could you post that too?

Also interested in what happens if you use ftransform in your vertex shader.

Name 777
06-30-2011, 10:48 PM
mhagain,
Below are the functions you requested. Note that it is redundant when combined with the init code from my previous post; this bit was simplified and moved around a bit while trying to work through this problem. The setupMatrices is called in drawGL since my original project required multiple passes for rendering, each with a different set of matrices. Removing the redundant projection code and moving the setupMatrices call to the init function appears to have no affect.

ftransform appears to have no affect in my Vertex shader when I replace the glModelViewProjectionMatrix * gl_Vertex calculation.

Thanks for your help.


static void reloadMatrices(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

static void setupMatrices(float pos_x,float pos_y,float pos_z,float lookAt_x,float lookAt_y,float lookAt_z)
{
reloadMatrices(kWindowWidth,kWindowHeight);
gluLookAt(pos_x,pos_y, pos_z, lookAt_x, lookAt_y, lookAt_z, 0.0,1.0,0.0);
}

Name 777
07-10-2011, 02:38 PM
Quick update:
I have implemented the same draw code in Cocoa's NSOpenGLView. Initially the draw code didn't work. However, adding 3 or more auxiliary buffers to the instance of this view in the .xib file yielded the results I have been looking for. Why would that change the results?

Name 777
07-17-2011, 05:57 PM
Update:
Setting 3+ aux buffers forces the driver to fall back on software rendering, since my card only supports up to 2 aux buffers. It looks like my rendering issues are likely driver related. My machine is running the standard Apple ATI/AMD drivers for the Radeon X1600.

Is there anything I should be looking for in the driver profile that could help me narrow down what is going on?

Thanks

Name 777
08-01-2011, 09:39 PM
Final followup, just in case anyone is following this thread:
Got some new hardware, and updated the OS to 10.7, which in turn bumped up the GLSL libraries. This seemed to solve my problem. I'm guessing there was an issue/bug with the Radeon x1600 drivers that was causing my headaches, but there really isn't any way for me to confirm this.

Thanks.