Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:46:13+00:00 2026-06-15T17:46:13+00:00

I am trying to find the screen coordinates from the opengl coordinates(projected in 3D

  • 0

I am trying to find the screen coordinates from the opengl coordinates(projected in 3D space) I have used glproject call for this purpose,i have used rotation and translation to in my code

At certain point after performing some transformation, i called glproject api to get the screen coordinate of a particular projected point P(x,y,z)

glproject(x,y,z,modelMatrix,projectionmatrix,viewport,*x_s,*y_s,*z_s);

I am able to get x screen coordinate correctly in x_s , but y coordinates are different

The only change in y which is not in x is when initially i called the glperspective to set fovy(The field of view angle, in degrees, in the y-direction). gluPerspective(60.0f, Width/Height,0.0001f,1000.0f);

Let me Rephrase the question I have created a 3D point on screen and now i am getting the 2d(x,y) coordinates of that point through GLproject they come different from the mouse coordinates What could be the possible solution to get correct coordinates.

Here is the code snippet

#include<GL/glut.h>     /* Header File For The GLUT Library */
GLint Window;              /* The number of our GLUT window */
float tmp_x,tmp_y,tmp_z;
GLfloat w = 1200;         /* Window size. Global for use in rotation routine */
GLfloat h = 1200;
GLint prevx, prevy;        /* Remember previous x and y positions */
GLfloat xt=1.0,yt=1.0,zt=1.0;   /* translate */
int width = 1600;
int height = 1200;

//This function will set windowing transformation
void transform(GLfloat Width , GLfloat Height )
{
    glViewport(0,0,  (GLfloat)Width,  (GLfloat)Height);
    glPushMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0f, Width/Height,0.0001f,1000.0f); 
    glTranslatef(0.0, 0.0, -15.0f);     /* Centre and away the viewer */
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}


GLvoid draw_room() 
{   
int i;
glPushMatrix();
glShadeModel(GL_SMOOTH); 
glLineWidth(1.0);                             
glPointSize(4.0);                             /* Add point size, to make it clear */
glBegin(GL_POINTS);                /* start drawing the cube.*/
glColor3f(0.0f,0.0f,1.0f);            /* Set The Color To Orange*/
glColor3f(1.0f,0.0f,0.0f);            /* Set The Color To Orange*/
glVertex3f(3.1f,2.1f,2.1f); 
glEnd();                              /* Done Drawing The Cube*/
glEnable(GL_DEPTH_TEST); 
}
//OpenGl Display  callback Function It call init room
void DrawGLScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    printf("%f %f %f\n",xt,yt,zt);
    glPushMatrix();
    glMatrixMode(GL_PROJECTION);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(xt,yt, zt);
    draw_room();
    glPopMatrix();
    glutSwapBuffers();               /* Swap buffers */
    glFlush();
}

GLvoid Mouse( int b , int s, int xx, int yy)
{

    double a1,a2,a3;    
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16]; 
    glGetIntegerv(GL_VIEWPORT, viewport);  
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);   
    glGetDoublev(GL_PROJECTION_MATRIX, projection);  
    //gluProject(xt+3.1f,yt+2.1f,zt+2.1f, modelview, projection, viewport, &a1, &a2, &a3);
    gluProject(3.1f,2.1f,2.1f, modelview, projection, viewport, &a1, &a2, &a3);

    printf("Mouse: %d %d\n",xx,yy);                // Both Print are giving different coordinaes.
    printf("Unproject %f %f %f\n",a1,a2,a3);    
    switch (b) {
    case GLUT_LEFT_BUTTON:  /* only stash away for left mouse */
        prevx = xx - w/2;
        prevy = h/2 - yy;
        break;
    case GLUT_MIDDLE_BUTTON:
        break;
    case GLUT_RIGHT_BUTTON:
        break;
    }
}

What could be the possible solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:46:15+00:00Added an answer on June 15, 2026 at 5:46 pm

    OpenGL uses lower left corner of screen as coordinate system orign. Window system usually uses upper left corner as coordinate system orign. You need to handle this difference. Example:

    printf("Mouse: %d %d\n",xx,yy);                
    printf("Unproject %f %f %f\n",a1,viewport[1]-a2,a3);   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find this issue here, but no topic seems to
I'm trying to find me a image on the screen. I have the image
I have the following image I'm trying to find the pixel coordinates of main
I'm trying to find a way to prevent the login screen from showing up
Trying to find a way to remove blank pages from a document I wrote
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to find the average pixel color of the entire screen or section
I have been trying to find a good-looking design using Aero in Delphi 2010.
I'm trying to find the way to face this situation. Having these tables in
I am trying to find a way how to get the twitter ID from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.