dungeon_crawler
annotate prototype/src/light.cc @ 4:158de53b4e18
tile work
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 11 Aug 2012 05:44:52 +0300 |
parents | |
children | 0588f8a1a351 |
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@4 | 22 void Light::use(int id) const |
nuclear@4 | 23 { |
nuclear@4 | 24 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x); |
nuclear@4 | 25 glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x); |
nuclear@4 | 26 } |
nuclear@4 | 27 |
nuclear@4 | 28 |
nuclear@4 | 29 PointLight::PointLight() |
nuclear@4 | 30 { |
nuclear@4 | 31 atten[0] = 1.0f; |
nuclear@4 | 32 atten[1] = 0.0f; |
nuclear@4 | 33 atten[2] = 0.0f; |
nuclear@4 | 34 } |
nuclear@4 | 35 |
nuclear@4 | 36 PointLight::PointLight(const Vector3 &pos, const Color &col) |
nuclear@4 | 37 : Light(col) |
nuclear@4 | 38 { |
nuclear@4 | 39 this->pos = pos; |
nuclear@4 | 40 atten[0] = 1.0f; |
nuclear@4 | 41 atten[1] = 0.0f; |
nuclear@4 | 42 atten[2] = 0.0f; |
nuclear@4 | 43 } |
nuclear@4 | 44 |
nuclear@4 | 45 void PointLight::set_position(const Vector3 &pos) |
nuclear@4 | 46 { |
nuclear@4 | 47 this->pos = pos; |
nuclear@4 | 48 } |
nuclear@4 | 49 |
nuclear@4 | 50 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad) |
nuclear@4 | 51 { |
nuclear@4 | 52 atten[0] = att_const; |
nuclear@4 | 53 atten[1] = att_lin; |
nuclear@4 | 54 atten[2] = att_quad; |
nuclear@4 | 55 } |
nuclear@4 | 56 |
nuclear@4 | 57 void PointLight::use(int id) const |
nuclear@4 | 58 { |
nuclear@4 | 59 float lpos[] = {pos.x, pos.y, pos.z, 1.0f}; |
nuclear@4 | 60 glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos); |
nuclear@4 | 61 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]); |
nuclear@4 | 62 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]); |
nuclear@4 | 63 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]); |
nuclear@4 | 64 } |
nuclear@4 | 65 |
nuclear@4 | 66 |
nuclear@4 | 67 void set_light(int id, const Light *lt) |
nuclear@4 | 68 { |
nuclear@4 | 69 if(lt) { |
nuclear@4 | 70 glDisable(GL_LIGHT0 + id); |
nuclear@4 | 71 } else { |
nuclear@4 | 72 glEnable(GL_LIGHT0 + id); |
nuclear@4 | 73 lt->use(id); |
nuclear@4 | 74 } |
nuclear@4 | 75 } |