dungeon_crawler

view 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
line source
1 #include "opengl.h"
2 #include "light.h"
4 Light::Light(const Color &col)
5 : color(col)
6 {
7 intensity = 1.0;
8 }
10 Light::~Light() {}
12 void Light::set_intensity(float val)
13 {
14 intensity = val;
15 }
17 void Light::set_color(const Color &col)
18 {
19 color = col;
20 }
22 Color Light::get_color(bool with_intensity) const
23 {
24 return with_intensity ? color * intensity : color;
25 }
27 void Light::use(int id) const
28 {
29 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
30 glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
31 }
34 PointLight::PointLight()
35 {
36 atten[0] = 1.0f;
37 atten[1] = 0.0f;
38 atten[2] = 0.0f;
39 radius = 1.0;
40 }
42 PointLight::PointLight(const Vector3 &pos, const Color &col)
43 : Light(col)
44 {
45 this->pos = pos;
46 atten[0] = 1.0f;
47 atten[1] = 0.0f;
48 atten[2] = 0.0f;
49 radius = 1.0;
50 }
52 void PointLight::set_position(const Vector3 &pos)
53 {
54 this->pos = pos;
55 }
57 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
58 {
59 atten[0] = att_const;
60 atten[1] = att_lin;
61 atten[2] = att_quad;
62 }
64 void PointLight::set_radius(float rad)
65 {
66 radius = rad;
67 }
69 float PointLight::get_radius() const
70 {
71 return radius;
72 }
74 void PointLight::use(int id) const
75 {
76 float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
77 glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
78 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
79 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
80 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
81 }
84 void set_light(int id, const Light *lt)
85 {
86 if(lt) {
87 glDisable(GL_LIGHT0 + id);
88 } else {
89 glEnable(GL_LIGHT0 + id);
90 lt->use(id);
91 }
92 }