Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 35 | chris | 1 | unit app_particle_part; |
| 2 | |||
| 3 | interface |
||
| 4 | |||
| 5 | uses sux_constant, sux_object, app_constant, resource_texture, |
||
| 6 | resource_list, app_particle_vertexbuffer; |
||
| 7 | |||
| 8 | type |
||
| 9 | TSAnimation = record |
||
| 10 | addpos: SXVertex2D; |
||
| 11 | addrot: SXFloat; |
||
| 12 | addsize: SXFloat; |
||
| 13 | addcolor: SXColorRGB; |
||
| 14 | adddensity: SXFloat; |
||
| 15 | movealongrot: SXVertex2D; |
||
| 16 | end; |
||
| 17 | TSettings = record |
||
| 18 | typus: SXState; |
||
| 19 | pos: SXVertex2D; |
||
| 20 | rot: SXFloat; |
||
| 21 | size: SXFloat; |
||
| 22 | color: SXColorRGB; |
||
| 23 | density: SXFloat; |
||
| 24 | animation: TSAnimation; |
||
| 25 | lifetime: SXFloat; |
||
| 26 | end; |
||
| 27 | SAPPart=class(SUXObject) |
||
| 28 | settings: TSettings; |
||
| 29 | |||
| 30 | procedure onRender(const vertexbuffer: SAPVertexBuffer; const particleindex: SXInt); overload; virtual; |
||
| 31 | procedure onRender; overload; virtual; |
||
| 32 | |||
| 33 | procedure setTypus(const typus: SXState); |
||
| 34 | procedure setPosition(const pos: SXVertex2D); |
||
| 35 | procedure setRotation(const rot: SXFloat); |
||
| 36 | procedure setSize(const size: SXFloat); |
||
| 37 | procedure setColor(const color: SXColorRGB); |
||
| 38 | procedure setDensity(const density: SXFloat); |
||
| 39 | procedure setAnimation(const addpos: SXVertex2D; const addrot, addsize: SXFloat; const addcolor: SXColorRGB; const adddensity: SXFloat); |
||
| 40 | procedure setAnimationMoveAlongRot(const movealongrot: SXVertex2D); |
||
| 41 | function getTypus: SXState; |
||
| 42 | function getPosition: SXVertex2D; |
||
| 43 | function getRotation: SXFloat; |
||
| 44 | function getSize: SXFloat; |
||
| 45 | function getColor: SXColorRGB; |
||
| 46 | function getDensity: SXFloat; |
||
| 47 | |||
| 48 | function getVectorFromRotation: SXVertex2D; |
||
| 49 | procedure processAnimation; |
||
| 50 | procedure deleteIfInvisible; |
||
| 51 | procedure onTimer; virtual; |
||
| 52 | |||
| 53 | procedure delete; |
||
| 54 | constructor Create(const parent: TObject); |
||
| 55 | destructor Destroy; override; |
||
| 56 | end; |
||
| 57 | |||
| 58 | implementation |
||
| 59 | |||
| 60 | uses main, gl_main, OpenGL, app_particle_main, Math; |
||
| 61 | |||
| 62 | |||
| 63 | // --- SAPPart |
||
| 64 | |||
| 65 | |||
| 66 | procedure SAPPart.onRender(const vertexbuffer: SAPVertexBuffer; const particleindex: SXInt); |
||
| 67 | var vertexindex: SXInt; |
||
| 68 | upvector: SXVertex2D; |
||
| 69 | pos3D, upvectorandsize: SXVertex3D; |
||
| 70 | color: SXColorRGBA; |
||
| 71 | angle: SXFloat; |
||
| 72 | begin |
||
| 73 | vertexindex := particleindex * 4; |
||
| 74 | |||
| 75 | angle := (settings.rot); |
||
| 76 | angle := degtorad(angle); |
||
| 77 | upvector := sx.convert.Vertex2DOf(sin(angle), cos(angle)); |
||
| 78 | |||
| 79 | pos3D := sx.convert.Vertex3DOf(settings.pos.x, settings.pos.y, 0); |
||
| 80 | upvectorandsize := sx.convert.Vertex3DOf(upvector.x, upvector.y, settings.size); |
||
| 81 | color := sx.convert.ColorRGBAOf(settings.color.r, settings.color.g, settings.color.b, settings.density); |
||
| 82 | |||
| 83 | vertexbuffer.setVertex(vertexindex, pos3D, color, |
||
| 84 | sx.convert.Vertex2DOf(0, 0), |
||
| 85 | sx.convert.Vertex2DOf(-1, -1), upvectorandsize); |
||
| 86 | |||
| 87 | vertexbuffer.setVertex(vertexindex + 1, pos3D, color, |
||
| 88 | sx.convert.Vertex2DOf(1, 0), |
||
| 89 | sx.convert.Vertex2DOf( 1, -1), upvectorandsize); |
||
| 90 | |||
| 91 | vertexbuffer.setVertex(vertexindex + 2, pos3D, color, |
||
| 92 | sx.convert.Vertex2DOf(1, 1), |
||
| 93 | sx.convert.Vertex2DOf( 1, 1), upvectorandsize); |
||
| 94 | |||
| 95 | vertexbuffer.setVertex(vertexindex + 3, pos3D, color, |
||
| 96 | sx.convert.Vertex2DOf(0, 1), |
||
| 97 | sx.convert.Vertex2DOf(-1, 1), upvectorandsize); |
||
| 98 | end; |
||
| 99 | |||
| 100 | |||
| 101 | procedure SAPPart.onRender; |
||
| 102 | begin |
||
| 103 | // Default rendering method |
||
| 104 | glPushMatrix; |
||
| 105 | |||
| 106 | glTranslatef(settings.pos.x,settings.pos.y,0); |
||
| 107 | glRotatef(360 - settings.rot,0,0,1); |
||
| 108 | glScalef(settings.size,settings.size,1); |
||
| 109 | |||
| 110 | glColor4f(settings.color.r, settings.color.g, settings.color.b, settings.density); |
||
| 111 | glBegin(GL_QUADS); |
||
| 112 | glTexCoord2f(0,0); glVertex2f(-1,-1); |
||
| 113 | glTexCoord2f(1,0); glVertex2f( 1,-1); |
||
| 114 | glTexCoord2f(1,1); glVertex2f( 1, 1); |
||
| 115 | glTexCoord2f(0,1); glVertex2f(-1, 1); |
||
| 116 | glEnd(); |
||
| 117 | |||
| 118 | glPopMatrix; |
||
| 119 | end; |
||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | procedure SAPPart.setTypus(const typus: SXState); |
||
| 125 | begin |
||
| 126 | settings.typus := typus; |
||
| 127 | end; |
||
| 128 | |||
| 129 | |||
| 130 | procedure SAPPart.setPosition(const pos: SXVertex2D); |
||
| 131 | begin |
||
| 132 | settings.pos := pos; |
||
| 133 | end; |
||
| 134 | |||
| 135 | |||
| 136 | procedure SAPPart.setRotation(const rot: SXFloat); |
||
| 137 | begin |
||
| 138 | settings.rot := rot; |
||
| 139 | end; |
||
| 140 | |||
| 141 | |||
| 142 | procedure SAPPart.setSize(const size: SXFloat); |
||
| 143 | begin |
||
| 144 | settings.size := size; |
||
| 145 | end; |
||
| 146 | |||
| 147 | |||
| 148 | procedure SAPPart.setColor(const color: SXColorRGB); |
||
| 149 | begin |
||
| 150 | settings.color := color; |
||
| 151 | end; |
||
| 152 | |||
| 153 | |||
| 154 | procedure SAPPart.setDensity(const density: SXFloat); |
||
| 155 | begin |
||
| 156 | settings.density := density; |
||
| 157 | end; |
||
| 158 | |||
| 159 | |||
| 160 | procedure SAPPart.setAnimation(const addpos: SXVertex2D; const addrot, addsize: SXFloat; const addcolor: SXColorRGB; const adddensity: SXFloat); |
||
| 161 | begin |
||
| 162 | settings.animation.addpos := addpos; |
||
| 163 | settings.animation.addrot := addrot; |
||
| 164 | settings.animation.addsize := addsize; |
||
| 165 | settings.animation.addcolor := addcolor; |
||
| 166 | settings.animation.adddensity := adddensity; |
||
| 167 | end; |
||
| 168 | |||
| 169 | |||
| 170 | procedure SAPPart.setAnimationMoveAlongRot(const movealongrot: SXVertex2D); |
||
| 171 | begin |
||
| 172 | settings.animation.movealongrot := movealongrot; |
||
| 173 | end; |
||
| 174 | |||
| 175 | |||
| 176 | function SAPPart.getTypus: SXState; |
||
| 177 | begin |
||
| 178 | result := settings.typus; |
||
| 179 | end; |
||
| 180 | |||
| 181 | |||
| 182 | function SAPPart.getPosition: SXVertex2D; |
||
| 183 | begin |
||
| 184 | result := settings.pos; |
||
| 185 | end; |
||
| 186 | |||
| 187 | |||
| 188 | function SAPPart.getRotation: SXFloat; |
||
| 189 | begin |
||
| 190 | result := settings.rot; |
||
| 191 | end; |
||
| 192 | |||
| 193 | |||
| 194 | function SAPPart.getSize: SXFloat; |
||
| 195 | begin |
||
| 196 | result := settings.size; |
||
| 197 | end; |
||
| 198 | |||
| 199 | |||
| 200 | function SAPPart.getColor: SXColorRGB; |
||
| 201 | begin |
||
| 202 | result := settings.color; |
||
| 203 | end; |
||
| 204 | |||
| 205 | |||
| 206 | function SAPPart.getDensity: SXFloat; |
||
| 207 | begin |
||
| 208 | result := settings.density; |
||
| 209 | end; |
||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | function SAPPart.getVectorFromRotation: SXVertex2D; |
||
| 215 | var vector: SXVertex2D; |
||
| 216 | begin |
||
| 217 | vector.x := sx.math._sin(settings.rot); |
||
| 218 | vector.y := sx.math._cos(settings.rot); |
||
| 219 | result := sx.math.normalizeVector(vector); |
||
| 220 | end; |
||
| 221 | |||
| 222 | |||
| 223 | procedure SAPPart.processAnimation; |
||
| 224 | var timefactor: SXFloat; |
||
| 225 | vector, normal: SXVertex2D; |
||
| 226 | begin |
||
| 227 | timefactor := sx.timer.getTimer; |
||
| 228 | with settings do |
||
| 229 | begin |
||
| 230 | pos := sx.math.addVertices(pos, sx.math.multiplyVertex(animation.addpos, timefactor)); |
||
| 231 | rot := rot + animation.addrot * timefactor; |
||
| 232 | size := size + animation.addsize * timefactor; |
||
| 233 | color := sx.math.color.addColors(color, sx.math.color.multiplyColor(animation.addcolor, timefactor)); |
||
| 234 | density := density + animation.adddensity * timefactor; |
||
| 235 | |||
| 236 | // Get movement vector |
||
| 237 | if (animation.movealongrot.x <> 0) or (animation.movealongrot.y <> 0) then |
||
| 238 | begin |
||
| 239 | vector := getVectorFromRotation; |
||
| 240 | normal.x := -vector.y; |
||
| 241 | normal.y := vector.x; |
||
| 242 | |||
| 243 | vector := sx.math.multiplyVertex(vector, animation.movealongrot.y); |
||
| 244 | vector := sx.math.addVertices(vector, sx.math.multiplyVertex(normal, animation.movealongrot.x)); |
||
| 245 | pos := sx.math.addVertices(pos, sx.math.multiplyVertex(vector, timefactor)); |
||
| 246 | end; |
||
| 247 | end; |
||
| 248 | end; |
||
| 249 | |||
| 250 | |||
| 251 | procedure SAPPart.deleteIfInvisible; |
||
| 252 | begin |
||
| 253 | if (settings.size <= 0) or (settings.density <= 0) then |
||
| 254 | begin |
||
| 255 | delete; |
||
| 256 | end; |
||
| 257 | end; |
||
| 258 | |||
| 259 | |||
| 260 | procedure SAPPart.onTimer; |
||
| 261 | begin |
||
| 262 | sx.timer.incTimer(settings.lifetime); |
||
| 263 | processAnimation; |
||
| 264 | deleteIfInvisible; |
||
| 265 | end; |
||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | procedure SAPPart.delete; |
||
| 271 | begin |
||
| 272 | SAParticle(getParent).deleteParticle(self); |
||
| 273 | end; |
||
| 274 | |||
| 275 | |||
| 276 | constructor SAPPart.Create(const parent: TObject); |
||
| 277 | begin |
||
| 278 | inherited Create(parent, 'Particle Part'); |
||
| 279 | |||
| 280 | settings.typus := 0; |
||
| 281 | settings.pos := sx.convert.Vertex2DOf(0, 0); |
||
| 282 | settings.rot := 0; |
||
| 283 | settings.size := 1; |
||
| 284 | settings.color := sx.convert.ColorRGBOf(1, 1, 1); |
||
| 285 | settings.density := 1; |
||
| 286 | settings.animation.addpos := sx.convert.Vertex2DOf(0, 0); |
||
| 287 | settings.animation.addrot := 0; |
||
| 288 | settings.animation.addsize := 0; |
||
| 289 | settings.animation.addcolor := sx.convert.ColorRGBOf(0, 0, 0); |
||
| 290 | settings.animation.adddensity := 0; |
||
| 291 | settings.animation.movealongrot := sx.convert.Vertex2DOf(0, 0); |
||
| 292 | settings.lifetime := 0; |
||
| 293 | end; |
||
| 294 | |||
| 295 | |||
| 296 | destructor SAPPart.Destroy; |
||
| 297 | begin |
||
| 298 | inherited; |
||
| 299 | end; |
||
| 300 | |||
| 301 | |||
| 302 | end. |