Click here to Skip to main content
15,881,882 members
Articles / Multimedia / OpenGL
Tip/Trick

GridLine, opengl with C++

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
7 Jan 2012CPOL 34K   3   3
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInfinite Lines Pin
Madhan Mohan Reddy P15-Apr-13 1:11
professionalMadhan Mohan Reddy P15-Apr-13 1:11 
Hi, I am getting infinite lines , when try to draw the coordinate system in my code..
can you tell me why ?

code snippet

C#
gl.LineWidth(2.000000f);
           gl.MatrixMode(gl.MODELVIEW);
           gl.Begin(gl.LINES);
           {
               gl.Color3f(1.000000f, 0.000000f, 0.000000f);
               gl.Vertex3d(0, 0, 0);
               gl.Vertex3d(1, 0, 0);
               gl.Color3f(0.000000f, 1.000000f, 0.000000f);
               gl.Vertex3d(0, 0, 0);
               gl.Vertex3d(0, 1, 0);
               gl.Color3f(0.000000f, 0.000000f, 1.000000f);
               gl.Vertex3d(0, 0, 0);
               gl.Vertex3d(0, 0, 1);
           }
           gl.End();

GeneralYou could use glVertex2f, for 2D case. Also, in second code ... Pin
SiarheiY3-Jan-12 5:32
SiarheiY3-Jan-12 5:32 
GeneralRe: ok, I fixed it. Pin
tombog04-Jan-12 8:18
tombog04-Jan-12 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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