-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
154 lines (123 loc) · 3.2 KB
/
main.c
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
#include <GL/glut.h>
#include <stdio.h>
//Exercício Pedro Lopes Perozin RA: 1648730
int rotation = 0;
int translation = 1;
float tx = 0;
float ty = 0;
int rx = 5;
int ry = 5;
int angulo = 0;
void drawSquare()
{ //desenha o quadrado
glColor3f(1.0, 0.0, 0.0); //altera o atributo de cor
glBegin(GL_POLYGON); //desenha uma linha
glVertex2i(0, 0); //P1 origem
glVertex2i(10, 0); //P2 x = 10 y = 0
glVertex2i(10, 10); //P3 x = 10 y = 10
glVertex2i(0, 10); //P4 x = 0 y = 10
glEnd();
}
void selectedKey(unsigned char key, int x, int y)
{ //função de reconhecimento de tecla selecionada Ex: T ou R
switch (key)
{
case 't':
translation = 1;
rotation = 0;
break;
case 'r':
rotation = 1;
translation = 0;
break;
}
glutPostRedisplay();
}
//selectedDirection
void selectedDirection(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
if (translation == 1)
{
tx--;
break;
}
if (rotation == 1)
{
angulo ++ ;
break;
}
break;
case GLUT_KEY_RIGHT:
if (translation == 1)
{
tx++;
break;
}
if (rotation == 1)
{
angulo --;
break;
}
break;
case GLUT_KEY_UP:
if (translation == 1)
{
ty++;
break;
}
if (rotation == 1)
{
break;
}
case GLUT_KEY_DOWN:
if (translation == 1)
{
ty--;
break;
}
if (rotation == 1)
{
break;
}
}
glutPostRedisplay();
}
//void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
//void glutSpecialFunc(void (*func)(int key, int x, int y));
int init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); //define a cor de fundo
glMatrixMode(GL_PROJECTION); //carrega a matriz de projeção
gluOrtho2D(0.0, 100.0, 0.0, 100.0); //define projeção ortogonal 2D
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //desenha o fundo (limpa a janela)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(tx, ty, 0);
glTranslatef(rx,ry,0);
glRotatef(angulo,0,0,1);
glTranslatef(-(rx),-(ry),0);
drawSquare();
glFlush(); //desenha os comandos não executados
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); //inicializa o GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //configura o modo de display
glutInitWindowPosition(200, 0);
glutInitWindowSize(500, 500); //configura a largura e altura da janela de exibição
glutCreateWindow("Exercicio bonus 1"); //cria a janela de exibição
init(); //executa função de inicialização
glutDisplayFunc(display); //estabelece a função "display" como a função callback de exibição.
glutKeyboardFunc(selectedKey);
glutSpecialFunc(selectedDirection);
glutMainLoop(); //mostre tudo e espere
rx = tx + 5;
ry = ty + 5;
return 0;
}