Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:13:58+00:00 2026-06-01T20:13:58+00:00

Consider this code: #include <stdlib.h> #include <stdarg.h> #include <GLUT/GLUT.h> #include <OpenGL/OpenGL.h> double width=600; double

  • 0

Consider this code:

#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

double width=600;
double height=600;

void processMouse(int button, int state, int x, int y)
{
    glColor4f(1.0,0.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glFlush();
}

static void render()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glutMouseFunc(processMouse);
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);                                         
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
    glutInitWindowSize(width, height);
    glutCreateWindow("Board");  
    glutDisplayFunc(render);
    glutMainLoop();
}

The render function is executed, and every time a click is performed, it should start the function processMouse.
So if the mouse is clicked, all the window should become red with the instructions:

    glColor4f(1.0,0.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glFlush();

But when I click the mouse I notice a strange behaviour: only a part of the window gets colored, the part at bottom left (instead of all the screen).
The window remains in this state, until I open a google chrome window.If I open a google chrome (or another graphical application), all the window become colored of red.
Why this? I also have problems with more complex programs, it seems like sometime the glVertex instructions are ignored.If I try debugging the program with fprintf it appears that everything is ok, and everything seems to go like expected (for example I tried to print mouse coordinates in the processMouse function, they were ok), except for the fact that what I draw is ignored.

Edit:
I have modified this code but it still has the same problem:

#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

double width=600;
double height=600;
bool down=false;;

// http://elleestcrimi.me/2010/10/06/mouseevents-opengl/


static void render()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    if(down)
    {
        glColor4f(1.0,0.0,0.0,0.0);
        glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(1.0, 1.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
        glEnd();
        glFlush();
    }
}

void processMouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN)
    {
        down=true;
        glutPostRedisplay();
    }
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);                                         
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
glutInitWindowSize(width, height);
glutCreateWindow("Board"); 
glutMouseFunc(processMouse);
glutDisplayFunc(render);
glutMainLoop();
}

Still getting only a part of the screen red.

PS: Solved using glutSwapBuffers(), thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:13:58+00:00Added an answer on June 1, 2026 at 8:13 pm

    When you are using double buffering with GLUT, you need to call glutSwapBuffers() to see the result of the draw.

    Add this to the end of your render() function and it will work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned
Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider this code: #include <iostream> template<class C> struct time { }; int main() {
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.