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 3 of 3

Thread: Help.... Please!

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2002
    Location
    Orl,fl,USA
    Posts
    6

    Help.... Please!

    Hi,
    I'm using the following code to toggle b/n
    Ortho and Perspective.
    But it doesn't toggle properly (toggles only first time).
    Also, the image is inverted when I use
    glOrtho
    Please help me modify code. The code is given below:
    #include "glut.h"
    #include <math.h>
    #include "gl\gl.h"
    #include "gl\glu.h"
    #include <iostream.h>
    #include <stdlib.h>
    static GLdouble ortho_left = -5;
    static GLdouble ortho_right = 5;
    static GLdouble ortho_bottom = -5;
    static GLdouble ortho_top = 5;
    static GLdouble ortho_near = 1;
    static GLdouble ortho_far = 30;
    static float angle=0.0,ratio;
    static float x=0.0f,y=1.75f,z=5.0f;
    static float lx=0.0f,ly=0.0f,lz=-1.0f;
    static GLint snowman_display_list;

    static int k=0;

    void renderScene(void);

    void changeSize(int w, int h)
    {

    if(k==0)
    {
    if(h == 0)
    h = 1;
    ratio = 1.0f * w / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(45,ratio,1,1000);
    renderScene();
    glutPostRedisplay();
    }
    else
    {
    if(h == 0)
    h = 1;
    ratio = 1.0f * w / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    //glOrtho(-200.0,200.0,-200.0,200.0,10.0,250.0);
    if (w>h) glOrtho(ortho_left*w/h,ortho_right *w/h,ortho_top,ortho_bottom,ortho_near,ortho_far);
    else glOrtho(ortho_left,ortho_right,ortho_top*h/w,ortho_bottom*h/w,ortho_near,ortho_far);
    renderScene();
    glutPostRedisplay();
    }

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);

    glutPostRedisplay();

    }


    void drawSnowMan() {

    glColor3f(1.0f, 1.0f, 1.0f);

    // Body
    glTranslatef(0.0f ,0.75f, 0.0f);
    glutSolidSphere(0.75f,20,20);


    // Head
    glTranslatef(0.0f, 1.0f, 0.0f);
    glutSolidSphere(0.25f,20,20);

    // Eyes
    glPushMatrix();
    glColor3f(0.0f,0.0f,0.0f);
    glTranslatef(0.05f, 0.10f, 0.18f);
    glutSolidSphere(0.05f,10,10);
    glTranslatef(-0.1f, 0.0f, 0.0f);
    glutSolidSphere(0.05f,10,10);
    glPopMatrix();

    // Nose
    glColor3f(1.0f, 0.5f , 0.5f);
    glRotatef(0.0f,1.0f, 0.0f, 0.0f);
    glutSolidCone(0.08f,0.5f,10,2);
    }

    GLuint createDL()
    {
    GLuint snowManDL;
    snowManDL = glGenLists(1);
    glNewList(snowManDL,GL_COMPILE);
    drawSnowMan();
    glEndList();
    return(snowManDL);
    }

    void initScene()
    {

    glEnable(GL_DEPTH_TEST);
    snowman_display_list = createDL();

    }
    void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Draw ground

    glColor3f(0.6f, 0.6f, 0.6f);
    glBegin(GL_QUADS);
    glVertex3f(-100.0f, 0.0f, -100.0f);
    glVertex3f(-100.0f, 0.0f, 100.0f);
    glVertex3f( 100.0f, 0.0f, 100.0f);
    glVertex3f( 100.0f, 0.0f, -100.0f);
    glEnd();

    // Draw 4 SnowMen

    for(int i = 0; i < 2; i++)
    for(int j=0; j < 2; j++) {
    glPushMatrix();
    glTranslatef(i*10.0,0,j * 10.0);
    glCallList(snowman_display_list);;
    glPopMatrix();
    }

    glutSwapBuffers();
    }

    void orientMe(float ang) {


    lx = sin(ang);
    lz = -cos(ang);
    glLoadIdentity();
    gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);
    }


    void moveMeFlat(int i)
    {
    x = x + i*(lx)*0.1;
    z = z + i*(lz)*0.1;
    glLoadIdentity();
    gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,1.0f);
    }
    void moveMeFlat1(int i)
    {
    x = x ;
    z = z ;
    glLoadIdentity();
    gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,0.0f,0.0f);
    }
    void processNormalKeys(unsigned char key, int x, int y) {

    if (key == 27)
    exit(0);
    switch(key)
    {
    case 'p': if (k==0) {k=1;}
    else if (k==1) {k=0;}

    if (k==0)
    {
    cout<<"persp"<<endl;
    gluPerspective(45,1.0f *(640/360),1,1000);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);
    glEnable(GL_DEPTH_TEST);
    renderScene();
    glutPostRedisplay();
    }
    else
    {
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(ortho_left,ortho_right,ortho_top,ortho_bot tom,ortho_near,ortho_far);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(x, y, z,x + lx,y + ly,z + lz,1.0f,1.0f,0.0f);
    cout<<"ortho"<<endl;
    renderScene();
    glutPostRedisplay();
    }
    glutPostRedisplay();


    break;
    }
    }

    void inputKey(int key, int x, int y) {

    switch (key) {
    case GLUT_KEY_LEFT :
    if(k==0)
    {
    angle -= 0.01f;orientMe(angle);
    }
    else
    {
    angle -= 0.01f;orientMe(angle);
    }
    break;
    case GLUT_KEY_RIGHT :
    if(k==0)
    {
    angle += 0.01f;orientMe(angle);
    }
    else
    {
    angle += 0.01f;orientMe(angle);
    }
    break;
    break;
    case GLUT_KEY_UP :
    if (k==0){
    moveMeFlat(1);} else {moveMeFlat1(1);}break;
    case GLUT_KEY_DOWN :
    if (k==0){
    moveMeFlat(-1);} else {moveMeFlat1(-1);} break;
    }
    }
    int main(int argc, char **argv)
    {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(640,360);
    glutCreateWindow("");
    initScene();
    glutKeyboardFunc(processNormalKeys);
    glutSpecialFunc(inputKey);
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutMainLoop();
    return(0);
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: Help.... Please!

    Would you please stop to post the same question under different topic names, one times is perfectley fine.

    Mikael

  3. #3
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: Help.... Please!

    I am not sure about what's wrong, and I am not using GLUT so I am not of much help, but you are calling OpenGL functions from all over the place, both the render scene, the key handlers and the on changed size handler, can you do this in GLUT ?, if the context is setup correct, then it's fine, but I don't know GLUT to well.

    You are also using code line: renderScene(); glutRedisplay();, is this the way top do it, by the name it sounds like glutRedisplay will take care of rerendering of the scene, so in that case you should not call renderScene() youself.

    Run the code in a debugger and check the parameters you pass to glOrtho to see what you end up with.

    Mikael

Posting Permissions

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