Very simple example of Vertex Arrays, VBO's, Point Sprites

#1

I’ve posted some sample code for using Vertex Arrays, VBO’s and Point Sprites at
http://www.memo.tv/files/memotv/oF-VA+VBO+PS.zip

its very very basic and is aimed more at showing those who don’t know how to use the mentioned things. You can easily get hundreds of thousands of basic particles, if not millions at decent framerates (depending on hardware of course).

You will need to hack ofTexture.cpp to only use GL_TEXTURE_2D…

check testApp::keyPressed to see what keys do what…

1 Like
#2

Jesus! never knew my intel graphics card could draw that many particles :slight_smile:

Rui

#3

Yeah!!! Thanks Memo for this performance information.

#4

hi,

im running into error when building this example in 006.

has anyone been able to port it to version 006?

L.

#5

hi julapy,

i just did it…

basically, you have to add ‘texData.’ before all calls to ofTexture data members (like tex_t, rendertarget…) in MSATexture.h and MSAImage.h

Hope it help.

#6

yup, for 006 you just need to add texData like martignasse mentioned.

In addition, you don’t need to hack ofTexture to always use GL_TEXTURE_2D, but you can do it externally by just calling ofDisableArbTex() once (e.g. in your setup, this disables the use of GL_TEXTURE_RECTANGLE_ARB). After you’ve loaded the texture you want to use for the point sprite, you can re-enable GL_TEXTURE_RECTANGLE_ARB by calling ofEnableArbTex() if you want to create new textures (e.g. load images etc.) and use rectangular textures for them.

P.S. it will be more optimal to use square power of two textures (e.g. 128x128, 256x256, 512x512, 1024x1024 - if your card supports it).

#7

This is soo good. Is there away to apply this to a vector of particles?

#8

Yup, when you’re cycling through the vector of particles updating them, also write their position and color to arrays to be sent to the card (I think thats what the example is already doing, though with arrays instead of vectors).

#9

I guess im kinda confused on how to approach it:

If i have a vector of particles like

  
  
vector <Particle*> particles;  
  
//mouse Click  
particle.push_back(new Particle(x, y));  
  
//update  
for(int i=0; i<particles.size(); i++) {  
     particles[i]->update();  
}  
  
//draw  
//eekk  

you have in your example:

  
  
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[0]);  
		glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, numParticles * 3 * sizeof(float), pos);  
		glVertexPointer(3, GL_FLOAT, 0, 0);  
		  
		glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[1]);  
		glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, numParticles * 4 * sizeof(float), col);  
		glColorPointer(4, GL_FLOAT, 0, 0);  

How would i get pos and col to be from the particles vector? Do i create a new pos and col array every frame? For numParticles i would just use particles.size(), but im stuck after that.

#10

ah yea, interesting problem.

I’ve not encountered this problem cos I generally use a fixed size array of particles, capped to a predetermined maximum size, and whenever I need to add a new particle, I just reset the oldest particle (that way I have control over the maximum number of particles, so system can’t grind to a halt under heavy circumstances, and also no object creation or deletion happening every frame).

In your case, you could create a vector for pos and another vector for col as well. Everytime you add or remove a particle, you’d need to add or remove from the pos and col vectors as well. Then you can pass &pos[0] as your position buffer (pointer to the first element in the vector, which stores all data sequentially so you can use it as a normal array).

Maybe there is a better/more efficient way but isn’t popping into my head right now.

#11

This is an issue I was also having recently.

Adding and removing from a vectors for particles gets very slow when you get to large numbers of particles, so you should use a fixed size array for your particles.

i.e. have MAX_PARTICLES and NUM_PARTICLES

like memo said, knowing which index in the array to use is the trouble. I have a list of particles that are alive, and when adding new particles look up an index to use from the list of particles that are dead.

Also when you loop through to update the arrays for the vbo, you need to loop through the number of alive particles, and not the maximum number of particles, which is where my list of alive particles comes in.

#12

Hi Of People

If anyone need here is the modified MSATEXURE for 0.06 only download MEMO Version and copy and replace this file :slight_smile: Thx Memo Nice Applications :slight_smile:

MSATexture.rar

#13

I have tried to build oF_VA+VBO+PS.zip with modified MSATexture.h, but unsuccessfully :frowning:

I’m on Windows XP, Visual Studio C++ 2008, of_preRelease_v0.06_windows_VS2008_FAT

Can someone tell me what I’m doing wrong…

Solution file is in attachment.

Compiling…
main.cpp
d:\radni\openframeworks\of_prerelease_v0.06_windows_vs2008_fat\addons\ofaddons.h(1) : fatal error C1021: invalid preprocessor command ‘warning’
testApp.cpp
d:\radni\openframeworks\of_prerelease_v0.06_windows_vs2008_fat\addons\ofaddons.h(1) : fatal error C1021: invalid preprocessor command ‘warning’
:
particle - 2 error(s), 0 warning(s)

Thanx!
Mladen

particle.rar

#14

I have tried to build oF_VA+VBO+PS.zip with modified MSATexture.h, but unsuccessfully

I’m on Windows XP, Visual Studio C++ 2008, of_preRelease_v0.06_windows_VS2008_FAT

Can someone tell me what I’m doing wrong…

Solution file is in attachment.

Compiling…
main.cpp
d:\radni\openframeworks\of_prerelease_v0.06_windows_vs2008_fat\addons\ofaddons.h(1) : fatal error C1021: invalid preprocessor command ‘warning’
testApp.cpp
d:\radni\openframeworks\of_prerelease_v0.06_windows_vs2008_fat\addons\ofaddons.h(1) : fatal error C1021: invalid preprocessor command ‘warning’
:
particle - 2 error(s), 0 warning(s)

Thanx!
Mladen

Hi Potter3366

i download your file but all its ok, i see your error and maybe you maybe not foloww the correct process…

Copy the alladdonfiles Projects to the folder apps/addonsExamples and change to name, and copy and paste the files Main.cpp, TespApp.cpp, TestApp.h, MSAImage.h, MSATexture.h, in src folder like your example, and add by hand to the project in visual Studio, and look if you can see the files in your branch folder… and run…

If not solution your problem adviceme and i install visual in my house because muy compnay only works in codebloks…

Have Fun and Take care

John Avila

#15

im trying to draw ribbon trails using VBO.
my ribbon trail vertex array and colour array is like this,

float tvr[ MAX_PARTICLES ][ 6 * MAX_TRAIL_LENGTH ]; // trail vertex.
float tcl[ MAX_PARTICLES ][ 4 * MAX_TRAIL_LENGTH ]; // trail colour.

so each particle has a ribbon trail of MAX_TRAIL_LENGTH
with two vertex for each point on the ribbon.

currently im drawing using glBegin( GL_QUAD_STRIP ) although would like to optimise using VBOs.

with this point sprite example, im assuming its just grabbing every 2 vertex in the vertex array and using them as the location to draw the point sprite.

do i have to use a VBO for each trail?
or is there a way i can pass in my trail vertex and colour arrays into to VBO to render all at once?

#16

Strange - I’ve compiled this under 0.06 with the changes to MSATexture.h and it runs fine with no errors, but the framerate are low and don’t seem to make sense to me. With the default settings, I’m getting ~12fps with the useVA:0 and useVBO:0. Turning the VBO and or Vertex Array on actually makes it slightly slower by about 1fps!

It’s with a decently fast card - radeon hd 4850

Any ideas? :shock:

#17

Hi.
I just compiled the latest files under OF06 and updated MSATexture.h with the one uploaded in this thread.
The compiler gave me two errors. Both are

  
 'struct ofTextureData' has no member named 'textureName'  

so I changed both the

  
texData.textureName[0]  

into

  
texData.textureID  

Now the application compiles and runs but I get just a screen with a changing background (and indications on the current FPR, particles count etc) BUT NO PARTICLES.

Weird. Any idea?

#18

Hey all, sorry for not updating the example sooner, been a bit busy.

In 006+ actually it is much simpler, you don’t need MSAImage or MSATexture at all as the required functionality is now in the core. You can use a plain old ofImage. Updated example at
http://www.memo.tv/vertex-arrays-vbos-a-…-eworks-006

#19

I have the same problem as mattjakob. I’m running the Particle example from the ofxOpenCL library. I get this error:

‘struct ofTextureData’ has no member named ‘textureName’

Changing to textureID just creates a changing background.

What do I need to tweak to run the example?

#20

Thank you for the update!!