Grid creation with OpenGl problem

here is code that should create a grid based on the inputted number of rows and columns, the problem is its not printing the entire grid for example for a 3 X 3 grid it will draw to the screen this

|||_
|||_
|||_

i cant figure out why its not printing the whole grid
here is my code

#include <Gl/glut.h>
#include <iostream.h>
#include <stdlib.h>

#define winH 480
#define winW 640

int window,rows=0,cols=0;
int north[50][50],east[50][50];

void init(void)
{
glViewport(0,0,winW,winH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,100.0,0.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0,0.0,0.0,0.0);
}

void keypress(unsigned char key, int x, int y)
{

if (key==27) 
{ 
	glutDestroyWindow(window); 

	exit(0);                   
}

}

void idle(void)
{
for(int x=0;x<rows;x++){
for(int y=0;y<cols;y++){

		if(x&gt;-1 && y&gt;-1)
			 north[x][y]=1;
		else north[x][y]=0;
		
		if(x&gt;-1 && y&gt;-1)
			 east[x][y]=1;
		else east[x][y]=0;

	}
}
glutPostRedisplay();

}

void gl_Draw(void)
{
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int i,j;

glClear(GL_COLOR_BUFFER_BIT);

for(i=0,x1=30,x2=34,x3=30,x4=30;i&lt;rows+1;i++,x1+=4,x2+=4,x3+=4,x4+=4){
	
	for(j=0,y1=20,y2=20,y3=25,y4=20;j&lt;cols+1;j++,y1+=5,y2+=5,y3+=5,y4+=5){

		
		if(north[i][j]==1 && east[i][j]==1)
		{
			glBegin(GL_LINES);
			glVertex2i(x1,y1);
			glVertex2i(x2,y2);
			glVertex2i(x3,y3);
			glVertex2i(x4,y4);
			glEnd();

		}
		
	}
}
glutSwapBuffers();

}

void main (int argc,char** argv)
{

cout&lt;&lt;"Welcome to the maze"&lt;&lt;endl&lt;&lt;endl;
cout&lt;&lt;"Enter in the rows:";
cin&gt;&gt;rows;
cout&lt;&lt;endl;
cout&lt;&lt;"Enter in the columns:";
cin&gt;&gt;cols;
cout&lt;&lt;endl;

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(winW,winH);

glutInitWindowPosition(100,100);

window=glutCreateWindow("Project1-The Maze");	

glutDisplayFunc(&gl_Draw);

glutIdleFunc(&idle);

glutKeyboardFunc(&keypress);

init();

glutMainLoop();

}

At first glance, this seems strange:

void idle(void)
{
for(int x=0;x<rows;x++){
for(int y=0;y<cols;y++){
if(x>-1 && y>-1)
north[y]=1;
else north[y]=0;
if(x>-1 && y>-1)
east[y]=1;
else east[y]=0;
}
}
}

  • The conditions are always true. PS: and identical, no need to compare twice!
  • The loops do not iterate to the same values as your drawing loops, but one less.
  • Why is that done in the idle loop? Isn’t this one-time init stuff.

[This message has been edited by Relic (edited 02-06-2001).]

thanks for the response but how do i fix it so it draws the last column correctly and the top row, also this is done in the idle loop because eventually its going to be a random maze generator and the values of the arrays will change in hte idle function so the drawing part can draw the maze. i just want to get the grid drawn first then i will worry about doing all the randomization

I haven’t done a maze for a very long time now. There must be some documentation on mazes on the web.

I think you should spend a little time on the data structures. With a regular grid, I would not define walls, but free space (0) and blocked space (1).
Iterating over the 2D grid, a wall must be drawn for all grid state changes, means between two cells containing 0 and 1, or 1 and 0). Very simple.
This also allows a simple check for the collision with the walls whil emoving through the maze.
You’ll figure it out.

With a small 2D culling, which only takes the grid cells into consideration which can be seen from the viewpers position and orientation the complexity of the drawing loop can be optimized.