-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathGLLight.cpp
executable file
·86 lines (74 loc) · 2.22 KB
/
GLLight.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "GLLight.h"
#include "GL.h"
using namespace GLDraw;
inline void glLightv(GLenum light, GLenum pname,const GLfloat* v)
{
glLightfv(light,pname,v);
}
inline void glLightv(GLenum light, GLenum pname,const GLdouble* v)
{
GLfloat vf[4];
vf[0]=(GLfloat)v[0];
vf[1]=(GLfloat)v[1];
vf[2]=(GLfloat)v[2];
vf[3]=(GLfloat)v[3];
glLightfv(light,pname,vf);
}
GLLight::GLLight()
:position(Zero),att2(0),att1(0),att0(1),
spot_direction(Zero),spot_exponent(0),spot_cutoff(0)
{}
GLLight::GLLight(const Vector3& direction)
:att2(0),att1(0),att0(1)
{
setDirectionalLight(direction);
}
GLLight::GLLight(const Vector3& position,const Vector3& direction)
:att2(0),att1(0),att0(1)
{
setSpotLight(position,direction);
}
GLLight::GLLight(const GLLight& light)
:position(light.position),att2(light.att2),att1(light.att2),att0(light.att0),
diffuse(light.diffuse), specular(light.diffuse),
spot_direction(light.spot_direction),spot_exponent(light.spot_exponent),spot_cutoff(light.spot_cutoff)
{
}
void GLLight::setColor(const GLColor& col) { diffuse=specular=col; }
void GLLight::setPointLight(const Vector3& pos)
{
position.set(pos.x,pos.y,pos.z,1);
spot_direction.setZero();
spot_exponent=0;
spot_cutoff=180;
}
void GLLight::setDirectionalLight(const Vector3& dir)
{
position.set(dir.x,dir.y,dir.z,0);
spot_direction.setZero();
spot_exponent=0;
spot_cutoff=180;
}
void GLLight::setSpotLight(const Vector3& pos,const Vector3& dir,float exponent,float cutoff)
{
position.set(pos.x,pos.y,pos.z,1);
spot_direction=dir;
assert(exponent>=0 && exponent<=128); //max GL range
assert(cutoff>=0 && (cutoff<=90 || cutoff==180)); //max GL range
spot_exponent=exponent;
spot_cutoff=cutoff;
}
void GLLight::setCurrentGL(const int id)
{
GLenum gl_light_id=GL_LIGHT0+id;
glLightv(gl_light_id,GL_DIFFUSE,diffuse.rgba);
glLightv(gl_light_id,GL_SPECULAR,specular.rgba);
glLightv(gl_light_id,GL_POSITION,position);
glLightf(gl_light_id,GL_QUADRATIC_ATTENUATION,att2);
glLightf(gl_light_id,GL_LINEAR_ATTENUATION,att2);
glLightf(gl_light_id,GL_CONSTANT_ATTENUATION,att0);
glLightv(gl_light_id,GL_SPOT_DIRECTION,spot_direction);
glLightf(gl_light_id,GL_SPOT_EXPONENT,spot_exponent);
glLightf(gl_light_id,GL_SPOT_CUTOFF,spot_cutoff);
glEnable(gl_light_id);
}