dungeon_crawler

annotate prototype/src/light.cc @ 21:0588f8a1a351

converting LIGHT meshes to lights
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 06:33:36 +0300
parents 158de53b4e18
children fa8f89d06f6f
rev   line source
nuclear@4 1 #include "opengl.h"
nuclear@4 2 #include "light.h"
nuclear@4 3
nuclear@4 4 Light::Light(const Color &col)
nuclear@4 5 : color(col)
nuclear@4 6 {
nuclear@4 7 intensity = 1.0;
nuclear@4 8 }
nuclear@4 9
nuclear@4 10 Light::~Light() {}
nuclear@4 11
nuclear@4 12 void Light::set_intensity(float val)
nuclear@4 13 {
nuclear@4 14 intensity = val;
nuclear@4 15 }
nuclear@4 16
nuclear@4 17 void Light::set_color(const Color &col)
nuclear@4 18 {
nuclear@4 19 color = col;
nuclear@4 20 }
nuclear@4 21
nuclear@21 22 Color Light::get_color(bool with_intensity) const
nuclear@21 23 {
nuclear@21 24 return with_intensity ? color * intensity : color;
nuclear@21 25 }
nuclear@21 26
nuclear@4 27 void Light::use(int id) const
nuclear@4 28 {
nuclear@4 29 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
nuclear@4 30 glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
nuclear@4 31 }
nuclear@4 32
nuclear@4 33
nuclear@4 34 PointLight::PointLight()
nuclear@4 35 {
nuclear@4 36 atten[0] = 1.0f;
nuclear@4 37 atten[1] = 0.0f;
nuclear@4 38 atten[2] = 0.0f;
nuclear@21 39 radius = 1.0;
nuclear@4 40 }
nuclear@4 41
nuclear@4 42 PointLight::PointLight(const Vector3 &pos, const Color &col)
nuclear@4 43 : Light(col)
nuclear@4 44 {
nuclear@4 45 this->pos = pos;
nuclear@4 46 atten[0] = 1.0f;
nuclear@4 47 atten[1] = 0.0f;
nuclear@4 48 atten[2] = 0.0f;
nuclear@21 49 radius = 1.0;
nuclear@4 50 }
nuclear@4 51
nuclear@4 52 void PointLight::set_position(const Vector3 &pos)
nuclear@4 53 {
nuclear@4 54 this->pos = pos;
nuclear@4 55 }
nuclear@4 56
nuclear@4 57 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
nuclear@4 58 {
nuclear@4 59 atten[0] = att_const;
nuclear@4 60 atten[1] = att_lin;
nuclear@4 61 atten[2] = att_quad;
nuclear@4 62 }
nuclear@4 63
nuclear@21 64 void PointLight::set_radius(float rad)
nuclear@21 65 {
nuclear@21 66 radius = rad;
nuclear@21 67 }
nuclear@21 68
nuclear@21 69 float PointLight::get_radius() const
nuclear@21 70 {
nuclear@21 71 return radius;
nuclear@21 72 }
nuclear@21 73
nuclear@4 74 void PointLight::use(int id) const
nuclear@4 75 {
nuclear@4 76 float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
nuclear@4 77 glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
nuclear@4 78 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
nuclear@4 79 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
nuclear@4 80 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
nuclear@4 81 }
nuclear@4 82
nuclear@4 83
nuclear@4 84 void set_light(int id, const Light *lt)
nuclear@4 85 {
nuclear@4 86 if(lt) {
nuclear@4 87 glDisable(GL_LIGHT0 + id);
nuclear@4 88 } else {
nuclear@4 89 glEnable(GL_LIGHT0 + id);
nuclear@4 90 lt->use(id);
nuclear@4 91 }
nuclear@4 92 }