glTexGen in OpenGL ES 2.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
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 mediu开发者_开发知识库mp 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.
From the glTexGen documentation ::
void glTexGenfv ( GLenum coord , GLenum pname , const GLfloat *params );
- 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
is used, where g is the value computed for the coordinate named in coord,...
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
glTexGen(GL_S, GL_OBJECT_LINEAR, {ps0,ps1,ps2,ps3})
glTexGen(GL_T, GL_OBJECT_LINEAR, {pt0,pt1,pt2,pt3})
is similar to
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);
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);
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 :
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);
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 !
精彩评论