aboutsummaryrefslogtreecommitdiffstats
path: root/gltest.c
blob: e40ee0f921fea086fec5205939c15735974fea5e (plain)
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
/*
 * 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 "linmath.h"
#include "loadgl.h"
#include "shaders.h"

enum {
	WIDTH = 800,
	HEIGHT = 600,
	LOGSIZE = 1024,
};

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);
}

GLuint load_shader(GLenum type, const char *data, GLint size)
{
	GLuint shdr;
	GLint success;
	char log[LOGSIZE];

	shdr = gl_shdr_create(type);
	gl_shdr_source(shdr, 1, &data, &size);
	gl_shdr_compile(shdr);
	gl_shdr_param(shdr, GL_COMPILE_STATUS, &success);

	if (!success) {
		gl_shdr_infolog(shdr, sizeof log, NULL, log);
		eprintf("Failed to compile shader:\n%s", log);
	}

	return shdr;
}

GLuint load_prog(void)
{
	GLuint vert, frag, prog;
	GLuint success;
	char log[LOGSIZE];

	vert = load_shader(GL_VERTEX_SHADER, shader_vert_data, shader_vert_size);
	frag = load_shader(GL_FRAGMENT_SHADER, shader_frag_data, shader_frag_size);
	prog = gl_prog_create();
	gl_prog_attachshdr(prog, vert);
	gl_prog_attachshdr(prog, frag);
	gl_prog_link(prog);
	gl_prog_param(prog, GL_COMPILE_STATUS, &success);
	if (!success) {
		gl_prog_infolog(prog, sizeof log, NULL, log);
		eprintf("Failed to link program:\n%s", log);
	}
	gl_shdr_del(vert);
	gl_shdr_del(frag);

	return prog;
}

int main(int argc, char **argv)
{
	GLFWwindow *win;
	int ret;
	GLuint ebo, vbo, vao, prog;
	vec3 verts[] = {
		{  0.5f,  0.5f, 0.0f },
		{  0.5f, -0.5f, 0.0f },
		{ -0.5f, -0.5f, 0.0f },
		{ -0.5f,  0.5f, 0.0f },
	};
	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, 3, GL_FLOAT, GL_FALSE, sizeof *verts, 0);
	gl_va_enable(0);

	prog = load_prog();

	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();
}