View Full Version : Interleaved (Vertex) Arrays with different types?
12-24-2002, 04:33 AM
I hope I formulated the topic in a proper way, but I think you will soon understand what I mean anyway.
Here we go:
I was considering to use this type GL_T2F_C4UB_V3F .
But I don't know what type the array should be declared as. As a float? What is the point then of using unsigned bytes for the colors?
A byte takes less memory than a float, right? This memory will be lost due to the float array, right?
Also if I am using integers for the TexCoords right now, this memory will also be lost?
I hope you can clear things upp a bit for me, thanks!
[This message has been edited by B_old (edited 12-24-2002).]
It doesnīt matter which type you use for the array. Think of the array only as a chunk of memory. First you calculate the amount of memory you need. Say you need memory for 10 vertices, each with texcoords and color = 10 * ((2 * sizeof (float)) + (4 * sizeof (byte)) + (3 * sizeof (float)))
This yields to:
8 byte for texcoords
4 byte for colors
12 byte for the vertex
times 10 for all vertices = 240 byte
And now you allocate your array. How you do this exactly depends on your favor. Which type of array you use does not matter. BUT you have to consider, that different types have different sizes:
BYTE* array = new byte[size]
is not equal to
float* array = new float[size]
because sizeof (float) = 4 bytes and sizeof (byte) = 1 bytes.
BYTE* array = new byte[size]
is equal to
float* array = new float[size/4]
After all OpenGL doesnīt work with floats if you allocated a float array. It just takes a pointer to your data (and an argument that tells gl how much data there is) and uses this data.
I advise you to always use byte* arrays instead of float* arrays. There is no speed difference or anything else. The only difference is, that one easily forgets, that a float is 4 bytes big. Therefore it could happen to you (as it happened to me), that you allocate an array, that is 4 times bigger than needed.
Also remember that
byte *pointer = someadress;
pointer++; //increases the pointer by 1
is not equal to
float *pointer = someadress;
pointer++; //increases the pointer by 4!
I hope you now understand, that your choice for the arraytype has no influence on OpenGLīs behaviour at all.
12-24-2002, 12:07 PM
WOW, that was a great answer, thanks a lot!
I have just a minor question left:
GL_T2F_C4UB_V3F, this means that OGL is expection floats for the texcoords, right?
Do I have to locate space for floats or can I save them as integers and OGL transforms them to it's liking?
hmmm I guess that will mess things up.
I am only considering interleaved arrays because I can make all I need with so little code, but if I took integers instead of floats I had to mess around with stride and stuff like that, don't I?
Is there an advantage with interleaved arrays apart from the fact that it looks tidy?
Thanks again for the detailed answer!
12-24-2002, 12:43 PM
For interleaved arrays, it can also be useful to just create a struct of the format you plan to use. In your case the struct would look something like so:
float s, t;
unsigned char r, g, b, a;
float x, y, z;
You can then create an array of your struct to store your data in and just pass a pointer to that array to glInterleavedArrays.
12-25-2002, 02:08 AM
Hmmm, that is not an bad idea I think, thanks.
Still, what about the integers that are supposed to be floats?
12-25-2002, 10:22 AM
Well... that format you are using expects to use floats for the texcoords. You should also be aware that even if you were to use integers for the texcoords, the range of the tex coords are still 0-1. So unless you were to use the texture matrix to scale it differently, using integers you could only really specify the outermost corners of the texture as the texcoords.
12-25-2002, 01:32 PM
This is all for a particle renderer.
So I guess I will never use anything but the whole texture? hmmm Or maybe for animated particles it would be nice to let the texture scroll like a movie, interesting point indeed!
Would it be possible to use 3D-textures for this? I have no idea what they actually mean and if they would be any good in this case...
By the way it seems that there is no interleaved array that lets me choose float for 2 texcoords and float for RGBA and float for 3 vertices. Isn't that kind of uncool?
Thanks for your help all.
12-26-2002, 04:53 AM
Yeah, it appears that glInterleavedArrays doesn't allow you to use that specific format. You can always setup your own format of interleaved arrays by enabling the right arrays and setting all the strides correctly, though. It's a bit more work, but it is an option.
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.