Nothing drawing on screen OpenGL with GLSL

Posted by codemonkey on Game Development See other posts from Game Development or by codemonkey
Published on 2012-11-12T13:09:38Z Indexed on 2012/11/12 17:23 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

I hate to be asking this kind of question here, but I am at a complete loss as to what is going wrong, so please bear with me. I am trying to render a single cube (voxel) in the center of the screen, through OpenGL with GLSL on Mac

I begin by setting up everything using glut

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
glutCreateWindow("Cubez-OSX");
glutReshapeFunc(reshape);
glutDisplayFunc(render);
glutIdleFunc(idle);
_electricSheepEngine=new ElectricSheepEngine(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
_electricSheepEngine->initWorld();
glutMainLoop();

Then inside the engine init camera & projection matrices:

cameraPosition=glm::vec3(2,2,2);
cameraTarget=glm::vec3(0,0,0);
cameraUp=glm::vec3(0,0,1);
glm::vec3 cameraDirection=glm::normalize(cameraPosition-cameraTarget);
cameraRight=glm::cross(cameraDirection, cameraUp);
cameraRight.z=0;
view=glm::lookAt(cameraPosition, cameraTarget, cameraUp);
lensAngle=45.0f;
aspectRatio=1.0*(windowWidth/windowHeight);
nearClippingPlane=0.1f;
farClippingPlane=100.0f;
projection=glm::perspective(lensAngle, aspectRatio, nearClippingPlane, farClippingPlane);

then init shaders and check compilation and bound attributes & uniforms to be correctly bound (my previous question)

These are my two shaders, vertex:

#version 120
attribute vec3 position;
attribute vec3 inColor;
uniform mat4 mvp;
varying vec3 fragColor;
void main(void){
    fragColor = inColor;
    gl_Position = mvp * vec4(position, 1.0);
}

and fragment:

#version 120
varying vec3 fragColor;
void main(void) {
    gl_FragColor = vec4(fragColor,1.0);
}

init the cube:

setPosition(glm::vec3(0,0,0));
struct voxelData data[]={
    //front face
    {{-1.0, -1.0,  1.0}, {0.0, 0.0, 1.0}},
    {{ 1.0, -1.0,  1.0}, {0.0, 1.0, 1.0}},
    {{ 1.0,  1.0,  1.0}, {0.0, 0.0, 1.0}},
    {{-1.0,  1.0,  1.0}, {0.0, 1.0, 1.0}},
    //back face
    {{-1.0, -1.0, -1.0}, {0.0, 0.0, 1.0}},
    {{ 1.0, -1.0, -1.0}, {0.0, 1.0, 1.0}},
    {{ 1.0,  1.0, -1.0}, {0.0, 0.0, 1.0}},
    {{-1.0,  1.0, -1.0}, {0.0, 1.0, 1.0}}
};
glGenBuffers(1, &modelVerticesBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, modelVerticesBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
const GLubyte indices[] = {
    // Front
    0, 1, 2,
    2, 3, 0,
    // Back
    4, 6, 5,
    4, 7, 6,
    // Left
    2, 7, 3,
    7, 6, 2,
    // Right
    0, 4, 1,
    4, 1, 5,
    // Top
    6, 2, 1,
    1, 6, 5,
    // Bottom
    0, 3, 7,
    0, 7, 4
};
glGenBuffers(1, &modelFacesBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelFacesBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

and then the render call:

glClearColor(0.52, 0.8, 0.97, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

//use the shader
glUseProgram(shaderProgram);

//enable attributes in program
glEnableVertexAttribArray(shaderAttribute_position);
glEnableVertexAttribArray(shaderAttribute_color);

//model matrix using model position vector
glm::mat4 mvp=projection*view*voxel->getModelMatrix();
glUniformMatrix4fv(shaderAttribute_mvp, 1, GL_FALSE, glm::value_ptr(mvp));

glBindBuffer(GL_ARRAY_BUFFER, voxel->modelVerticesBufferObject);
glVertexAttribPointer(shaderAttribute_position, // attribute
                      3, // number of elements per vertex, here (x,y)
                      GL_FLOAT, // the type of each element
                      GL_FALSE, // take our values as-is
                      sizeof(struct voxelData), // coord every (sizeof) elements
                      0 // offset of first element
                      );

glBindBuffer(GL_ARRAY_BUFFER, voxel->modelVerticesBufferObject);
glVertexAttribPointer(shaderAttribute_color, // attribute
                      3, // number of colour elements per vertex, here (x,y)
                      GL_FLOAT, // the type of each element
                      GL_FALSE, // take our values as-is
                      sizeof(struct voxelData), // coord every (sizeof) elements
                      (GLvoid *)(offsetof(struct voxelData, color3D)) // offset of colour data
                      );

//draw the model by going through its elements array
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, voxel->modelFacesBufferObject);
int bufferSize;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
glDrawElements(GL_TRIANGLES, bufferSize/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

//close up the attribute in program, no more need
glDisableVertexAttribArray(shaderAttribute_position);
glDisableVertexAttribArray(shaderAttribute_color);

but on screen all I get is the clear color :$

I generate my model matrix using:

modelMatrix=glm::translate(glm::mat4(1.0), position);

which in debug turns out to be for the position of (0,0,0):

|1, 0, 0, 0|
|0, 1, 0, 0|
|0, 0, 1, 0|
|0, 0, 0, 1|

Sorry for such a question, I know it is annoying to look at someone's code, but I promise I have tried to debug around and figure it out as much as I can, and can't come to a solution

Help a noob please?

EDIT: Full source here, if anyone wants

© Game Development or respective owner

Related posts about c++

Related posts about opengl