问题描述
我不知道为什么当我运行此代码并使用菜单时,我在屏幕上看不到任何形状。当我用glu [insertshape]和相同的参数替换glCallLists行时,它可以很好地工作,但是完全相同的代码行,但是这次是通过显示列表调用的,所以行不通。
我什至使用了print语句进行检查,并且代码肯定正在运行
有人可以帮忙吗?
#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,4,25);
printf("cylinder");
}
void drawdisk()
{
gludisk(ptrdisk,1.5,25);
printf("disk");
}
void drawPartdisk()
{
gluPartialdisk(ptrPartdisk,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,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glCallList(currentListID);
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-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;
}
解决方法
我不确定这是否是OPs程序中唯一的问题,但绝对是一个问题:
void initLists()
{
sphereListID = glGenLists(0);
cylinderListID = glGenLists(0);
diskListID = glGenLists(0);
partialDiskListID = glGenLists(0);
文档。 glGenLists()的内容对此非常明确:
。
glGenLists
有一个参数, range 。它返回整数 n ,从而创建 range 连续的空显示列表,命名为n,n + 1,...,n + range-1。 如果 range 为0,,如果不存在一组范围连续的名称,或者生成任何错误,则不生成显示列表,并返回0
(强调我的。)
所以,我建议:
- 使用
glGenLists(1)
代替glGenLists(0)
(甚至使用单个glGenLists(4)
一次生成所有列表ID)。 - 检查
glGenLists()
的返回值是否不为0。
在切换到OpenGL 3+之前,我们使用显示列表来改善Visual Simulation应用程序中的性能。效果非常好(我们甚至在使用缓冲区和着色器的OpenGL 3+中甚至都很难获得相同的性能)。
使用显示列表仍然可以在最近的专业(昂贵)图形卡上正常使用。在(便宜得多的)消费类图形卡上,我们注意到旧代码的性能下降不令人满意,而仅使用OpenGL 3+功能时,我们可以达到可比的性能。