~cairo-dock-team/ubuntu/oneiric/cairo-dock/2.3.0-3

« back to all changes in this revision

Viewing changes to src/cairo-dock-particle-system.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
* This file is a part of the Cairo-Dock project
3
 
*
4
 
* Copyright : (C) see the 'copyright' file.
5
 
* E-mail    : see the 'copyright' file.
6
 
*
7
 
* This program is free software; you can redistribute it and/or
8
 
* modify it under the terms of the GNU General Public License
9
 
* as published by the Free Software Foundation; either version 3
10
 
* of the License, or (at your option) any later version.
11
 
*
12
 
* This program is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
* GNU General Public License for more details.
16
 
* You should have received a copy of the GNU General Public License
17
 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
*/
19
 
 
20
 
#include <math.h>
21
 
#include <string.h>
22
 
#include <stdlib.h>
23
 
#include <cairo.h>
24
 
 
25
 
#include "cairo-dock-draw-opengl.h"
26
 
#include "cairo-dock-particle-system.h"
27
 
 
28
 
static GLfloat s_pCornerCoords[8] = {0.0, 0.0,
29
 
        0.0, 1.0,
30
 
        1.0, 1.0,
31
 
        1.0, 0.0};
32
 
 
33
 
void cairo_dock_render_particles_full (CairoParticleSystem *pParticleSystem, int iDepth)
34
 
{
35
 
        _cairo_dock_enable_texture ();
36
 
        
37
 
        if (pParticleSystem->bAddLuminance)
38
 
                _cairo_dock_set_blend_over ();
39
 
                //glBlendFunc (GL_SRC_ALPHA, GL_ONE);
40
 
        else
41
 
                _cairo_dock_set_blend_alpha ();
42
 
        
43
 
        glBindTexture(GL_TEXTURE_2D, pParticleSystem->iTexture);
44
 
        
45
 
        GLfloat *vertices = pParticleSystem->pVertices;
46
 
        GLfloat *coords = pParticleSystem->pCoords;
47
 
        GLfloat *colors = pParticleSystem->pColors;
48
 
        GLfloat *vertices2 = &pParticleSystem->pVertices[pParticleSystem->iNbParticles * 4 * 3];
49
 
        GLfloat *coords2 = &pParticleSystem->pCoords[pParticleSystem->iNbParticles * 4 * 2];
50
 
        GLfloat *colors2 = &pParticleSystem->pColors[pParticleSystem->iNbParticles * 4 * 4];
51
 
        
52
 
        GLfloat x,y,z;
53
 
        GLfloat w, h;
54
 
        GLfloat fHeight = pParticleSystem->fHeight;
55
 
        
56
 
        int numActive = 0;
57
 
        CairoParticle *p;
58
 
        int i;
59
 
        for (i = 0; i < pParticleSystem->iNbParticles; i ++)
60
 
        {
61
 
                p = &pParticleSystem->pParticles[i];
62
 
                if (p->iLife == 0 || iDepth * p->z < 0)
63
 
                        continue;
64
 
                
65
 
                numActive += 4;
66
 
                w = p->fWidth * p->fSizeFactor;
67
 
                h = p->fHeight * p->fSizeFactor;
68
 
                x = p->x * pParticleSystem->fWidth / 2;
69
 
                y = p->y * pParticleSystem->fHeight;
70
 
                z = p->z;
71
 
                
72
 
                vertices[0] = x - w;
73
 
                vertices[2] = z;
74
 
                vertices[3] = x - w;
75
 
                vertices[5] = z;
76
 
                vertices[6] = x + w;
77
 
                vertices[8] = z;
78
 
                vertices[9] = x + w;
79
 
                vertices[11] = z;
80
 
                if (pParticleSystem->bDirectionUp)
81
 
                {
82
 
                        vertices[1] = y + h;
83
 
                        vertices[4] = y - h;
84
 
                        vertices[7] = y - h;
85
 
                        vertices[10] = y + h;
86
 
                }
87
 
                else
88
 
                {
89
 
                        vertices[1] = fHeight - y + h;
90
 
                        vertices[4] = fHeight - y - h;
91
 
                        vertices[7] = fHeight - y - h;
92
 
                        vertices[10] = fHeight - y + h;
93
 
                }
94
 
                vertices += 12;
95
 
                
96
 
                memcpy (coords, s_pCornerCoords, sizeof (s_pCornerCoords));
97
 
                coords += 8;
98
 
 
99
 
                colors[0] = p->color[0];
100
 
                colors[1] = p->color[1];
101
 
                colors[2] = p->color[2];
102
 
                colors[3] = p->color[3];
103
 
                memcpy (colors + 4, colors, 4*sizeof (GLfloat));
104
 
                memcpy (colors + 8, colors, 8*sizeof (GLfloat));
105
 
                colors += 16;
106
 
                
107
 
                if (pParticleSystem->bAddLight)
108
 
                {
109
 
                        w/=1.6;
110
 
                        h/=1.6;
111
 
                        vertices2[0] = x - w;
112
 
                        vertices2[2] = z;
113
 
                        vertices2[3] = x - w;
114
 
                        vertices2[5] = z;
115
 
                        vertices2[6] = x + w;
116
 
                        vertices2[8] = z;
117
 
                        vertices2[9] = x + w;
118
 
                        vertices2[11] = z;
119
 
                        if (pParticleSystem->bDirectionUp)
120
 
                        {
121
 
                                vertices2[1] = y + h;
122
 
                                vertices2[4] = y - h;
123
 
                                vertices2[7] = y - h;
124
 
                                vertices2[10] = y + h;
125
 
                        }
126
 
                        else
127
 
                        {
128
 
                                vertices2[1] = fHeight - y + h;
129
 
                                vertices2[4] = fHeight - y - h;
130
 
                                vertices2[7] = fHeight - y - h;
131
 
                                vertices2[10] = fHeight - y + h;
132
 
                        }
133
 
                        vertices2 += 12;
134
 
                        
135
 
                        memcpy (coords2, s_pCornerCoords, sizeof (s_pCornerCoords));
136
 
                        coords2 += 8;
137
 
 
138
 
                        colors2[0] = 1;
139
 
                        colors2[1] = 1;
140
 
                        colors2[2] = 1;
141
 
                        colors2[3] = colors[3];
142
 
                        memcpy (colors2 + 4, colors2, 4*sizeof (GLfloat));
143
 
                        memcpy (colors2 + 8, colors2, 8*sizeof (GLfloat));
144
 
                        colors2 += 16;
145
 
                }
146
 
        }
147
 
        
148
 
        glEnableClientState(GL_COLOR_ARRAY);
149
 
        glEnableClientState (GL_TEXTURE_COORD_ARRAY);
150
 
        glEnableClientState (GL_VERTEX_ARRAY);
151
 
        
152
 
        glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), pParticleSystem->pCoords);
153
 
        glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), pParticleSystem->pVertices);
154
 
        glColorPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), pParticleSystem->pColors);
155
 
 
156
 
        glDrawArrays(GL_QUADS, 0, pParticleSystem->bAddLight ? numActive*2 : numActive);
157
 
 
158
 
        glDisableClientState(GL_COLOR_ARRAY);
159
 
        glDisableClientState (GL_TEXTURE_COORD_ARRAY);
160
 
        glDisableClientState (GL_VERTEX_ARRAY);
161
 
        
162
 
        _cairo_dock_disable_texture ();
163
 
}
164
 
 
165
 
CairoParticleSystem *cairo_dock_create_particle_system (int iNbParticles, GLuint iTexture, double fWidth, double fHeight)
166
 
{
167
 
        g_return_val_if_fail (iNbParticles > 0, NULL);
168
 
        CairoParticleSystem *pParticleSystem = g_new0 (CairoParticleSystem, 1);
169
 
        pParticleSystem->iNbParticles = iNbParticles;
170
 
        pParticleSystem->pParticles = g_new0 (CairoParticle, iNbParticles);
171
 
        
172
 
        pParticleSystem->iTexture = iTexture;
173
 
        
174
 
        pParticleSystem->fWidth = fWidth;
175
 
        pParticleSystem->fHeight = fHeight;
176
 
        pParticleSystem->bDirectionUp = TRUE;
177
 
        
178
 
        pParticleSystem->pVertices = malloc(iNbParticles * 4 * 3 * sizeof(GLfloat)*2);
179
 
        pParticleSystem->pCoords = malloc(iNbParticles * 4 * 2 * sizeof(GLfloat)*2);
180
 
        pParticleSystem->pColors = malloc(iNbParticles * 4 * 4 * sizeof(GLfloat)*2);
181
 
        return pParticleSystem;
182
 
}
183
 
 
184
 
 
185
 
void cairo_dock_free_particle_system (CairoParticleSystem *pParticleSystem)
186
 
{
187
 
        if (pParticleSystem == NULL)
188
 
                return ;
189
 
        
190
 
        g_free (pParticleSystem->pParticles);
191
 
        
192
 
        free (pParticleSystem->pVertices);
193
 
        free (pParticleSystem->pCoords);
194
 
        free (pParticleSystem->pColors);
195
 
        
196
 
        g_free (pParticleSystem);
197
 
}
198
 
 
199
 
 
200
 
gboolean cairo_dock_update_default_particle_system (CairoParticleSystem *pParticleSystem, CairoDockRewindParticleFunc pRewindParticle)
201
 
{
202
 
        gboolean bAllParticlesEnded = TRUE;
203
 
        CairoParticle *p;
204
 
        int i;
205
 
        for (i = 0; i < pParticleSystem->iNbParticles; i ++)
206
 
        {
207
 
                p = &(pParticleSystem->pParticles[i]);
208
 
                
209
 
                p->fOscillation += p->fOmega;
210
 
                p->x += p->vx + (p->z + 2)/3. * .02 * sin (p->fOscillation);  // 3%
211
 
                p->y += p->vy;
212
 
                p->color[3] = 1.*p->iLife / p->iInitialLife;
213
 
                p->fSizeFactor += p->fResizeSpeed;
214
 
                if (p->iLife > 0)
215
 
                {
216
 
                        p->iLife --;
217
 
                        if (pRewindParticle && p->iLife == 0)
218
 
                        {
219
 
                                pRewindParticle (p, pParticleSystem->dt);
220
 
                        }
221
 
                        if (bAllParticlesEnded && p->iLife != 0)
222
 
                                bAllParticlesEnded = FALSE;
223
 
                }
224
 
                else if (pRewindParticle)
225
 
                        pRewindParticle (p, pParticleSystem->dt);
226
 
        }
227
 
        return ! bAllParticlesEnded;
228
 
}