-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_event.cpp
executable file
·127 lines (116 loc) · 3.24 KB
/
mouse_event.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
126
127
#include "mouse_event.h"
#include "GL/freeglut.h"
#include "surface_rendering.h"
#include "matrix.h"
#include "vector.h"
#include "transformation.h"
extern Display r;
extern Camera cam1;
extern Object o[3];
void mouse_event(int button, int state, int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
r.clearZbuffer();
// Wheel reports as button 3(scroll up) and button 4(scroll down)
if (button == 3) // It's a wheel event
{
// Each wheel event reports like a button click, GLUT_DOWN then GLUT_UP
if (state == GLUT_UP) return; // Disregard redundant GLUT_UP events
Matrix scl = scale(Vector_3d(1.5,1.5,1.5));
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = scl*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
else if (button == 4)
{
// Each wheel event reports like a button click, GLUT_DOWN then GLUT_UP
Matrix scl = scale(Vector_3d(.5,.5,.5));
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = scl*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
else if (button == 1)
{
}
else
{
cam1.render(o, 3);
}
glFlush();
glutSwapBuffers();
}
void motion_event (int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
r.clearZbuffer();
if (is_first_time) {
is_first_time = false;
prev_mouse_X = x;
prev_mouse_Y = y;
return;
}
GLfloat deltaX = prev_mouse_X - x;
GLfloat deltaY = prev_mouse_Y - y;
if ( deltaX > 1.0f )
{
Matrix roty = rotate_yaxis(5);
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = roty*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
if ( deltaX < -1.0f )
{
Matrix roty = rotate_yaxis(-5);
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = roty*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
if ( deltaY > 1.0f )
{
Matrix rotz = rotate_zaxis(-5);
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = rotz*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
if ( deltaY < -1.0f )
{
Matrix rotz = rotate_zaxis(5);
for (int i=0; i<3; i++)
{
for(unsigned int j=0; j<o[i].total_face; j++)
{
o[i].vertices[j].position = rotz*o[i].vertices[j].position;
}
}
cam1.render(o, 3);
}
prev_mouse_X = x;
prev_mouse_Y = y;
glFlush();
// glutSwapBuffers();
}