Hi everyone,
So I have sorted out the problems I had earlier in loading geometry from a file and setting up the buffers etc. and am now running without errors but am still seeing a blank screen.
I am trying to test everything with a cube I have hard coding in and still get a blank screen where previously I was able to render a cube!!
Now sure what is happening and I have followed the code several times all the way through without seeing where I could be going wrong.
Some notes about the code:
I am not worried about the pixel colors in my vertices array in CreateGeometry. I just set all fragments to red so I can clearly see when I have successfully drawn my geometry. I want to get the basics down as step by step as possible.
I have made sure backface culling is off so I can see some geometry regardless of the face. I try to minimize any calls like this that would prevent rendering my cube even when its right there.
Basically, UpdateShaders() creates my shader program and compiles, links, etc without error. I am pretty sure its correct. UpdateUniforms() just sets a couple global variables to hold uniform locations and then assigns the current matrixes for those uniforms to them. Fairly short, I think its correct.
The issue might be in CreateGeometry. I’m not sure if it sets everything up right.
My render function calls DrawCube() and then swaps the buffers. All these functions are below for reference.
Can anyone see what is wrong?
Here is what I do before entering glutMainLoop:
UpdateShaders();
UpdateUniforms();
CreateGeometry();
void UpdateShaders()
{
std::cout << "
Initializing shaders...
";
current_program = glCreateProgram();
ExitOnGLError("ERROR: Could not create the shader program");
/* Load vertex and fragment shaders */
current_frag_shader = CreateShader("fragmentshader.glsl", GL_FRAGMENT_SHADER);
current_vert_shader = CreateShader("vertexshader.glsl", GL_VERTEX_SHADER);
std::cout << "Current frag shader: " << current_frag_shader << std::endl;
std::cout << "Current vert shader: " << current_vert_shader << std::endl;
// Bind attributes
glBindAttribLocation(current_program, coords_location, "in_coords");
glBindAttribLocation(current_program, normals_location, "in_normals");
// Attach shaders
glAttachShader(current_program, current_vert_shader);
ExitOnGLError("ERROR: Could not attach vertex shader.");
glAttachShader(current_program, current_frag_shader);
ExitOnGLError("ERROR: Could not attach fragment shader.");
// Link shader program
glLinkProgram(current_program);
ExitOnGLError("ERROR: Could not link the shader program.");
glUseProgram(current_program);
ExitOnGLError("ERROR: Cannot use program in UpdateShaders.");
std::cout << "Shaders initialized...
";
}
void UpdateUniforms(){
glUseProgram(current_program);
// Set matrix uniform locations
ModelMatrixLoc = glGetUniformLocation(current_program, "ModelMatrix");
ExitOnGLError("ERROR: Could not get modelmatrix shader uniform locations in UpdateUniforms");
glUniformMatrix4fv(ModelMatrixLoc, 1, GL_FALSE, ModelMatrix.data());
ExitOnGLError("ERROR: Could not set shader in UpdateUniforms");
ViewMatrixLoc = glGetUniformLocation(current_program, "ViewMatrix");
ExitOnGLError("ERROR: Could not get viewmatrix shader uniform locations in UpdateUniforms");
glUniformMatrix4fv(ViewMatrixLoc, 1, GL_FALSE, ViewMatrix.data());
ExitOnGLError("ERROR: Could not set shader in UpdateUniforms");
}
void CreateGeometry()
{
/* Create vao and vbo for the cube */
vaos = new GLuint[1];
vbos = new GLuint[1];
ibos = new GLuint[1];
/* Define the cursor and cube for the user. */
Vertex vertices[16];
/* Cursor */
vertices[0].position = vector<float, fixed<4,-1>>(-1.0f, -1.0f, 1.0f, 1);
vertices[1].position = vector<float, fixed<4,-1>>(-1.0f, 1.0f, 1.0f, 1);
vertices[2].position = vector<float, fixed<4,-1>>(1.0f, 1.0f, 1.0f, 1);
vertices[3].position = vector<float, fixed<4,-1>>(1.0f, -1.0f, 1.0f, 1);
vertices[4].position = vector<float, fixed<4,-1>>(-1.0f, -1.0f, -1.0f, 1);
vertices[5].position = vector<float, fixed<4,-1>>(-1.0f, 1.0f, -1.0f, 1);
vertices[6].position = vector<float, fixed<4,-1>>(1.0f, 1.0f, -1.0f, 1);
vertices[7].position = vector<float, fixed<4,-1>>(1.0f, -1.0f, -1.0f, 1);
vertices[0].color = vector<float, fixed<4,-1>>(0, 0, 1, 1);
vertices[1].color = vector<float, fixed<4,-1>>(1, 0, 0, 1);
vertices[2].color = vector<float, fixed<4,-1>>(0, 1, 0, 1);
vertices[3].color = vector<float, fixed<4,-1>>(1, 1, 0, 1);
vertices[4].color = vector<float, fixed<4,-1>>(1, 1, 1, 1);
vertices[5].color = vector<float, fixed<4,-1>>(1, 0, 0, 1);
vertices[6].color = vector<float, fixed<4,-1>>(1, 0, 1, 1);
vertices[7].color = vector<float, fixed<4,-1>>(0, 0, 1, 1);
/* Cube */
vertices[8].position = vector<float, fixed<4,-1>>(-5.0f, -5.0f, 5.0f, 1);
vertices[9].position = vector<float, fixed<4,-1>>(-5.0f, 5.0f, 5.0f, 1);
vertices[10].position = vector<float, fixed<4,-1>>(5.0f, 5.0f, 5.0f, 1);
vertices[11].position = vector<float, fixed<4,-1>>(5.0f, -5.0f, 5.0f, 1);
vertices[12].position = vector<float, fixed<4,-1>>(-5.0f, -5.0f, -5.0f, 1);
vertices[13].position = vector<float, fixed<4,-1>>(-5.0f, 5.0f, -5.0f, 1);
vertices[14].position = vector<float, fixed<4,-1>>(5.0f, 5.0f, -5.0f, 1);
vertices[15].position = vector<float, fixed<4,-1>>(5.0f, -5.0f, -5.0f, 1);
vertices[8].color = vector<float, fixed<4,-1>>(0, 0, 1, 1);
vertices[9].color = vector<float, fixed<4,-1>>(1, 0, 0, 1);
vertices[10].color = vector<float, fixed<4,-1>>(0, 1, 0, 1);
vertices[11].color = vector<float, fixed<4,-1>>(1, 1, 0, 1);
vertices[12].color = vector<float, fixed<4,-1>>(1, 1, 1, 1);
vertices[13].color = vector<float, fixed<4,-1>>(1, 0, 0, 1);
vertices[14].color = vector<float, fixed<4,-1>>(1, 0, 1, 1);
vertices[15].color = vector<float, fixed<4,-1>>(0, 0, 1, 1);
/* Define how to draw the cube and cursor*/
const GLuint indices[36] =
{
0,2,1, 0,3,2,
4,3,0, 4,7,3,
4,1,5, 4,0,1,
3,6,2, 3,7,6,
1,6,5, 1,2,6,
7,5,6, 7,4,5
};
/* Generate vertex array objects */
glGenVertexArrays(1, &vaos[0]);
ExitOnGLError("ERROR: Could not generate the VAO");
glBindVertexArray(vaos[0]);
ExitOnGLError("ERROR: Could not bind the VAO");
/* Generate buffer to hold vertex data */
glGenBuffers(1, &vbos[0]);
ExitOnGLError("ERROR: Could not generate the buffer objects");
/* Bind vertex buffer object and give it data */
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");
/* Generate buffer to hold index data */
glGenBuffers(1, &ibos[0]);
ExitOnGLError("ERROR: Could not generate the buffer objects");
/* Bind index buffer and give it the index data */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibos[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the IBO to the VAO");
/* Enable vertex attributes */
glEnableVertexAttribArray(0);
//glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");
/* Set up vertex attribute pointers so that the shaders know what their input is */
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid*)0);
//glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid*)sizeof(vertices[0].position));
ExitOnGLError("ERROR: Could not set VAO attributes");
glBindVertexArray(0);
}
void DrawCube()
{
std::cout << "Drawing cube.
";
//std::cout << "Cube x rotation: " << cube_angle[0] << std::endl;
ModelMatrix.identity();
/* Rotate cube. */
matrix_rotate_about_local_x(ModelMatrix, (float) cube_angle[0]);
matrix_rotate_about_local_y(ModelMatrix, (float) cube_angle[1]);
matrix_rotate_about_local_z(ModelMatrix, (float) cube_angle[2]);
/* Position cube. */
matrix_set_translation(ModelMatrix, cube_position[0], cube_position[1], cube_position[2]);
glUseProgram(current_program);
ExitOnGLError("ERROR: Could not use the shader program");
glBindVertexArray(vaos[0]);
ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");
glUniformMatrix4fv(ModelMatrixLoc, 1, GL_FALSE, ModelMatrix.data());
glUniformMatrix4fv(ViewMatrixLoc, 1, GL_FALSE, ViewMatrix.data());
ExitOnGLError("ERROR: Could not set the shader uniforms");
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
ExitOnGLError("ERROR: Could not draw the cube");
glDrawElementsBaseVertex(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0, 8);
ExitOnGLError("ERROR: Could not draw the cube");
glBindVertexArray(0);
glUseProgram(0);
}