1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
He-man
  • 13
  • 3
  • you can use two different texture coordinates for both textures. – Summit Jan 24 '23 at 06:39
  • @Summit Thanks for the suggestion but how exactly can i do that, since i am already using one VBO to pass texture info to shader `glBindBuffer(GL_ARRAY_BUFFER, Objects[i].VBO[0]); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, (void*)0);` like this how can i do same for another. – He-man Jan 24 '23 at 07:07

1 Answers1

0

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;
}
Summit
  • 2,112
  • 2
  • 12
  • 36