-
Notifications
You must be signed in to change notification settings - Fork 2
/
Viewer.cpp
80 lines (70 loc) · 1.47 KB
/
Viewer.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
/*
* File: Viewer.cpp
* Author: swl
*
* Created on February 5, 2016, 5:07 PM
*/
#include "Viewer.h"
#include "ObjRenderer.h"
float theta = M_PI/4;
float phi = M_PI/12;
float dist = 4;
int mouse_x, mouse_y;
void mouseFunc(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouse_x = x;
mouse_y = y;
}
else if(button == 3)
dist *= 1.05;
else if(button == 4)
dist *= 0.95;
}
void motionFunc(int x, int y)
{
int dx = x - mouse_x;
int dy = y - mouse_y;
theta += dx*0.01;
if(theta > 2*M_PI)
theta -= 2*M_PI;
if(theta < -2*M_PI)
theta += 2*M_PI;
phi -= dy*0.01;
if(phi < M_PI/12)
phi = M_PI/12;
if(phi > 11*M_PI/12)
phi = 11*M_PI/12;
mouse_x = x;
mouse_y = y;
}
void keyboardFunc(unsigned char key, int x, int y)
{
if(key >= '1' && key <= '9')
ObjRenderer::setShaderOutputID(key-'0');
switch(key)
{
case 27:
exit(0);
break;
}
}
void idleFunc()
{
ObjRenderer::setEyePos(dist*glm::vec3(sin(phi)*cos(theta), cos(phi), sin(phi)*sin(theta)));
glutPostRedisplay();
}
void Viewer::run()
{
glutShowWindow();
glutMainLoop();
}
void Viewer::init()
{
glutMotionFunc(motionFunc);
glutMouseFunc(mouseFunc);
glutIdleFunc(idleFunc);
glutKeyboardFunc(keyboardFunc);
ObjRenderer::setEyePos(dist*glm::vec3(sin(phi)*cos(theta), cos(phi), sin(phi)*sin(theta)));
}