-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
125 lines (95 loc) · 3.33 KB
/
Camera.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
120
121
122
123
124
125
#include "Camera.h"
/*class Camera {
protected:
Point focus_location;
Point camera_location;
#endif*/
Camera::Camera() {
yaw = 0.0f;
//pitch = 0.0f;
roll = 0.0f;
}
/*void Camera::gluPerspective(float fov, float aspect, float near_plane, float far_plane) {
double left, right;
double bottom, top;
top = tan(fov*SG_PI/360.0)*near_plane;
bottom = -top;
left = aspect * bottom;
right = aspect * top;
glFrustum(left, right, bottom, top, near_plane, far_plane);
}*/
void Camera::initialize(CameraContext * cctxt) {
context = cctxt;
context->shaders->oneShot("default", "vertex", "shader.vp", "fragment", "sharder.fp");
context->shaders->storeUniform("default", "cameraPosition");
context->shaders->storeUniform("default", "lightPosition");
context->shaders->storeUniform("default", "lightColor");
}
void Camera::setupProjection(ALLEGRO_DISPLAY_MODE _display_data) {
display_data = _display_data;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)display_data.width/(GLdouble)display_data.height, 0.01f, 500.0f);
glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLfloat light_position[] = {0, 0.75, -1.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
}
void Camera::applyRotation(float a, Vector3f axis) {
rotation.rotate(a, axis.x, axis.y, axis.z);
}
void Camera::changePitch(float a) {
pitch.rotate(a, 1.0, 0.0, 0.0);
}
void Camera::preRender() {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)display_data.width/(GLdouble)display_data.height, 0.01f, 500.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Matrix4f t;
t.asTranslate(camera_location.x, camera_location.y, camera_location.z);
Matrix4f projection_matrix;
projection_matrix.asIdentity();
projection_matrix = pitch.getConjugate().toMatrix() * rotation.toMatrix() * t * projection_matrix;
Vector3f light_position(0, 0.75, -1.0, 1.0);
//Vector3f new_light_position = projection_matrix * light_position;
GLfloat gl_light_pos[] = {light_position.x, light_position.y, light_position.z, 0};
glLightfv(GL_LIGHT0, GL_POSITION, gl_light_pos);
float projection_result[16];// = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
projection_matrix.asColumnMajor(projection_result);
glMultMatrixf(projection_result);
context->shaders->useProgram("default");
GLfloat camera_pos[] = {camera_location.x, camera_location.y, camera_location.z};
GLfloat light_pos[] = {gl_light_pos[0], gl_light_pos[1], gl_light_pos[2]};
GLfloat light_color[] = {0.2, 0.5, 0.9};
context->shaders->setUniform3v("cameraPosition", 1, camera_pos);
context->shaders->setUniform3v("lightPosition", 1, light_pos);
context->shaders->setUniform3v("lightColor", 1, light_color);
glPushMatrix();
}
void Camera::postRender() {
glPopMatrix();
}
void Camera::moveTo(Vector3f location) {
//cam_pos += rotation * Vector3f(0.4f, 0.0f, 0.0f);
camera_location += rotation.getConjugate().toMatrix() * location;
}
void Camera::moveY(float inc) {
camera_location.y += inc;
}
void Camera::lookAt(Vector3f location) {
}
Camera::~Camera() {
}