/* * Copyright (C) 2018 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ #include #include #include "assets.h" #include "eprintf.h" #include "gldefs.h" #include "glprog.h" #include "linmath.h" #include "loadgl.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, vert_glsl.data, vert_glsl.size }, { GL_FRAGMENT_SHADER, frag_glsl.data, frag_glsl.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(); }