Initial Comit: First commit.
[SauerEngine.git] / src / engine / lightning.h
blob98b133ae50f24975dc9e89e81cfabd63b9bf7116
1 #define MAXLIGHTNINGSTEPS 64
2 #define LIGHTNINGSTEP 8
3 int lnjitterx[MAXLIGHTNINGSTEPS], lnjittery[MAXLIGHTNINGSTEPS];
4 int lastlnjitter = 0;
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)
24 vec step(d);
25 step.sub(o);
26 float len = step.magnitude();
27 int numsteps = clamp(int(ceil(len/LIGHTNINGSTEP)), 2, MAXLIGHTNINGSTEPS);
28 step.div(numsteps+1);
29 int jitteroffset = detrnd(int(o.x+o.y+o.z), MAXLIGHTNINGSTEPS);
30 vec cur(o), up, right;
31 up.orthogonal(step);
32 up.normalize();
33 right.cross(up, step);
34 right.normalize();
35 glBegin(GL_QUAD_STRIP);
36 loopj(numsteps)
38 vec next(cur);
39 next.add(step);
40 if(j+1==numsteps) next = d;
41 else
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;
47 dir1.sub(cur);
48 dir2.sub(camera1->o);
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);
53 if(j+1==numsteps)
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);
58 cur = next;
60 glEnd();
63 struct lightningrenderer : listrenderer
65 lightningrenderer()
66 : listrenderer("packages/particles/lightning.jpg", PT_LIGHTNING|PT_TRACK|PT_GLARE, 0, 0)
69 void startrender()
71 glDisable(GL_CULL_FACE);
74 void endrender()
76 glEnable(GL_CULL_FACE);
79 void update()
81 setuplightning();
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);
89 else
90 glColor4ub(color[0], color[1], color[2], blend);
91 float tx = 0, ty = 0, tsz = 1;
92 if(type&PT_RND4)
94 int i = detrnd((size_t)p, 4);
95 tx = 0.5f*(i&1);
96 ty = 0.5f*((i>>1)&1);
97 tsz = 0.5f;
99 renderlightning(o, d, p->size, tx, ty, tsz);
102 static lightningrenderer lightnings;