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: Gluunproject returning wrong coordinates

  1. #1
    Intern Newbie
    Join Date
    Dec 2002
    Location
    Ssdas
    Posts
    42

    Gluunproject returning wrong coordinates

    I programed this code as example code for later use on my modeller andyway.
    The code says to move the polygon where to the mouse clicks.

    Oviously I had to implement GluUnproject command anyway I did and it moves to where I click but it moves some but not to the position of the mouse pointer any way heres my code PLEASE HELP!!


    GLfloat rtri; // Angle For The Triangle ( NEW )
    GLfloat rquad; // Angle For The Quad ( NEW )
    GLfloat tranX;

    double tempX, tempY, tempZ;
    static int w, h, S_x1, S_x2, S_y1, Sy2;
    #define TRIANGLE 1
    #define CUBE 2
    #define AXIS 3
    #define zPos 0.1
    #define FEED_BUFF_SIZE 4096

    int m_num_vertices;
    int m_num_triangles;
    int id,count;
    int test;
    GLdouble CurentModelMatrix [16];
    GLdouble CurentProjMatrix [16];
    char cMessage[64];

    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

    GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
    {
    if (height==0) // Prevent A Divide By Zero By
    {
    height=1; // Making Height Equal One
    }

    w = width;
    h = height;
    glViewport(0,0,width,height); // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity(); // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity(); // Reset The Modelview Matrix
    }

    int InitGL(GLvoid) // All Setup For OpenGL Goes Here
    {
    glShadeModel(GL_SMOOTH); // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
    glClearDepth(1.0f); // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST); // Enables Depth Testing
    glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
    return TRUE; // Initialization Went OK


    }

    int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
    {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity(); // Reset The Current Modelview Matrix

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    glInitNames();
    glPushName(0);

    glLoadIdentity(); // Reset The Current Modelview Matrix
    glPushMatrix();
    glTranslatef(tempX,0.0f,-7.0f);
    glBegin(GL_QUADS);
    glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
    glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
    glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
    glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glEnd();
    glPopMatrix();
    }

    GLvoid ProcessSelection(int xPos, int yPos)
    {
    GLuint selectBuff[64];
    GLint hits, viewport[4];

    // Select buffer parameters
    glSelectBuffer(64, selectBuff);
    glGetIntegerv(GL_VIEWPORT, viewport);

    // Enter to selection mode
    glDisable(GL_LIGHTING);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glRenderMode(GL_SELECT);
    glLoadIdentity();
    // Set-up pick matrix
    gluPickMatrix(xPos,viewport[3]-yPos,5,5,viewport);


    // Set our perpective transformation matrix
    gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, 1.0, 425.0);

    glMatrixMode(GL_MODELVIEW);

    // Render all scene and fill selection buffer
    DrawGLScene();

    // Get hits and go back to normal rendering
    hits = glRenderMode(GL_RENDER);

    // Get first model, in selection buffer stack

    glMatrixMode(GL_PROJECTION);

    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glGetDoublev (GL_MODELVIEW_MATRIX, CurentModelMatrix);
    glGetDoublev (GL_PROJECTION_MATRIX, CurentProjMatrix);
    gluUnProject ((GLdouble) xPos,
    (GLdouble) yPos,
    (GLdouble) zPos,
    CurentModelMatrix,
    CurentProjMatrix,
    viewport,
    &tempX,
    &tempY,
    &tempZ);


    }
    PLZ Help!! This is URGENT AAGHGHH

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    3,115

    Re: Gluunproject returning wrong coordinates

    You have different projection matrices when drawing and picking. Check out the two calls to gluPerspective.

  3. #3
    Intern Newbie
    Join Date
    Dec 2002
    Location
    Ssdas
    Posts
    42

    Re: Gluunproject returning wrong coordinates

    Changed both the gluperspective commands to the same but the problem is still the same.
    Anybody got anyideas!! PlZ

Posting Permissions

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