-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewerdata.cpp
121 lines (109 loc) · 2.82 KB
/
viewerdata.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
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include "viewerdata.h"
Camera::Camera():
pos(0.0, 0.0, -5.0),
lookat(0.0, 0.0, 0.0),
vup(0.0, 1.0, 0.0)
{
}
void ViewerData::moveCamera(Vec newPosCam)
{
camera.pos = newPosCam;
updateMatrix();
}
void ViewerData::updateMatrix()
{
glViewport(0, 0, (GLsizei)win_width, (GLsizei)win_height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera.pos.x,
camera.pos.y,
camera.pos.z,
camera.lookat.x,
camera.lookat.y,
camera.lookat.z,
camera.vup.x,
camera.vup.y,
camera.vup.z);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(anglePespective,
(float)win_width/(float)win_height,
znear, zfar);
}
ViewerData::ViewerData():
camera(),
anglePespective(60.0),
znear(1.0),
zfar(100.0),
win_width(640),
win_height(480)
{
updateMatrix();
}
Vec ViewerData::pixelToWorld(const int wx, const int wy)const
{
glLoadIdentity();
GLdouble rx,ry,rz;
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - (GLint)wy - 1;
gluUnProject((GLdouble)wx, (GLdouble)realy, 0.0,
mvmatrix, projmatrix, viewport,
&rx,
&ry,
&rz);
return glm::normalize(Vec(rx,ry,rz) - camera.pos);
}
// void ViewerData::pixelToWorld(const int wx, const int wy, Vec &posWorld)
// {
// glLoadIdentity();
// GLdouble rx,ry,rz;
// GLint viewport[4];
// GLdouble mvmatrix[16], projmatrix[16];
// GLint realy; /* OpenGL y coordinate position */
// // updateMatrix();
// glGetIntegerv(GL_VIEWPORT, viewport);
// glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
// glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
//
// realy = viewport[3] - (GLint)wy - 1;
//
// gluUnProject((GLdouble)wx, (GLdouble)realy, 0.0,
// mvmatrix, projmatrix, viewport,
// &rx,
// &ry,
// &rz);
// posWorld = glm::normalize(Vec(rx,ry,rz) - camera.pos);
// }
void ViewerData::setWindowSize(int win_width, int win_height)
{
this->win_width = win_width;
this->win_height= win_height;
updateMatrix();
}
void ViewerData::setCamera(const Camera &camera)
{
this->camera.pos = camera.pos;
this->camera.lookat = camera.lookat;
this->camera.vup = camera.vup;
updateMatrix();
}
void ViewerData::setPespective(float angleP, float znear, float zfar)
{
this->anglePespective = angleP;
this->znear = znear;
this->zfar = zfar;
updateMatrix();
}
void ViewerData::setLookAt(const Vec &at)
{
this->camera.lookat = at;
updateMatrix();
}