Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lightning.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #define MAXLIGHTNINGSTEPS 64
4 #define LIGHTNINGSTEP 8
7 
8 static void calclightningjitter(int frame)
9 {
11  {
12  lnjitterx[lnjitterframe][i] = -lnjitterradius + rnd(2*lnjitterradius + 1);
13  lnjittery[lnjitterframe][i] = -lnjitterradius + rnd(2*lnjitterradius + 1);
14  }
15 }
16 
17 static void setuplightning()
18 {
19  if(!lastlnjitter || lastmillis-lastlnjitter > lnjittermillis)
20  {
22  lastlnjitter = lastmillis - (lastmillis%lnjittermillis);
24  }
25 }
26 
27 static void renderlightning(Texture *tex, const vec &o, const vec &d, float sz)
28 {
29  vec step(d);
30  step.sub(o);
31  float len = step.magnitude();
32  int numsteps = clamp(int(ceil(len/LIGHTNINGSTEP)), 2, MAXLIGHTNINGSTEPS);
33  step.div(numsteps+1);
34  int jitteroffset = detrnd(int(d.x+d.y+d.z), MAXLIGHTNINGSTEPS);
35  vec cur(o), up, right;
36  up.orthogonal(step);
37  up.normalize();
38  right.cross(up, step);
39  right.normalize();
40  float scroll = -float(lastmillis%lnscrollmillis)/lnscrollmillis,
41  scrollscale = lnscrollscale*(LIGHTNINGSTEP*tex->ys)/(sz*tex->xs),
42  blend = pow(clamp(float(lastmillis - lastlnjitter)/lnjittermillis, 0.0f, 1.0f), lnblendpower),
43  jitter0 = (1-blend)*lnjitterscale*sz/lnjitterradius, jitter1 = blend*lnjitterscale*sz/lnjitterradius;
44  gle::begin(GL_TRIANGLE_STRIP);
45  loopj(numsteps)
46  {
47  vec next(cur);
48  next.add(step);
49  if(j+1==numsteps) next = d;
50  else
51  {
52  int lj = (j+jitteroffset)%MAXLIGHTNINGSTEPS;
53  next.add(vec(right).mul((jitter1*lnjitterx[lnjitterframe][lj] + jitter0*lnjitterx[lnjitterframe^1][lj])));
54  next.add(vec(up).mul((jitter1*lnjittery[lnjitterframe][lj] + jitter0*lnjittery[lnjitterframe^1][lj])));
55  }
56  vec dir1 = next, dir2 = next, across;
57  dir1.sub(cur);
58  dir2.sub(camera1->o);
59  across.cross(dir2, dir1).normalize().mul(sz);
60  gle::attribf(cur.x-across.x, cur.y-across.y, cur.z-across.z);
61  gle::attribf(scroll, 1);
62  gle::attribf(cur.x+across.x, cur.y+across.y, cur.z+across.z);
63  gle::attribf(scroll, 0);
64  scroll += scrollscale;
65  if(j+1==numsteps)
66  {
67  gle::attribf(next.x-across.x, next.y-across.y, next.z-across.z);
68  gle::attribf(scroll, 1);
69  gle::attribf(next.x+across.x, next.y+across.y, next.z+across.z);
70  gle::attribf(scroll, 0);
71  }
72  cur = next;
73  }
74  gle::end();
75 }
76 
78 {
80  : listrenderer("particle/lightning.jpg", 2, PT_LIGHTNING|PT_TRACK|PT_GLARE)
81  {}
82 
83  void startrender() override
84  {
85  glDisable(GL_CULL_FACE);
86  gle::defattrib(gle::ATTRIB_VERTEX, 3, GL_FLOAT);
88  }
89 
90  void endrender() override
91  {
92  glEnable(GL_CULL_FACE);
93  }
94 
95  void update() override
96  {
98  }
99 
100  void seedemitter(particleemitter &pe, const vec &o, const vec &d, int fade, float size, int gravity) override
101  {
102  pe.maxfade = max(pe.maxfade, fade);
103  pe.extendbb(o, size);
104  pe.extendbb(d, size);
105  }
106 
107  void renderpart(listparticle *p, const vec &o, const vec &d, int blend, int ts) override
108  {
109  blend = min(blend<<2, 255);
110  if(type&PT_MOD) //multiply alpha into color
111  gle::colorub((p->color.r*blend)>>8, (p->color.g*blend)>>8, (p->color.b*blend)>>8);
112  else
113  gle::color(p->color, blend);
114  renderlightning(tex, o, d, p->size);
115  }
116 };
118 
ivec cur
Definition: octaedit.cpp:167
Texture * tex
Definition: particles.hpp:110
Definition: glemu.hpp:20
const T & max(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:224
void extendbb(const vec &o, float size=0)
Definition: renderparticles.cpp:82
Definition: particles.hpp:45
vec & mul(const vec &o)
scalar multiplication
Definition: geom.hpp:168
float size
Definition: particles.hpp:84
Definition: particles.hpp:58
Definition: particles.hpp:38
void begin(GLenum mode)
Definition: glemu.cpp:220
static void calclightningjitter(int frame)
Definition: lightning.hpp:8
int lnjitterframe
Definition: lightning.hpp:6
lightningrenderer()
Definition: lightning.hpp:79
int lastlnjitter
Definition: lightning.hpp:6
static void renderlightning(Texture *tex, const vec &o, const vec &d, float sz)
Definition: lightning.hpp:27
ICOMMAND * f(float *a, float *b), floatret(*a **b)
static void setuplightning()
Definition: lightning.hpp:17
float magnitude() const
vector's magnitude in all 3 (XYZ) dimensions
Definition: geom.hpp:195
int xs
Definition: texture.hpp:43
else loopi(numargs)
Definition: command.cpp:3019
Definition: glemu.hpp:18
bvec color
Definition: particles.hpp:82
float detrnd(int seed, int Rmax)
Definition: tools.hpp:70
void startrender() override
Definition: lightning.hpp:83
int lastmillis
Definition: legacy_time.cpp:14
int rnd(int Rmax)
Function alias. Should be replaced inline actually!
Definition: tools.hpp:64
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
int lnjittery[2][MAXLIGHTNINGSTEPS]
Definition: lightning.hpp:5
vec & sub(const vec &o)
scalar subtraction
Definition: geom.hpp:177
const T & min(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:210
Definition: particles.hpp:157
Definition: particles.hpp:151
Definition: particles.hpp:41
void orthogonal(const vec &d)
orthogonal projection
Definition: geom.hpp:278
void defattrib(int type, int size, int format)
Definition: glemu.cpp:109
int d
Definition: octaedit.cpp:1749
int lnjitterx[2][MAXLIGHTNINGSTEPS]
Definition: lightning.hpp:5
void renderpart(listparticle *p, const vec &o, const vec &d, int blend, int ts) override
Definition: lightning.hpp:107
physent * camera1
special ent that acts as camera, same object as player1 in FPS mode
Definition: rendergl.cpp:868
int end()
Definition: glemu.cpp:256
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
vec o
Definition: ents.hpp:130
static lightningrenderer lightnings
Definition: lightning.hpp:117
Definition: lightning.hpp:77
vec & add(const vec &o)
scalar sum
Definition: geom.hpp:174
#define loopj(m)
Definition: cube_loops.hpp:9
int maxfade
Definition: particles.hpp:65
#define LIGHTNINGSTEP
Definition: lightning.hpp:4
vec & div(const vec &o)
scalar division
Definition: geom.hpp:171
void update() override
Definition: lightning.hpp:95
static void color(const bvec &v, uchar alpha=255)
Definition: glemu.hpp:71
void seedemitter(particleemitter &pe, const vec &o, const vec &d, int fade, float size, int gravity) override
Definition: lightning.hpp:100
vec & cross(const A &a, const B &b)
template based vector cross product
Definition: geom.hpp:211
Definition: particles.hpp:44
vec & normalize()
normalize vector: divide it by its length (magnitude)
Definition: geom.hpp:198
int ys
Definition: texture.hpp:43
#define MAXLIGHTNINGSTEPS
Definition: lightning.hpp:3
uint type
Definition: particles.hpp:113
A Texture entry, holding info about a texture on the GPU.
Definition: texture.hpp:27
void endrender() override
Definition: lightning.hpp:90