-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_canvas2d.cpp
320 lines (269 loc) · 6.94 KB
/
gl_canvas2d.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
/**
* Programa para ilustrar os elementos mais basicos do OpenGL e Glut.
* - Apresenta os principais recursos do sistema de Janelas GLUT
*
* Autor: Cesar Tadeu Pozzer
* UFSM - 2020
*
* pozzer@inf.ufsm.br
* pozzer3@gmail.com
*
* Referencias GLUT: http://www.opengl.org/documentation/specs/glut/
* http://www.opengl.org/documentation/specs/glut/spec3/node1.html
**/
#include "gl_canvas2d.h"
#include <GL/glut.h>
int *scrWidth, *scrHeight; //guarda referencia para as variaveis de altura e largura da main()
//conjunto de cores predefinidas. Pode-se adicionar mais cores.
float Colors[14][3]=
{
{0, 0, 0}, //Black
{0.5, 0.5, 0.5}, //Gray
{1, 0, 0}, //Red
{0, 1, 0}, //Green
{0, 0, 1}, //Blue
{0, 1, 1}, //Cyan
{1, 0, 1}, //Magenta
{1, 1, 0}, //Yellow
{1, 0.5, 0}, //Orange
{0.5, 0, 0}, //Brown
{0.5, 0.5, 0}, //Olive
{0, 0.5, 0.5}, //
{0.5, 0, 0.5}, //
{1, 1, 1}, //white
};
void ConvertMouseCoord(int button, int state, int wheel, int direction, int x, int y);
//funcoes de CALLBACK da biblioteca Glut
void keyboard(int key);
void keyboardUp(int key);
void specialUp(int key);
void mouse(int bt, int st, int wheel, int direction, int x, int y);
void mouseWheelCB(int wheel, int direction, int x, int y);
void render();
void CV::point(float x, float y)
{
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}
void CV::line( float x1, float y1, float x2, float y2 )
{
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
}
void CV::rect( float x1, float y1, float x2, float y2 )
{
glBegin(GL_LINE_LOOP);
glVertex2d(x1, y1);
glVertex2d(x1, y2);
glVertex2d(x2, y2);
glVertex2d(x2, y1);
glEnd();
}
void CV::rectFill( float x1, float y1, float x2, float y2 )
{
glBegin(GL_QUADS);
glVertex2d(x1, y1);
glVertex2d(x1, y2);
glVertex2d(x2, y2);
glVertex2d(x2, y1);
glEnd();
}
void CV::polygon(float vx[], float vy[], int elems)
{
int cont;
glBegin(GL_LINE_LOOP);
for(cont=0; cont<elems; cont++)
{
glVertex2d(vx[cont], vy[cont]);
}
glEnd();
}
void CV::polygonFill(float vx[], float vy[], int elems)
{
int cont;
glBegin(GL_POLYGON);
for(cont=0; cont<elems; cont++)
{
glVertex2d(vx[cont], vy[cont]);
}
glEnd();
}
void CV::text(float x, float y, const int i)
{
char str[20];
sprintf(str, "%d", i);
text(x, y, str, 13);
}
void CV::text(float x, float y, const char *t, int tamanho)
{
int tam = (int)strlen(t);
for(int c=0; c < tam; c++)
{
glRasterPos2i(x + c*10, y);
if (tamanho == 13) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, t[c]);
} else if (tamanho == 15) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, t[c]);
}
}
}
void CV::clear(float r, float g, float b)
{
glClearColor( r, g, b, 1 );
}
void CV::circle( float x, float y, float radius, int div )
{
float ang = 0, x1, y1;
float inc = PI_2/div;
glBegin(GL_LINE_LOOP);
for(int lado = 1; lado <= div; lado++) //GL_LINE_LOOP desenha um poligono fechado. Liga automaticamente o primeiro e ultimio vertices.
{
x1 = (cos(ang)*radius);
y1 = (sin(ang)*radius);
glVertex2d(x1+x, y1+y);
ang+=inc;
}
glEnd();
}
void CV::circleFill( float x, float y, float radius, int div )
{
float ang = 0, x1, y1;
float inc = PI_2/div;
glBegin(GL_POLYGON);
for(int lado = 1; lado <= div; lado++) //GL_POLYGON desenha um poligono CONVEXO preenchido.
{
x1 = (cos(ang)*radius);
y1 = (sin(ang)*radius);
glVertex2d(x1+x, y1+y);
ang+=inc;
}
glEnd();
}
//coordenada de offset para desenho de objetos.
//nao armazena translacoes cumulativas.
void CV::translate(float offsetX, float offsetY)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(offsetX, offsetY, 0);
}
void CV::color(float r, float g, float b)
{
glColor3d(r, g, b);
}
void CV::color(int idx)
{
glColor3fv(Colors[idx]);
}
void CV::color(float r, float g, float b, float alpha)
{
glColor4d(r, g, b, alpha);
}
void special(int key, int , int )
{
keyboard(key+100);
}
void specialUp(int key, int , int )
{
keyboardUp(key+100);
}
void keyb(unsigned char key, int , int )
{
keyboard(key);
}
void keybUp(unsigned char key, int , int )
{
keyboardUp(key);
}
void mouseClick(int button, int state, int x, int y)
{
ConvertMouseCoord(button, state, -2, -2, x, y);
}
void mouseWheelCB(int wheel, int direction, int x, int y)
{
ConvertMouseCoord(-2, -2, wheel, direction, x, y);
}
void motion(int x, int y)
{
ConvertMouseCoord(-2, -2, -2, -2, x, y);
}
void ConvertMouseCoord(int button, int state, int wheel, int direction, int x, int y)
{
#if Y_CANVAS_CRESCE_PARA_CIMA == TRUE
y = *scrHeight - y; //deve-se inverter a coordenada y do mouse se o y da canvas crescer para cima. O y do mouse sempre cresce para baixo.
#else
//nao faz nada.
#endif
mouse(button, state, wheel, direction, x, y);
}
//funcao chamada sempre que a tela for redimensionada.
void reshape (int w, int h)
{
*scrHeight = h; //atualiza as variaveis da main() com a nova dimensao da tela.
*scrWidth = w;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//cria uma projecao ortografica com z entre (-1, 1).
#if Y_CANVAS_CRESCE_PARA_CIMA == TRUE
//parametros: left, right, bottom, top
gluOrtho2D (0.0, w, 0.0, h); //o eixo y cresce para cima.
#else
//parametros: left, right, bottom, top
gluOrtho2D (0.0, w, h, 0.0); //o eixo y cresce para baixo
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
//definicao de valores para limpar buffers
void inicializa()
{
glClearColor(1,1,1,1);
glPolygonMode(GL_FRONT, GL_FILL);
}
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render();
glFlush();
glutSwapBuffers();
}
////////////////////////////////////////////////////////////////////////////////////////
// inicializa o OpenGL
////////////////////////////////////////////////////////////////////////////////////////
void CV::init(int *w, int *h, const char *title)
{
int argc = 0;
glutInit(&argc, NULL);
scrHeight = h;
scrWidth = w;
//habilita MSAA
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE);
//glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (*w, *h);
glutInitWindowPosition (50, 50);
glutCreateWindow (title);
inicializa();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyb);
glutKeyboardUpFunc(keybUp);
glutSpecialUpFunc(specialUp);
glutSpecialFunc(special);
glutIdleFunc(display);
glutMouseFunc(mouseClick);
glutPassiveMotionFunc(motion);
glutMotionFunc(motion);
glutMouseWheelFunc(mouseWheelCB);
printf("GL Version: %s", glGetString(GL_VERSION));
}
void CV::run()
{
glutMainLoop();
}