-
Notifications
You must be signed in to change notification settings - Fork 1
/
Light.cpp
120 lines (97 loc) · 2.2 KB
/
Light.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Light.h"
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <iostream>
// Public part :
Light::Light()
:lastUpdate(glutGet(GLUT_ELAPSED_TIME)), speed(defaultSpeed), angle(0.0),
radius(defaultRadius), height(defaultHeight)
{
pos[3] = 1.0;
initProjMatrix();
update();
}
Light::~Light()
{
}
void Light::update()
{
float currentTime = glutGet(GLUT_ELAPSED_TIME);
angle += (currentTime - lastUpdate) * speed;
if(angle > 360.0)
angle -= 360.0;
/*pos[0] = radius * cos(angle);
pos[1] = height;
pos[2] = radius * sin(angle);*/
pos[0] = 10000.0;
pos[1] = 10000.0;
pos[2] = -10000.0;
glLightfv(GL_LIGHT0, GL_POSITION, pos);
lastUpdate = currentTime;
updateViewMatrix();
}
void Light::activateRendering()
{
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glCullFace(GL_FRONT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadMatrixf(projMatrix);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//glLoadMatrixf(viewMatrix);
}
void Light::deactivateRendering()
{
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glCullFace(GL_BACK);
}
float *Light::getProjMatrix()
{
return projMatrix;
}
float *Light::getViewMatrix()
{
return viewMatrix;
}
// Private part :
const float Light::defaultSpeed = 0.001;
const float Light::defaultRadius = 2.0;
const float Light::defaultHeight = 2.0;
void Light::initProjMatrix()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 15.0);
glGetFloatv(GL_MODELVIEW_MATRIX, projMatrix);
glPopMatrix();
}
void Light::updateViewMatrix()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(pos[0], pos[1], pos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);
glPopMatrix();
}
void Light::setModelViewMatrix(float* in)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrixf(viewMatrix);
glMultMatrixf(in);
glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
glPopMatrix();
}
float* Light::getModelViewMatrix()
{
return modelViewMatrix;
}