Click here to Skip to main content
Click here to Skip to main content

GridLine, opengl with C++

, 7 Jan 2012
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)

About the Author

tombog0

United States United States
No Biography provided

Comments and Discussions

 
QuestionInfinite Lines PinmemberMadhan Mohan Reddy15-Apr-13 1:11 
GeneralYou could use glVertex2f, for 2D case. Also, in second code ... PinmemberSiarheiY3-Jan-12 5:32 
GeneralRe: ok, I fixed it. Pinmembertombog04-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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Jan 2012
Article Copyright 2011 by tombog0
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid