]> git.tdb.fi Git - libs/gl.git/blob - source/light.cpp
Improve smoothing in blender exporter
[libs/gl.git] / source / light.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "except.h"
9 #include "light.h"
10 #include "misc.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Light::Light():
18         ambient(0),
19         diffuse(1),
20         specular(1),
21         position(0, 0, 1, 0),
22         spot_dir(0, 0, -1),
23         spot_exp(0),
24         spot_cutoff(180)
25 {
26         attenuation[0] = 1;
27         attenuation[1] = 0;
28         attenuation[2] = 0;
29 }
30
31 void Light::set_ambient(const Color &c)
32 {
33         ambient = c;
34 }
35
36 void Light::set_diffuse(const Color &c)
37 {
38         diffuse = c;
39 }
40
41 void Light::set_specular(const Color &c)
42 {
43         specular = c;
44 }
45
46 void Light::set_position(const Vector4 &p)
47 {
48         position = p;
49 }
50
51 void Light::set_spot_direction(const Vector3 &d)
52 {
53         spot_dir = d;
54 }
55
56 void Light::set_spot_exponent(float e)
57 {
58         spot_exp = e;
59 }
60
61 void Light::set_spot_cutoff(float c)
62 {
63         spot_cutoff = c;
64 }
65
66 void Light::set_attenuation(float c, float l, float q)
67 {
68         attenuation[0] = c;
69         attenuation[1] = l;
70         attenuation[2] = q;
71 }
72
73 void Light::bind() const
74 {
75         if(current_lights[current_unit]!=this)
76         {
77                 GLenum l = GL_LIGHT0+current_unit;
78                 enable(l);
79                 glLightfv(l, GL_AMBIENT, &ambient.r);
80                 glLightfv(l, GL_DIFFUSE, &diffuse.r);
81                 glLightfv(l, GL_SPECULAR, &specular.r);
82                 glLightfv(l, GL_POSITION, &position.x);
83                 glLightfv(l, GL_SPOT_DIRECTION, &spot_dir.x);
84                 glLightf(l, GL_SPOT_EXPONENT, spot_exp);
85                 glLightf(l, GL_SPOT_CUTOFF, spot_cutoff);
86                 glLightf(l, GL_CONSTANT_ATTENUATION, attenuation[0]);
87                 glLightf(l, GL_LINEAR_ATTENUATION, attenuation[1]);
88                 glLightf(l, GL_QUADRATIC_ATTENUATION, attenuation[2]);
89                 current_lights[current_unit] = this;
90         }
91 }
92
93 void Light::bind_to(unsigned i) const
94 {
95         activate(i);
96         bind();
97 }
98
99 void Light::activate(unsigned i)
100 {
101         static unsigned max_lights = get_i(GL_MAX_LIGHTS);
102
103         if(i>=max_lights)
104                 throw InvalidParameterValue("Light unit index out of range");
105
106         if(i>=current_lights.size())
107                 current_lights.resize(i+1);
108
109         current_unit = i;
110 }
111
112 void Light::unbind()
113 {
114         if(current_lights[current_unit])
115         {
116                 disable(GL_LIGHT0+current_unit);
117                 current_lights[current_unit] = 0;
118         }
119 }
120
121 unsigned Light::current_unit = 0;
122 vector<const Light *> Light::current_lights(1);
123
124 } // namespace GL
125 } // namespace Msp