Jump to content
  • Advertisement
Sign in to follow this  
GavRobbs

OpenGL *RESOLVED*Drawing a grid in openGL

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Use GL_LINES. E.G:


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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:


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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!