I tried to search online but I couldn't understand how to add a texture for my model. (It can be seted from the start) I having trouble with it and I would like for some help in the changes I should make with my code.
My texture setting function is:
void Texture::setTexture(unsigned char* data, unsigned int width, unsigned int height) {
glGenTextures(1, &m_texture[unit]); // Genatate 1 texture in m_texture
glBindTexture(GL_TEXTURE_2D, m_texture[unit]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
My bind function:
void Texture::Bind(unsigned int unit) {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture[unit]);
And my vertex shaders looks like this:
attribute vec3 position; // Position values
attribute vec2 texCoord; // Texture coordinates values
attribute vec3 normal; // Normal
varying vec2 texCoord0;
varying vec3 normal0;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(position, 1.0);
texCoord0 = texCoord;
normal0= (transform * vec4(normal, 0.0)).xyz;
}
fragment shader:
varying vec2 texCoord0;
varying vec3 normal0;
uniform sampler2D diffuse;
void main()
{
gl_FragColor = texture2D(diffuse, texCoord0) *
clamp(dot(-vec3(0,0,1), normal0), 1.0, 1.0);
}
Shader ctor:
Shader::Shader(const string& fileName)
{
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
glBindAttribLocation(m_program, 2, "normal");
glLinkProgram(m_program);
glValidateProgram(m_program);
m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_program, "transform");
}
I want to choose choose some vertices to use the first texture and some the other one. For example, a cube which each square has a different texture.
How can I add another texture? And what changes should I make in my Mesh class?
Sorry for the stupid question, I'm new in openGL :) Thanks in advance!