I'm trying to create a few demos to help my understanding of OpenGL, GLEW and GLFW. I found something interesting that I cannot seem to correct. I'm trying to render a simple set of a checkerboard.
float points[] = {
0.0f, 1000.0f, 0.0f,
500.0f, 500.0f, 0.0f,
0.0f, 500.0f, 0.0f,
0.0f, 1000.0f, 0.0f,
500.0f, 500.0f, 0.0f,
500.0f, 1000.0f, 0.0f,
1000.0f, 0.0f, 0.0f,
500.0f, 500.0f, 0.0f,
1000.0f, 500.0f, 0.0f,
1000.0f, 0.0f, 0.0f,
500.0f, 500.0f, 0.0f,
500.0f, 0.0f, 0.0f
};
If I create the window with the initial size 1000,1000:
GLFWwindow* window = glfwCreateWindow(1000, 1000, "Simple example", NULL, NULL);
The checkerboard shows up correctly:
however if I create the window initially with 640,480 size and resize it, I get the following image:
I've tried a few different ways of resizing, this seemed to be the simplest:
int FB_width, FB_height;
glfwGetFramebufferSize(window, &FB_width, &FB_height);
int win_width, win_height;
glfwGetWindowSize(window, &win_width, &win_height);
float width = 1000;
float height = 1000;
glfwSetWindowSize(window, width, height);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers (window);
where:
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
I've tried creating the window larger initially (1200,1600) and then resizing smaller (640, 480) and then increasing it again to 1000,1000: same results. I know there is a difference between window coordinates and pixel coordinates, however I thought those would be correlated with the callback function.
I did not see a way to set the framebuffer size directly, is there something else that needs to be done?
Alas, it was in my Projection matrix to the vertex shader, I was using the wrong parameter for PRO_mat, my width and height were wrong.
// Vector of 3 points, add the 4th value in the vertex shader
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"uniform mat4 PRO_mat;"
"void main () {"
" gl_Position = PRO_mat*vec4 (vp, 1.0);"
"}";
Eigen::Matrix<GLfloat,4,4> pro_mat_inv = Eigen::Matrix4f::Identity();
pro_mat_inv(0,0) = width/2;
pro_mat_inv(0,3) = width/2;
pro_mat_inv(1,1) = height/2;
pro_mat_inv(1,3) = height/2;