VAO with a lot of triangles. Just want to texture one..

Hello my friends!!

This is my first post here, and I’m a complete newbie to OpenGL…so please be gentle :smiley:

I’ve followed up some tutorials and I’m using: OpenGL 3.0, GSLM, GLEW, GLM and SOIL.

I’ve created a few triangles, rendered on screen, pretending it is a matrix of squares.

Something like the image bellow

I’ve created my class that generates the array for vertices (to populate my VBO), and for indices (to populate my EBO).
So I have my VAO with all the data that I want.

[ATTACH=CONFIG]1104[/ATTACH]

Now I want to add a texture just to one square (one pair of triangles), so I’ve created successfully my texture, and loaded with SOIL and bind it while rendering like this:

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

Now if I draw it like this:

        glBindVertexArray(VAO);
			glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLuint), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

I see all my squares with the same texture…which makes sense :o

Like this:
[ATTACH=CONFIG]1105[/ATTACH]

But now I want to add my texture to just one square… to be like this bellow:

[ATTACH=CONFIG]1106[/ATTACH]

So, I’ve tried using the

glDrawRangeElements

instead of the

glDrawElements

Like this:

#define BUFFER_OFFSET(i) ((char *)NULL + (i))
glDrawRangeElements (GL_TRIANGLES, 0, 6, 3, GL_UNSIGNED_INT, BUFFER_OFFSET(2));

But it doesn’t work :frowning:

THE QUESTION:
How can I tell openGL to just add the texture to just one of my triangles that are on the same VAO?

I have my vertices like:
[0, 1, 0, 1, 1, 0, …]
And my indices like:
[0, 1, 3, 1, 2, 3, …]

And I just want to add a texture to a set of 2 triangles (1 square).

Thank you in advance :slight_smile:

I may be wrong, but I’m 99.99% sure VAOs do not store texture state information. I believe only display lists do that (but I may be wrong). Try binding the texture, draw the pair of triangles you need to draw the texture on, then unbind the texture and draw whatever else.

You don’t actually need glDrawRangeElements(); it’s equivalent to calling glDrawElements() with the same values. It just allows you to specify the minimum and maximum indices so that the implementation doesn’t need to either transfer data for vertices which aren’t used or scan the array of indices to determine which vertices are used.

To draw a subset of the elements, just modify the [var]count[/var] and [var]indices[/var] parameters. But note that [var]count[/var] is the total number of indices (so for a pair of triangles, it should be 6), and [var]indices[/var] is the offset in bytes (so if you want to skip the first pair of triangles, it should be 6 indices * 4 bytes per index = 24 bytes). E.g.:


glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6*4));

[QUOTE=GClements;1267723]You don’t actually need glDrawRangeElements(); it’s equivalent to calling glDrawElements() with the same values. It just allows you to specify the minimum and maximum indices so that the implementation doesn’t need to either transfer data for vertices which aren’t used or scan the array of indices to determine which vertices are used.

To draw a subset of the elements, just modify the [var]count[/var] and [var]indices[/var] parameters. But note that [var]count[/var] is the total number of indices (so for a pair of triangles, it should be 6), and [var]indices[/var] is the offset in bytes (so if you want to skip the first pair of triangles, it should be 6 indices * 4 bytes per index = 24 bytes). E.g.:


glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6*4));

[/QUOTE]

That’s it GClements!!! :smiley: Exactly what I wanted! :cool:

Thank you GClements and Oply1953 for your help!