opengltexturestransformationmultitexturing

OpenGL texture transformation when two textures on same mesh


I have a situation where i have two textures on a single mesh. I want to transform these textures independently. I have base code wherein i was able to load and transform one texture. Now i have code to load two textures but the issue is that when i try to transform the first texture both of them gets transformed as we are modifying texture coordinates.

Green one is the first texture and star is the second texture.

I have no idea how to transform just the second texture. Guide me with any solution you have.


Solution

  • You can do it in many ways , one of them would be to have two different texture Matrices.

    and than pass them to the vertex shader.

    #version 400 compatibility
    layout (location = 0) in vec3 aPos;
    layout (location = 1) in vec3 aNormal;
    layout (location = 2) in vec2 aTexCoord;
    
    out vec2 TexCoord;
    out vec2 TexCoord2;
    
    uniform mat4 textureMatrix;
    uniform mat4 textureMatrix2;
    
    
    void main()
    {
        vec4 mTex2;
        vec4 mTex; 
        Normal = mat3(NormalMatrix) * aNormal;
        Tex2Matrix = textureMatrix2;
        ViewDirMatrix = textureMatrix;
         mTex =  textureMatrix * vec4( aTexCoord.x , aTexCoord.y , 0.0 , 1.0 ) ;
         mTex2 =  textureMatrix2 * vec4( aTexCoord.x , aTexCoord.y , 0.0 , 1.0 ) ; 
        TexCoord = vec2(mTex.x , mTex.y );
        TexCoord2 = vec2(mTex2.x , mTex2.y );
        FragPos = vec3( ubo_model * (vec4( aPos, 1.0 ))); 
        gl_Position =   ubo_projection * ubo_view *  (vec4(FragPos, 1.0));
    }
    

    This is how you can create a texture matrix.

    glm::mat4x4 GetTextureMatrix()
    {
        glm::mat4x4 matrix = glm::mat4x4(1.0f); 
        matrix = glm::translate(matrix, glm::vec3(-PositionX + 0.5, PositionY + 0.5, 0.0));
        matrix = glm::scale(matrix, glm::vec3(1.0 / ScalingX, 1.0 /  ScalingY, 0.0));
        matrix = glm::rotate(matrix, glm::radians(RotationX) ,  glm::vec3(1.0, 0.0, 0.0));
        matrix = glm::rotate(matrix, glm::radians( RotationY), glm::vec3(0.0, 1.0, 0.0));
        matrix = glm::rotate(matrix, glm::radians(-RotationZ), glm::vec3(0.0, 0.0, 1.0));
        matrix = glm::translate(matrix, glm::vec3(-PositionX -0.5, -PositionY  -0.5, 0.0));
        matrix = glm::translate(matrix, glm::vec3(PositionX, PositionY, 0.0));
        return matrix;
    }