Use different textures for specific triangles in VBO

I have 9 quads from triangles, for example:

enter image description here

I use VBOto store data about them - their position and texture coordinates.

My question is: is it possible for square 5 to have a different texture than the rest of the squares using only one VBOand shader?:

enter image description here

Green represents texture 1 and yellow texture 2.

So far I have received:

GLfloat vertices[] = { 
    // Positions
    0.0f, 0.0f,
    ...

    // Texture coordinates
    0.0f, 0.0f, 
    ...
};

I create VBOusing this array vertices[], and then I bind my first texture m_texture1(I also have access to the second - m_texture2) and the shader call:

glBindTexture(GL_TEXTURE_2D, m_texture1);

glUseProgram(program);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vboId);          // for vertex coordinates
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);                                      // Vertices
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(GLfloat)*108));           // Texture
glDrawArrays(GL_TRIANGLES, 0, 108);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glUseProgram(0);

My vertex shader:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec2 texcoord;

out vec2 Texcoord;

void main()
{
    gl_Position = position;
    Texcoord = texcoord;
}

And the fragment shader:

#version 330

in vec2 Texcoord;

out vec4 outputColor;

uniform sampler2D tex;

void main()
{
    outputColor = texture(tex, Texcoord) * vec4(1.0,1.0,1.0,1.0);
}

So, basically I only use one texture here, because I have no idea how to use the second.

+5
1

, uniform sampler2D tex.

glActiveTexture (GL_TEXTURE0), , GL_TEXTURE1 . , 0 .

: . , , uniform sampler2D tex, glUniform1i . , , glDrawArrays VBO, , .

, , , . , .

+2

All Articles