• ### Popular Now

• 13
• 18
• 19
• 27
• 10

#### Archived

This topic is now archived and is closed to further replies.

# Trying to draw a simple grid

This topic is 5667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ve done lots of 2 dimensional opengl programming, but have just started with 3 dimensional. I was wondering if someone might help me. I''ve been used to using gluOrtho2D to set up my coordinate system, and now I''m trying to switch to 3d. I don''t really understand how this is working though, as you are not really setting up a coordinate system, I mean, like what are coordinate system are you in to begin with before anything happens, is it based on window pixels or what, and when you use gluLookAt and gluPerspective, how can you really figure out what coordinate will go where? I''ve tried to draw just a simple plane on the x and z axis, but can''t see it and don''t understand why not. Here is the source


#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <glut.h>
#include <cstdlib>

void Init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor3f(0.0, 1.0f, 1.0f);
glViewport(0, 0, 1024, 768);

glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,(GLfloat)1076/(GLfloat)768, 0.5f, 150.0f);

glMatrixMode(GL_MODELVIEW);
}

void DrawPlane(double StartX, double EndX, double StartZ, double EndZ)
{
glBegin(GL_LINES);
for(int i = StartX; i < EndX; i++)
glVertex3f(i, 0, EndX);
glVertex3f(i, 0, StartX);
glEnd();

glBegin(GL_LINES);
for(int j = StartZ; j < EndZ; j++)
glVertex3f(50, 0, EndZ);
glVertex3f(-50, 0, StartZ);
glEnd();
}

void MyDisplay(void)
{
gluLookAt(0.0f, 5.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawPlane(-1000, 1000, -1000, 1000);
glutSwapBuffers();
}

void MyKeyboard(unsigned char key, int x, int y)
{
switch(key)
{
case ''Q'':
case ''q'':
exit(0);
break;
default:
break;
}
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("My Window");
glutFullScreen();
Init();
glutDisplayFunc(MyDisplay);
glutKeyboardFunc(MyKeyboard);
glutMainLoop();
return EXIT_SUCCESS;
}

thanks for any help in advance

##### Share on other sites
Well, from what I can see, you're trying to draw a grid in the y = 0 plane. However, as the camera also resides in the y = 0 plane, you can't see it (I'm assuming from the gluLookAt function that you're looking down and haven't moved the camera. I may be wrong; I have no experience with gluLookAt). Try this: reset the modelview matrix and you'll be looking directly down the -z axis. Replace the gluLookAt call with this line:

glTranslatef(0.0f, -10.0f, 0.0f);

before you call DrawPlane(). This will translate whatever you draw ten units down the y axis (or possibly up. I forget.) You should now be able to see the grid below (or above) you, stretching out into the distance, like a perspective projection should. Also, you might want to increase the far argument in gluPerspective. Make it something like 500.0f, rather than 150. You'll be able to see more...

Movie Quote of the Week:

"You mean, let me understand this...cuz I...maybe it's me,
maybe I'm a little f**ked up maybe. I'm funny how, I mean
funny, like I'm a clown? I amuse you? I make you laugh?
I'm here to f**kin' amuse you? Whattya you mean funny?
Funny how? How am I funny?"
- Tommy, Goodfellas.
Try http://uk.geocities.com/mentalmantle. Seriously. It's brilliant.
And I just completely redesigned it so it looks even better.
DarkVertex Beta 0.8 available soon!

[edited by - iNsAn1tY on September 13, 2002 9:22:28 PM]