Advertisement

Problem: Displaylist gives different scene than direct function call

Started by July 19, 2004 08:02 AM
0 comments, last by Enigma 20 years, 4 months ago
I want to draw a cube with the vertices 0/0/0 0/0/10 0/10/0 0/10/10 10/0/0 10/0/10 10/10/0 10/10/10 Now I want a little sphere at each vertex. To do this I have a display list that creates an approximated sphere around 0/0/0. The following C++ function is used to generate a sphere at each vertex: void pre_opengl_graph::opengl_draw_spheres() { for (pre_opengl_object<pre_opengl_vertex>* vertex=v.first(); vertex!=0; vertex=vertex->next) { // cycling through all vertices glPushMatrix(); glTranslated(vertex->item->x(),vertex->item->y(),vertex->item->z()); // vertex->item->x() gives the x coordinate of the current vertex glCallList(spherelist); // this display list generates an approximated sphere around 0/0/0 glPopMatrix(); } } However, if I execute the following in the function that draws the scene, glNewList(drawlist,GL_COMPILE_AND_EXECUTE); graph->opengl_draw_spheres(); // calls the function above glEndList(); I get 1 (one) sphere only at 0/0/0. If I execute graph->opengl_draw_spheres(); // calls the function above instead of the above (in the same line), I get all 8 spheres nicely positioned at the vertices of the cube. Has anybody any idea why OpenGL behaves this way (or what I am doing wrong)?
The following program should do exactly what your code is trying to do. Run it and see if you get the same problem. If the same thing happens then it's either a problem with your drivers or the code doesn't respect the OpenGL spec in some way (couldn't see any problems but I only make a cursory check). If you get the correct output from the program below then the problem is somewhere else in your code, probably memory corruption seeing as your using lots of raw pointers.
#include <algorithm>#include <vector>#include <GL/glut.h>// comment this out to render without compiling the result into the draw list#define COMPILE_DRAW_LISTconst GLuint sphereList = 1;const GLuint drawList = 2;class Vertex{	public:		Vertex(GLfloat vertexX = 0, GLfloat vertexY = 0, GLfloat vertexZ = 0)			:			x(vertexX),			y(vertexY),			z(vertexZ)		{		}		void locate() const		{			glTranslatef(x, y, z);		}	private:		GLfloat x, y, z;};std::vector<Vertex> v;class DrawSphereAtVertex{	public:		void operator()(const Vertex& vertex) const		{			glPushMatrix();				vertex.locate();				glCallList(sphereList);			glPopMatrix();		}};void drawSpheres(){	std::for_each(v.begin(), v.end(), DrawSphereAtVertex());}void testFunc(){#if defined(COMPILE_DRAW_LIST)	glNewList(drawList,GL_COMPILE_AND_EXECUTE);		drawSpheres();	glEndList();#else	drawSpheres();#endif}void buildCube(){	v.push_back(Vertex(0, 0, 0));	v.push_back(Vertex(0, 10, 0));	v.push_back(Vertex(10, 0, 0));	v.push_back(Vertex(10, 10, 0));	v.push_back(Vertex(0, 0, 10));	v.push_back(Vertex(0, 10, 10));	v.push_back(Vertex(10, 0, 10));	v.push_back(Vertex(10, 10, 10));}void buildSphereList(){	glNewList(sphereList, GL_COMPILE);		glutWireSphere(1, 8, 8);	glEndList();}void reshape(int width, int height){	glViewport(0, 0, width, height);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,50.0f);	glMatrixMode(GL_MODELVIEW);}void render(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glTranslatef(-5, -5, -30);	testFunc();	glutSwapBuffers();}int main(int argc, char** argv){	glutInit(&argc, argv);	glutInitWindowPosition(0, 0);	glutInitWindowSize(800, 600);	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);	glutCreateWindow("Display List Test");	glutDisplayFunc(render);	glutIdleFunc(render);	glutReshapeFunc(reshape);	buildSphereList();	buildCube();	glutMainLoop();}


Enigma

This topic is closed to new replies.

Advertisement