2d grid problem

Hi, I’ve been working on a project for a graphics class and after a lot of headache I’ve decided to just ask for a hint in the right direction.

I draw the grid like this and it works fine, the problem is when I try to use glutSolidCube to draw a cube at each grid intersection. I can’t get it working at all, maybe I’ve misunderstood how the glTranslatef function works. This is for a school project so I’d like to have some explanation rather than to just have someone post code that works.


//set camera projection matrix
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(90.0, 1.0, 0.1, 100);
   
   //set camera position and orientation
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
   glTranslatef(0.0f, 0.0f, -0.25f);
   
   //setup the grid display list
   glNewList(GRID, GL_COMPILE);
   	glBegin(GL_LINES);
   	glColor3f(0.75f, 0.75f, 0.75f);
   	glLineWidth(1.0f);
   	
   	for(float i=-10.0; i<=10.0f; i+=1.0f)
   	{
      		glVertex3f(i, -10.0f, 0.0f); 
      		glVertex3f(i, +10.0f, 0.0f);
   	}
   	for(float j=-10.0f; j<=10.0f; j+=1.0f)
   	{
      		glVertex3f(-10.0f, j, 0.0f); 
      		glVertex3f(+10.0f, j, 0.0f);
   	}   	
   	glEnd();
   	glPushMatrix();
   	//glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
   	for(float i=-10.0f; i<10.0f; i+=1.0f)
   	for(float j=-10.0f; j<10.0f; j+=1.0f)
   	{
   		glColor3f(0.0f, 0.0f, 1.0f);
   		glLoadIdentity();
  		glTranslatef(i, j, 0.0f);
  		glutSolidCube(0.5f);  			  
   	}   	
   	glPopMatrix();   	   		 
   glEndList();

Ok, got it working.

When you draw your cube,calling glLoadIdentity will void all your previous transformation. Do you want this? And be careful when to push and pop matrix. See the SDK for explanation

After when your transformations are working, you will notice that
the end conditions of your for loop are wrong.

I’m still having trouble understanding exactly how to get the correct translation if I do something like this


   glColor3f(1.0f, 1.0f, 0.0f);
   glPointSize(1.0f);
   glBegin(GL_POINTS);
   for(float i=-10.0; i<10.0; i+=1.0)
   for(float j=-10.0; j<10.0; j+=1.0)
   {
      glVertex3f(i, j, 0.0f);  
   }
   glEnd();

there are points at the vertices of my grid where I am trying to draw the cube. Why doesn’t:

glPushMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glTranslatef(i, j, 0.0);
glutSolidCube(0.1f);
glPopMatrix();

work the same as:

glVertex3f(i, j, 0.0f);

EDIT: I have a reshape function


void reshape(int width, int height)
{
	float ratio = (float)width/(float)height;
	glViewport(0,0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective( 90.0, ratio, 0.1, 100 );
}

If I comment out this function the code puts the cubes at the correct intersection, why is this? Is it because this code is messing with the projection matrix?

This is my whole program:


void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
   //display the grid
   glCallList(GRID);

	glutSwapBuffers();
}

void idle()
{
   glutPostRedisplay();
}
	
void CreateGlutWindow()
{
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
   glutInitWindowPosition (5, 5);
   glutInitWindowSize (512, 512);
   win = glutCreateWindow ("Homework");
}

void CreateGlutCallbacks()
{
	glutDisplayFunc(display);
	glutIdleFunc	(idle);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutSpecialFunc(special);
}

void InitOpenGL()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glEnable(GL_DEPTH_TEST);

   //set camera projection matrix
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(90.0, 1.0, 0.1, 100);
   
   //set camera position and orientation
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
   glTranslatef(0.0f, 0.0f, -0.25f);
   
   //setup the grid display list
   glNewList(GRID, GL_COMPILE);
   	glBegin(GL_LINES);
   	glColor3f(0.75f, 0.75f, 0.75f);
   	glLineWidth(1.0f);
   	
   	for(float i=-10.0; i<=10.0f; i+=1.0f)
   	{
      		glVertex3f(i, -10.0f, 0.0f); 
      		glVertex3f(i, +10.0f, 0.0f);
   	}
   	for(float j=-10.0f; j<=10.0f; j+=1.0f)
   	{
      		glVertex3f(-10.0f, j, 0.0f); 
      		glVertex3f(+10.0f, j, 0.0f);
   	}   	
   	glEnd();
   	
   	   
      	
      	//glLoadIdentity();
      	for(float i=-10.0; i<10.0; i+=2.0)
   	for(float j=-10.0; j<10.0; j+=2.0)
   	{
   		
   		glPushMatrix();
   		glColor3f(0.0f, 0.0f, 1.0f);
  		glTranslatef(i, j, -0.25);
  				
  		glutSolidCube(0.5f);
  		glPopMatrix();
  	}
   	   	   		 
   glEndList();
}

void reshape(int width, int height)
{
	float ratio = (float)width/(float)height;
	glViewport(0,0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective( 90.0, ratio, 0.1, 100 );
}

void keyboard(unsigned char key, int x, int y)
{
	if(key == 27) exit(0);
}

void special(int key, int x, int y)
{
	if(key == GLUT_KEY_UP)
	{
		
	} else if(key == GLUT_KEY_DOWN)
	{

	} else if(key == GLUT_KEY_LEFT)
	{

	} else if(key == GLUT_KEY_RIGHT)
	{

	}
}

void ExitGlut()
{
   glutDestroyWindow(win);
   exit(0);
}

int main (int argc, char **argv)
{
	glutInit(&argc, argv); 
	CreateGlutWindow();
	CreateGlutCallbacks();
	InitOpenGL();
  
   	glutMainLoop();

	ExitGlut();
	return 0;
}

Your reshape function change the current matrix to the GL_PROJECTION. Be sure to set it back to GL_MODELVIEW.

awesome it works, thanks Zbuffer

In your reshape function you forget to call glMatrixMode(GL_MODELVIEW) at the end. So when this function is called, all subsequent transform modify the projection matrix.

glPushMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glTranslatef(i, j, 0.0);
glutSolidCube(0.1f);
glPopMatrix();

work the same as:

glVertex3f(i, j, 0.0f);

and for example only, work the same as


glPushMatrix();
glTranslatef(i, j, 0.0);
glVertex(0.0,0.0,0.0);
glPopMatrix();

Readthis to understand how viewing and modeling transform work with OpenGL.