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, ¶ms);
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, &vertbuff);
glBindBuffer(GL_ARRAY_BUFFER, vertbuff);
glBufferData(GL_ARRAY_BUFFER, sizeof(drawVert)*4, verts, GL_STATIC_DRAW);
glGenBuffers(1, &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.