我不知道为什么当我运行此代码并使用菜单时,我在屏幕上看不到任何形状。当我用 glu[insertshape] 和相同的参数替换 glCallLists 行时,它可以完美运行,但代码行完全相同,但这次通过显示列表调用它并不起作用。
我什至使用打印语句进行检查,代码肯定正在运行
任何人都可以帮忙吗?
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <cstdio>
GLUquadricObj* ptrSphere;
GLUquadricObj* ptrCylinder;
GLUquadricObj* ptrDisk;
GLUquadricObj* ptrPartDisk;
GLuint sphereListID;
GLuint cylinderListID;
GLuint diskListID;
GLuint partialDiskListID;
GLuint menuID;
GLuint currentListID;
void drawSphere()
{
gluSphere(ptrSphere, 2, 25, 25);
printf("sphere");
}
void drawCylinder()
{
gluCylinder(ptrCylinder, 3, 3, 4, 25, 25);
printf("cylinder");
}
void drawDisk()
{
gluDisk(ptrDisk, 1.5, 3, 25, 25);
printf("disk");
}
void drawPartDisk()
{
gluPartialDisk(ptrPartDisk, 1.5, 3, 25, 25, 0, 90);
printf("part disk");
}
void initQuadrics()
{
ptrSphere = gluNewQuadric();
gluQuadricDrawStyle(ptrSphere, GLU_LINE);
ptrCylinder = gluNewQuadric();
gluQuadricDrawStyle(ptrCylinder, GLU_LINE);
ptrDisk = gluNewQuadric();
gluQuadricDrawStyle(ptrDisk, GLU_LINE);
ptrPartDisk = gluNewQuadric();
gluQuadricDrawStyle(ptrPartDisk, GLU_LINE);
}
void initLists()
{
sphereListID = glGenLists(0);
cylinderListID = glGenLists(0);
diskListID = glGenLists(0);
partialDiskListID = glGenLists(0);
glNewList(sphereListID, GL_COMPILE);
drawSphere();
glEndList();
glNewList(cylinderListID, GL_COMPILE);
drawCylinder();
glEndList();
glNewList(diskListID, GL_COMPILE);
drawDisk();
glEndList();
glNewList(partialDiskListID, GL_COMPILE);
drawPartDisk();
glEndList();
currentListID = sphereListID;
}
void myMenu(int value)
{
switch(value)
{
case(1):
currentListID = sphereListID;
break;
case(2):
currentListID = cylinderListID;
break;
case(3):
currentListID = diskListID;
break;
case(4):
currentListID = partialDiskListID;
break;
}
glutPostRedisplay();
}
void initMenu()
{
menuID = glutCreateMenu(myMenu);
glutSetMenu(menuID);
glutAddMenuEntry("Sphere", 1);
glutAddMenuEntry("Cylinder", 2);
glutAddMenuEntry("Disk", 3);
glutAddMenuEntry("Partial Disk", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init()
{
initQuadrics();
initLists();
initMenu();
}
void display()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(currentListID);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - First window demo");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}