Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lensflare.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 static struct flaretype
4 {
5  int type; /* flaretex index, 0..5, -1 for 6+random shine */
6  float loc; /* postion on axis */
7  float scale; /* texture scaling */
8  uchar alpha; /* color alpha */
9 } flaretypes[] =
10 {
11  {2, 1.30f, 0.04f, 153}, //flares
12  {3, 1.00f, 0.10f, 102},
13  {1, 0.50f, 0.20f, 77},
14  {3, 0.20f, 0.05f, 77},
15  {0, 0.00f, 0.04f, 77},
16  {5, -0.25f, 0.07f, 127},
17  {5, -0.40f, 0.02f, 153},
18  {5, -0.60f, 0.04f, 102},
19  {5, -1.00f, 0.03f, 51},
20  {-1, 1.00f, 0.30f, 255}, //shine - red, green, blue
21  {-2, 1.00f, 0.20f, 255},
22  {-3, 1.00f, 0.25f, 255}
23 };
24 
25 struct flare
26 {
28  float size;
30  bool sparkle;
31 };
32 
34 {
36  unsigned int shinetime;
38 
39  flarerenderer(const char *texname, int maxflares)
40  : partrenderer(texname, 3, PT_FLARE), maxflares(maxflares), numflares(0), shinetime(0)
41  {
42  flares = new flare[maxflares];
43  }
44  ~flarerenderer() override
45  {
46  delete[] flares;
47  }
48 
49  void reset() override
50  {
51  numflares = 0;
52  }
53 
54  void newflare(vec &o, const vec &center, uchar r, uchar g, uchar b, float mod, float size, bool sun, bool sparkle)
55  {
56  if(numflares >= maxflares) return;
57  vec target; //occlusion check (neccessary as depth testing is turned off)
58  if(!raycubelos(o, camera1->o, target)) return;
59  flare &f = flares[numflares++];
60  f.o = o;
61  f.center = center;
62  f.size = size;
63  f.color = bvec(uchar(r*mod), uchar(g*mod), uchar(b*mod));
64  f.sparkle = sparkle;
65  }
66 
67  void addflare(vec &o, uchar r, uchar g, uchar b, bool sun, bool sparkle)
68  {
69  //frustrum + fog check
70  if(isvisiblesphere(0.0f, o) > (sun?VFC_FOGGED:VFC_FULL_VISIBLE)) return;
71  //find closest point between camera line of sight and flare pos
72  vec flaredir = vec(o).sub(camera1->o);
73  vec center = vec(camdir).mul(flaredir.dot(camdir)).add(camera1->o);
74  float mod, size;
75  if(sun) //fixed size
76  {
77  mod = 1.0;
78  size = flaredir.magnitude() * flaresize / 100.0f;
79  }
80  else
81  {
82  mod = (flarecutoff-vec(o).sub(center).squaredlen())/flarecutoff;
83  if(mod < 0.0f) return;
84  size = flaresize / 5.0f;
85  }
86  newflare(o, center, r, g, b, mod, size, sun, sparkle);
87  }
88 
90  {
91  numflares = 0; //regenerate flarelist each frame
92  shinetime = lastmillis/10;
93 
94  if(editmode || !flarelights) return;
95 
97  extern const vector<int> &checklightcache(int x, int y);
98  const vector<int> &lights = checklightcache(int(camera1->o.x), int(camera1->o.y));
99  loopv(lights)
100  {
101  entity &e = *ents[lights[i]];
102  if(e.type != ET_LIGHT) continue;
103  bool sun = (e.attr1==0);
104  float radius = float(e.attr1);
105  vec flaredir = vec(e.o).sub(camera1->o);
106  float len = flaredir.magnitude();
107  if(!sun && (len > radius)) continue;
108  if(isvisiblesphere(0.0f, e.o) > (sun?VFC_FOGGED:VFC_FULL_VISIBLE)) continue;
109  vec center = vec(camdir).mul(flaredir.dot(camdir)).add(camera1->o);
110  float mod, size;
111  if(sun) //fixed size
112  {
113  mod = 1.0;
114  size = len * flaresize / 100.0f;
115  }
116  else
117  {
118  mod = (radius-len)/radius;
119  size = flaresize / 5.0f;
120  }
121  newflare(e.o, center, e.attr2, e.attr3, e.attr4, mod, size, sun, sun);
122  }
123  }
124 
125  int count() override
126  {
127  return numflares;
128  }
129 
130  bool haswork() override
131  {
132  return (numflares != 0) && !glaring && !reflecting && !refracting;
133  }
134 
135  void render() override
136  {
137  textureshader->set();
138  glDisable(GL_DEPTH_TEST);
139  if(!tex) tex = textureload(texname);
140  glBindTexture(GL_TEXTURE_2D, tex->id);
141  gle::defattrib(gle::ATTRIB_VERTEX, 3, GL_FLOAT);
143  gle::defattrib(gle::ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE);
144  gle::begin(GL_QUADS);
146  {
147  const flare &f = flares[i];
148  vec center = f.center;
149  vec axis = vec(f.o).sub(center);
150  bvec4 color(f.color, 255);
151  loopj(f.sparkle?12:9)
152  {
153  const flaretype &ft = flaretypes[j];
154  vec o = vec(axis).mul(ft.loc).add(center);
155  float sz = ft.scale * f.size;
156  int tex = ft.type;
157  if(ft.type < 0) //sparkles - always done last
158  {
159  shinetime = (shinetime + 1) % 10;
160  tex = 6+shinetime;
161  color.r = 0;
162  color.g = 0;
163  color.b = 0;
164  color[-ft.type-1] = f.color[-ft.type-1]; //only want a single channel
165  }
166  color.a = ft.alpha;
167  const float tsz = 0.25; //flares are aranged in 4x4 grid
168  float tx = tsz*(tex&0x03), ty = tsz*((tex>>2)&0x03);
169  gle::attribf(o.x+(-camright.x+camup.x)*sz, o.y+(-camright.y+camup.y)*sz, o.z+(-camright.z+camup.z)*sz);
170  gle::attribf(tx, ty+tsz);
171  gle::attrib(color);
172  gle::attribf(o.x+( camright.x+camup.x)*sz, o.y+( camright.y+camup.y)*sz, o.z+( camright.z+camup.z)*sz);
173  gle::attribf(tx+tsz, ty+tsz);
174  gle::attrib(color);
175  gle::attribf(o.x+( camright.x-camup.x)*sz, o.y+( camright.y-camup.y)*sz, o.z+( camright.z-camup.z)*sz);
176  gle::attribf(tx+tsz, ty);
177  gle::attrib(color);
178  gle::attribf(o.x+(-camright.x-camup.x)*sz, o.y+(-camright.y-camup.y)*sz, o.z+(-camright.z-camup.z)*sz);
179  gle::attribf(tx, ty);
180  gle::attrib(color);
181  }
182  }
183  gle::end();
184  glEnable(GL_DEPTH_TEST);
185  }
186 
187  //square per round hole - use addflare(..) instead
188  particle *addpart(const vec &o, const vec &d, int fade, int color, float size, int gravity = 0) override { return nullptr; }
189 };
190 static flarerenderer flares("<grey>particle/lensflares.png", 64);
191 
int count() override
Definition: lensflare.hpp:125
Definition: particles.hpp:108
const vector< int > & checklightcache(int x, int y)
Definition: lightmap.cpp:1264
Texture * tex
Definition: particles.hpp:110
void makelightflares()
Definition: lensflare.hpp:89
Definition: glemu.hpp:20
Vector template.
Definition: cube_vector.hpp:22
short attr1
Definition: ents.hpp:69
int isvisiblesphere(float rad, const vec &cv)
Definition: renderva.cpp:71
static flarerenderer flares("<grey>particle/lensflares.png", 64)
Definition: lensflare.hpp:3
float squaredlen() const
calculate the squared length of this vector (pythagoras)
Definition: geom.hpp:159
vec & mul(const vec &o)
scalar multiplication
Definition: geom.hpp:168
Shader * textureshader
Definition: shader.cpp:33
void begin(GLenum mode)
Definition: glemu.cpp:220
unsigned int shinetime
Definition: lensflare.hpp:36
vector< extentity * > ents
Definition: entities.cpp:75
vec camdir
Definition: rendergl.cpp:698
Definition: octree.hpp:344
vec o
Definition: lensflare.hpp:27
uchar alpha
Definition: lensflare.hpp:8
void addflare(vec &o, uchar r, uchar g, uchar b, bool sun, bool sparkle)
Definition: lensflare.hpp:67
vec center
Definition: lensflare.hpp:27
flarerenderer(const char *texname, int maxflares)
Definition: lensflare.hpp:39
void render() override
Definition: lensflare.hpp:135
uchar type
Definition: ents.hpp:70
persistent map entity.
Definition: ents.hpp:66
Texture * textureload(const char *name, int clamp, bool mipit, bool msg, bool threadsafe)
Definition: texture.cpp:501
Definition: ents.hpp:14
bool reflecting
Definition: water.cpp:820
bvec color
Definition: lensflare.hpp:29
ICOMMAND * f(float *a, float *b), floatret(*a **b)
Definition: particles.hpp:39
float magnitude() const
vector's magnitude in all 3 (XYZ) dimensions
Definition: geom.hpp:195
float scale
Definition: lensflare.hpp:7
Definition: particles.hpp:78
else loopi(numargs)
Definition: command.cpp:3019
Definition: octree.hpp:346
Definition: glemu.hpp:18
bool editmode
Definition: octaedit.cpp:170
Definition: glemu.hpp:19
float dot(const vec &o) const
dot product (line by line) of all 3 dimensions
Definition: geom.hpp:163
int lastmillis
Definition: legacy_time.cpp:14
int refracting
Definition: water.cpp:818
vec camright
Definition: rendergl.cpp:698
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
vec & sub(const vec &o)
scalar subtraction
Definition: geom.hpp:177
vec camup
Definition: rendergl.cpp:698
particle * addpart(const vec &o, const vec &d, int fade, int color, float size, int gravity=0) override
Definition: lensflare.hpp:188
3-dimensional UNSIGNED CHAR vector.
Definition: geom.hpp:1398
bool glaring
Definition: glare.cpp:38
bool raycubelos(const vec &o, const vec &dest, vec &hitpos)
Definition: physics.cpp:459
void defattrib(int type, int size, int format)
Definition: glemu.cpp:109
GLuint id
Definition: texture.hpp:45
int d
Definition: octaedit.cpp:1749
int numflares
Definition: lensflare.hpp:35
short attr3
Definition: ents.hpp:69
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
physent * camera1
special ent that acts as camera, same object as player1 in FPS mode
Definition: rendergl.cpp:868
const char * texname
Definition: particles.hpp:111
int end()
Definition: glemu.cpp:256
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
vector< extentity * > & getents()
Definition: entities.cpp:77
void newflare(vec &o, const vec &center, uchar r, uchar g, uchar b, float mod, float size, bool sun, bool sparkle)
Definition: lensflare.hpp:54
int type
Definition: lensflare.hpp:5
vec o
Definition: ents.hpp:130
vec & add(const vec &o)
scalar sum
Definition: geom.hpp:174
static struct flaretype flaretypes[]
static void attrib(T x)
Definition: glemu.hpp:106
Definition: lensflare.hpp:33
void reset() override
Definition: lensflare.hpp:49
#define loopj(m)
Definition: cube_loops.hpp:9
bool sparkle
Definition: lensflare.hpp:30
bool haswork() override
Definition: lensflare.hpp:130
float loc
Definition: lensflare.hpp:6
short attr4
Definition: ents.hpp:69
Definition: lensflare.hpp:25
float size
Definition: lensflare.hpp:28
static void color(const bvec &v, uchar alpha=255)
Definition: glemu.hpp:71
int maxflares
Definition: lensflare.hpp:35
~flarerenderer() override
Definition: lensflare.hpp:44
Definition: geom.hpp:1455
bool target(fpsent *d, aistate &b, int pursue=0, bool force=false, float mindist=0.f)
Definition: ai.cpp:393
#define loopv(v)
Definition: cube_loops.hpp:21
vec o
Definition: ents.hpp:68
void set()
Definition: shader.hpp:278
flare * flares
Definition: lensflare.hpp:37
short attr2
Definition: ents.hpp:69