2

I draw many lines to form a grid. I want to see the grid rotated on its X-axis, but I never get the intended result. I tried glRotatef and gluLookAt which does not work the way I want. Please see the pictures below.

this is the grid

this is how I want to see it

Edit: geez, posting the code here is also hard, lol, anyway here it is. Edit2: removed, only leave the code that has issues.

Please find the code below, no matter how I set the gluLookAt, the grid result won't be in the perspective I want.

#include <GL/glut.h>

void display() {

    ...

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_LINES);
    for (int i = 0; i < 720; i += 3)
    {   
        glColor3f(0, 1, 1);
        glVertex3f(linePoints[i], linePoints[i + 1], linePoints[i + 2]);
    }
    glEnd();

    glFlush();
}
void init() {

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, 4.0 / 3.0, 1, 40);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, -2, 1.25, 0, 0, 0, 0, 1, 0);
}

1 Answers1

1

Lets assume, that you have a grid in the xy plane of the world:

glColor3f(0, 1, 1);
glBegin(GL_LINES);
for (int i = 0; i <= 10; i ++)
{   
    // horizontal
    glVertex3f(-50.0f + i*10.0f, -50.0f, 0.0f);
    glVertex3f(-50.0f + i*10.0f,  50.0f, 0.0f);

    // vertical
    glVertex3f(-50.0f, -50.0f + i*10.0f, 0.0f);
    glVertex3f( 50.0f, -50.0f + i*10.0f, 0.0f);
}
glEnd();

Ensure that the distance of to the far plane of the projection is large enough (see gluPerspective). All the geometry which is not in between the near an far plane of the Viewing frustum is clipped.
Further more ensure that the aspect ratio (4.0 / 3.0) match the ratio of the viewport rectangle (window).

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 4.0 / 3.0, 1, 200);

For the use of gluLookAt, the up vector of the view has to be perpendicular to the grid. If the grid is arranged parallel to the xy plane, then the up vector is z axis (0, 0, 1).
The target (center) is the center of the grid (0, 0, 0).
The point of view (eye position) is ought to be above and in front of the grid, for instance (0, -55, 50). Note the point of view is used for a grid with the bottom left of (-50, -50, 0) and a top right of (50, 50, 0).

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, -55.0, 50.0, 0, 0, 0, 0, 0, 1);

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you for the explanation, I tried again with modification and it works. I also understand about gluPerspective and gluLookAt better now. – Mario Lintang May 27 '20 at 12:03