-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48027134y_bloc1.c
More file actions
187 lines (127 loc) · 4 KB
/
48027134y_bloc1.c
File metadata and controls
187 lines (127 loc) · 4 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
#include <GL/gl.h>
#include <GL/freeglut.h>
#include <stdio.h>
int x0,y0;
float res_x = 0;
float res_y = 0;
int index = 0;
float array[6];
int canviar_color = 0;
int dibuixar_triangle = 0;
//Inici array coordenades
void iniciar_array(){
array[0] = -0.5;
array[1] = -0.3;
array[2] = 0.5;
array[3] = -0.3;
array[4] = 0.0;
array[5] = 0.6;
}
//Cas en que arrastrem el ratolí
void onMotion(int x, int y) {
if(canviar_color == 1){
x0 = x; y0 = y;
glutPostRedisplay();
}
}
//Captura clicks mouse per dibuixar el nou triangle
void mouse(int button,int state,int x, int y){
//Si hem pres la tecla 't' i hem clicat el botó esquerre del ratolí
if(dibuixar_triangle == 1){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
//Agafem les mesures
res_x = (float)glutGet(GLUT_WINDOW_WIDTH);
res_y = (float)glutGet(GLUT_WINDOW_HEIGHT);
//Calculem les noves coordenades dels vèrtexs
float coord_x = ((float)x/(res_x/2)) - 1;
float coord_y = ((float)y/(res_y/2)) - 1;
//Guardem les coordenades en un array i adjustem el signe segons el quadrant on es dibuixi el vèrtex
if(((float)x > res_x/2) && ((float)y > res_y/2)) {
array[index] = coord_x; index++;
array[index] = -1.0*coord_y; index++;
}
else if(((float)x >= res_x/2) && ((float)y <= res_y/2)) {
array[index] = coord_x; index++;
array[index] = -1.0*coord_y; index++;
}
else if(((float)x < res_x/2) && ((float)y < res_y/2)) {
array[index] = coord_x; index++;
array[index] = -1.0*coord_y; index++;
}
else if(((float)x < res_x/2) && ((float)y >= res_y/2)){
array[index] = coord_x; index++;
array[index] = -1.0*coord_y; ;index++;
}
else{
array[index] = 0; index++;
array[index] = 0; index++;
}
}
//Si ja hem omplert l'array, deixem dibuixar el triangle
if (index == 6){
//Posem l'índex a 0 perquè tornarem a omplir l'array si volem dibuixar un altre triangle
index = 0;
glutPostRedisplay();
}
}
}
void keyboard(unsigned char c, int x, int y){
//Si s'ha polsat 'ESC' sortim
if(c == 27) exit(0);
//Si s'ha polsat 'h' surt l'ajuda
else if (c == 'h'){
printf("\n");
printf("Heu desplegat l'ajuda: \n \n");
printf("-Si es reescala la finestra el dibuix no es deformarà \n");
printf("-Si s'arrossega el ratoli horitzontalment canviara el color de fons \n");
printf("-Si es prem ESC s'aturara el programa \n");
printf("-Si es prem la tecla 'h' es desplega l'ajuda \n");
printf("-Si es prem la tecla 't' es permet clicar i escollir els vertexs d'un nou triangle \n");
printf("-Si es prem la tecla 'f', es permet fer el color de fons més fosc o més clar \n \n");
}
//Si s'ha polsat 'f' aclarim o obscurim el color de fons
else if(c == 'f'){
if (canviar_color == 0) canviar_color = 1;
else if (canviar_color == 1) canviar_color = 0;
}
//Si polsem 't', amb els clicks seleccionem els nous vèrtexs
else if(c == 't'){
if (dibuixar_triangle == 0) dibuixar_triangle = 1;
else if (dibuixar_triangle == 1) dibuixar_triangle = 0;
}
}
void display(void) {
//Calculem el paràmetre que variarà el color de fons de la finestra openGL
float colorx = (float)x0/1000;
//Establim color de fons
glClearColor(colorx,colorx,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0);
glVertex3f(array[0],array[1],0.0);
glColor3f(1.0,0.0,1.0);
glVertex3f(array[2],array[3],0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(array[4],array[5],0.0);
glEnd();
glutSwapBuffers();
}
void reshape(int x, int y) {
//Calculem el nou aspect ratio i adjustem viewport
int nouorigen = (x-y)/2;
glViewport(nouorigen, 0, y, y);
}
int main (int argc, char **argv){
iniciar_array();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow("INDI: practiques openGL");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(onMotion);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}