Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / OpenGL

GridLine, opengl with C++

4.00/5 (1 vote)
7 Jan 2012CPOL 34.7K  
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):
C#
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).
C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)