GlTexGen in OpenGL ES 2.0

I have been trying for several hours to perform a GLSL replacement for glTexGen with GL_OBJECT_LINEAR. For OpenGL ES 2.0. Ogl GLSL has gl_TextureMatrix, which makes it easier, but it is not available for OpenGL ES 2.0 / OpenGL ES Shader Language 1.0

Several sites mentioned that this should be β€œeasy” in the GLSL vert shader. But I just can't get it to work.

My guess is that I am not setting the planes correctly, or am missing something in my understanding.

I took care of the network. But most sites talk about predictable textures, I just want to create a UV based on a planar projection. Models are built in Maya, have 50k polygons, and the designer uses glider mapping, but Maya will not export UVs. So I'm trying to figure it out.

I looked at the glTexGen man page information:

g = p1xo + p2yo + p3zo + p4wo

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

I browsed 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 understand how the coordinate (UV vec2 in my mind) is equal to the point product (scalar value)? The same problem that I encountered earlier with "g".

What should I set for the plane? In my opengl C ++ 3.0 code, I installed it in [0, 0, 1, 0] (mostly unit z) and glTexGen works fine.

I still missed something.

My vertex shader looks basically like this: WVPMatrix = Project View Matrix. POSITION is the position of the vertices of the model.

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

 }

Fragment 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 tried texture2DProj in frag shader

Here are some of the other links I have been looking for.

http://www.gamedev.net/topic/407961-texgen-not-working-with-glsl-with-fixed-pipeline-is-ok/

Thanks in advance.

+1
source share
1 answer

From the glTexGen documentation ::

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

  • coord - sets the coordinate of the texture. Must be one of GL_S, GL_T, GL_R or GL_Q.
  • pname - if the texture generation function GL_OBJECT_LINEAR, the function

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

where g is the value calculated for the coordinate named in the coordinate, ...

therefore g is indeed a scalar value, which can be either s or the t-component of your value vec2 uv, depending on the coordinate value (GL_S or GL_T).

To be more explicit, the challenges

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

looks like

 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 may be outside the range (0,1). 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 directly map the x coordinate of your model vertices to the s values ​​of your texture coordinates, the same for y and t. Thus, if your model has a coordinate range outside (0,1), UV values ​​will follow.

To make the texture fit your model, you need to adapt the projection scale to the size of your model. For an approximate model size of 100.0, this will produce 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 are modeling in the center (0,0,0). To adjust this, you can add an offset to the resulting UV values. For example:

 kOutBaseTCoord.st += vec(0.5); 

Please note that it all depends on your application and what you are trying to achieve with your texture projection. Feel free to adjust your formulas according to your needs, for which shaders are needed!

+3
source

All Articles