Clear only left half of the window with red color

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;
        return -1;

    // Make the window's context current

    // 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

        // Swap front and back buffers

        // Poll for and process events

    // Terminate GLFW
    return 0;

Hi @fonis,

Welcome to the GLFW forum and to starting OpenGL.

The viewport does not affect the glClear operation, instead you need to use glScissor.

For example add:

glScissor(0, 0, width/2, height);

I get a red left half and black right half if I use this on my PC, however it should be noted that the state of an uncleared buffer might be undefined, and you may need to clear the right half to black to guarantee this is what you get.

I am not sure why you are getting a green window, this seems like a driver issue as glClear defines the parameters as red, green, blue, alpha.

Good luck with learning OpenGL.


I apologize for saying " But the resulting windows is completely green.". The code that I shared makes the window completely red. I have fixed the problem description.

Your suggestion does work. Now, I am able to draw in parts of the window by calling glScissor() as required.

Thanks a lot. :grinning: