*RESOLVED*Drawing a grid in openGL
This question sounds noobish, but its been giving me problems for a while. Here goes...
HOW DO I DRAW A GRID IN OPENGL? Its just a simple 2D grid. I'm using an orthographic projection with a maximum x and maximum y values of 100 and minimums of 0. I need a little bit of code....I'm not lazy, I'm going MAD!!!
I want the user to be able to specify the dimensions of a map, and a grid of lines to be drawn. With all the previous pieces of code I tried, I get a grid of lines, but some of them are offset, messing up the grid. I want each space to be equal, even if the grid goes offscreen.
I'm actually using this for a tile based RPG. If anyone here has ever used RPG Maker 2003 or such, it would be easier to understand the sort of grid I'm talking about.
Thanks in advance.
[Edited by - GavRobbs on December 30, 2007 10:29:04 AM]
Use GL_LINES. E.G:
replace the constants in that code (the numbers I hard coded) with some variables, and put the code in a function, something like:
drawGrid(float xpos, float ypos, float xsize, float ysize, float xspacing, float yspacing);
I'll leave the rest to you.
for(int x = 0; x < 10; x++){ glBegin(GL_LINE_LOOP); glVertex3f(x,0,0); glVertex3f(x,10,0); glEnd();};for(int y = 0; y < 10; y++){ glBegin(GL_LINE_LOOP); glVertex3f(y,0,0); glVertex3f(y,10,0); glEnd();};
replace the constants in that code (the numbers I hard coded) with some variables, and put the code in a function, something like:
drawGrid(float xpos, float ypos, float xsize, float ysize, float xspacing, float yspacing);
I'll leave the rest to you.
Been there, done that. The squares on the grid always turn out too small. If I set the numbers in the coordinates of glVertex to bigger numbers, it ends up with a shape like a big box in the middle, but a lot of lines around the edges.
[source lang = "cpp"]void CApplication::DrawBox(){ glBegin(GL_LINE_LOOP); glVertex2i(0,0); glVertex2i(0,10); glVertex2i(10,10); glVertex2i(10,0); glEnd(); };void CApplication::DrawGrid(){ int i = 0; int j = 0; for(i = 0; i < data.width;i++) { for(j = 0; j < data.height;j++) { glLoadIdentity(); glTranslatef((i * 10),(j * 10),0.0f); DrawBox(); }; }; DrawBox();};
There it is. I hope that you can help me out. Remember the max is (100,100) and (0,0) is the minimum.
If you draw them as boxes, you're duplicating a lot of lines. There isn't any reason to do two loops either. If you're in ortho mode with a height and width of 100:
And of course use dimension variables where appropriate.
[Edited by - tstrimp on December 29, 2007 10:03:19 PM]
glBegin(GL_LINES);for(int i = 0; i <= 100; i += 10){ glVertex3f((float)i, 0.0f, 0.0f); glVertex3f((float)i, 100.0f, 0.0f); glVertex3f(0.0f, (float)i, 0.0f); glVertex3f(100.0f, (float)i, 0.0f)}glEnd();
And of course use dimension variables where appropriate.
[Edited by - tstrimp on December 29, 2007 10:03:19 PM]
im drunk so ill answer
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin( GL_LINES );
float X = 20; // how many lines
for ( float f=-1.0 f<=1.0; f += 2.0/(X))
glVertex3f(f,-1);
glVertex3f(f,1);
glEnd();
im sure u can manage the horizontal lines
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin( GL_LINES );
float X = 20; // how many lines
for ( float f=-1.0 f<=1.0; f += 2.0/(X))
glVertex3f(f,-1);
glVertex3f(f,1);
glEnd();
im sure u can manage the horizontal lines
Actually, my drawing code was correct. My problem was with the window. The window was set to be 800 * 600. Well, when the window is normal sized, everything seems okay, but when maximized there's a problem. Then I realized... Normal sized is 800*600 and maximized is 1024 * 768, my current screen size. So therefore, the window shouldn't be maximized. DUH! lol.
Thanks for all the help anyway.
Thanks for all the help anyway.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement