Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Fading from red to white


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 griffenjam   Members   -  Reputation: 193

Like
Likes
Like

Posted 25 October 2001 - 04:23 AM

I just recently wrote a particle engine but am having a problem with the colors of the particles. I would like a particle to start red, then fade to white. The problem is I don''t know how to do this smoothly. Any help or ideas? Jason Mickela ICQ : 873518 E-Mail: jmickela@sbcglobal.net ------------------------------ "Evil attacks from all sides but the greatest evil attacks from within." Me ------------------------------

Sponsor:

#2 Mayrel   Members   -  Reputation: 348

Like
Likes
Like

Posted 25 October 2001 - 04:37 AM

For each particle, set:

glColor3f(1.0f, particle.age, particle.age)

Where particle.age is a value between 0.0 (newborn) and 1.0 (dead). If age is to be a integer, you can use

glColor3f(1.0f, (float)particle.age / MAX_PARTICLE_AGEF, (float)particle.age / MAX_PARTICLE_AGEF)

If your particle is textured, you should ensure it's grayscale, or the colours might not be quite right.


Uuuuuulrika-ka-ka-ka-ka-ka

Edited by - Mayrel on October 25, 2001 11:38:58 AM

#3 Mayrel   Members   -  Reputation: 348

Like
Likes
Like

Posted 25 October 2001 - 05:07 AM

Here's some code to support arbitary RGBA gradients.

    
typedef struct ps_colorband_s ps_colorband_t;
struct ps_colorband_s {
GLfloat start;
GLfloat end;
GLfloat c1[4];
GLfloat c2[4];
ps_colorband_t *next;
};

ps_colorband_t *_ps_cb = NULL;

GLvoid psAddColorBand4 (GLfloat start, GLfloat end,
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2)
{
ps_colorband_t *cb = malloc(sizeof(ps_colorband_t));
cb->start = start;
cb->end = end;
cb->c1[0] = r1; cb->c1[1] = g1; cb->c1[2] = b1; cb->c1[3] = a1;
cb->c2[0] = r2; cb->c2[1] = g2; cb->c2[2] = b2; cb->c2[3] = a2;
cb->next = _ps_cb;
_ps_cb = cb;
}

GLboolean psGetColorBand4 (GLfloat where, GLfloat *c)
{
ps_colorband_t *cb = _ps_cb;
GLfloat n;
int i;

/* For each colorband */
while (cb) {
/* If this point is between start and end, it's in the
* colorband. */
if ((cb->start <= where) && (cb->end >= where)) {
/* Normalises n between start and end: 0.0 is start,
* 1.0 is end. */
n = 1.0f / (cb->end - cb->start) * (where - cb->start);
for (i = 0; i < 4; ++i)
/* Compute the correct colour. */

c[i] = (cb->c2[i] * n) + (cb->c1[i] * (1.0f - n));
return TRUE;
}
cb = cb->next;
}

return FALSE;
}


I've tested something almost identical to this, but I don't have the code with me, so this version may have typos or bugs.

You use the code by first setting up the gradients:

  
psSetColorBand4(0.0,0.1, 1.0,1.0,1.0,1.0, 0.0,0.0,1.0,1.0);
psSetColorBand4(0.2,0.4, 0.0,0.0,1.0,1.0, 1.0,1.0,0.0,0.7);
psSetColorBand4(0.4,0.7, 1.0,1.0,0.0,0.7, 1.0,0.0,0.0,0.5);
psSetColorBand4(0.7,1.0, 1.0,0.0,0.0,0.5, 0.0,0.0,0.0,0.0);


This example produces particles that fade from white, to blue, to yellow to red to black. It's a fire gradient.

To set the color of each particle, use the following code:

  
Glfloat c[4];
...
for each particle
...
psGetColorBand4((float)particle.age / MAX_AGE, c);
glColor4fv©;
...


Uuuuuulrika-ka-ka-ka-ka-ka

Edited by - Mayrel on October 25, 2001 12:10:15 PM

The syntax highlighter is delightfully naive.

Edited by - Mayrel on October 25, 2001 12:12:58 PM




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS