cfreeglut

C : Glut don't refresh the window, why?


I was trying to display a simple triangle with only 2 face, but when I execute it, trying to spin the triangle, there was just no refresh :( (refreshing only when I go fullscreen, or minimize and re-maximize it)

my code :

#include <GL/freeglut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <math.h>//useless but I will need it soon.

float rotx, roty = 0.0f;

char srotx[40];

int win;

void display()
{
    
    glViewport(0,0,1000,800);
    
    glBegin(GL_QUADS);
    glColor3f(0.5,0,0);
    glVertex3f(0.5,0.5,0);
    glColor3f(0,0.5,0);
    glVertex3f(-0.5,0.5,0.5);
    glColor3f(0,0,0.5);
    glVertex3f(0,-0.5,0.5);
    glColor3f(0.5,0.5,0);
    glVertex3f(-0.5,0.5,-0.5);
    glEnd();
    
    glRotatef(rotx,1.0f,0.0f,0.0f);
    glRotatef(roty,0.0f,1.0f,0.0f);
    
    glutSetWindowTitle(srotx);//not working
    glutSwapBuffers();
    
}

void BindInput(unsigned char key, int Mx, int My)
{
    switch(key)
    {
        case 'z' :
            rotx += 0.2f;
            break;
        case 's' :
            rotx -= 0.2f;
            break;
        case 'd' :
            roty += 0.2f;
            break;
        case 'q' :
            roty -= 0.2f;
            break;
    }

    if(key == 27)
    {
        glFinish();
        exit(0);
    }
    if(rotx >= 360.0f || key == 'a' || roty >= 360.0f)
    {
        glutPostWindowRedisplay(win);//testing...
        rotx = 0.0f;
        roty = 0.0f;
    }
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(1000,800);
  
  sprintf(srotx, "DEBUG Mayo Test, rotx : %f", rotx);
  win = glutCreateWindow(srotx);
  glutSetCursor(GLUT_CURSOR_DESTROY);
  glEnable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);
  glutDisplayFunc(display);
  glutIdleFunc(display);
  glutKeyboardFunc(BindInput);
  glutMainLoop();

  return 0;
 
}

screenshots : Starting spining it, no refresh

also the tasks.json :

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc générer le fichier actif",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lglut",
                "-lGLU",
                "-lGL"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Tâche générée par le débogueur."
        },
    ],
    "version": "2.0.0"
}

I tried to search on internet but every things that I found was just not working.

Using Xubuntu 22.04 (X11) on Visual Studio code. Thanks in advance for any reply.


Solution

  • Paring this example down:

    Moving glutPostRedisplay to an idle callback function, and using glClear in the display callback would appear to give something approaching a result.

    #include <GL/freeglut.h>
    #include <GL/gl.h>
    
    static float rotx, roty;
    
    static void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glViewport(0, 0, 1000, 800);
    
        glBegin(GL_QUADS);
        {
            glColor3f(0.5, 0, 0);
            glVertex3f(0.5, 0.5, 0);
    
            glColor3f(0, 0.5, 0);
            glVertex3f(-0.5, 0.5, 0.5);
    
            glColor3f(0, 0, 0.5);
            glVertex3f(0, -0.5, 0.5);
    
            glColor3f(0.5, 0.5, 0);
            glVertex3f(-0.5, 0.5, -0.5);
        }
        glEnd();
    
        glRotatef(rotx, 1.0f, 0.0f, 0.0f);
        glRotatef(roty, 0.0f, 1.0f, 0.0f);
    
        glutSwapBuffers();
    }
    
    static void idle(void)
    {
        glutPostRedisplay();
    }
    
    static void input(unsigned char key, int x, int y)
    {
        (void) x;
        (void) y;
    
        switch (key) {
            case 'z' :
                rotx += 0.2f;
                break;
            case 's' :
                rotx -= 0.2f;
                break;
            case 'd' :
                roty += 0.2f;
                break;
            case 'q' :
                roty -= 0.2f;
                break;
        }
    
        if (27 == key) {
            glFinish();
            exit(0);
        }
    
        if ('a' == key || rotx >= 360.0f || roty >= 360.0f) {
            rotx = 0.0f;
            roty = 0.0f;
        }
    }
    
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(1000, 800);
    
        glutCreateWindow("example"); // this leaks
    
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
    
        glutDisplayFunc(display);
        glutIdleFunc(idle);
        glutKeyboardFunc(input);
    
        glutMainLoop();
    }
    

    object spinning