Click here to Skip to main content
11,711,537 members (80,631 online)
Click here to Skip to main content

Tagged as

GridLine, opengl with C++

, 7 Jan 2012 CPOL 22.7K 3
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

tombog0
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionInfinite Lines Pin
Madhan Mohan Reddy15-Apr-13 1:11
memberMadhan Mohan Reddy15-Apr-13 1:11 
GeneralYou could use glVertex2f, for 2D case. Also, in second code ... Pin
SiarheiY3-Jan-12 5:32
memberSiarheiY3-Jan-12 5:32 
GeneralRe: ok, I fixed it. Pin
tombog04-Jan-12 8:18
membertombog04-Jan-12 8:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 7 Jan 2012
Article Copyright 2011 by tombog0
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid