I'm trying to do basic multitexturing of terrain in OpenGL.
Im building the terrain with an image representing different elements (beach, water, jungle...) :
Im trying to map each color of this image with a texture in my game: green color means grassTexture (id 0), blue color means waterTexture (id 1), etc...
My fragment shader contains an uniform array containing all textures that I use (grass, water, ...) :
// Terrain.frag
uniform sampler2D terrainTextures[50];
For each vertex of the terrain, I pass a float in_TextureId corresponding to a texture id :
// Terrain.vert
layout (location = 0) in vec3 in_Vertex;
layout (location = 1) in vec4 in_Color;
layout (location = 2) in vec3 in_Normal;
layout (location = 3) in vec2 in_TexCoord;
layout (location = 4) in float in_TextureId;
Then im trying to display the correct texture for a vertex with this textureId in the fragment shader :
outputColor = texture(terrainTextures[int(textureId)], texCoord);
With this line, I have this result :
If I modify the code of the fragment shader with the following code :
highp int tid = int(textureId);
if(tid == 2)
{
outputColor = texture(terrainTextures[int(textureId)], texCoord);
}
else if(tid == 1)
{
outputColor = texture(terrainTextures[int(textureId)], texCoord);
}
else outputColor = texture(terrainTextures[int(textureId)], texCoord);`
It works better :
I can't understand why it's working with this code, i'm doing exactly the same thing in the first example.
Maybe it's not the good way to access an uniform array ?