glTexGen in OpenGL ES 2.0

前端 未结 1 1364
無奈伤痛
無奈伤痛 2021-01-25 07:47

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

相关标签:
1条回答
  • 2021-01-25 07:57

    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 !

    0 讨论(0)
提交回复
热议问题