GLSL:如何获得像素x,y,z的世界位置? [英] GLSL: How to get pixel x,y,z world position?

查看:152
本文介绍了GLSL:如何获得像素x,y,z的世界位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据它们在世界上哪个xyz位置来调整颜色.

I want to adjust the colors depending on which xyz position they are in the world.

我在片段着色器中尝试过:

I tried this in my fragment shader:

varying vec4 verpos;

void main(){
    vec4 c;
    c.x = verpos.x;
    c.y = verpos.y;
    c.z = verpos.z;
    c.w = 1.0;

    gl_FragColor = c;
}

但是颜色似乎根据我的相机角度/位置而变化,我如何使坐标独立于我的相机位置/角度?

but it seems that the colors change depending on my camera angle/position, how do i make the coords independent from my camera position/angle?

这里是我的顶点着色器:

Heres my vertex shader:

varying vec4 verpos;

void main(){
    gl_Position = ftransform();
    verpos = gl_ModelViewMatrix*gl_Vertex;
}

Edit2:更改了标题,所以我想要世界坐标,而不是屏幕坐标!

changed title, so i want world coords, not screen coords!

Edit3 :添加了我的完整代码

added my full code

推荐答案

在顶点着色器中,您具有gl_Vertex(如果不使用固定管线,则可以使用其他名称),它是顶点在模型坐标中的位置.将模型矩阵乘以gl_Vertex,您将获得世界坐标中的顶点位置.将其分配给 varying 变量,然后在片段着色器中读取其值,您将获得片段在世界坐标中的位置.

In vertex shader you have gl_Vertex (or something else if you don't use fixed pipeline) which is the position of a vertex in model coordinates. Multiply the model matrix by gl_Vertex and you'll get the vertex position in world coordinates. Assign this to a varying variable, and then read its value in fragment shader and you'll get the position of the fragment in world coordinates.

现在的问题是,如果您使用默认的OpenGL模型视图矩阵(它是模型矩阵和视图矩阵的组合),则不一定有任何模型矩阵.通常,我通过使用两个单独的矩阵而不是一个模型视图矩阵来解决此问题:

Now the problem in this is that you don't necessarily have any model matrix if you use the default modelview matrix of OpenGL, which is a combination of both model and view matrices. I usually solve this problem by having two separate matrices instead of just one modelview matrix:

  1. 模型矩阵(将模型坐标映射到世界坐标),并且
  2. 视图矩阵(将世界坐标映射到相机坐标).

因此,只需将两个不同的矩阵分别传递到您的顶点着色器.您可以通过定义

So just pass two different matrices to your vertex shader separately. You can do this by defining

uniform mat4 view_matrix;
uniform mat4 model_matrix;

在顶点着色器的开始.然后代替ftransform(),说:

In the beginning of your vertex shader. And then instead of ftransform(), say:

gl_Position = gl_ProjectionMatrix * view_matrix * model_matrix * gl_Vertex;

在主程序中,您必须将值写入这两个新矩阵.首先,要获取视图矩阵,请使用glLoadIdentity(),glTranslate(),glRotate()或gluLookAt()或任何您通常喜欢的方式进行摄影机转换,然后调用glGetFloatv(GL_MODELVIEW_MATRIX,& array) ;为了得到矩阵数据到一个数组.其次,以类似的方式,要获取模型矩阵,还可以调用glLoadIdentity();.并使用glTranslate(),glRotate(),glScale()等进行对象转换,最后调用glGetFloatv(GL_MODELVIEW_MATRIX,& array);从OpenGL中获取矩阵数据,因此可以将其发送到顶点着色器.特别要注意的是,在开始转换对象之前,您需要调用glLoadIdentity().通常,您首先要变换摄影机,然后变换对象,这将产生一个既具有视图功能又具有模型功能的矩阵.但是,因为您使用的是单独的矩阵,所以需要在使用glLoadIdentity()进行摄像机转换后重置矩阵.

In the main program you must write values to both of these new matrices. First, to get the view matrix, do the camera transformations with glLoadIdentity(), glTranslate(), glRotate() or gluLookAt() or what ever you prefer as you would normally do, but then call glGetFloatv(GL_MODELVIEW_MATRIX, &array); in order to get the matrix data to an array. And secondly, in a similar way, to get the model matrix, also call glLoadIdentity(); and do the object transformations with glTranslate(), glRotate(), glScale() etc. and finally call glGetFloatv(GL_MODELVIEW_MATRIX, &array); to get the matrix data out of OpenGL, so you can send it to your vertex shader. Especially note that you need to call glLoadIdentity() before beginning to transform the object. Normally you would first transform the camera and then transform the object which would result in one matrix that does both the view and model functions. But because you're using separate matrices you need to reset the matrix after camera transformations with glLoadIdentity().

gl_FragCoord是像素坐标,而不是世界坐标.

gl_FragCoord are the pixel coordinates and not world coordinates.

这篇关于GLSL:如何获得像素x,y,z的世界位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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