I am a beginner of both OpenGL and GLFW.
I am trying to write my first OpenGL+GLFW program with inspiration from LearnOpenGL.
I am trying to clear only the left half with red color. But the resulting windows is completely red.
Please help me understand what I am doing wrong:
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(800, 600, "Red Left Half", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Set the framebuffer resize callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Loop until the user closes the window
while (!glfwWindowShouldClose(window)) {
// Set the clear color to red
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Red
// Clear the left half of the window
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width / 2, height); // Adjust viewport to cover left half
glClear(GL_COLOR_BUFFER_BIT);
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
// Terminate GLFW
glfwTerminate();
return 0;
}