-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlutApp.cpp
166 lines (136 loc) · 3.94 KB
/
GlutApp.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "GlutApp.h"
// Make this static so that it will not be accessible in other source files
static GlutApp* app;
GlutApp::GlutApp(const char* label, int x, int y, int w, int h) {
// Set app pointer to be the current object
app = this;
// Setup window position, size, and title
glutInitWindowPosition(x, y);
glutInitWindowSize(w, h);
glutCreateWindow(label);
// Setup some OpenGL options
glEnable (GL_DEPTH_TEST);
glEnable (GL_POINT_SMOOTH);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
glPointSize (4);
glLineWidth (2);
// Setup GLUT callbacks for different events
glutDisplayFunc(displayCB);
glutMouseFunc(mouseCB);
glutMotionFunc(motionCB);
glutKeyboardFunc(keyboardCB);
glutSpecialFunc(specialCB);
glutIdleFunc(idleCB);
glutReshapeFunc(reshapeCB);
glutPassiveMotionFunc(passiveCB);
}
void GlutApp::windowToScene ( float& x, float &y )
{
// Converting window coordinates [0..width] x [0..height] to [-1..1] x [-1..1]
x = (2.0f*(x/float(width))) - 1.0f;
y = 1.0f - (2.0f*(y/float(height)));
// Take care of issue in Windows where y coordinate is a little off
#if defined WIN32
y -= 0.03;
#endif
}
void GlutApp::draw(){
// Default behavior for a display method
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set background color to black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Set up the transformations stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// We have been drawing everything to the back buffer
// Swap the buffers to see the result of what we drew
glFlush();
glutSwapBuffers();
}
void GlutApp::resize ( int w, int h ) {
// Update the width and height
width = w;
height = h;
// Tell OpenGL to use the whole window
glViewport( 0, 0, w, h );
}
void GlutApp::keyPress(unsigned char key){
// Exit the application when any key is pressed
exit(0);
}
void GlutApp::run() {
// Run the main loop
glutMainLoop();
}
void GlutApp::redraw() {
// Redraw the display
glutPostRedisplay();
}
void GlutApp::displayCB(){
// When GLUT needs to redraw itself, call our draw method
app->draw();
}
void GlutApp::reshapeCB(int w, int h){
// When the window is resized, call our resize method
app->resize(w, h);
}
void GlutApp::idleCB(){
// Call out idle method when application idles
app->idle();
}
void GlutApp::mouseCB(int b, int s, int x, int y){
// Convert mouse position to scene coordinates
float mx = (float) x;
float my = (float) y;
app->windowToScene(mx, my);
// Determine button and state and call appropriate handler
if (b == 0){
// Left click
if (s == 0){
// Left down
app->mouseDown(mx, my);
}
else {
// Left up
app->mouseUp(mx, my);
}
}
else {
// Right click
if (s == 0){
// Right down
app->mouseRightDown(mx, my);
}
else {
// Right up
app->mouseRightUp(mx, my);
}
}
}
void GlutApp::motionCB(int x, int y){
// Convert mouse position to scene coordinates
float mx = (float) x;
float my = (float) y;
app->windowToScene(mx, my);
// Call drag handler
app->mouseDrag(mx, my);
}
void GlutApp::passiveCB (int x, int y){
// Convert mouse position to scene coordinates
float mx = (float) x;
float my = (float) y;
app->windowToScene(mx, my);
// Call mouse motion handler
app->mouseMove(mx, my);
}
void GlutApp::keyboardCB(unsigned char key, int x, int y){
// When a key is pressed, call our keypress handler
app->keyPress(key);
}
void GlutApp::specialCB(int key, int x, int y){
// When a special key is pressed, call our handler for special keys
app->specialKeyPress(key);
}