dungeon_crawler
view prototype/src/light.cc @ 52:bcdea26c8f27
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 18 Sep 2012 18:34:28 +0300 |
parents | acfe0c0110fc |
children | aa86119e3295 |
line source
1 #include <stdlib.h>
2 #include "opengl.h"
3 #include "light.h"
4 #include "renderer.h"
5 #include "timer.h"
7 Light::Light(const Color &col)
8 : color(col)
9 {
10 intensity = 1.0;
11 vbo = 0;
12 num_faces = 0;
14 flicker_offset = 2.0 * (float)rand() / (float)RAND_MAX;
15 }
17 Light::~Light() {}
19 void Light::set_intensity(float val)
20 {
21 intensity = val;
22 }
24 void Light::set_color(const Color &col)
25 {
26 color = col;
27 }
29 Color Light::get_color(bool with_intensity) const
30 {
31 return with_intensity ? color * intensity : color;
32 }
34 void Light::use(int id) const
35 {
36 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
37 glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
38 }
40 void Light::draw() const
41 {
42 if(!vbo) {
43 if(!((Light*)this)->create_mesh()) {
44 return;
45 }
46 }
48 glEnableClientState(GL_VERTEX_ARRAY);
49 glBindBuffer(GL_ARRAY_BUFFER, vbo);
50 glVertexPointer(3, GL_FLOAT, 0, 0);
52 glDrawArrays(GL_TRIANGLES, 0, num_faces * 3);
54 glBindBuffer(GL_ARRAY_BUFFER, 0);
55 glDisableClientState(GL_VERTEX_ARRAY);
56 }
58 bool Light::create_mesh()
59 {
60 fprintf(stderr, "%s: undefined\n", __FUNCTION__);
61 return false;
62 }
65 PointLight::PointLight()
66 : Light(Color(1.0, 1.0, 1.0))
67 {
68 atten[0] = 1.0f;
69 atten[1] = 0.0f;
70 atten[2] = 0.0f;
71 radius = 1.0;
72 }
74 PointLight::PointLight(const Vector3 &pos, const Color &col)
75 : Light(col)
76 {
77 this->pos = pos;
78 atten[0] = 1.0f;
79 atten[1] = 0.0f;
80 atten[2] = 0.0f;
81 radius = 1.0;
82 }
84 void PointLight::set_position(const Vector3 &pos)
85 {
86 this->pos = pos;
87 }
89 const Vector3 &PointLight::get_position() const
90 {
91 return pos;
92 }
94 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
95 {
96 atten[0] = att_const;
97 atten[1] = att_lin;
98 atten[2] = att_quad;
99 }
101 void PointLight::set_radius(float rad)
102 {
103 radius = rad;
104 }
106 float PointLight::get_radius() const
107 {
108 return radius;
109 }
111 void PointLight::use(int id) const
112 {
113 float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
114 glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
115 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
116 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
117 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
118 }
120 void PointLight::draw() const
121 {
122 unsigned int sdr = rend->get_current_program();
123 if(sdr) {
124 int loc;
125 if((loc = glGetUniformLocation(sdr, "light_radius")) != -1) {
126 glUniform1f(loc, radius);
127 }
128 if((loc = glGetUniformLocation(sdr, "light_color")) != -1) {
129 float t = get_time_msec() / 1000.0 + flicker_offset * 4.0;
130 float intens = fbm1(t * 2.0, 2) * 0.5 + 1.0;
131 glUniform3f(loc, color.x * intens, color.y * intens, color.z * intens);
132 }
133 }
135 glMatrixMode(GL_MODELVIEW);
136 glPushMatrix();
137 glTranslatef(pos.x, pos.y, pos.z);
138 glScalef(radius, radius, radius);
140 Light::draw();
142 glPopMatrix();
143 }
147 Vector3 sphvertex(float u, float v)
148 {
149 float theta = u * M_PI * 2.0;
150 float phi = v * M_PI;
152 Vector3 res;
153 res.x = sin(theta) * sin(phi);
154 res.y = cos(phi);
155 res.z = cos(theta) * sin(phi);
156 return res;
157 }
160 bool PointLight::create_mesh()
161 {
162 const static int udiv = 8;
163 const static int vdiv = 4;
165 int nquads = udiv * vdiv;
166 num_faces = nquads * 2;
167 int nverts = num_faces * 3;
169 float du = 1.0 / (float)udiv;
170 float dv = 1.0 / (float)vdiv;
172 glGenBuffers(1, &vbo);
173 glBindBuffer(GL_ARRAY_BUFFER, vbo);
174 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof(Vector3), 0, GL_STATIC_DRAW);
176 Vector3 *vptr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
178 for(int i=0; i<vdiv; i++) {
179 float v = (float)i / (float)vdiv;
180 for(int j=0; j<udiv; j++) {
181 float u = (float)j / (float)udiv;
183 *vptr++ = sphvertex(u, v + dv);
184 *vptr++ = sphvertex(u + du, v);
185 *vptr++ = sphvertex(u, v);
187 *vptr++ = sphvertex(u, v + dv);
188 *vptr++ = sphvertex(u + du, v + dv);
189 *vptr++ = sphvertex(u + du, v);
190 }
191 }
192 glUnmapBuffer(GL_ARRAY_BUFFER);
193 glBindBuffer(GL_ARRAY_BUFFER, 0);
194 return true;
195 }
197 void set_light(int id, const Light *lt)
198 {
199 if(lt) {
200 glDisable(GL_LIGHT0 + id);
201 } else {
202 glEnable(GL_LIGHT0 + id);
203 lt->use(id);
204 }
205 }