我试图显示一个只有两个面的简单三角形,但是当我执行它时,尝试旋转三角形,只是没有刷新:((仅当我全屏显示,或最小化并重新最大化它时刷新) )
我的代码:
#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();
}
关于C : Glut don't refresh the window, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77310291/