简体   繁体   English

透视投影 OPENGL 和计算着色器

[英]Perspective Projection OPENGL and Compute Shaders

I am following this site to learn ray tracing using compute shaders: https://github.com/LWJGL/lwjgl3-wiki/wiki/2.6.1.-Ray-tracing-with-OpenGL-Compute-Shaders-%28Part-I%29我正在关注此站点以使用计算着色器学习光线跟踪: https : //github.com/LWJGL/lwjgl3-wiki/wiki/2.6.1.-Ray-tracing-with-OpenGL-Compute-Shaders-%28Part-I %29

My question, The tutorial details a procedure to get the perspective projection.我的问题,教程详细介绍了获取透视投影的过程。 I think I followed his steps correctly but I am getting the wrong result and I believe I made a mistake in my matrix computations.我想我正确地遵循了他的步骤,但我得到了错误的结果,我相信我在矩阵计算中犯了一个错误。

My code for the perspective 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);

Result of above tranformations:以上转换的结果:

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

As additional information, I am calling my compute shaders using作为附加信息,我使用以下方法调用我的计算着色器

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

I am also passing the values to the shader using the我还使用以下方法将值传递给着色器

//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);

Updated Answer following derhass suggestion.根据derhass建议更新答案。

My image now looks like : Latest Image我的图像现在看起来像:最新图像

The glm library uses the standard OpenGL matrix conventions, meaning that the matrices are created with the multiplication order Matrix * Vector in mind. glm 库使用标准的 OpenGL 矩阵约定,这意味着创建Matrix * Vector时考虑到乘法顺序Matrix * Vector So the following code is wrong:所以下面的代码是错误的:

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

The composition of the view matrix (transforming from world space to eye space) and the projection matrix (transforming from eye space to clip space) is projection * view , not view * projection as you put it (which would apply the projection before the view).视图矩阵(从世界空间转换到眼睛空间)和投影矩阵(从眼睛空间转换到剪辑空间)的组成是projection * view ,而不是你放置的view * projection (它将在视图之前应用投影)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM