This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
241 lines (208 loc) · 5.67 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
/**
* @author Gustavo Vicente Dauer <[email protected]>.
* @name main.cpp
* @date 17/05/2020
*/
#include <GLFW/glfw3.h>
#define SPEED 0.015f
#define COLOR_VISIBLE 100
#define COLOR_NOT_VISIBLE 0
#define RESIZE_Y 0.019f
#define RESIZE_X 0.01f
float topVertexPositionY = 0.5;
float topVertexPositionX = 0.0;
float leftVertexPositionY = -0.5;
float leftVertexPositionX = -0.5;
float rightVertexPositionY = -0.5;
float rightVertexPositionX = 0.5;
float redColorStrength = COLOR_VISIBLE;
float blueColorStrength = COLOR_VISIBLE;
float greenColorStrength = COLOR_VISIBLE;
/**
* Move triangle to left direction.
*
* @return void
*/
void moveTriangleLeftDirection()
{
topVertexPositionX = topVertexPositionX - SPEED;
leftVertexPositionX = leftVertexPositionX - SPEED;
rightVertexPositionX = rightVertexPositionX - SPEED;
}
/**
* Move triangle to right direction.
*
* @return void
*/
void moveTriangleRightDirection()
{
topVertexPositionX = topVertexPositionX + SPEED;
leftVertexPositionX = leftVertexPositionX + SPEED;
rightVertexPositionX = rightVertexPositionX + SPEED;
}
/**
* Move triangle to up direction.
*
* @return void
*/
void moveTriangleUpDirection()
{
topVertexPositionY = topVertexPositionY + SPEED;
leftVertexPositionY = leftVertexPositionY + SPEED;
rightVertexPositionY = rightVertexPositionY + SPEED;
}
/**
* Move triangle to down direction.
*
* @return void
*/
void moveTriangleDownDirection()
{
topVertexPositionY = topVertexPositionY - SPEED;
leftVertexPositionY = leftVertexPositionY - SPEED;
rightVertexPositionY = rightVertexPositionY - SPEED;
}
/**
* Change triangle color to white.
*
* @return void
*/
void paintTriangleWithWhiteColor()
{
redColorStrength = COLOR_VISIBLE;
blueColorStrength = COLOR_VISIBLE;
greenColorStrength = COLOR_VISIBLE;
}
/**
* Change triangle color to red.
*
* @return void
*/
void paintTriangleWithRedColor()
{
redColorStrength = COLOR_VISIBLE;
blueColorStrength = COLOR_NOT_VISIBLE;
greenColorStrength = COLOR_NOT_VISIBLE;
}
/**
* Change triangle color to green.
*
* @return void
*/
void paintTriangleWithGreenColor()
{
redColorStrength = COLOR_NOT_VISIBLE;
blueColorStrength = COLOR_NOT_VISIBLE;
greenColorStrength = COLOR_VISIBLE;
}
/**
* Change triangle color to blue.
*
* @return void
*/
void paintTriangleWithBlueColor()
{
redColorStrength = COLOR_NOT_VISIBLE;
blueColorStrength = COLOR_VISIBLE;
greenColorStrength = COLOR_NOT_VISIBLE;
}
/**
* Shrink triangle size.
*
* @return void
*/
void shrinkTriangleSize()
{
topVertexPositionY = topVertexPositionY - RESIZE_Y;
leftVertexPositionX = leftVertexPositionX + RESIZE_X;
rightVertexPositionX = rightVertexPositionX - RESIZE_X;
}
/**
* Increase triangle size.
*
* @return void
*/
void increaseTriangleSize()
{
topVertexPositionY = topVertexPositionY + RESIZE_Y;
leftVertexPositionX = leftVertexPositionX - RESIZE_X;
rightVertexPositionX = rightVertexPositionX + RESIZE_X;
}
/**
* Triangle control callback function.
*
* Control the triangle form based on event poll from glfw.
*
* @param GLFWwindow* window
* @param int key
* @param int scanCode
* @param int action
* @param int mods
*/
void triangleControl(GLFWwindow* window, int key, int scanCode, int action, int mods)
{
if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) {
moveTriangleLeftDirection();
} else if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
moveTriangleRightDirection();
} else if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
moveTriangleUpDirection();
} else if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) {
moveTriangleDownDirection();
} else if (key == GLFW_KEY_W && action == GLFW_PRESS) {
paintTriangleWithWhiteColor();
} else if (key == GLFW_KEY_R && action == GLFW_PRESS) {
paintTriangleWithRedColor();
} else if (key == GLFW_KEY_G && action == GLFW_PRESS) {
paintTriangleWithGreenColor();
} else if (key == GLFW_KEY_B && action == GLFW_PRESS) {
paintTriangleWithBlueColor();
} else if ((key == GLFW_KEY_MINUS || key == GLFW_KEY_KP_SUBTRACT) && action == GLFW_PRESS) {
shrinkTriangleSize();
} else if (key == GLFW_KEY_KP_ADD && action == GLFW_PRESS) {
increaseTriangleSize();
}
}
/**
* Main function.
*
* @return int
*/
int main() {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit()) {
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(800, 600, "FMU - OpenGL implementation", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Clear Background buffer */
glClear(GL_COLOR_BUFFER_BIT);
/* Begin triangle form */
glBegin(GL_TRIANGLES);
/* Triangle RGB values */
glColor3b(redColorStrength, greenColorStrength, blueColorStrength);
/* Vertex creation */
glVertex2f(topVertexPositionX, topVertexPositionY); // top vertex
glVertex2f(leftVertexPositionX, leftVertexPositionY); // left vertex
glVertex2f(rightVertexPositionX, rightVertexPositionY); // right vertex
/* Listen to events */
glfwSetKeyCallback(window, triangleControl);
glEnd();
glFlush();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}