user5572483
user5572483

Reputation:

OpenGL drawing a grid

Im new in OpenGL and im trying to make a 12x15 grid, so it appears something like an array but still a grid. I have this code so far:

#include <windows.h>
#include <GL/glut.h>

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_LINES);
    // Horizontal lines.
    for (int i=0; i<=12; i++) {
      glVertex2f(0, i);
      glVertex2f(15, i);
    }
    // Vertical lines.
    for (int i=0; i<=15; i++) {
      glVertex2f(i, 0);
      glVertex2f(i, 12);
    }
    glEnd();

    glFlush();
}

void handleKeypress(unsigned char key, int x, int y){
    switch (key){
    case 27: //Escape key
        exit(0);
    }
}

main(int argc, char** argv){
    glutInit(&argc, argv);
    glutCreateWindow("Grid Test");
    glutInitWindowSize(600, 480);
    glutInitWindowPosition(100, 100);
    glutDisplayFunc(display);
    glutKeyboardFunc(handleKeypress);
    glutMainLoop();
}

and yet the program window has this: test grid

What is the mistake I made? Should I write a function for the grid drawing out of the display function?

Upvotes: 0

Views: 1632

Answers (1)

BDL
BDL

Reputation: 22157

When no projection or other transformation is applied, the visible coordinates range from -1 to 1 on each axis. What you see is the lower left part starting from (0,0). If you want to see the whole grid, you will have to set transformation matrices to get it where you want.

Upvotes: 2

Related Questions