You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GLUT窗口旋转图形时无法实时刷新,求排查解决

问题:GLUT旋转图形时窗口不刷新,仅切换窗口状态后更新

我尝试使用GLUT绘制一个简单图形,运行程序后按按键旋转图形时,窗口完全不刷新,仅在切换至全屏或最小化后恢复窗口时才会刷新。

我的代码

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

截图说明

  • 启动状态:窗口显示初始绘制的图形
  • 旋转时无刷新:按下旋转按键后窗口内容无变化,仅切换窗口状态(全屏/最小化恢复)后才会显示旋转后的图形

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

当前环境为Xubuntu 22.04(X11)+Visual Studio Code,已在网上搜索相关解决方案但均无效,恳请提供解决思路。

内容的提问来源于stack exchange,提问作者youma64

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:35:53