1
votes

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!

1
In short, you need to make use of more than one texture unit with each unit then mapping to a sampler2D in your fragment shader. Some excellent information can be found here. - G.M.

1 Answers

0
votes

Don't know if this is what you're looking for. But, here i go..

In the fragment shader, you would be using multiple sampler2D for example:

uniform sampler2D tex0;
uniform sampler2D tex1;

And you need to do some adjustment to the c++ code too assigning the sampler2D like so:

glUniform1i(glGetUniformLocation(shader, "tex0"), 0);
glUniform1i(glGetUniformLocation(shader, "tex1"), 1);

The 0 and 1 is the active texture number from GL_TEXTURE0 and GL_TEXTURE1. it would be N if it is GL_TEXTUREN. So that being said, you should bind those texture before with the according glActiveTexture like so:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex1);

Which i believe you already did so.


Oh, and perhaps you can use array too in the fragment shader like so:

uniform sampler2D tex[2];

And the c++ code would be something like this:

for (int i = 0; i < 2; i++) {
    glUniform1i(glGetUniformLocation(shader, "tex[" + std::to_string(i) + "]"), i);
}

Edit: I haven't test if "tex[" + std::to_string(i) + "]" works. But i think you get the idea. Might be ("tex[" + std::to_string(i) + "]").c_str()

Edit: Talking about arrays, there is also something called texture arrays. For details and more options consider seeing this