12,457,187 members (57,405 online)
Tip/Trick
alternative version

24.5K views
3 bookmarked
Posted

# GridLine, opengl with C++

, 7 Jan 2012 CPOL
 Rate this:
GridLine, opengl with C++

This is simple code for gridline.
There are 2 options that I will show here (I guess there are more options, for example with vector list or what's up your mind).

First, if you want similar distance for x and y (the distance from line to line will be similar to the height and width):
```const float sizeL = 3.f;
const float grid = 1.f;

glPointSize(sizeL);
glBegin(GL_LINES);
for(float i=-10; i<10; i+=grid)
{
glVertex3f(-10.f, i, 0.f);
glVertex3f(10.f, i, 0.f);

glVertex3f(i, -10.f, 0.f);
glVertex3f(i, 10.f, 0.f);
}
glEnd();
Write```
Second, if you want a different distance between x and y (the distance from line to line will be different, the height-distance will be diffrent from the width-distance).
```const float sizeL = 3.f;
const float gridX = 1.f;
const float gridY = 1.f;

glPointSize(sizeL);
glBegin(GL_LINES);
for(float i=-10; i<10; i+=gridX)
{
glVertex2f(-10.f, i);
glVertex2f(10.f, i);
}
for(float i=-10; i<10; i+=gridY)
{
glVertex2f(-10.f, i);
glVertex2f(10.f, i);
}
glEnd();```
You can change the numbers as you want.
I hope it helped someone.

## About the Author

 United States
No Biography provided

## You may also be interested in...

 Pro Pro

## Comments and Discussions

 First Prev Next
 Infinite Lines Madhan Mohan Reddy15-Apr-13 1:11 Madhan Mohan Reddy 15-Apr-13 1:11
 You could use glVertex2f, for 2D case. Also, in second code ... SiarheiY3-Jan-12 5:32 SiarheiY 3-Jan-12 5:32
 Re: ok, I fixed it. tombog04-Jan-12 8:18 tombog0 4-Jan-12 8:18
 Last Visit: 31-Dec-99 18:00     Last Update: 31-Aug-16 0:32 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.