2016-04-24 75 views
0

我下面這個網站了解射線使用計算着色器跟蹤:https://github.com/LWJGL/lwjgl3-wiki/wiki/2.6.1.-Ray-tracing-with-OpenGL-Compute-Shaders-%28Part-I%29透視投影OpenGL和計算着色器

我的問題,該教程詳細的程序,以獲得透視投影。我想我正確地遵循了他的步驟,但是我得到了錯誤的結果,我相信我在矩陣計算中犯了一個錯誤。

我對透視代碼projection-

//Getting the perspective projection using glm::perspective 
glm::mat4 projection = glm::perspective(60.0f, 1024.0f/768.0f, 1.0f, 2.0f); 

//My Camera Position 
glm::vec3 camPos=glm::vec3(3.0, 2.0, 7.0); 

//My View matrix using glm::lookAt 
glm::mat4 view = glm::lookAt(camPos, glm::vec3(0.0, 0.5, 0.0),glm::vec3(0.0, 1.0, 0.0)); 

//Calculating inverse of the view*projection 
glm::mat4 inv = glm::inverse(view*projection); 

//Calculating the rays from camera position to the corners of the frustum as detailed in the site. 
glm::vec4 ray00=glm::vec4(-1, -1, 0, 1) * inv; 
ray00 /= ray00.w; 
ray00 -= glm::vec4(camPos,1.0); 

glm::vec4 ray10 = glm::vec4(+1, -1, 0, 1) * inv; 
ray10 /= ray10.w; 
ray10 -= glm::vec4(camPos,1.0); 

glm::vec4 ray01=glm::vec4(-1, 1, 0, 1) * inv; 
ray01 /= ray01.w; 
ray01 -= glm::vec4(camPos,1.0); 

glm::vec4 ray11 = glm::vec4(+1, +1, 0, 1) * inv; 
ray11 /= ray11.w; 
ray11 -= glm::vec4(camPos,1.0); 

上述tranformations的結果:[![此處輸入圖像的描述] [1]]

[1]

作爲附加信息,我打電話給我的計算着色器使用

//Dispatch Shaders. 
glDispatchCompute ((GLuint)1024.0/16, (GLuint)768.0f/8 , 1); 

我也通過值到着色器使用

//Querying the location for ray00 and assigning the value. Similarly for the rest 
GLuint ray00Id = glGetUniformLocation(computeS, "ray00"); 
glUniform3f(ray00Id, ray00.x, ray00.y, ray00.z); 

GLuint ray01Id = glGetUniformLocation(computeS, "ray01"); 
glUniform3f(ray01Id, ray01.x, ray01.y, ray01.z); 

GLuint ray10Id = glGetUniformLocation(computeS, "ray10"); 
glUniform3f(ray10Id, ray10.x, ray10.y, ray10.z); 

GLuint ray11Id = glGetUniformLocation(computeS, "ray11"); 
glUniform3f(ray11Id, ray11.x, ray11.y, ray11.z); 

GLuint camId = glGetUniformLocation(computeS, "eye"); 
glUniform3f(camId, camPos.x, camPos.y, camPos.z); 

更新回答關於derhass的建議。

我的形象現在看起來像: Latest Image

+0

我建議你也運行相應的lwjgl代碼並比較矩陣和向量。還要檢查統一位置的有效性(!= -1)或使用[顯式位置](https://www.opengl.org/wiki/Layout_Qualifier_%28GLSL%29#Explicit_uniform_location) – elect

+0

我已經檢查過統一的有效性他們是正確的。根據你的建議,我給了他們明確的位置,但結果保持不變。我將嘗試運行lwjl代碼並比較矩陣。 – AlexanderTG

回答

1

GLM的庫使用標準的OpenGL矩陣慣例,這意味着矩陣在頭腦裏的乘法順序Matrix * Vector創建。所以下面的代碼是錯誤的:

//Calculating inverse of the view*projection 
glm::mat4 inv = glm::inverse(view*projection); 

視圖矩陣和投影矩陣(從世界空間到眼睛空間變換)的(從眼睛空間變換到剪裁空間)的組成projection * view,因爲你把不view * projection它(它將在視圖之前應用投影)。

+0

對不起,延遲迴復,我已經添加了您的建議,但似乎沒有奏效。我已更新我的問題以包含您的建議。因爲,我沒有足夠的聲望,我不能有超過2張圖片:)謝謝! – AlexanderTG

+0

嗨,我用glm :: mat4 inv = glm :: transpose(glm :: inverse(projection * view))取代了上面的行。 。我只是換了,它。謝謝你的幫助。 – AlexanderTG