Hi,
I'm trying to render multiple textures on a single mesh. What i'm doing is binding three different textures using texture units and i'm adding those textureIDs to my vertex buffer. So my vertex buffer looks something like this
[vec3 , vec4 , vec2 , vec3 , float ]
Vertex Buffer = [vertex position, vertex color, texcoord, normal, textureID]
and in my pixel shader I have an array of texture samplers and I render the correct texture using textureID as an index to the array.
This works great, my only problem is that i'm getting some texture artifacting. From the looks of it some textures are partly being drawn on top of other textures.
I don't know if this a good way of drawing multiple textures on a mesh. If you guys know a more efficient way of doing this I would love to know about it.
here is how I draw everything
void init()
{
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//--------
void Draw()
{
//I think the problem is this block of code
//-----
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(glshader->GetProgramID(), "textures[0]"), 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(glshader->GetProgramID(), "textures[1]"), 1);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, texture3);
glUniform1i(glGetUniformLocation(glshader->GetProgramID(), "textures[2]"), 2);
//-----
vertexBuffer->Bind();
indexBuffer->Bind();
glDrawElements(GL_TRIANGLES, indexBuffer->GetIndicesCount(), GL_UNSIGNED_INT, nullptr);
indexBuffer->Unbind();
vertexBuffer->Unbind();
}
here is my vertex shader
#version 450 core
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec4 inColor;
layout(location = 2) in vec2 inUV;
layout(location = 3) in vec3 inNormals;
layout(location = 4) in float inTextureID;
struct data
{
vec2 UV;
float textureID;
};
out data shared_data;
void main()
{
shared_data.UV = inUV;
shared_data.textureID = inTextureID;
}
here is my pixel shader
#version 450 core
struct data
{
vec2 UV;
float textureID;
};
in data shared_data;
out vec4 color;
uniform sampler2D textures[3];
void main()
{
int texID = int(shared_data.textureID);
color = texture(textures[texID], shared_data.UV);
}