PDA

View Full Version : How to add two texture to two different objects



helloworld1993
10-03-2017, 02:07 AM
Current, I am able to add one texture to one object using imagemagic.


gSampler = m_shader->GetUniformLocation("gSampler");
if (gSampler == INVALID_UNIFORM_LOCATION)
{
printf("gSample not found\n");
return false;
}



//assert(glGetError() == GL_NO_ERROR);


in initialization:


try
{
m_image.read("../src/test/checker.jpg");
m_image.write(&m_blob, "RGBA");

}
catch (Magick::Error& Error)
{
std::cout << "Error loading texture"<< std::endl;
return false;
}

glGenTextures(1, &m_textureObj);

//glActiveTexture(GL_TEXTURE0);


glBindTexture(GL_TEXTURE_2D, m_textureObj);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_image.columns(), m_image.rows(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob.data());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);


In render():

void Object::Render(GLuint m_textureObj)
{

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);



for (unsigned int i = 0 ; i < m_Entries.size() ; i++)
{
glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,uv));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureObj);

glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
}


glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}


fragment shader:

#version 330
smooth in vec2 TexCoord0;
out vec4 frag_color;
uniform sampler2D gSampler;
void main(void)
{
frag_color = texture2D(gSampler, TexCoord0.xy);
}


vertex shader:


#version 330
layout (location = 0) in vec3 v_position;
layout (location = 1) in vec2 TexCoord;
smooth out vec2 TexCoord0;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
void main(void)
{
vec4 v = vec4(v_position, 1.0);

gl_Position = (projectionMatrix*viewMatrix*modelMatrix)*v;
TexCoord0 = TexCoord;
}


How can I add two texture to two different objects(which are stored in the same obj file)?