/
display.cpp
260 lines (224 loc) · 6.06 KB
/
display.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (c) 2013, Todd E Brandt <tebrandt@frontier.com>.
*
* This program is licensed under the terms and conditions of the
* GPL License, version 2.0. The full text of the GPL License is at
* http://www.gnu.org/licenses/gpl-2.0.html
*
*/
#include <stdio.h>
#include "display.h"
#include "GL/gl.h"
#include "GL/glext.h"
#include <math.h>
enum {
MOUSE_LEFT = 0,
MOUSE_MIDDLE,
MOUSE_RIGHT,
MOUSE_WHEEL_UP,
MOUSE_WHEEL_DOWN
};
Display *display = NULL;
MouseState mousestate;
static void keyPress(unsigned char, int, int);
static void mousePress(int, int, int, int);
static void mouseMove(int, int);
static void drawScreen();
static void reshapeScreen(int, int);
float lighting_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
float lighting_diffuse[] = { 1.2, 1.2, 1.2, 1.0 };
float lighting_position[] = { 2.0, 2.0, 0.0, 1.0 };
unsigned char primary_color = GREEN;
unsigned char highlight_color = RED;
Display::Display()
{
paused = false;
redraw = false;
rotation[0] = 0;
rotation[1] = 0;
winWidth = 1280;
winHeight = 720;
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutInitWindowPosition(WINDOW_X, WINDOW_Y);
window = glutCreateWindow("Musualizer");
glutDisplayFunc(drawScreen);
glutIdleFunc(drawScreen);
glutReshapeFunc(reshapeScreen);
glutKeyboardFunc(keyPress);
glutMouseFunc(mousePress);
glutMotionFunc(mouseMove);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glLineWidth(LINESIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutFullScreen();
// gluPerspective(30, (GLfloat)1920/(GLfloat)1080,0.1f,5);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, lighting_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, lighting_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lighting_diffuse);
glEnable (GL_LIGHT0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
redraw = true;
}
Display::~Display()
{
glutDestroyWindow(window);
}
void Display::draw()
{
if(!redraw || paused)
return;
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -3);
if(mousestate.buttons[MOUSE_LEFT])
{
float ax = (float)mousestate.dy*180.0/winHeight;
float ay = (float)mousestate.dx*180.0/winWidth;
glRotatef(rotation[0]-ax, 1.0, 0.0, 0.0);
glRotatef(rotation[1]-ay, 0.0, 1.0, 0.0);
}
else
{
glRotatef(rotation[0], 1.0, 0.0, 0.0);
glRotatef(rotation[1], 0.0, 1.0, 0.0);
}
drawContent();
glutSwapBuffers();
redraw = false;
}
void Display::drawContent()
{
/* empty, leave to derived classes */
}
void Display::drawPolygon(
float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3)
{
float vx = (y3-y1)*(z2-z1) - (z3-z1)*(y2-y1);
float vy = (z3-z1)*(x2-x1) - (x3-x1)*(z2-z1);
float vz = (x3-x1)*(y2-y1) - (y3-y1)*(x2-x1);
float a = pow(vx, 2) + pow(vy, 2) + pow(vz, 2);
if(a > 0)
{
a = pow((1 / a), 0.5);
vx *= a;
vy *= a;
vz *= a;
}
glBegin(GL_POLYGON);
glNormal3f(vx, vy, vz);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();
}
void Display::changeRotation(float xval, float yval)
{
rotation[0] -= xval;
rotation[1] -= yval;
redraw = true;
}
void Display::changeScale(float val)
{
redraw = true;
}
static void keyPress(unsigned char key, int x, int y)
{
if(display == NULL)
return;
switch(key) {
case 'p': display->paused = !display->paused; break;
case 'g': primary_color = GREEN; break;
case 'b': primary_color = BLUE; break;
case 'm': primary_color = MAGENTA; break;
case 'w': primary_color = WHITE; break;
case 'c': primary_color = CYAN; break;
case 'r': primary_color = RED; break;
case 'y': primary_color = YELLOW; break;
case 'G': highlight_color = GREEN; break;
case 'B': highlight_color = BLUE; break;
case 'M': highlight_color = MAGENTA; break;
case 'W': highlight_color = WHITE; break;
case 'C': highlight_color = CYAN; break;
case 'R': highlight_color = RED; break;
case 'Y': highlight_color = YELLOW; break;
case 'q': exit(0); break;
default:
if(verbose)
printf("KEY: %c, %x\n", key, key);
}
display->redraw = true;
}
static void mousePress(int button, int state, int x, int y)
{
if(display == NULL)
return;
if(button < 5)
{
mousestate.buttons[button] = (state==GLUT_DOWN);
if(state==GLUT_DOWN)
{
mousestate.px = x;
mousestate.py = y;
}
}
if(((button == MOUSE_WHEEL_UP)||(button == MOUSE_WHEEL_DOWN))&&
(state==GLUT_DOWN))
{
display->changeScale((button == MOUSE_WHEEL_UP)?1.1:0.9);
}
if((button == MOUSE_LEFT)&&(state==GLUT_UP))
{
float ax = (float)mousestate.dy*180.0/display->winHeight;
float ay = (float)mousestate.dx*180.0/display->winWidth;
mousestate.dy = 0;
mousestate.dx = 0;
display->changeRotation(ax, ay);
}
}
static void mouseMove(int x, int y)
{
if(mousestate.buttons[MOUSE_LEFT])
{
mousestate.dx = mousestate.px - x;
mousestate.dy = mousestate.py - y;
display->redraw = true;
}
}
static void reshapeScreen(int Width, int Height)
{
/* sanity check */
if(Height < 1) Height = 1;
if(Width < 1) Width = 1;
display->winWidth = Width;
display->winHeight = Height;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, (GLfloat)Width/(GLfloat)Height,0.1f,5);
glMatrixMode(GL_MODELVIEW);
display->redraw = true;
display->draw();
}
static void drawScreen()
{
display->draw();
}
void Display::doRedraw()
{
display->redraw = true;
}
void Display::create()
{
if(display == NULL)
display = new Display();
}