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: Transform from window coordinate to OpenGL coordinate

  1. #1
    Intern Newbie
    Join Date
    Dec 2006
    Location
    China
    Posts
    30

    Transform from window coordinate to OpenGL coordinate

    I am trying to use gluUnProject()transform window coordinate to OpenGL coordinate, but I found that I can only get correct coordinate for x axis, the y axis is a not correct, any idear about this?

    I use a cross line to trace mouse position.
    code:
    Code :
    #include <iostream>
    #include <windows.h>
    #include "GL/glut.h" 
     
    using namespace std ;
     
    //Threee variable indicate the coordiante after transformation
    GLdouble	objX ;
    GLdouble	objY ;
    GLdouble	objZ ;
    void MotionFunc(int x, int y)
    {
     
    	//Get Model matrix
    	GLdouble *model = new GLdouble[16] ;
    	glGetDoublev(GL_MODELVIEW_MATRIX,model) ;
    	//Get project matrix
    	GLdouble *proj = new GLdouble[16] ; 
    	glGetDoublev(GL_PROJECTION_MATRIX,proj) ;
    	//Get viewport 
    	GLint *view = new GLint[4] ;
    	glGetIntegerv(GL_VIEWPORT,view) ;
    	//Transform
    	gluUnProject( x, y, 1.0, model, proj, view, &amp;objX, &amp;objY, &amp;objZ) ;
     
    }
     
    void init(void)
    {
    	glClearColor(1.0, 1.0, 1.0, 1.0) ;
    }
     
    void display(void)
    {
     
    	glClear(GL_COLOR_BUFFER_BIT) ;
    	glColor3f(1.0, 0.0, 0.0) ;
    	//Draw a vertical line and a horizontal line
    	glBegin(GL_LINES) ;
    		glVertex2f((objX-4.0)<-2.0?-2.0:(objX-4.0), objY) ;
    		glVertex2f((objX+4.0)>2.0?2.0:(objX+4.0), objY) ;
    		glVertex2f(objX, (objY-3.0)<-1.5?-1.5:(objY-3.0)) ;
    		glVertex2f(objX, (objY+3.0)>1.5?1.5:(objY+3.0)) ;
    	glEnd() ;
    	glutSwapBuffers() ;
     
    }
     
    void reshape(int w, int h)
    {
    	glViewport(0, 0, (GLsizei)w, (GLsizei)h) ;
    	glMatrixMode(GL_PROJECTION) ;
    	glLoadIdentity() ;
    	if(w<=h)
    		glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
    		1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0) ;
    	else
    		glOrtho(-1.5*(GLfloat)w/(GLfloat)h, 
    		1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0) ;
    	glMatrixMode(GL_MODELVIEW) ;
    	glLoadIdentity() ;
    }
     
    int main(int argc, char **argv)
    {
    	glutInit(&amp;argc, argv) ;
    	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB) ;
    	glutInitWindowSize(1024, 768) ;
    	glutInitWindowPosition(0, 0) ;
    	glutCreateWindow("MouseSample");	
    	init() ;
    	glutPassiveMotionFunc(MotionFunc);
    	glutDisplayFunc(display);
    	glutIdleFunc(display) ;
    	glutReshapeFunc(reshape) ;
    	glutMainLoop();
     
    	return 0 ;
    }
    MSN:douglas2008@hotmail.com

  2. #2
    Intern Contributor
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    84

    Re: Transform from window coordinate to OpenGL coordinate

    Hi,

    Try this:
    On windows the 0,0 is the upper left corner, but 0,0 for OpenGL is on the lower left corner.
    You need to send: window heigh - y.

    Ido
    Ido Ilan

  3. #3
    Intern Newbie
    Join Date
    Dec 2006
    Location
    China
    Posts
    30

    Re: Transform from window coordinate to OpenGL coordinate

    Thank you very much! it works now, change it like the following:

    Code :
    glVertex2f((objX-4.0)<-2.0?-2.0:(objX-4.0), -objY) ;		
    glVertex2f((objX+4.0)>2.0?2.0:(objX+4.0), -objY) ;
    MSN:douglas2008@hotmail.com

Posting Permissions

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