PDA

View Full Version : VAO with a lot of triangles. Just want to texture one..



joo50135
06-14-2015, 03:01 AM
Hello my friends!!

This is my first post here, and I'm a complete newbie to OpenGL...so please be gentle :D

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.

1871

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:
1872

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

1873

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 :(

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 :)

Oply1953
06-14-2015, 05:41 AM
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.

GClements
06-14-2015, 07:11 AM
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 count and indices parameters. But note that count is the total number of indices (so for a pair of triangles, it should be 6), and indices 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));

joo50135
06-14-2015, 09:28 AM
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 count and indices parameters. But note that count is the total number of indices (so for a pair of triangles, it should be 6), and indices 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));


That's it GClements!!! :D Exactly what I wanted! :cool:

Thank you GClements and Oply1953 for your help!