Multiple Particle Effects
3. Multiple Textures
Introduction
We are going to modify the gl renderer to make use of different textures for the various particle types parameter.

Before you start, you should have already implemented the second part of this tutorial.
Definitions
In the file ref_gl\gl_particles.h, add a new definition to the end of your list of particle types
#define PT_DEFAULT		0
#define PT_BEAM			1
#define PT_SPIRAL		2
#define PT_BUBBLE		3

#define PT_MAX			4 //c14 Add this line
You need to have PT_MAX equal to the number of particle types that you have defined. I have 4 particle types numbered 0..3 so I set PT_MAX to 4.

Also, you need to include particles.h near the top of ref_gl\gl_local.h
In the file ref_gl\gl_rmain.c, find this line:
image_t		*r_particletexture;
and replace it thus:
image_t		*r_particletexture[PT_MAX];	// c14 multiple particle textures
We have r_particletexture is used to store the texture information for the particles. We have changed it from one storage area, to an array, large enough for all of our particle textures.
We need to do a similar thing in ref_gl\gl_local.h, find this line:
extern	image_t		*r_particletexture;
and replace it thus:
image_t		*r_particletexture[PT_MAX];	// c14 multiple particle textures
Loading the textures
The particle texture is loaded in ref_gl\gl_rmisc.c. Find this section, in the function R_InitParticleTexture:
	r_particletexture = Draw_FindPic("particle");

	if (!r_particletexture) {
		r_particletexture = GL_LoadPic ("***particle***", (byte *)data, 16, 16, it_sprite, 32);
	}

This above is taken from my Soft Particles tutorial. If you have implemented external particle textures differently, then you should still have something similar

The code above tries to load particle.pcx, or particle.tga or some such. If it can't load that image, then it uses an internally generated texture.

For the multiple textures, I have given the textures different names, and put them in a particle directory. So my code to replace that above is like this:
    // c14 multiple particle textures
	r_particletexture[PT_DEFAULT] = Draw_FindPic("particle/default");
	r_particletexture[PT_BEAM] = Draw_FindPic("particle/beam");
	r_particletexture[PT_SPIRAL] = Draw_FindPic("particle/spiral");
	r_particletexture[PT_BUBBLE] = Draw_FindPic("particle/bubble");

	for (x = 0; x < PT_MAX; x++) {
		if (!r_particletexture[x]) {
			r_particletexture[x] = GL_LoadPic ("***particle***", (byte *)data, 16, 16, it_sprite, 32);
		}
	}

In the function GL_FreeUnusedImages in ref_gl\gl_image.c, there is a section of code like this:
	r_particletexture->registration_sequence = registration_sequence;
This marks the particle texture as used, preventing it from being unloaded between games. All we need to do here, is loop through our texture array, marking them all.
	// c14 multiple particle textures
	for (i = 0; i < PT_MAX; i++) {
		r_particletexture[i]->registration_sequence = registration_sequence;
	}
Using the textures
This is the last part, in our GL_DrawParticles function, in the file ref_gl\gl_rmain.c, The following line should crop up once in each of your particle rendering loops.
    GL_Bind(r_particletexture->texnum);
Each time this line crops up, change it like so, and specify the type of particle that you want to use inside the square brackets.
GL_Bind(r_particletexture[PT_SPIRAL]->texnum);
That's all there is to it. Don't forget to put some textures in the particle directory under your pics directory.

In the next part of this tutorial, we will see how to use our beam particle for lasers

Go to part 4
Bubbles