dungeon_crawler

view prototype/src/light.cc @ 20:48cfd351a9be

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