OpenGL Drawing Grid (help)

Hi
I have a header file like that. I’m willing to create a grid. By using OpenGl and C++. The problem is in there cpp file. I can’t excatly draw. Hope you can help it.In the for loop i don’t know how to fill inside the vertex blocks

#ifndef Grid_h
#define Grid_h
#include "Shape.h"

class Grid :: public Shape
{

public:

Grid();

Grid(int numberOfLines,int distanceBetweenLines);

void Draw();

int distanceBetweenLines;

int numberOfLines;

};

#endif
#include "Grid.h"
#include <GL/freeglut.h>

Grid :: Grid()
{
numberOfLines = 0.0f;

distanceBetweenLines = 0.0f;
}
Grid:: Grid(int numberOfLines , int distanceBetweenLines )
{ 
cizimSekli = GL_LINES; //drawing way
numberOfLines= 50; //number of Lines
renk = Renk(0.0f, 1.0f, 0.0f); //Colour
distanceBetweenLines = 20;

for(int x = 0; x < numberOfLines; x++){

glBegin(GL_LINES);
glVertex2f(x,0);
glVertex2f(0,x);
glEnd();
};
for(int y = 0; y < numberOfLines; y++){
glBegin(GL_LINES);
glVertex2f(y,0);
glVertex2f(0,y);
glEnd();
}; 
}

Can someone please help me ?

Hi,
PS: You can use [ code ][/ code] tags to insert code snippets instead of the quote tag.

Back to your answer. You can do something along these lines to render a grid.


void DrawGrid(int HALF_GRID_SIZE)
{
    glBegin(GL_LINES);
    glColor3f(0.75f, 0.75f, 0.75f);
    for(int i=-HALF_GRID_SIZE;i<=HALF_GRID_SIZE;i++)
    {
        glVertex3f((float)i,0,(float)-HALF_GRID_SIZE);
        glVertex3f((float)i,0,(float)HALF_GRID_SIZE);

        glVertex3f((float)-HALF_GRID_SIZE,0,(float)i);
        glVertex3f((float)HALF_GRID_SIZE,0,(float)i);
    }
    glEnd();

    //call it like this
    DrawGrid(10);
}

This basically renders two set of lines in XZ plane. See if this helps.
Mobeen

[QUOTE=mobeen;1250557]Hi,
PS: You can use [ code ][/ code] tags to insert code snippets instead of the quote tag.

Back to your answer. You can do something along these lines to render a grid.


void DrawGrid(int HALF_GRID_SIZE)
{
    glBegin(GL_LINES);
    glColor3f(0.75f, 0.75f, 0.75f);
    for(int i=-HALF_GRID_SIZE;i<=HALF_GRID_SIZE;i++)
    {
        glVertex3f((float)i,0,(float)-HALF_GRID_SIZE);
        glVertex3f((float)i,0,(float)HALF_GRID_SIZE);

        glVertex3f((float)-HALF_GRID_SIZE,0,(float)i);
        glVertex3f((float)HALF_GRID_SIZE,0,(float)i);
    }
    glEnd();

    //call it like this
    DrawGrid(10);
}

This basically renders two set of lines in XZ plane. See if this helps.
Mobeen[/QUOTE]

Thank you so much for the interest.

What if we write like the code like i’ve written at the top message ? I just need that two for loops fixed and Vertex2f i guess.

Thanks, Ensar.

Hi,

I’m trying to do the above using vbo. What’s the recommended way of doing it? Been trying to do it via glDrawArrays(GL_LINE_LOOP, …) to no success. Is the only way is to call multiple glDrawArrays or is there a workaround? What if the grid is to be drawn using glElementArras()? Should it be called multiple times too?

Thanks.

For a grid, glDrawArrays(GL_LINES, …). There isn’t much point in using glDrawElements() as only the four corner vertices will be used more than once.