I'm trying to render in opengl using shaders, the background colour generates and no error messages appear but the object I'm trying to render doesn't show. I've been trying all day to solve this but have come to nothing. I'm reading vertices and indices in from a .txt
file but that doesn't seem to be the problem as all the numbers are exactly what they should be. Here is my init:
void init() {
readFile("pendulum.txt", pVert, pIndices, pCols);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable( GL_DEPTH_TEST );
glClearColor(0.5,0.5,0.5,1.0);
program = InitShader( "aVertexShader64.glsl", "aFragShader63.glsl" );
glUseProgram( program );
modelView = glGetUniformLocation( program, "model_view" );
projection = glGetUniformLocation( program, "projection" );
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize two buffer objects
glGenBuffers( 2, buffers);
//one buffer for the vertexPositions and colours
glBindBuffer( GL_ARRAY_BUFFER, buffers[0]);
glBufferData( GL_ARRAY_BUFFER, numVertexPositionBytes + numVertexColourBytes,NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions );
glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, vertexColours);
//one buffer for the indices
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData( GL_ELEMENT_ARRAY_BUFFER, numVertexIndexBytes,vertexIndices, GL_STATIC_DRAW );
// set up vertex arrays
GLuint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
GLuint vColor = glGetAttribLocation( program, "vColor" );
glEnableVertexAttribArray( vColor );
glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertexPositionBytes) );
}
Main:
int main( int argc, char **argv ){
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow( "pendulum" );
glewInit();
init();
//initialiseBoxCoordinates();
//glutTimerFunc(1,timer,0);
glutReshapeFunc(reshape);
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutIdleFunc( idle );
glutMainLoop();
return 0;
}
And Display:
void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
pStack.pushMatrix();
pStack.loadIdentity();
pStack.rotated(theta,0.0,1.0,0.0);
pStack.translated(0.0,0.0,-1.0);
for(int i = 0; i<NumPVerts; i++){
pStack.transformd(&pVert[i*4],&pVertActual[i*4]);
}
glBindVertexArray(lineVao);
glDrawArrays(GL_LINES,0, lineVertSize/3);
glBindVertexArray(pVao);
glBindBuffer(GL_ARRAY_BUFFER, pBuffers[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, numPVertexBytes, pVertActual);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pBuffers[1]);
glDrawElements(GL_TRIANGLES, NumPIndices, GL_UNSIGNED_BYTE, 0);
pStack.popMatrix();
//switch buffers
glutSwapBuffers();
}
After the matrix transformations the vertices are all where they should be and I have used the same matrix class before with no problems so I don't think it's that. I think it has something to do with the shaders but I'm still new to opengl and am really not sure. Here are the vertex shader:
in vec4 vPosition;
in vec4 vColor;
out vec4 color;
uniform mat4 model_view;
uniform mat4 projection;
void main() {
gl_Position = projection*model_view*vPosition/vPosition.w;
color = vColor;
}
And the fragment shader:
in vec4 color;
out vec4 fColor;
void main() {
fColor = color;
}