-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGLUT_Plotter.cpp
More file actions
207 lines (153 loc) · 3.69 KB
/
GLUT_Plotter.cpp
File metadata and controls
207 lines (153 loc) · 3.69 KB
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
/*
* GLUT_Plotter.cpp
*
* Created on: Mar 21, 2014
* Author: Dr. Booth
*/
#include "GLUT_Plotter.h"
GLUT_Plotter* g;
GLUT_Plotter::GLUT_Plotter(int w, int h){
width = w;
height = h;
buffer = new char[width*height*3];
g = this;
init();
callBacks();
Draw();
}
void GLUT_Plotter::init(){
int *argc=new int(1);
char **argv = new char*[1];
argv[0] = new char[10];
argv[0][0] = '\0';
glutInitWindowSize(width, height);
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Dr. Booth's Plotter Class");
//glutFullScreen();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void GLUT_Plotter::init(int *argc, char **argv){
glutInitWindowSize(width, height);
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Dr. Booth's Plotter Class");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
Draw();
}
void GLUT_Plotter::MainLoop(){
glutMainLoop();
}
void GLUT_Plotter::RegisterKeyboardFunc(void (*func)(unsigned char key, int x, int y)){
glutKeyboardFunc(func);
}
void GLUT_Plotter::RegisterSpecialKeyboardFunc(void (*func)(int key, int x, int y)){
glutSpecialFunc(func);
}
void GLUT_Plotter::RegisterDisplayFunc(void (*func)(void)){
glutDisplayFunc(func);
}
void GLUT_Plotter::RegisterMouseFunc(void (*func)(int button, int state,int x, int y)){
glutMouseFunc(func);
}
void GLUT_Plotter::RegisterIdleFunc(void (*func)(void)){
glutIdleFunc(func);
}
void GLUT_Plotter::Clear(){
memset(buffer,0,width*height*3);
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glutSwapBuffers();
}
void GLUT_Plotter::Draw(void){
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glutSwapBuffers();
// instruct event system to call 'drawfunc' again
//glutPostRedisplay();
}
int GLUT_Plotter::getWidth(){
return width;
}
int GLUT_Plotter::getHeight(){
return height;
}
char* GLUT_Plotter::getBuffer(){
return buffer;
}
void GLUT_Plotter::setColor(unsigned int c){
color = c;
}
unsigned int GLUT_Plotter::getColor(){
return color;
}
void GLUT_Plotter::plot(int x, int y){
unsigned int r,g,b;
r = (color & 0xff0000)>>16;
g = (color & 0x00FF00)>>8;
b = (color & 0x0000FF)>>0;
setpixel(buffer, x, y, r, g, b, width);
}
void GLUT_Plotter::callBacks(){
RegisterDisplayFunc(&drawFunction);
RegisterKeyboardFunc(keyboardFunction);
RegisterSpecialKeyboardFunc(SpecialKeyboardFunction);
RegisterMouseFunc(mouseFunction);
}
unsigned char GLUT_Plotter::getKey(){
int key = -1;
if(keyboardQueue.size() > 0){
key = keyboardQueue.front();
keyboardQueue.pop();
}
return key;
}
Click GLUT_Plotter::getClick(){
Click c;
c.button = -1;
c.state = -1;
c.x = -1;
c.y = -1;
if(mouseQueue.size() > 0){
c = mouseQueue.front();
mouseQueue.pop();
}
return c;
}
void GLUT_Plotter::addClick(Click c){
mouseQueue.push(c);
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
void keyboardFunction(unsigned char k, int x, int y) {
g->addKeyHit(k);
}
void SpecialKeyboardFunction(int k, int x, int y) {
k+=64;
keyboardFunction(k,x,y);
}
void mouseFunction(int button, int state,int x, int y){
Click c;
c.button = button;
c.state = state;
c.x = x;
c.y = y;
if(state == 0){
g->addClick(c);
}
}
void drawFunction(void){
}
void setpixel(char *buf, int x, int y, int r, int g, int b, int width) {
buf[(y*width+x)*3+0] = r;
buf[(y*width+x)*3+1] = g;
buf[(y*width+x)*3+2] = b;
}