1 #define MAXLIGHTNINGSTEPS 64
2 #define LIGHTNINGSTEP 8
3 int lnjitterx
[MAXLIGHTNINGSTEPS
], lnjittery
[MAXLIGHTNINGSTEPS
];
6 VAR(lnjittermillis
, 0, 100, 1000);
7 VAR(lnjitterradius
, 0, 2, 100);
9 static void setuplightning()
11 if(lastmillis
-lastlnjitter
> lnjittermillis
)
13 lastlnjitter
= lastmillis
- (lastmillis
%lnjittermillis
);
14 loopi(MAXLIGHTNINGSTEPS
)
16 lnjitterx
[i
] = -lnjitterradius
+ rnd(2*lnjitterradius
+ 1);
17 lnjittery
[i
] = -lnjitterradius
+ rnd(2*lnjitterradius
+ 1);
22 static void renderlightning(const vec
&o
, const vec
&d
, float sz
, float tx
, float ty
, float tsz
)
26 float len
= step
.magnitude();
27 int numsteps
= clamp(int(ceil(len
/LIGHTNINGSTEP
)), 2, MAXLIGHTNINGSTEPS
);
29 int jitteroffset
= detrnd(int(o
.x
+o
.y
+o
.z
), MAXLIGHTNINGSTEPS
);
30 vec
cur(o
), up
, right
;
33 right
.cross(up
, step
);
35 glBegin(GL_QUAD_STRIP
);
40 if(j
+1==numsteps
) next
= d
;
43 next
.add(vec(right
).mul(sz
*lnjitterx
[(j
+jitteroffset
)%MAXLIGHTNINGSTEPS
]));
44 next
.add(vec(up
).mul(sz
*lnjittery
[(j
+jitteroffset
)%MAXLIGHTNINGSTEPS
]));
46 vec dir1
= next
, dir2
= next
, across
;
49 across
.cross(dir2
, dir1
).normalize().mul(sz
);
50 float tx1
= j
&1 ? tx
: tx
+tsz
, tx2
= j
&1 ? tx
+tsz
: tx
;
51 glTexCoord2f(tx2
, ty
+tsz
); glVertex3f(cur
.x
-across
.x
, cur
.y
-across
.y
, cur
.z
-across
.z
);
52 glTexCoord2f(tx2
, ty
); glVertex3f(cur
.x
+across
.x
, cur
.y
+across
.y
, cur
.z
+across
.z
);
55 glTexCoord2f(tx1
, ty
+tsz
); glVertex3f(next
.x
-across
.x
, next
.y
-across
.y
, next
.z
-across
.z
);
56 glTexCoord2f(tx1
, ty
); glVertex3f(next
.x
+across
.x
, next
.y
+across
.y
, next
.z
+across
.z
);
63 struct lightningrenderer
: listrenderer
66 : listrenderer("packages/particles/lightning.jpg", PT_LIGHTNING
|PT_TRACK
|PT_GLARE
, 0, 0)
71 glDisable(GL_CULL_FACE
);
76 glEnable(GL_CULL_FACE
);
84 void renderpart(listparticle
*p
, const vec
&o
, const vec
&d
, int blend
, int ts
, uchar
*color
)
86 blend
= min(blend
<<2, 255);
87 if(type
&PT_MOD
) //multiply alpha into color
88 glColor3ub((color
[0]*blend
)>>8, (color
[1]*blend
)>>8, (color
[2]*blend
)>>8);
90 glColor4ub(color
[0], color
[1], color
[2], blend
);
91 float tx
= 0, ty
= 0, tsz
= 1;
94 int i
= detrnd((size_t)p
, 4);
99 renderlightning(o
, d
, p
->size
, tx
, ty
, tsz
);
102 static lightningrenderer lightnings
;