Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Particle System question  (Read 2917 times)
0 Members and 1 Guest are viewing this topic.
Offline sgthale

Senior Newbie





« Posted 2012-02-27 06:01:22 »

I have a particle system and one of the behaviors is Smoke. So each particle starts small and grows big until it fades away. A problem with this is the fact that since the texture is being enlarged, it appears as if it is moving to the right. This can be resolved by rendering the particle┬┤s x position minus half its width. However, this small division when coupled with thousands of particles can cause performance drops. My question is, is there another way to remove the "drifting to the right" bug? Perhaps an optimization? My code with the division is...


P.S. The bug also affects the image to look like its drifting down. Thus the minus half the height in the code:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
glBegin(GL_QUADS);
   glTexCoord2f(ci.getXD(),ci.getYD());// Upper-left
  glVertex2i((int)p.getX()-p.getWidth()/2, (int)p.getY()-p.getHeight()/2);
   glTexCoord2f(ci.getXWD(), ci.getYD());// Upper-right
  glVertex2i((int)p.getX()+p.getWidth()/2, (int)p.getY()-p.getHeight()/2);
   glTexCoord2f(ci.getXWD(), ci.getYHD());// Bottom-right
  glVertex2i((int)p.getX()+p.getWidth()/2, (int)p.getY()+p.getHeight()/2);
   glTexCoord2f(ci.getXD(), ci.getYHD()); // Bottom-left
  glVertex2i((int)p.getX()-p.getWidth()/2, (int)p.getY()+p.getHeight()/2);
glEnd();
Offline sgthale

Senior Newbie





« Reply #1 - Posted 2012-02-27 06:46:42 »

I managed to remove 6 of the divisions by precalculating the width and height offsets. Still i hate using divisions in loops. Heres the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
int hw = p.getWidth()/2;
int hh = p.getHeight()/2;
glBegin(GL_QUADS);
   glTexCoord2f(ci.getXD(),ci.getYD());// Upper-left
  glVertex2i((int)p.getX()-hw, (int)p.getY()-hh);
   glTexCoord2f(ci.getXWD(), ci.getYD());// Upper-right
  glVertex2i((int)p.getX()+hw, (int)p.getY()-hh);
   glTexCoord2f(ci.getXWD(), ci.getYHD());// Bottom-right
  glVertex2i((int)p.getX()+hw, (int)p.getY()+hh);
   glTexCoord2f(ci.getXD(), ci.getYHD()); // Bottom-left
  glVertex2i((int)p.getX()-hw, (int)p.getY()+hh);
glEnd();
Offline davedes
« Reply #2 - Posted 2012-02-27 07:16:10 »

You're going a bit crazy with premature optimization here. In a particle system your bottleneck is much more likely to lie in fill rate / GPU. A few extra divisions (or primitive type casts for that matter) are negligible, i.e. including them won't affect your particle system's performance in any detectable way.

A more sane optimization would be to only call glBegin/glEnd once per loop; then repeat the drawing code (glTexCoord/glVertex) as necessary for each particle.

Better still would be to use vertex arrays:
http://www.songho.ca/opengl/gl_vertexarray.html

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sgthale

Senior Newbie





« Reply #3 - Posted 2012-02-27 07:59:42 »

You're going a bit crazy with premature optimization here. In a particle system your bottleneck is much more likely to lie in fill rate / GPU. A few extra divisions (or primitive type casts for that matter) are negligible, i.e. including them won't affect your particle system's performance in any detectable way.

A more sane optimization would be to only call glBegin/glEnd once per loop; then repeat the drawing code (glTexCoord/glVertex) as necessary for each particle.

Better still would be to use vertex arrays:
http://www.songho.ca/opengl/gl_vertexarray.html

Oh great. K thanks. By the way, do you know any tutorials for vertex array buffers for 2D?
Offline theagentd
« Reply #4 - Posted 2012-02-27 15:20:36 »

Oh, god... You're doing 10 OpenGL calls for each particle... Kill it with fire! Store the data in a ByteBuffer and upload it to a VBO each frame, and draw everything with glDrawArrays.

If you're still having a problem with the texture drifting then I suspect the problem is your texture coordinates. If you can't figure it out, post the code for the methods that generate the texture coordinates.

Myomyomyo.
Offline sgthale

Senior Newbie





« Reply #5 - Posted 2012-02-27 23:26:03 »

Oh, god... You're doing 10 OpenGL calls for each particle... Kill it with fire! Store the data in a ByteBuffer and upload it to a VBO each frame, and draw everything with glDrawArrays.

If you're still having a problem with the texture drifting then I suspect the problem is your texture coordinates. If you can't figure it out, post the code for the methods that generate the texture coordinates.

Yeah thats my next task for optimizing this code. But i dont know how to use VBO arrays. Im still looking for a tutorial specifically for 2D only...
Offline StumpyStrust
« Reply #6 - Posted 2012-02-28 01:16:48 »

I just got my particles system working using vertex arrays and the performance boost was insane. Almost double the particle count. It is 2D and I could send you the code or you can google opengl vertex arrays/VBO if your using lwjgl then

http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29

hope this helps.

Offline sgthale

Senior Newbie





« Reply #7 - Posted 2012-02-28 02:44:18 »

I just got my particles system working using vertex arrays and the performance boost was insane. Almost double the particle count. It is 2D and I could send you the code or you can google opengl vertex arrays/VBO if your using lwjgl then

http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29

hope this helps.

I didnt understand it very well...  Clueless . Ill research more sources...
Offline sgthale

Senior Newbie





« Reply #8 - Posted 2012-03-20 00:42:18 »

K i understand it now. However, when I bind a texture to use for VBO, I use a large sprite sheet. Obviously I dont want to render the entire sheet so how do i bind a specific texture coordinate of a large image?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (28 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!