I'm just starting out using GLEW and GLFW in a C++ project for my rendering code, but I'm having trouble actually getting anything to render.
I know OpenGL is working in some capacity, because I am able to change the background colour, but nothing else is working beyond that.
I'm setting up the OpenGL context with this code:
//Attempts to initiate GLFW. If it doesn't, it returns immediately
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return;
}
//glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
window = GLFWwindowPtr();
window.reset(glfwCreateWindow(width, height, windowTitle, NULL, NULL), DeleteWindow);
if (window.get() == nullptr) {
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
return;
}
glfwMakeContextCurrent(window.get());
glewExperimental = GL_TRUE; // Needed in core profile
int errorCode = glewInit();
if (errorCode) {
printf("%i", errorCode);
fprintf(stderr, "Failed to initialize GLEW\n");
return;
}
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window.get());
A little while after that in my main loop, I call this code every frame:
float ratio;
int width, height;
glfwGetFramebufferSize(WindowHandler::getMainWindowGLPointer(), &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClearColor(red / 255, green / 255, blue / 255, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
Now this code works. I'm incrementing the red, green and blue variables every frame so that I can check that OpenGL is working as a whole and I see the background colour change.
However, when I put the following code after it, I'm seeing nothing different appear.
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(red/255, green/255, blue/255); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
I used the code from an existing example found on https://www3.ntu.edu.sg/home/ehchua/programming/opengl/CG_Introduction.html so I had a better chance of it being something in my implementation and not me messing up trying to write OpenGL code.