C : Glut don't refresh the window, 为什么?

标签 c freeglut

我试图显示一个只有两个面的简单三角形,但是当我执行它时,尝试旋转三角形,只是没有刷新:((仅当我全屏显示,或最小化并重新最大化它时刷新) )

我的代码:

#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;
 
}

截图: Starting spining it, no refresh

还有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"
}

我尝试在互联网上搜索,但我发现的所有内容都不起作用。

在 Visual Studio 代码上使用 Xubuntu 22.04 (X11)。 预先感谢您的回复。

最佳答案

简化此示例:

glutPostRedisplay 移动到空闲回调函数,并在显示回调中使用 glClear 似乎会给出接近结果的结果。

#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

关于C : Glut don't refresh the window, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77310291/

相关文章:

c++ - 设置 OpenGL - 它不会工作

c - MKL BLAS 函数的行为不符合预期

c - 使用 MPI 在后台收听和回复

c - 使用 "byte"数据类型与 protobuf-c 的示例

c++ - 有很多过剩错误

相机在 OpenGL 中不会移动

c++ - OPENGL 显示白色盒子而不是可移动立方体

C - 指向 int 的指针以获取堆栈中的元素

java - 查找所有此类三元组(从给定的一组数字中)其数字在 A.P. 中的数量的有效方法?

macos - 错误 : functions that differ only in their return type cannot be overloaded