1

当我尝试绘制圆环时,没有显示任何内容,但代码在我朋友的 PC 上完美运行。绘制立方体或其他简单形状时我没有问题。我是否忘记了什么或可能是什么问题?

代码:

#include <GL/glut.h>
#include <stdio.h>


GLint width = 600;
GLint height = 600;

GLdouble eyex = 3.0, eyey = 0.0, eyez = 5.0;
GLdouble atx = 0.0, aty = 0.0, atz = 0.0;
GLdouble vupx = 0.0, vupy = 1.0, vupz = 0.0;

GLdouble fovy = 120.0, aspect = 1.0, Znear = 1.0, Zfar = 10.0;

GLfloat position[] = {10.0, 0.0, 1.5, 10.0 };


static void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5,0.1,1.1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, atx, aty, atz, vupx, vupy, vupz);
glColor3f(1.0,0.0,0.0);


glutSolidTorus(1.0,2.0,100,100);
glFlush();

}


void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_FLAT);
}


void reshape(int winw, int winh) {
glViewport(0,0,winw,winh); //Otvor
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, aspect, Znear, Zfar);

}

static void key(unsigned char key, int x, int y)
{
switch (key)
{
    case 27 :
        exit(0);
        break;
}
}

void mouse(int button, int state, int x, int y)
{

if (button == GLUT_LEFT_BUTTON)
{
    glPushMatrix();
glRotatef(60,0.0,5.0,0.0);;

glLightfv(GL_LIGHT0,GL_POSITION,position);
glPopMatrix();
glFlush();
}
}


int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(width,height);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutCreateWindow("Light");
init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glLightfv(GL_LIGHT0,GL_POSITION,position);


glutKeyboardFunc(key);
glutMainLoop();

//return EXIT_SUCCESS;
//return exit_success;
}
4

1 回答 1

0

请求深度缓冲区:

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

并确保清除它:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
于 2012-06-05T15:55:00.370 回答