/* * Copyright (C) 2018 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ #include #include #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(); }