简体   繁体   English

如何在渲染全屏四边形时获取片段着色器中的查看方向

[英]How to get the Viewing Direction in the fragment shader while rendering a Fullscreen Quad

I have to render a scene in 2 steps. 我必须分两个步骤渲染场景。 First I render into a Frame Buffer Object and I reuse the texture of the FBO for the next step. 首先,我渲染到帧缓冲区对象中,然后将FBO的纹理重新用于下一步。 I render the texture on a fullscreen quad like this with a shader attached: 我像这样在全屏四边形上渲染纹理,并附加了一个着色器:

glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();
   gluLookAt(0.0,0.0,0.0, 0.0,0.0,-10.0, 0.0,1.0,0.0);

   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
   glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);

   glBegin(GL_QUADS);
        glTexCoord2f(0.0,0.0);
        glVertex3f(-1.0,-1.0,0.0);
        glTexCoord2f(1.0,0.0);
        glVertex3f(1.0,-1.0,0.0);
        glTexCoord2f(1.0,1.0);
        glVertex3f(1.0,1.0,0.0);
        glTexCoord2f(0.0,1.0);
        glVertex3f(-1.0,1.0,0.0);
   glEnd();

   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();

In the attached Fragment Shader I need the viewing direction to the actual fragment of the old scene in Eye Space Coords. 在所附的“片段着色器”中,我需要查看“眼空间坐标”中旧场景实际片段的观看方向。 My first idea was like this: 我的第一个想法是这样的:

vec2 uv = gl_TexCoord[0].st;
vec3 viewDirEye = normalize(vec3(uv.x*2.0-1.0, (uv.y)*2.0-1.0, -1.0));

But it seems not working, and I don't know why. 但这似乎不起作用,我也不知道为什么。 It would be great if someone can help me out here. 如果有人可以在这里帮助我,那将是很棒的。

Your formula does set up a direction vector from the texture coordinates in normalized device coordinates , but not in eye space . 您的公式的确从规范化设备坐标中的纹理坐标设置了方向矢量,但未在眼睛空间中设置方向矢量。

You have to take the projection matrix into account (which will define the horizontal and vertical field of view angle). 您必须考虑投影矩阵(这将定义水平和垂直视场角)。 You can use the inverse projection matrix to transform into eye space. 您可以使用投影矩阵将其转换为眼睛空间。

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

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