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
|
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "shader.h"
#include "die.h"
#include "config.h"
#include "hello_vert.h"
#include "hello_frag.h"
void prepare_tri() {
const float vertices[] = {
-1, -1, 0,
3, -1, 0,
-1, 3, 0,
};
// initialize vertex {buffer,attribute} object buffers
GLuint VBO, VAO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// copy vertex data into VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// set VAO pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// draw (only) this triangle
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(VAO);
}
int main(int argc, char** argv) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// force floating window w/ i3
glfwWindowHint(GLFW_FLOATING, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
// initialize window
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "vis", NULL, NULL);
if (window == NULL)
die("error: could not create window\n");
glfwMakeContextCurrent(window);
// initialize GLEW
glewInit();
// create single triangle
prepare_tri();
// prepare shaders
GLuint shader = link_shaders(
vert_shader(hello_vert, hello_vert_size),
frag_shader(hello_frag, hello_frag_size)
);
// main draw loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glUseProgram(shader);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
}
return EXIT_SUCCESS;
}
|