gluUnProject的使用 [英] Usage of gluUnProject

查看:159
本文介绍了gluUnProject的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gluUnProject coverting筛网坐标为世界坐标,但unfortunetaly我得到小值作为回报。它把(0,0)到约(-0.80,0.40),但它应该转换为约(4,2)...在code:

 ((GL11)GL).glGetIntegerv(GL11.GL_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,视口,0,pointInPlane,0);

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

main.x和main.y的屏幕坐标。这是其他变量的定义:

 公共静态INT []视=新INT [16];
     公共静态浮动[] modelViewMatrix =新的浮动[16];
     公共静态浮动[] projectionMatrix =新的浮动[16];
     公共静态浮动[] pointInPlane =新的浮动[16];
 

解决方案

gluUnProject需要你想从窗口unproject点的Z深度坐标为世界坐标。在您的code,您使用的是'1',该点的深度,这就是为什么它是给你的,有大约1长度的结果矢量。

要获得特定像素的深度unprojecting,你通常会做一些事情是这样的:

 浮动ž;
glReadPixels(main.x,main.y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&安培; z)的;
 

如果你再通过传递Z值gluUnProject,而不是'1',这应该给你正确的世界上的地位。

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];  

解决方案

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.

这篇关于gluUnProject的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆