0
votes

So I draw an 'I' and use gluLookAt(0.f,0.f,3.f,0.f,0.f,0.f,0.f,1.f,0.f), and the I is moderate size. Then I add a drawScene() function which draw the background with gradient color, and then the 'I' becomes super big. I guess it is because I change matrix mode to GL_PROJECTION and GL_MODELVIEW in drawScene(), and those change the perspective maybe? I guess glPushMatrix() and glPopMatrix() are needed to reserve matrix status, but I have hard time finding where to put them. So how can I make the 'I' look normal size? Here are my drawI() and drawScene():

void drawI(int format)
{
    glBegin(format);
    glColor3f(0, 0, 1);
    glVertex2f(point[3][0], point[3][1]);
    glVertex2f(point[2][0], point[2][1]);
    glVertex2f(point[1][0], point[1][1]);
    glVertex2f(point[12][0], point[12][1]);
    glVertex2f(point[10][0], point[10][1]);
    glEnd();

    glBegin(format);
    glVertex2f(point[10][0], point[10][1]);
    glVertex2f(point[11][0], point[11][1]);
    glVertex2f(point[12][0], point[12][1]);
    glEnd();

    glBegin(format);
    glVertex2f(point[9][0], point[9][1]);
    glVertex2f(point[10][0], point[10][1]);
    glVertex2f(point[3][0], point[3][1]);
    glVertex2f(point[4][0], point[4][1]);
    glVertex2f(point[6][0], point[6][1]);
    glColor3f(1, 0.5, 0);
    glVertex2f(point[7][0], point[7][1]);
    glVertex2f(point[8][0], point[8][1]);
    glEnd();

    glBegin(format);
    glColor3f(0, 0, 1);
    glVertex2f(point[5][0], point[5][1]);
    glVertex2f(point[6][0], point[6][1]);
    glVertex2f(point[4][0], point[4][1]);
    glEnd();

}

void drawScene()
{
    glBegin(GL_QUADS);
    //red color
    glColor3f(1.0,0.0,0.0);
    glVertex2f(-1.0,-1.0);
    glVertex2f(1.0,-1.0);
    //blue color
    glColor3f(0.0,0.0,1.0);
    glVertex2f(1.0, 1.0);
    glVertex2f(-1.0, 1.0);
    glEnd();


}

Thanks a lot!

So I take glMatrixMode() and glLoadIdentity() out of drawScene() and drawI() and put them in display(). I changed drawScene() and drawI() above, and here is my display()

void display()
{    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.f,1.f,0.001f,30.f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    drawScene();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0.f,0.f,3.f,0.f,0.f,0.f,0.f,1.f,0.f);

    drawI(GL_TRIANGLE_FAN);

    glutSwapBuffers();  
}
1
You have no call to drawI and you never create a projection matrix other than the identity?Tommy
I called drawScene() and then drawI() in another function called display(). Yes I didn't create projection matrix, is this why? Sorry the question might be dumb im new to OpenGLuser3799934
I think we still need to see the code that calls these two functions to see how and where you're calling gluLookAt.jwlaughton
I take glMatrixMode() and glLoadIdentity out of helper functions and put them in display() before calling drawScene() and drawI().user3799934

1 Answers

0
votes

The normal way to do this (in a 3D mode) is in your code, before you call drawI or drawScene would be:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far); // fov is camera angle in degrees, aspect is width/height of your viewing area, near and far are your near and far clipping planes.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)

In your 2D rendering, you probably don't need the call to gluPerspective, but these calls should be done in your code before you call drawI or drawScene. Do this and delete the glMatrixMode() and glLoadIdentity() calls from drawI and drawScene.

Edit:

If your "I" is still too big, there are a number of things you could do, but you should probably be operating in 3D (giving a Z coordinate also).

You could scale the object:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)
glScalef(0.5, 0.5, 0.5);

You could move the camera further back (you'll need to include the gluPerspective() call as well):

gluLookAt(0.0,0.0,50.0,0.0,0.0,0.0,0.0,1.0,0.0);

Perhaps the easiest way to control the rendered image size in a 3D mode is to both move the camera (eye) back a way and then control the image size by changing the camera aperture angle (fov in the gluPerspective() call). A wider fov will shrink the rendered image; a smaller fov will enlarge it.

I don't know what the values for your coordinates are in drawI since they're variables, but a camera position of 3.0, an fov of 70.0 and an aspect of 1 should give you left, right, top and bottom clipping planes of about +/- 2.1 at Z = 0.

If you kept everything else the same and moved the camera to 50.0, the clipping planes would be at about +/- 35.0, so your "I" would occupy a much smaller portion of the viewing area.

If you then left the camera position at 50.0, but changed the fov to 40.0, the clipping planes would be at about +/- 18.2. Your "I" would fill a larger area than it did at cameraZ = 50.0, fov = 70.0, but a smaller area than cameraZ = 3.0, fov = 70.0.

You can play with camera position and fov to get the image size you want, or you could just scale the image. I like to keep camera position constant and change the fov. If I provide a function that changes the fov based on user input (maybe a mouse scroll), it's a good way to provide a zoom in/out effect.

BTW, in your original code, if you called:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)

Then later in DrawI or drawScene call:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

You've trashed the matrix loaded by your earlier call to gluLookAt().