I also posted this on the main StackExchange, but this seems like a better place, but for give me for the double post if it shows up twice.

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:

Mathematics of glTexGen

Another size explains the same function:

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

I don't get how coord (a 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

TexGen not working with GLSL, with fixed pipeline is ok


The scalar product is a special case of the more general matrix multiplication. In the case of

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

imagine that P1 to P4 are actually vectors the same size as your texture coordinates. You can also think of P1 to P4 as columns of your object-coordinates to texture-coordinates matrix. In your case, where you want 2D coordinates, it'd be a 2x4 matrix. The columns of this matrix are what you would ordinarily submit as the planes when using glTexGen. The first 3 elements in each row are the direction of the respective texture axis. The length of this direction is inversely proportional to the stretch of the texture in that direction (I.e. the longer the direction, the more greater the texture coordinates and the texture will be smaller and repeat more often, if you use GL_REPEAT). You need to pick those direction orthogonally to your plane's normal! The last component in each row is used to move the texture around on that plane.

I'm not entirely sure that this explanation is what you're looking for, but I hope it helps.

  • \$\begingroup\$ Thanks for your response. I'm still a bit stuck on: "object-coordinates to texture-coordinates matrix", but that is because the object space to texture space is still a black hole to me, and I don't expect you to solve that one for me :). If my plane normal is [0, 0, 1] (z-axis), wouldn't my orthogonal rows make the matrix look something like this: [1, 0, 0,0 0, 1, 0,0 0, 0, 0, 0 0, 0, 0, 0] So, agian, assuming the object's plane normal is [0, 0, 1] what would GL_S, GL_T be? And if I'm just using standard textures, do I need anything for GL_R & GL_Q? Thanks again! \$\endgroup\$ – visualjc Feb 21 '11 at 18:29
  • \$\begingroup\$ GL_S and GL_T could, for example, be 1,0,0,0 and 0,1,0,0. U don't need anything for R and Q for 2D textures. \$\endgroup\$ – ltjax Feb 21 '11 at 19:09
  • \$\begingroup\$ Thanks Itjax. Here is my real problem (as I understand it) GL_S = [1,0,0,0], VPos = [50, 0, 0, 0] coord = dot(VPOS, GL_S) makes coord = 50. How the heck to I turn that 50 into texture space [0-1]? In glsl on Opengl 3, I'd could take that 50 and apply the gl_Texture matrix to it, but in OpenGL ES 2.0 there is no such gl_Texture. Any thoughts? \$\endgroup\$ – visualjc Feb 21 '11 at 19:20
  • \$\begingroup\$ Nah, the texture matrix just allows more transformations. It's pretty useless with texture coordinate generation, since you can already incorporate it in the object to texture coordinate matrix. What happens to the 50 depends on the GL_TEXTURE_WRAP_S parameter. If it's GL_REPEAT, you get a 0 (since fmod(50, 1)=0) or you get a 1 with GL_CLAMP. Either way, you wouldn't normally do that in the shader. \$\endgroup\$ – ltjax Feb 21 '11 at 20:08
  • \$\begingroup\$ I still am not sure of "object to texture coordinate matrix"; where is this formulated? \$\endgroup\$ – visualjc Feb 21 '11 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.