0
votes

I'm developing a 3d program with OpenGL 3.3. So far I managed to render many cubes and some spheres. I need to texture all the faces of all the cubes with a common texture except one face which should have a different texture. I tried with a single texture and everything worked fine but when I try to add another one the program seems to behave randomly.
My questions:
is there a suitable way of passing multiple textures to the shaders?
how am I supposed to keep track of faces in order to render the right texture?
Googling I found out that it could be useful to define vertices twice, but I don't really get why.

2
Also, you should check out computergraphics.stackexchange.com and gamedev.stackexchange.com which tend to be better at answering these types of questions.default

2 Answers

2
votes

Is there a suitable way of passing multiple textures to the shaders?

You'd use glUniform1i() along with glActiveTexture(). Thus given your fragment shader has multiple uniform sampler2D:

uniform sampler2D tex1;
uniform sampler2D tex2;

Then as you're setting up your shader, you set the sampler uniforms to the texture units you want them associated with:

glUniform1i(glGetUniformLocation(program, "tex1"), 0)
glUniform1i(glGetUniformLocation(program, "tex2"), 1)

You then set the active texture to either GL_TEXTURE0 or GL_TEXTURE1 and bind a texture.

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture1)

glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, texture2)

How am I supposed to keep track of faces in order to render the right texture?

It depends on what you want.

  • You could decide which texture to use based the normal (usually done with tri-planar texture mapping).
  • You could also have another attribute that decides how much to crossfade between the two textures.

    color = mix(texture(tex1, texCoord), texture(tex2, texCoord), 0.2)
    
  • Like before, you can equally have a uniform float transition. This would allow you to fade between textures, making it possible to fade between slides like in PowerPoint, so to speak.

Try reading LearnOpenGL's Textures tutorial.

Lastly there's a minimum of 80 texture units. You can check specifically how many you have available using GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS.

0
votes

You can use index buffers. Define the vertices once, and then use one index buffer to draw the portion of the mesh with the first texture, then use the second index buffer to draw the portion that needs the second texture.

Here's the general formula:

  1. Setup the vertex buffer
  2. Setup the shader
  3. Setup the first texture
  4. Setup and draw the index buffer for the part of the mesh that should use the first texture
  5. Setup the second texture
  6. Setup and draw the index buffer for the part of the mesh that should use the second texture.