3D场景转换不太工作 [英] 3D Scene transformations not quite working
问题描述
为了将屏幕空间坐标转换为世界空间坐标,我已经进行了以下计算:
In an attempt to convert screen space coordinates into world space coordinates, I've been doing the following calculation:
WorldSpace Vector = inverse(Projection Matrix)* inverse (View Matrix)* ScreenSpace向量
WorldSpace Vector = inverse(Projection Matrix) * inverse(View Matrix) * ScreenSpace vector
到现在为止,我相信我的大部分计算都是正确的,但是我不知道如何执行最后一步。
Up to this point, I believe I have most of my calculations right, however I'm unsure of how to perform the last step needed in order to have my vector transformed to world coordinates.
问题:
- 我被告知,这个过程的最后一步是将我的结果除以变量w,因为我们使用齐次坐标。但是我不知道w代表什么。
我定义了以下变量(x和y是鼠标坐标的整数):
I've defined the following variables (x and y are integers of the mouse coordinates):
GLint viewport[4];
GLfloat winX, winY;
glGetIntegerv(GL_VIEWPORT, viewport);
winX = ((float)x/viewport[2]*2)-1;
winY = ((float)(viewport[3]-y)/viewport[3]*2)-1;
glm::mat4x4 mMatrix;
glm::mat4x4 vMatrix;
glm::mat4x4 cameraTransformation;
我进行了以下转换:
cameraTransformation = glm::rotate(cameraTransformation, (float)alpha*(float)M_PI/180, glm::vec3(0, 1, 0));
cameraTransformation = glm::rotate(cameraTransformation, (float)beta*(float)M_PI/180, glm::vec3(1, 0, 0));
glm::vec4 cameraPosition = (cameraTransformation * glm::vec4(camX, camY, distance, 0));
glm::vec4 cameraUpDirection = cameraTransformation * glm::vec4(0, 1, 0, 0);
vMatrix = glm::lookAt(glm::vec3(cameraPosition[0],cameraPosition[1],cameraPosition[2]), glm::vec3((float)camX, (float)camY, 0.0), glm::vec3(cameraUpDirection[0],cameraUpDirection[1],cameraUpDirection[2]));
glm::mat4x4 mat = glm::inverse(vMatrix) * glm::inverse(mMatrix) * glm::inverse(pMatrix);
glm::vec4 wSC = (mat) * glm::vec4(winX,winY,0,0);
在我的resize事件中,我的pMatrix预计为:
In my resize event, my pMatrix is projected as such:
pMatrix = glm::mat4x4(1.0);//sets identity
pMatrix = glm::perspective( (float)fov*(float)M_PI/180, (float) width / (float) height, (float)0.001, (float)10000 );
注意:我过去使用GLU库有问题,显然使用unProject功能,所以我选择自己执行计算。
Note: I've had issues in the past with using the GLU library, and apparently with using unProject functions overall, so I've elected to perform the calculations myself. At this point its almost a justification of effort, but I'm not going to use preexisting unprojection functions period.
推荐答案
在这个例子中,我的光线选择问题的最后阶段,我被告知我做错了几个事情:
In this last stage of my ray picking problem, I was informed that I was doing several things wrong:
- 我的z坐标应该是-1,而不是0
- 提醒我需要在相对平面上有2个独立的向量,不要只使用1来进行测试,而是通过实际的光线选择器来运行它工作原理。
- 使用w值为1,而不是0.这是
glm :: vec4 wSC =(mat)* glm的最后一个位置: :vec4(winX,winY,0,0);
行。 - 我需要在执行任何计算之前将鼠标坐标转换为NDC坐标, is:
- 将鼠标x除以屏幕宽度,乘以2,减1。
- 从 >屏幕高度,除以屏幕高度,乘以2,减去1:
winX =((float)x / viewport [2] * 2)-1;
winY =((float)(viewport [3] -y)/ viewport [3] * 2)-1;
- My z coordinate for a window space near plane should be -1, not 0
- Reminder that I need 2 separate vectors at the opposing planes, don't just use 1 for testing, run it through the actual ray picker to see if it works.
- Use a "w" value of 1, not 0. This is the last spot of the
glm::vec4 wSC = (mat) * glm::vec4(winX,winY,0,0);
line. - That I need to convert my mouse coordinates into NDC coordinates before performing any calculations, so this is:
- divide mouse x by screen width, times 2, minus 1.
- subtract mouse y from screen height, divide by screen height, times 2, minus 1:
winX = ((float)x/viewport[2]*2)-1; winY = ((float)(viewport[3]-y)/viewport[3]*2)-1;
由于我的问题延续了相当长的时间,并且遇到了几个问题,我欠几个帮助我的人方式:
Since my problem extended over quite a great length of time, and took up several questions, I owe credit to a few individuals who helped me along the way:
- sapsins of facepunch ,in this thread
- 从这里解开,在此问题和此讨论
- srobins of facepunch, in this thread
- derhass from here, in this question, and this discussion
这篇关于3D场景转换不太工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!