-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
224 lines (153 loc) · 6.27 KB
/
main.c
File metadata and controls
224 lines (153 loc) · 6.27 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
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
/* para linux, instalar os pacotes libglfw3-dev mesa-common-dev libglew-dev */
/* para compilar no linux: gcc main.c -lglfw -lGL -lGLEW -lm */
/* para windows, instalar bibliotecas compiladas do glfw3 e glew no ambiente mingw */
/* para compilar no windows: gcc main.c -lglfw3dll -lglew32 -lopengl32 */
#include <GL/glew.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> /* verifique no seu SO onde fica o glfw3.h */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
// variaveis globais
typedef struct{
float x, y;
} coordenadas;
float t_x = 0.0f;
float t_y = 0.0f;
float theta = 0.0f;
// funcao para processar eventos de teclado
static void key_event(GLFWwindow* window, int key, int scancode, int action, int mods){
printf("Pressionando tecla %d\n", key);
if(key==262) t_x += 0.01; // tecla para direita
if(key==263) t_x -= 0.01; // tecla para direita
if(key==265) t_y += 0.01; // tecla para cima
if(key==264) t_y -= 0.01; // tecla para baixo
if(key == 81) theta += 1.0f;
if(key == 69) theta -= 1.0f;
}
int main(void){
// inicicializando o sistema de\ janelas
glfwInit();
// deixando a janela invisivel, por enquanto
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
// criando uma janela
GLFWwindow* window = glfwCreateWindow(800, 800, "Minha Janela", NULL, NULL);
// tornando a janela como principal
glfwMakeContextCurrent(window);
// inicializando Glew (para lidar com funcoes OpenGL)
GLint GlewInitResult = glewInit();
printf("GlewStatus: %s", glewGetErrorString(GlewInitResult));
// GLSL para Vertex Shader
char* vertex_code =
"attribute vec2 position;\n"
"uniform mat4 mat_transformation;\n"
"void main()\n"
"{\n"
" gl_Position = mat_transformation*vec4(position, 0.0, 1.0);\n"
"}\n";
// GLSL para Fragment Shader
char* fragment_code =
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
// Requisitando slot para a GPU para nossos programas Vertex e Fragment Shaders
GLuint program = glCreateProgram();
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
// Associando nosso código-fonte GLSL aos slots solicitados
glShaderSource(vertex, 1, &vertex_code, NULL);
glShaderSource(fragment, 1, &fragment_code, NULL);
// Compilando o Vertex Shader e verificando erros
glCompileShader(vertex);
GLint isCompiled = 0;
glGetShaderiv(vertex, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE){
//descobrindo o tamanho do log de erro
int infoLength = 512;
glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &infoLength);
//recuperando o log de erro e imprimindo na tela
char info[infoLength];
glGetShaderInfoLog(vertex, infoLength, NULL, info);
printf("Erro de compilacao no Vertex Shader.\n");
printf("--> %s\n",&info);
}
// Compilando o Fragment Shader e verificando erros
glCompileShader(fragment);
isCompiled = 0;
glGetShaderiv(fragment, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE){
//descobrindo o tamanho do log de erro
int infoLength = 512;
glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &infoLength);
//recuperando o log de erro e imprimindo na tela
char info[infoLength];
glGetShaderInfoLog(fragment, infoLength, NULL, info);
printf("Erro de compilacao no Fragment Shader.\n");
printf("--> %s\n",&info);
}
// Associando os programas compilado ao programa principal
glAttachShader(program, vertex);
glAttachShader(program, fragment);
// Linkagem do programa e definindo como default
glLinkProgram(program);
glUseProgram(program);
// Preparando dados para enviar a GPU
coordenadas vertices[6] = { // criando tres vertices e preenchendo
{ 0.0f, +0.5f },
{ -0.5f, -0.5f },
{ +0.5f, -0.5f },
{ 0.0f, -0.5f },
{ -0.5f, +0.5f },
{ +0.5f, +0.5f }
};
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// Abaixo, nós enviamos todo o conteúdo da variável vertices.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// Associando variáveis do programa GLSL (Vertex Shaders) com nossos dados
GLint loc = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) 0); // https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml
// Associando nossa janela com eventos de teclado
glfwSetKeyCallback(window, key_event); // teclado
// Exibindo nossa janela
glfwShowWindow(window);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
// criando a matriz de translacao
float mat_translation[16] = {
1.0f, 0.0f, 0.0f, t_x ,
0.0f, 1.0f, 0.0f, t_y ,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// enviando a matriz de transformacao para a GPU, vertex shader, variavel mat_transformation
loc = glGetUniformLocation(program, "mat_transformation");
glUniformMatrix4fv(loc, 1, GL_TRUE, mat_translation);
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_DYNAMIC_DRAW);
float c = cos( ((theta) * M_PI / 180.0) ); // cos considerando conversao para radianos
float s = sin( ((theta) * M_PI / 180.0) );
float mat_rotation[16] = {
c , -s , 0.0f, 0.0f,
s , c , 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
// loc = glGetUniformLocation(program, "mat_transformation");
glUniformMatrix4fv(loc, 1, GL_TRUE, mat_rotation);
//renderizando
glDrawArrays(GL_TRIANGLES, 3, 5);
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}