• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
harshman_chris

Advice for a more efficient particle system.

6 posts in this topic

I need some some advice on my particle system.

 

Right now I have a particle emitter which holds a list of particles and each particle has a VBO for its vertices and uvs.

 

Which is bad I know this, it with drawing only 150 particles, I go from just over 100 draw calls, to 3000 draw calls using gDEBugger.

 

Each particle keeps track of its own local and world matrix.

 

I know the general idea is that I need to create a single VBO for each piece of data in the particle emitter, but then how to I pass the mvp for each particle, my particles are not always going to start at the same size.

 

Do I do something like this:

 

int i = 0;

// Pass in new mvp here and any other data that may have changed like colour

glDrawArrays(GL_TRIANGLE_STRIP, i, 4);

i+= 4;

 

 

My code is below:

// Particle Initialization

uvs.push_back(Vector2(1,1)); 
	vertices.push_back(Vector3(
		0+(particleInfo.sizeX/2),
			0+(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(0,1)); 
	vertices.push_back(Vector3(
			0-(particleInfo.sizeX/2),
			0+(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(1,0)); 
	vertices.push_back(Vector3(
			0+(particleInfo.sizeX/2),
			0-(particleInfo.sizeY/2),
			0));

	uvs.push_back(Vector2(0,0)); 
	vertices.push_back(Vector3(
			0-(particleInfo.sizeX/2),
			0-(particleInfo.sizeY/2),
			0));

// This is just a bind function.

// Binding the Data

void Particle::bind()
{
	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

	glGenBuffers(1, &uvbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
	glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(Vector2), &uvs[0], GL_STATIC_DRAW);
}

 

 

Here is where I update the particle based on the properties.

 

// Updating the Matrix

/* removed for space */

localMatrix = Matrix4::toMatrix4(rotation,position,scale);

 

 

 

Drawing the Particles

 

void Particle::draw()
{
	if(alive)
	{
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_ALWAYS);

		Camera *cam = this->particleInfo.scene->getActivePerspectiveCamera();

		worldMatrix = cam->getObjectToWorld().inverse() * localMatrix;

		Matrix4 mvp = cam->getProjectionMatrix() * worldMatrix;

		glUniformMatrix4fv((*particleInfo.shader)["mvp"], 1, GL_FALSE, mvp.transpose().getArray());

		glUniform4f((*particleInfo.shader)["colour"],
			particleInfo.colour.R,
			particleInfo.colour.G,
			particleInfo.colour.B,
			particleInfo.colour.A);

		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
		glVertexAttribPointer(
			(*particleInfo.shader)["modelVertex"],	// attribute
			3,							// size
			GL_FLOAT,					// type
			GL_FALSE,					// normalized?
			0,							// stride
			(void*)0					// array buffer offset
		);

		// 2nd attribute buffer : UVs
		glEnableVertexAttribArray(1);
		glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
		glVertexAttribPointer(
			(*particleInfo.shader)["uv"],			// attribute
			2,							// size
			GL_FLOAT,					// type
			GL_FALSE,					// normalized?
			0,							// stride
			(void*)0					// array buffer offset
		);

		// Draw the triangle !
		glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());

		glDisableVertexAttribArray(0);
		glDisableVertexAttribArray(1);

		glDepthFunc(GL_LESS);

		glDisable(GL_BLEND);

	}
}

 

 

Particle Emitter Draw

 

void ParticleEmitter::draw()
{
	Component::draw();

	if(running)
	{
		iter = particles.begin();

		this->getShader()->begin();

		textureDiffuse->bindTexture((*this->getShader())["diffuseTextureSampler"],0);
	
		while (iter != particles.end())
		{
			(*iter)->draw();
			++iter;
		}

		glBindTexture(GL_TEXTURE_2D, 0);

		this->getShader()->end();	
	}
}
Edited by harshman_chris
0

Share this post


Link to post
Share on other sites

You don’t pass a matrix for every particle.  You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

 

 

L. Spiro

1

Share this post


Link to post
Share on other sites
You don’t pass a matrix for every particle.  You calculate the positions of the vertices manually on the CPU and you put the already-transformed coordinates into the buffer.

Your shader should account for this by excluding any “world” transforms from its calculations.

 

 

L. Spiro

 

So what I should do then is hold the coordinates in the particle, then fetch them with the emitter every update and bind them to a buffer.

 


glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_STATIC_DRAW);

 

 

Using this,  But isnt it bad to change the size of a buffer every time particles are added.

 

Should I delete and recreate the buffer or just reuse it?

0

Share this post


Link to post
Share on other sites

You are forgetting that you are not required to draw the whole buffer.  Nor are you required to draw it from its first index.

Also, the buffer should be dynamic, not static.

 

It should never need resizing.

Firstly, you should start adding vertices from the last place on the buffer where you have no added any.  Then draw that segment, and keep adding vertices after that point.

Don’t draw a part of the buffer and then start overwriting that part.  Walk the buffer sequentially like a ring buffer.

When you run out of room, just issue a draw call from there and then continue walking over the buffer back at its start.

 

You may (definitely) also want to double-buffer these and change buffers each frame.

 

 

L. Spiro

1

Share this post


Link to post
Share on other sites

to add to L. Spiro's answer, when updating the vbo(which should be dynamic), do not use glBufferData, instead map the buffer, and write the values like so:

 

 

//assumed that everything is setup for drawing. 
MyVertexStruct *VBOMap = (MyVertexStruct*)
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); 
int o=0; 
for(int i=0;i<ParticlesCount;i++,o+=sizeof(MyVertexStruct)){   
 if(o>=BufferSize){    
  glUnMapBuffer(GL_ARRAY_BUFFER);    
  glDrawArrays(GL_TRIANGLES, 0, o);    
  VBOMap = (MyVertexStruct*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);    
  o=0;
 }
 (VBOMap+o).xyz = Particle[i].xyz;
}
glUnMapBuffer(GL_ARRAY_BUFFER);
glDrawArrays(GL_TRIANGLES, 0, o)
//other stuff.
Edited by slicer4ever
1

Share this post


Link to post
Share on other sites

A couple of questions.

 

how do I create my buffers; like this? 

glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vector3), &vertices[0], GL_DYNAMIC_DRAW);

 

That wont work because I if I create my buffer before any particle has been created and added to the list it wont work. Can I just Generate the buffer and bind it, without using glBufferData?

 

 

In the previous post you are using a array of whatever my struct is, which in this case is my Vector3 and Vector2 classes.

 

In addition, I am trying to figure out how to pass my colour property now for each particle, for that I guess I just need to create another VBO to how colour for each vertex, which seems like overkill since there is 1 colour per particle.

 

Going on to the glMapBuffer, and in general your code, you are using a single buffer to hold all the data write? where as I create a buffer for each type of data, I have never used the method you are using  before.

 

How exactly does MapBuffer work? how can I update my code as in seen in the first post? Why do you have two glDrawArrays, is it because you create a buffer of arbitrary size or do I never use glBufferData. Before your code I would need to bind the buffer as well.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0