Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: OpenGL Drawing Grid (help)

  1. #1
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4

    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


    Code :
    #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

    Code :
    #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();
    }; 
    }
    Last edited by IntelliSense; 05-04-2013 at 12:15 PM.

  2. #2
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4
    Can someone please help me ?

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    Quote Originally Posted by IntelliSense View Post
    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.
    Code :
    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
    Regards,
    Mobeen

  4. #4
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    4
    Quote Originally Posted by mobeen View Post
    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.
    Code :
    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
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2001
    Posts
    2
    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.

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •