gluProject question

Hi, I’m continuing a thread on textRendering here, since it’s morphed into a question about how to project world co-ordinates into screen co-ordinates.

I am trying to make sense of the gluProject method. My object’s coordinates are set to (0f, 0f, 0f) and I am getting this output from my function below.

window_coords x/y/z = -0.6213202977855181/-0.4142135216304729/ 4.000000059008596

How do I interpret this? I was expecting pixel values! And how do I interpret the z value?

In case this is useful info:
My screen size is 600px/400px.
I am setting perspective like so : glu.gluPerspective(45,(float)width/height,1,100);
My camera is at (0f,0f,0f)
I am calling a glTranslate(0f,0f,-10f) before drawing my square at coords (0f,0f,0f);


public void getScreenCoordsForObject(GL gl, GLU glu, Coord c)
  {
    double modelview[] = new double[16];
    double projection[] = new double[16];
    int viewport[] = new int[4];
    double windowCoords[] = new double[3];
    
    gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview, 0);
    gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection, 0);
    gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
    
    glu.gluUnProject(c.x, c.y, c.z,
            modelview, 0,
            projection, 0,
            viewport, 0,
            windowCoords, 0);
    
    System.out.println("window_coords x/y/z = " + windowCoords[0] + "/" + windowCoords[1] + "/ " +  windowCoords[2]);
  }

Thanks, spiraljetty

Hi, I will answer my own question. I was using gluUnProject instead of gluProject.

Oops! :stuck_out_tongue:

I am still unsure about what the z-value means exactly (it’s returning values between 0.0 and 1.0 it looks like). But it works great so far.

So the function, in case anyone is interested is:

public void getWindowCoordsFor3DPoint(GL gl, GLU glu, double x, double y, double z)
  {
    double modelview[] = new double[16];
    double projection[] = new double[16];
    int viewport[] = new int[4];
    double windowCoords[] = new double[3];
    
    gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview, 0);
    gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection, 0);
    gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
    
    glu.gluProject(x, y, z,
            modelview, 0,
            projection, 0,
            viewport, 0,
            windowCoords, 0);
    
    System.out.println("window_coords x/y/z = " + windowCoords[0] + "/" + windowCoords[1] + "/ " +  windowCoords[2]);
  }

the z value represents the depth-position in the view-frustum…

so 0 would be front-clipping-pane and 1 would be back clipping pane.