Hello forum,
i am trying to implement a simple multi-texturing sample as follows, but i just see output of one texture:
void startup()
{
loadShaders();
glEnable(GL_DEPTH_TEST);
//initiate the cube to render
cube = new VboCube(shader);
//create ids for 2 texture object
glGenTextures(2,textureIDs);
int textureWidth = 0;
int textureHeight = 0;
int channels = 0;
//load the brick texture into channel 0
glActiveTexture(GL_TEXTURE0);
std::string fileName = "images/brick1.tga";
GLubyte *data = SOIL_load_image(fileName.c_str(),
&textureWidth,
&textureHeight,
&channels,
SOIL_LOAD_AUTO);
if(data == NULL)
{
std::cerr << "Failed to load the texture image: " << fileName << std::endl;
exit(EXIT_FAILURE);
}
glBindTexture(GL_TEXTURE_2D,textureIDs[0]);
//allocate storage
glTexStorage2D(GL_TEXTURE_2D,1,GL_RGB8,textureWidth,textureHeight);
//copy the data to the storage
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,textureWidth,textureHeight,GL_RGB,GL_UNSIGNED_BYTE,data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//free the soil image data
SOIL_free_image_data(data);
//load the moss texture in the channel 1
glActiveTexture(GL_TEXTURE1);
textureWidth = 0;
textureHeight = 0;
channels = 0;
data = NULL;
fileName = "images/moss.tga";
data = SOIL_load_image(fileName.c_str(),
&textureWidth,
&textureHeight,
&channels,
SOIL_LOAD_AUTO);
if(data == NULL)
{
std::cerr << "Failed to load the texture image: " << fileName << std::endl;
exit(EXIT_FAILURE);
}
glBindTexture(GL_TEXTURE_2D,textureIDs[1]);
//allocate storage
glTexStorage2D(GL_TEXTURE_2D,1,GL_RGBA8,textureWidth,textureHeight);
//copy the image to the storage
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,textureWidth,textureHeight,GL_RGBA,GL_UNSIGNED_BYTE,data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//free the soil image data
SOIL_free_image_data(data);
}
The above snippet shows the texture image loading, creation of texture object and activating the channels. Check out the following shader entries:
Vertex shader
#version 430 core
//vertex attributes from the application
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec2 vertexTexCoord;
out VS_OUT
{
vec3 position;
vec3 normal;
vec2 texCoord;
} vs_out;
//uniform values from the application
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ModelViewProjectionMatrix;
void main()
{
//pass through the texture coordinate
vs_out.texCoord = vertexTexCoord;
//transform the normal and store it
vs_out.normal = normalize(NormalMatrix * vertexNormal);
//model position in the camera coordinates
vs_out.position = vec3(ModelViewMatrix * vec4(vertexPosition,1.0));
//model position in the clip coordinates
gl_Position = ModelViewProjectionMatrix * vec4(vertexPosition,1.0);
}
Fragment shader
#version 430 core
in VS_OUT
{
vec3 position;
vec3 normal;
vec2 texCoord;
} fs_in;
uniform sampler2D BrickTex;
uniform sampler2D MossTex;
struct LightInfo
{
// light position in eye coordinates
vec4 position ;
// A,D,S intensity
vec3 intensity;
};
uniform LightInfo light;
struct MaterialInfo
{
vec3 ka; // ambient reflectivity
vec3 kd; // diffuse reflectivity
vec3 ks; // specular reflectivity
float shininess; // specular shininess factor
};
uniform MaterialInfo material;
out vec4 color;
void phongModel( vec3 pos, vec3 norm, out vec3 ambAndDiff, out vec3 spec)
{
//vector to the light source
vec3 s = normalize(vec3(light.position) - pos);
vec3 v = normalize(-pos.xyz);
vec3 r = reflect( -s, norm);
vec3 ambient = light.intensity * material.ka;
float sDotN = max(dot(s,norm),0.0);
vec3 diffuse = light.intensity * material.kd * sDotN;
spec = vec3(0.0);
if( sDotN > 0.0 )
spec = light.intensity * material.ks * pow(max(dot(r,v),0.0),material.shininess);
ambAndDiff = ambient + diffuse;
}
void main()
{
vec3 ambAndDiff, spec;
vec4 brickTexColor = texture(BrickTex,fs_in.texCoord);
vec4 mossTexColor = texture(MossTex,fs_in.texCoord);
phongModel(fs_in.position,
fs_in.normal,
ambAndDiff,
spec);
vec4 texColor = mix(brickTexColor,mossTexColor,mossTexColor.a);
color = vec4(ambAndDiff,1.0) * texColor + vec4(spec,1.0);
}
Check the image :
I can only see the brick pattern and the other one is not visible.
Any hint on what i might be missing ?
Thanks