-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
324 lines (289 loc) · 7.33 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* This program is a modified version of the program "image.c"
* appearing in _The OpenGL Programming Guide_ (red book).
* It demonstrates drawing pixels and shows the effect
* of glDrawPixels() and glCopyPixels().
* Interaction: moving the mouse while pressing the mouse button
* will copy the image in the lower-left corner of the window
* to the mouse position.
* There is no attempt to prevent you from drawing over the original
* image.
*
* IMPORTANT: The window with the image must be the active window, i.e.,
* it must be clicked or the mouse must be over it, for input to occur.
* The result will be shown in the window you started the program from,
* but don't make the mistake of typing in this window: it won't work.
*
*/
extern "C"{
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//void makeCheckImage(void);
//void init(void);
//void display(void);
//void reshape(int w, int h);
//void motion(int x, int y);
//void mouse(int button, int press, int x, int y);
//void read_next_command(unsigned char key, int x, int y);
}
#include <vector>
#include <iostream>
#include <locale>
#include <fstream>
#include "Dispatcher.hh"
#include "Mover.hh"
#include "Drawer.hh"
#include "Colorer.hh"
#include "Reader.hh"
#include "TiffRead.hh"
#include "main.hh"
const int numNested=6;
//void main_loop(char line[]);
GLubyte checkImage[checkImageHeight][checkImageWidth][3];
static GLint height;
void
makeCheckImage(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
// checkImage[i][j][0] = (GLubyte) c;
// checkImage[i][j][1] = (GLubyte) c;
// checkImage[i][j][2] = (GLubyte) c;
}
}
}
/*
* Function name: init
* Description: Operations that only need be performed once for rendering
* are done here.
* Arguments: none
* Globals: none
* Returns: void
*/
void
init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
/*
* Function name: display
* Description: The glut display callback, operations to render and
* re-render are here.
* Arguments: none
* Globals: none
* Returns: void
*/
void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glRasterPos2i(0, 0);
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB,
GL_UNSIGNED_BYTE, checkImage);
glFlush();
}
/*
* Function name: reshape
* Description: glut reshape callback, what actions to take when the
* window is reshaped.
* Arguments: w - the new width
* h - the new height
* Globals: none
* Returns: void
*/
void
reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
height = (GLint) h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
* Function name: motion
* Description: glut mouse motion callback, copies the image.
* Arguments: x - x position of mouse
* y - y positions of mouse
* Globals: none
* Returns: void
*/
void
motion(int x, int y)
{
static GLint screeny;
screeny = height - (GLint) y;
glRasterPos2i (x, screeny);
glCopyPixels (0, 0, checkImageWidth, checkImageHeight, GL_COLOR);
glFlush ();
}
/*
* Function name: mouse
* Description: glut mouse callback, draws lines on the image.
First left mouse click puts red dot at mouse location
Subsequent right mouse clicks draw a red line from that
location to current mouse location. Second left mouse
click resets routine and redraws the screen.
* Arguments: button - which button (left, right, middle) is being pressed
press - is it down?
x - x position of mouse
* y - y position of mouse (note this is from upper left corner)
* Globals: none
* Returns: void
*/
void
mouse(int button, int press, int x, int y)
{
static int state, start_x, start_y;
GLint viewport[4];
int height;
printf("Mouse: %d %d\n", x, y);
switch(button)
{
case GLUT_LEFT_BUTTON:
if(press == GLUT_DOWN)
{
if(!state)
{
glGetIntegerv(GL_VIEWPORT, viewport);
height = abs(viewport[3]-viewport[1]);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x, height - y);
glEnd();
glFlush();
state = 1;
start_x = x;
start_y = y;
}
else
{
state = 0;
display();
}
}
break;
case GLUT_RIGHT_BUTTON:
if (press == GLUT_DOWN)
{
glGetIntegerv(GL_VIEWPORT, viewport);
height = abs(viewport[3]-viewport[1]);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2i(start_x, height - start_y);
glVertex2i(x, height - y);
glEnd();
glFlush();
}
break;
}
}
/*
* Function name: main_loop
* Description: Process the line of input
* Arguments: line - the line of input
* Globals: none
* Returns: void
*/
void
main_loop(char line[])
{
/* PUT YOUR CLI CODE HERE! */
Dispatcher::dispatch(line);
return;
}
#define MAXLINE 1024
/* ASCII Values for return, backspace, delete, escape and ctrl-d */
#define CR 13
#define BS 8
#define DEL 127
#define ESC 27
#define EOT 4
/*
* Function name: read_next_command
* Description: The glut callback function which is called when a key
* is pressed. ESC redraws the screen, ctrl-d exits.
* Arguments: key - The ASCII value of the key pressed.
* x - x position of the mouse (window-relative)
* y - y position of the mouse (window-relative)
* Globals: none
* Returns: void
*/
void
read_next_command(unsigned char key, int x, int y)
{
static char line[MAXLINE];
static int count;
if(count >= MAXLINE - 1)
{
printf("Error: Maximum line length exceeded. Discarded input.\n");
count = 0;
return;
}
putchar(key);
fflush(stdout); /* Immediate output (gratification) */
if (key != CR && key != BS && key != DEL && key != ESC && key != EOT)
{
line[count]=key;
count++;
}
else if (key == BS || key == DEL)
{
if(count == 0)
return;
fflush(stdout);
count--;
}
else if (key == CR)
{
printf("\n");
line[count] = '\0';
count = 0;
main_loop(line);
}
else if (key == ESC)
{
glutPostRedisplay();
}
else
{
printf("Exiting...\n");
exit(0);
}
}
/*
* Function name: main
* Description: Set up GL window and render
* Arguments: command line arguments.
* Globals: none
* Returns: void
*/
int
main(int argc, char** argv)
{
Dispatcher::get_max_read_recursion_depth()=numNested;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(read_next_command);
glutMouseFunc(mouse);
// glutMotionFunc(motion);
printf("CLI> ");
fflush(stdout);
glutMainLoop();
return 0;
}