Usage of gluUnProject

社会主义新天地 提交于 2019-12-06 11:39:37

问题


i'm using gluUnProject coverting sreen coordinates to world coordinates, but unfortunetaly i get to small value as a return. It converts (0,0) to about (-0.80,0.40), but it should convert to about (4,2)... The code:

((GL11) gl).glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);  
         ((GL11) gl).glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelViewMatrix, 0);  
         ((GL11) gl).glGetFloatv(GL11.GL_PROJECTION_MATRIX, projectionMatrix, 0); 

         GLU.gluUnProject(main.x, main.y, 1, modelViewMatrix, 0, projectionMatrix, 0, viewport, 0, pointInPlane, 0);

        xWcoord = pointInPlane[0];
        yWcoord = pointInPlane[1];
        zWcoord = pointInPlane[2];

main.x and main.y are screen coordinates. And this is the definition of other variables:

public static int[] viewport = new int[16];  
     public static float[] modelViewMatrix = new float[16];  
     public static float[] projectionMatrix = new float[16];  
     public static float[] pointInPlane = new float[16];  

回答1:


gluUnProject requires the z depth of the point you wish to unproject from window coordinates to world coordinates. In your code, you're using '1' for the depth of the point, which is why it is giving you a result vector that has a length of approximately 1.

To get the depth of the particular pixel for unprojecting, you would ordinarily do something like this:

float z;
glReadPixels( main.x, main.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z );

If you then pass that 'z' value through to gluUnProject instead of '1', that should give you the correct world position.



来源:https://stackoverflow.com/questions/9342878/usage-of-gluunproject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!