-
Notifications
You must be signed in to change notification settings - Fork 0
/
iGraphics.h
executable file
·472 lines (399 loc) · 11.3 KB
/
iGraphics.h
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//
// Original Author: S. M. Shahriar Nirjon
//
// Last Modified by: Mohammad Saifur Rahman
// last modified: December 20, 2015
//
// Version: 2.0.2012.2015
//
# include <stdio.h>
# include <stdlib.h>
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glaux.lib")
#include <GL/glut.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#include <GL/glaux.h>
void initText2D(const char * texturePath);
void printText2D(const char * text, int x, int y, int size);
void cleanupText2D();
int iScreenHeight, iScreenWidth;
int iMouseX, iMouseY;
int ifft=0;
void (*iAnimFunction[10])(void)={0};
int iAnimCount=0;
int iAnimDelays[10];
int iAnimPause[10];
void iDraw();
void iKeyboard(unsigned char);
void iSpecialKeyboard(unsigned char);
void iMouseMove(int, int);
void iMouse(int button, int state, int x, int y);
static void __stdcall iA0(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[0])iAnimFunction[0]();}
static void __stdcall iA1(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[1])iAnimFunction[1]();}
static void __stdcall iA2(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[2])iAnimFunction[2]();}
static void __stdcall iA3(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[3])iAnimFunction[3]();}
static void __stdcall iA4(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[4])iAnimFunction[4]();}
static void __stdcall iA5(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[5])iAnimFunction[5]();}
static void __stdcall iA6(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[6])iAnimFunction[6]();}
static void __stdcall iA7(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[7])iAnimFunction[7]();}
static void __stdcall iA8(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[8])iAnimFunction[8]();}
static void __stdcall iA9(HWND,unsigned int, unsigned int, unsigned long){if(!iAnimPause[9])iAnimFunction[9]();}
int iSetTimer(int msec, void (*f)(void))
{
int i = iAnimCount;
if(iAnimCount>=10){printf("Error: Maximum number of already timer used.\n");return -1;}
iAnimFunction[i] = f;
iAnimDelays[i] = msec;
iAnimPause[i] = 0;
if(iAnimCount == 0) SetTimer(0, 0, msec, iA0);
if(iAnimCount == 1) SetTimer(0, 0, msec, iA1);
if(iAnimCount == 2) SetTimer(0, 0, msec, iA2);
if(iAnimCount == 3) SetTimer(0, 0, msec, iA3);
if(iAnimCount == 4) SetTimer(0, 0, msec, iA4);
if(iAnimCount == 5) SetTimer(0, 0, msec, iA5);
if(iAnimCount == 6) SetTimer(0, 0, msec, iA6);
if(iAnimCount == 7) SetTimer(0, 0, msec, iA7);
if(iAnimCount == 8) SetTimer(0, 0, msec, iA8);
if(iAnimCount == 9) SetTimer(0, 0, msec, iA9);
iAnimCount++;
return iAnimCount-1;
}
void iPauseTimer(int index){
if(index>=0 && index <iAnimCount){
iAnimPause[index] = 1;
}
}
void iResumeTimer(int index){
if(index>=0 && index <iAnimCount){
iAnimPause[index] = 0;
}
}
//
// Puts a BMP image on screen
//
// parameters:
// x - x coordinate
// y - y coordinate
// filename - name of the BMP file
// ignoreColor - A specified color that should not be rendered. If you have an
// image strip that should be rendered on top of another back
// ground image, then the background of the image strip should
// not get rendered. Use the background color of the image strip
// in ignoreColor parameter. Then the strip's background does
// not get rendered.
//
// To disable this feature, put -1 in this parameter
//
void iShowBMP2(int x, int y, char filename[], int ignoreColor)
{
AUX_RGBImageRec *TextureImage;
TextureImage = auxDIBImageLoad(filename);
int i,j,k;
int width = TextureImage->sizeX;
int height = TextureImage->sizeY;
int nPixels = width * height;
int *rgPixels = new int[nPixels];
for (i = 0, j=0; i < nPixels; i++, j += 3)
{
int rgb = 0;
for(int k = 2; k >= 0; k--)
{
rgb = ((rgb << 8) | TextureImage->data[j+k]);
}
rgPixels[i] = (rgb == ignoreColor) ? 0 : 255;
rgPixels[i] = ((rgPixels[i] << 24) | rgb);
}
glRasterPos2f(x, y);
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgPixels);
delete []rgPixels;
free(TextureImage->data);
free(TextureImage);
}
void iShowBMP(int x, int y, char filename[])
{
iShowBMP2(x, y, filename, -1 /* ignoreColor */);
}
void iGetPixelColor (int cursorX, int cursorY, int rgb[])
{
GLubyte pixel[3];
glReadPixels(cursorX, cursorY,1,1,
GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);
rgb[0] = pixel[0];
rgb[1] = pixel[1];
rgb[2] = pixel[2];
//printf("%d %d %d\n",pixel[0],pixel[1],pixel[2]);
}
void iText(GLdouble x, GLdouble y, char *str, void* font=GLUT_BITMAP_8_BY_13)
{
glRasterPos3d(x, y,0);
int i;
for (i=0; str[i]; i++) {
glutBitmapCharacter(font, str[i]); //,GLUT_BITMAP_8_BY_13, GLUT_BITMAP_TIMES_ROMAN_24
}
}
void iPoint(double x, double y, int size=0)
{
int i, j;
glBegin(GL_POINTS);
glVertex2f(x, y);
for(i=x-size;i<x+size;i++)
{
for(j=y-size; j<y+size;j++)
{
glVertex2f(i, j);
}
}
glEnd();
}
void iLine(double x1, double y1, double x2, double y2)
{
glBegin(GL_LINE_STRIP);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void iFilledPolygon(double x[], double y[], int n)
{
int i;
if(n<3)return;
glBegin(GL_POLYGON);
for(i = 0; i < n; i++){
glVertex2f(x[i], y[i]);
}
glEnd();
}
void iPolygon(double x[], double y[], int n)
{
int i;
if(n<3)return;
glBegin(GL_LINE_STRIP);
for(i = 0; i < n; i++){
glVertex2f(x[i], y[i]);
}
glVertex2f(x[0], y[0]);
glEnd();
}
void iRectangle(double left, double bottom, double dx, double dy)
{
double x1, y1, x2, y2;
x1 = left;
y1 = bottom;
x2=x1+dx;
y2=y1+dy;
iLine(x1, y1, x2, y1);
iLine(x2, y1, x2, y2);
iLine(x2, y2, x1, y2);
iLine(x1, y2, x1, y1);
}
void iFilledRectangle(double left, double bottom, double dx, double dy)
{
double xx[4], yy[4];
double x1, y1, x2, y2;
x1 = left;
y1 = bottom;
x2=x1+dx;
y2=y1+dy;
xx[0]=x1;
yy[0]=y1;
xx[1]=x2;
yy[1]=y1;
xx[2]=x2;
yy[2]=y2;
xx[3]=x1;
yy[3]=y2;
iFilledPolygon(xx, yy, 4);
}
void iFilledCircle(double x, double y, double r, int slices=100)
{
double t, PI=acos(-1.0), dt, x1,y1, xp, yp;
dt = 2*PI/slices;
xp = x+r;
yp = y;
glBegin(GL_POLYGON);
for(t = 0; t <= 2*PI; t+=dt)
{
x1 = x + r * cos(t);
y1 = y + r * sin(t);
glVertex2f(xp, yp);
xp = x1;
yp = y1;
}
glEnd();
}
void iCircle(double x, double y, double r, int slices=100)
{
double t, PI=acos(-1.0), dt, x1,y1, xp, yp;
dt = 2*PI/slices;
xp = x+r;
yp = y;
for(t = 0; t <= 2*PI; t+=dt)
{
x1 = x + r * cos(t);
y1 = y + r * sin(t);
iLine(xp, yp, x1, y1);
xp = x1;
yp = y1;
}
}
void iEllipse(double x, double y, double a, double b, int slices=100)
{
double t, PI=acos(-1.0), dt, x1,y1, xp, yp;
dt = 2*PI/slices;
xp = x+a;
yp = y;
for(t = 0; t <= 2*PI; t+=dt)
{
x1 = x + a * cos(t);
y1 = y + b * sin(t);
iLine(xp, yp, x1, y1);
xp = x1;
yp = y1;
}
}
void iFilledEllipse(double x, double y, double a, double b, int slices=100)
{
double t, PI=acos(-1.0), dt, x1,y1, xp, yp;
dt = 2*PI/slices;
xp = x+a;
yp = y;
glBegin(GL_POLYGON);
for(t = 0; t <= 2*PI; t+=dt)
{
x1 = x + a * cos(t);
y1 = y + b * sin(t);
glVertex2f(xp, yp);
xp = x1;
yp = y1;
}
glEnd();
}
//
// Rotates the co-ordinate system
// Parameters:
// (x, y) - The pivot point for rotation
// degree - degree of rotation
//
// After calling iRotate(), evrey subsequent rendering will
// happen in rotated fashion. To stop rotation of subsequent rendering,
// call iUnRotate(). Typical call pattern would be:
// iRotate();
// Render your objects, that you want rendered as rotated
// iUnRotate();
//
void iRotate(double x, double y, double degree)
{
// push the current matrix stack
glPushMatrix();
//
// The below steps take effect in reverse order
//
// step 3: undo the translation
glTranslatef(x, y, 0.0);
// step 2: rotate the co-ordinate system across z-axis
glRotatef(degree, 0, 0, 1.0);
// step 1: translate the origin to (x, y)
glTranslatef(-x, -y, 0.0);
}
void iUnRotate()
{
glPopMatrix();
}
void iSetColor(double r, double g, double b)
{
double mmx;
mmx = r;
if(g > mmx)mmx = g;
if(b > mmx)mmx = b;
mmx = 255;
if(mmx > 0){
r /= mmx;
g /= mmx;
b /= mmx;
}
glColor3f(r, g, b);
}
void iDelay(int sec)
{
int t1, t2;
t1 = time(0);
while(1){
t2 = time(0);
if(t2-t1>=sec)
break;
}
}
void iClear()
{
glClear(GL_COLOR_BUFFER_BIT) ;
glMatrixMode(GL_MODELVIEW) ;
glClearColor(0,0,0,0);
glFlush();
}
void displayFF(void){
iDraw();
glutSwapBuffers() ;
}
void animFF(void)
{
if(ifft == 0){
ifft = 1;
iClear();
}
glutPostRedisplay();
}
void keyboardHandler1FF(unsigned char key, int x, int y)
{
iKeyboard(key);
glutPostRedisplay();
}
void keyboardHandler2FF(int key, int x, int y)
{
iSpecialKeyboard(key);
glutPostRedisplay();
}
void mouseMoveHandlerFF(int mx, int my)
{
iMouseX = mx;
iMouseY = iScreenHeight - my;
iMouseMove(iMouseX, iMouseY);
glFlush();
}
void mouseHandlerFF(int button, int state, int x, int y)
{
iMouseX = x;
iMouseY = iScreenHeight - y;
iMouse(button, state, iMouseX, iMouseY);
glFlush();
}
void iInitialize(int width=500, int height=500, char *title="iGraphics")
{
iScreenHeight = height;
iScreenWidth = width;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA) ;
glutInitWindowSize(width , height ) ;
glutInitWindowPosition( 10 , 10 ) ;
glutCreateWindow(title) ;
glClearColor( 0.0 , 0.0 , 0.0 , 0.0 ) ;
glMatrixMode( GL_PROJECTION) ;
glLoadIdentity() ;
glOrtho(0.0 , width , 0.0 , height , -1.0 , 1.0) ;
//glOrtho(-100.0 , 100.0 , -100.0 , 100.0 , -1.0 , 1.0) ;
//SetTimer(0, 0, 10, timer_proc);
iClear();
glutDisplayFunc(displayFF) ;
glutKeyboardFunc(keyboardHandler1FF); //normal
glutSpecialFunc(keyboardHandler2FF); //special keys
glutMouseFunc(mouseHandlerFF);
glutMotionFunc(mouseMoveHandlerFF);
glutIdleFunc(animFF) ;
//
// Setup Alpha channel testing.
// If alpha value is greater than 0, then those
// pixels will be rendered. Otherwise, they would not be rendered
//
glAlphaFunc(GL_GREATER,0.0f);
glEnable(GL_ALPHA_TEST);
glutMainLoop();
}