OpenGL ES 2.0中的glTexGen [英] glTexGen in OpenGL ES 2.0

查看:106
本文介绍了OpenGL ES 2.0中的glTexGen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了好几个小时,以用GL_OBJECT_LINEAR实现glTexGen的GLSL替代.对于OpenGL ES 2.0.在Ogl GLSL中,可以使用gl_TextureMatrix简化此操作,但是在OpenGL ES 2.0/OpenGL ES着色器语言1.0中不可用

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

几个站点提到在GLSL vert着色器中应该很容易做到这一点.但是我就是无法正常工作.

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.

我已经仔细研究了网络.但是大多数站点都在谈论投影纹理,我只是想基于平面投影创建UV.模型是在Maya中构建的,具有50k多边形,建模者正在使用平面映射,但是Maya不会导出UV.所以我想弄清楚.

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.

我已经查看了glTexGen的联机帮助信息:

I've looked at the glTexGen manpage information:

g = p1xo + p2yo + p3zo + p4wo

g = p1xo + p2yo + p3zo + p4wo

什么是g? g是texture2d调用中s的值吗?

What is g? Is g the value of s in the texture2d call?

我已经查看了该站点:

http://www.opengl.org/wiki/Mathematics_of_glTexGen

另一种尺寸说明了相同的功能:

Another size explains the same function:

坐标= P1 * X + P2 * Y + P3 * Z + P4 * W

coord = P1*X + P2*Y + P3*Z + P4*W

我不知道坐标(在我心中是UV vec2)与点积(标量值)如何相等?我以前在使用"g"时遇到了同样的问题.

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".

我将飞机设置为什么?在我的opengl c ++ 3.0代码中,将其设置为[0,0,1,0](基本上是z单位),glTexGen的效果很好.

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.

我仍然想念一些东西.

我的vert着色器基本上是这样的: WVPMatrix =世界视图项目矩阵. POSITION是模型顶点的位置.

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

}

碎片着色器


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

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/

谢谢.

推荐答案

来自glTexGen文档::

From the glTexGen documentation ::

void glTexGenfv ( GLenum coord , GLenum pname , const GLfloat *params );

void glTexGenfv ( GLenum coord , GLenum pname , const GLfloat *params );

  • 坐标-指定纹理坐标.必须 是GL_S,GL_T,GL_R或GL_Q之一.
  • pname -如果纹理生成功能为 GL_OBJECT_LINEAR,函数
  • coord - Specifies a texture coordinate. Must be one of GL_S, GL_T, GL_R, or GL_Q.
  • pname - If the texture generation function is GL_OBJECT_LINEAR, the function

g = p1 xo + p2 yo + p3 zo + p4 wo

g = p1 xo + p2 yo + p3 zo + p4 wo

,其中 g 是计算得出的值 对于以 coord ,...

is used, where g is the value computed for the coordinate named in coord,...

因此 g 确实是标量值,它可以是vec2 uv值的 s t 分量,具体取决于 coord 的值(GL_S或GL_T).

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).

更明确的是,对

glTexGen(GL_S, GL_OBJECT_LINEAR, {ps0,ps1,ps2,ps3})
glTexGen(GL_T, GL_OBJECT_LINEAR, {pt0,pt1,pt2,pt3})

类似于

vec4 sPlane = vec4(ps0,ps1,ps2,ps3);
vec4 tPlane = vec4(pt0,pt1,pt2,pt3);
kOutBaseTCoord.s = dot(vec4(POSITION, 1.0), sPlane);
kOutBaseTCoord.t = dot(vec4(POSITION, 1.0), tPlane);

根据POSITION值的大小,输出的 st 坐标可以在(0,1)范围之外.使用您的值:

Depending on the scale of your POSITION values, the output st coordinates can be outside the (0,1) range. Using your values :

vec4 sPlane = vec4(1.0, 0.0, 0.0, 0.0);
vec4 tPlane = vec4(0.0, 1.0, 0.0, 0.0);

会将模型顶点的 x 坐标直接映射到纹理坐标的 s 值,与 y 相同t .因此,如果您的模型的坐标范围超出(0,1),则UV值将随之变化.

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.

要使纹理适合您的模型,必须使投影的比例适应模型的尺寸.对于100.0的示例模型,将给出以下代码:

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 :

float MODEL_SIZE = 100.0;
vec4 sPlane = vec4(1.0/MODEL_SIZE, 0.0, 0.0, 0.0);
vec4 tPlane = vec4(0.0, 1.0/MODEL_SIZE, 0.0, 0.0);

此解决方案仍然可以为您提供<如果模型位于(0,0,0)的中心,则为0.要对此进行调整,您可以在生成的UV值上添加偏移量.如:

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 :

kOutBaseTCoord.st += vec(0.5);

请注意,所有这些都完全取决于您的应用程序,以及您尝试通过纹理投影实现的目标.不要犹豫,调整您的公式以适应您的需求,这就是着色器的用途!

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 !

这篇关于OpenGL ES 2.0中的glTexGen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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