1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
module br.particleImpl;
private import opengl;
private import std.math;
private import util.particle;
private import br.gamemanager;
private import util.parts;
private import util.vector;
private import util.shape;
private import util.matrix;
private import util.log;
public class ParticleImpl:Particle{
public override void draw(){
// glDisable(GL_BLEND);
//glLoadIdentity();
// double size = 10.0;
// Matrix rpose = poseBase;
// Matrix drawpose = rpose
//Matrix p = matRotateX(rposeX) * matRotateY(rposeY) * matRotateZ(rposeZ) * matScale(size * scaleX ,size * scaleY ,size * scaleZ) * poseBase;
glTranslatef(pos.x ,pos.y ,pos.z);
glScaled(size ,size ,size);
glRotated(pose ,poseX ,poseY ,poseZ);
if(shape !is null && (1e-6) < size){
// shape.transformMatrix(rpose);
Vector3[] av = shape.v;
glEnable(GL_BLEND);
// glEnable(GL_POLYGON_SMOOTH);
Vector3 ave;
if(drawing & Parts.POLYGON){
polygon[] pr = shape.polygons;
for(int i = 0; i < pr.length ;i ){
Vector3 normal = shape.polygons[i].normal;
glBegin(GL_POLYGON);
ave = new Vector3((av[pr[i].v[0]] av[pr[i].v[1]] av[pr[i].v[2]]) / 3.0);
double a = fmax(0.0,(1.0 (pos.z ave.z) / 1600.0) * alpha);
// glColor3d((R - bg.R)*a bg.R ,(G - bg.G)*a bg.G ,(B - bg.B)*a bg.B );
glColor4d(R ,G ,B ,a);
//glColor4d(R ,G ,B ,fmax(0.0,(1.0 (pos.z ave.z) / 1600.0) * alpha)); //glColor4f(1.0f ,1.0f ,1.0f,(1.0 (pos.z ave.z) / 1600.0) * 0.6);
for(int j = 0;j < 3 ;j ){
glVertex3f(av[pr[i].v[j]].x,av[pr[i].v[j]].y,av[pr[i].v[j]].z);
}
glEnd();
}
}
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
if(drawing & Parts.WIRE){
int[][] wires = shape.wires;
// double a = fmin(1.0 ,fmax(0.0 ,alpha));
for(int i = 0; i < wires.length ;i ){
// glColor3d(R * a ,G * a ,B * a);
// glColor4d(R ,G ,B ,fmax(0.0,alpha));
glBegin(GL_LINE_STRIP);
glVertex3f(0 ,0,0);
glVertex3f(0 ,0,0);
glColor4d(wR ,wG ,wB ,fmax(0.0,alpha));
for(int j = 0; j < wires[i].length;j ){
// double a = alpha * fmax(0.0,(1.0 (pos.z av[wires[i][j]].z 600.0) / 1200.0));
// glColor4d(R ,G ,B ,fmax(0.0,a));
glVertex3f(av[wires[i][j]].x ,av[wires[i][j]].y ,av[wires[i][j]].z);
//glVertex3f(av[wires[i][j 1]].x ,av[wires[i][j 1]].y ,av[wires[i][j 1]].z);
}
glEnd();
}
}
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
// shape.resetVertex();
}
// drawn = true;
}
}
public void makeParticle(Shape shape, double size, ubyte drawing, Vector3 pos ,Vector3 vel ,Matrix rpose ,double R ,double G ,double B ,double wR,double wG,double wB ,double alpha ,float speed = 5.0f){
if(shape !is null){
foreach(r;shape.polygons){
Vector3[] v;
int[][] wires;
polygon[] polygons;
v.length = r.v.length;
for(int i=0;i<v.length;i ){v[i]=shape.v[r.v[i]];}
wires.length = 1;
wires[0].length = r.v.length 1;
for(int i=0;i<wires[0].length;i ){
if(i == wires[0].length-1)wires[0][i]=0;
else wires[0][i]=i;
}
polygons.length = 1;
for(int i=0;i<polygons[0].v.length;i ){polygons[0].v[i]=i;}
Vector3 average = new Vector3();
foreach(Vector3 vec;v){
average = vec;
}
average = average / cast(double)v.length;
foreach(ref Vector3 vec;v){
vec = vec - average;
}
Vector3 rpos = pos vec3translate(average , rpose);
Shape s = new Shape(v ,wires ,polygons);
Vector3 velocity = vec3Normalize(average) * speed vel;
// new Particle_old(s ,1.0 ,drawing ,rpos ,velocity ,rpose ,R ,G ,B ,alpha);
ParticleImpl p = particleManager.getInstance;
if(p !is null)p.set(s ,size ,drawing ,rpos ,velocity ,rpose ,R ,G ,B ,wR,wG,wB,alpha);
//new Particle(s ,size ,drawing ,rpos ,velocity ,poseBase ,poseX ,poseY ,poseZ ,R ,G ,B ,alpha);
}
}
}
private Shape simpleParticleShape;
public void makeSimpleParticle(int num ,double size, ubyte drawing, Vector3 pos ,double R ,double G ,double B ,double wR,double wG,double wB,double alpha){
if(simpleParticleShape is null){
Vector3[] v;
int[][] wires;
polygon[] polygons;
v.length = 3;
// v[0] = new Vector3(0.7 ,0.0 ,0.0);
v[0] = new Vector3(0.7 ,-0.0 ,0.0);
v[1] = new Vector3(-0.7 ,-0.5 ,0.0);
v[2] = new Vector3(-0.7 ,0.5 ,0.0);
wires.length = 1;
wires[0].length = 4;
wires[0][0] = 0;wires[0][1] = 1;wires[0][2] = 2;wires[0][3] = 0;//wires[0][4] =0;
polygons.length = 1;
polygons[0].v[0]=0;polygons[0].v[1]=1;polygons[0].v[2]=2;//rects[0].v[3]=3;
simpleParticleShape = new Shape(v ,wires ,polygons);
}
Shape s = cast(Shape)simpleParticleShape.clone();
for(int i=0;i<num;i ){
ParticleImpl p = particleManager.getInstance;
if(p !is null){
Vector3 velocity = new Vector3(4.0 - rand.nextFloat(8.0) ,4.0 - rand.nextFloat(8.0) ,4.0 - rand.nextFloat(8.0) );
double theta ,phi;
theta = rand.nextFloat(360);
phi = rand.nextFloat(180);
p.set(s ,size ,drawing ,pos ,velocity ,matRotateX(theta)*matRotateY(phi) ,R ,G ,B ,wR,wG,wB,alpha);
}
}
}
public void makeParticleByShape(Shape shape, int num ,double size, ubyte drawing, Vector3 pos ,Vector3 vel ,double R ,double G ,double B ,double wR,double wG,double wB,double alpha ,double speed = 4.0){
Shape s = cast(Shape)shape.clone();
for(int i=0;i<num;i ){
ParticleImpl p = particleManager.getInstance;
if(p !is null){
Vector3 velocity = new Vector3(speed - rand.nextFloat(speed * 2.0) ,speed - rand.nextFloat(speed*2.0) ,speed - rand.nextFloat(speed*2.0) );
double theta ,phi;
theta = rand.nextFloat(360);
phi = rand.nextFloat(180);
p.set(s ,size ,drawing ,pos ,velocity ,matRotateX(theta)*matRotateY(phi) ,R ,G ,B ,wR,wG,wB,alpha);
}
}
}
public class ParticleImplManager{
public:
ParticleImpl[] particles;
//List!(Parts) parts;
protected:
//List!(int) van = new List!(int)();
int particleIdx = 0;
const int maxParticle;
public this() {
maxParticle = 16;
particles.length = maxParticle;
foreach(ref ParticleImpl p;particles){
p = new ParticleImpl();
}
//parts = new List!(Parts)();
particleIdx = 0;
}
public this(int n) {
maxParticle = n;
particles.length = maxParticle;
foreach(ref ParticleImpl p;particles){
p = new ParticleImpl();
}
//parts = new List!(Parts)();
particleIdx = 0;
}
public ParticleImpl getInstance(){
for(int i = 0;i < particles.length;i ){
particleIdx ;
if(particles.length <= particleIdx)particleIdx = 0;
if(!particles[particleIdx].exists()){
return particles[particleIdx];
}
}
return null;
//if(maxParts <= parts.size)return false;
//parts.push_back(p);
}
public void run() {
for (int i = 0;i < particles.length;i ){
ParticleImpl an = particles[i];
if (an.exists){
an.move();
}
}
}
public void draw() {
for (int i = 0;i < particles.length;i ){
ParticleImpl an = particles[i];
if (an.exists){
glPushMatrix();
an.draw();
glPopMatrix();
}
}
}
public void clear() {
foreach(ref ParticleImpl a;particles){
if(a !is null && a.exists){
a.vanish();
}
}
//parts.length = 0;
particleIdx = 0;
}
}
|