dungeon_crawler

annotate prototype/src/light.cc @ 73:67d330038629

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