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
|
/*
* Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com>
* SPDX-License-Identifier: MIT
*/
#include <GLFW/glfw3.h>
#include <stdbool.h>
#include "eprintf.h"
#include "gldefs.h"
#include "glprog.h"
#include "linmath.h"
#include "loadgl.h"
#include "shaders.h"
#include "nelem.h"
enum {
WIDTH = 800,
HEIGHT = 600,
};
void fb_size_cb(GLFWwindow *win, int width, int height)
{
(void)win;
gl_viewport(0, 0, width, height);
}
void take_input(GLFWwindow *win)
{
if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(win, true);
}
int main(int argc, char **argv)
{
GLFWwindow *win;
int ret;
GLuint ebo, vbo, vao, prog;
GLint ucolor;
struct vertex {
vec3 pos;
vec3 col;
vec2 uv;
} verts[] = {
{{ 0.5, 0.5, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, 1.0 }},
{{ 0.5, -0.5, 0.0 }, { 0.0, 1.0, 0.0 }, { 1.0, 0.0 }},
{{ -0.5, -0.5, 0.0 }, { 0.0, 0.0, 1.0 }, { 0.0, 0.0 }},
{{ -0.5, 0.5, 0.0 }, { 1.0, 1.0, 0.0 }, { 0.0, 1.0 }},
};
GLuint idxs[] = {
0, 1, 3,
1, 2, 3,
};
setprogname(argc >= 1 && argv[0] != NULL ? argv[0] : "gltest");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
win = glfwCreateWindow(WIDTH, HEIGHT, "gltest", NULL, NULL);
if (win == NULL)
eprintf("Failed to create GLFW window");
glfwMakeContextCurrent(win);
ret = lgl_load((lgl_loadfunc *)glfwGetProcAddress);
if (ret != 0)
eprintf("Failed to load GL: %s", lgl_strerror(ret));
glfwSetFramebufferSizeCallback(win, &fb_size_cb);
fb_size_cb(win, WIDTH, HEIGHT);
gl_buf_gen(1, &vbo);
gl_buf_gen(1, &ebo);
gl_va_gen(1, &vao);
gl_va_bind(vao);
gl_buf_bind(GL_ARRAY_BUFFER, vbo);
gl_buf_data(GL_ARRAY_BUFFER, sizeof verts, verts, GL_STATIC_DRAW);
gl_buf_bind(GL_ELEMENT_ARRAY_BUFFER, ebo);
gl_buf_data(GL_ELEMENT_ARRAY_BUFFER, sizeof idxs, idxs, GL_STATIC_DRAW);
gl_va_define(0, NELEM(verts[0].pos), GL_FLOAT, GL_FALSE, sizeof *verts, (void *)offsetof(struct vertex, pos));
gl_va_enable(0);
gl_va_define(1, NELEM(verts[0].col), GL_FLOAT, GL_FALSE, sizeof *verts, (void *)offsetof(struct vertex, col));
gl_va_enable(1);
gl_va_define(2, NELEM(verts[0].uv), GL_FLOAT, GL_FALSE, sizeof *verts, (void *)offsetof(struct vertex, uv));
gl_va_enable(2);
prog = glprog_load(2, (struct shdrdat []){
{ GL_VERTEX_SHADER, shader_vert_data, shader_vert_size },
{ GL_FRAGMENT_SHADER, shader_frag_data, shader_frag_size },
},
1, &(struct unidat){ "ucolor", &ucolor });
gl_poly_mode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(win)) {
take_input(win);
gl_clearcolor(0.2, 0.3, 0.3, 1.0);
gl_clear(GL_COLOR_BUFFER_BIT);
gl_prog_use(prog);
gl_va_bind(vao);
gl_draw_elems(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
gl_va_bind(0);
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
}
|