OpenGL ES 2.0 中的 glTexGen
我已经尝试了几个小时来用 GL_OBJECT_LINEAR 实现 glTexGen 的 GLSL 替换。对于 OpenGL ES 2.0。在 Ogl GLSL 中,有 gl_TextureMatrix 使这变得更容易,但这在 OpenGL ES 2.0 / OpenGL ES Shader Language 1.0 上不可用
。几个网站提到这在 GLSL vert 着色器中应该是“容易”做到的。但我就是无法让它发挥作用。
我的预感是我没有正确设置飞机,或者我在理解中遗漏了一些东西。
我仔细研究过网络。但大多数网站都在谈论投影纹理,我只是想基于平面投影创建 UV。这些模型是在 Maya 中构建的,有 50k 多边形,建模者使用平面贴图,但 Maya 不会导出 UV。所以我正在尝试解决这个问题。
我查看了 glTexGen 联机帮助页信息:
g = p1xo + p2yo + p3zo + p4wo
g 是什么? g 是texture2d 调用中s 的值吗?
我查看了该网站:
http://www.opengl.org/wiki/Mathematics_of_glTexGen
另一个尺寸解释了相同的功能:
coord = P1*X + P2*Y + P3*Z + P4*W
我不明白 coord (我心目中的 UV vec2)如何等于点积(标量值) ?我之前用“g”遇到过同样的问题。
我要把飞机设置成什么?在我的 opengl c++ 3.0 代码中,我将其设置为 [0, 0, 1, 0] (基本上是单位 z),glTexGen 效果很好。
我还缺少一些东西。
我的垂直着色器基本上是这样的: WVPMatrix = 世界视图项目矩阵。 POSITION 是模型顶点位置。
varying vec4 kOutBaseTCoord; void main() { gl_Position = WVPMatrix * vec4(POSITION, 1.0); vec4 sPlane = vec4(1.0, 0.0, 0.0, 0.0); vec4 tPlane = vec4(0.0, 1.0, 0.0, 0.0); vec4 rPlane = vec4(0.0, 0.0, 0.0, 0.0); vec4 qPlane = vec4(0.0, 0.0, 0.0, 0.0); kOutBaseTCoord.s = dot(vec4(POSITION, 1.0), sPlane); kOutBaseTCoord.t = dot(vec4(POSITION, 1.0), tPlane); //kOutBaseTCoord.r = dot(vec4(POSITION, 1.0), rPlane); //kOutBaseTCoord.q = dot(vec4(POSITION, 1.0), qPlane); }
碎片着色器
precision mediump float; uniform sampler2D BaseSampler; varying mediump vec4 kOutBaseTCoord; void main() { //gl_FragColor = vec4(kOutBaseTCoord.st, 0.0, 1.0); gl_FragColor = texture2D(BaseSampler, kOutBaseTCoord.st); }
我在碎片着色器中尝试过texture2DProj
以下是我查找过的一些其他链接
http://www.gamedev.net/topic/407961-texgen-not-working-with-glsl-with-fixed-pipeline-is -ok/
提前谢谢您。
I have been trying for several hours to implement a GLSL replacement for glTexGen with GL_OBJECT_LINEAR. For OpenGL ES 2.0. In Ogl GLSL there is the gl_TextureMatrix that makes this easier, but thats not available on OpenGL ES 2.0 / OpenGL ES Shader Language 1.0
Several sites have mentioned that this should be "easy" to do in a GLSL vert shader. But I just can not get it to work.
My hunch is that I'm not setting the planes up correctly, or I'm missing something in my understanding.
I've pored over the web. But most sites are talking about projected textures, I'm just looking to create UV's based on planar projection. The models are being built in Maya, have 50k polygons and the modeler is using planer mapping, but Maya will not export the UV's. So I'm trying to figure this out.
I've looked at the glTexGen manpage information:
g = p1xo + p2yo + p3zo + p4wo
What is g? Is g the value of s in the texture2d call?
I've looked at the site:
http://www.opengl.org/wiki/Mathematics_of_glTexGen
Another size explains the same function:
coord = P1*X + P2*Y + P3*Z + P4*W
I don't get how coord (an UV vec2 in my mind) is equal to the dot product (a scalar value)? Same problem I had before with "g".
What do I set the plane to be? In my opengl c++ 3.0 code, I set it to [0, 0, 1, 0] (basically unit z) and glTexGen works great.
I'm still missing something.
My vert shader looks basically like this:
WVPMatrix = World View Project Matrix.
POSITION is the model vertex position.
varying vec4 kOutBaseTCoord; void main() { gl_Position = WVPMatrix * vec4(POSITION, 1.0); vec4 sPlane = vec4(1.0, 0.0, 0.0, 0.0); vec4 tPlane = vec4(0.0, 1.0, 0.0, 0.0); vec4 rPlane = vec4(0.0, 0.0, 0.0, 0.0); vec4 qPlane = vec4(0.0, 0.0, 0.0, 0.0); kOutBaseTCoord.s = dot(vec4(POSITION, 1.0), sPlane); kOutBaseTCoord.t = dot(vec4(POSITION, 1.0), tPlane); //kOutBaseTCoord.r = dot(vec4(POSITION, 1.0), rPlane); //kOutBaseTCoord.q = dot(vec4(POSITION, 1.0), qPlane); }
The frag shader
precision mediump float; uniform sampler2D BaseSampler; varying mediump vec4 kOutBaseTCoord; void main() { //gl_FragColor = vec4(kOutBaseTCoord.st, 0.0, 1.0); gl_FragColor = texture2D(BaseSampler, kOutBaseTCoord.st); }
I've tried texture2DProj in frag shader
Here are some of the other links I've looked up
http://www.gamedev.net/topic/407961-texgen-not-working-with-glsl-with-fixed-pipeline-is-ok/
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 glTexGen 文档::
所以g确实是一个标量值,可以是s或t vec2 uv 值的组成部分,具体取决于coord 的值(GL_S 或 GL_T)。
更明确地说,对的调用
类似于
根据 POSITION 值的比例,输出 st 坐标可以超出 (0,1) 范围。使用您的值:
将直接将模型顶点的 x 坐标映射到纹理坐标的 s 值,y 和 相同>t。因此,如果模型的坐标范围超出 (0,1),则 UV 值将随之变化。
为了使纹理适合您的模型,您必须使投影的比例适应模型的大小。对于模型大小为 100.0 的示例,这将给出以下代码:
该解决方案仍然可以为您提供值 <如果模型以 (0,0,0) 为中心,则为 0。要对此进行调整,您可以向生成的 UV 值添加偏移量。例如:
请注意,所有这些都很大程度上取决于您的应用程序以及您尝试通过纹理投影实现的目标。请毫不犹豫地调整您的公式以满足您的需求,这就是着色器的用途!
From the glTexGen documentation ::
so g is indeed a scalar value, which can be either the s or t component of your vec2 uv value, depending on the value of coord (GL_S, or GL_T).
To be more explicit, the calls to
is similar to
Depending on the scale of your POSITION values, the output st coordinates can be outside the (0,1) range. Using your values :
will map directly the x coordinate of your model vertices to the s values of your texture coordinates, same for y and t. Thus, if your model has a coordinate range outside (0,1), the UV values will follow.
To make the texture fit your model, you have to adapt the scale of the projection to the size of your model. For an example model size of 100.0, this would give the following code :
This solution can still give you values < 0 if you model is centered in (0,0,0). To adjust this, you could add an offset to the resulting UV values. Such as :
Note that all this in wildly dependent of your application, and what you try to achieve with your texture projection. Don't hesitate to adjust your formulas to match your needs, that's what shaders are for !