examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 8968680f2b50176103d4422573d64478cf5994d4
parent c00d0b4d3c6d0a3eed86310c265f620c4a883264
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Fri, 22 Sep 2023 21:23:24 +0100

gl*: Small improvements

Diffstat:
MTupfile | 2+-
Msrc/gl-3d.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/gl.c | 6+++++-
Msrc/glob.c | 8+++++++-
4 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/Tupfile b/Tupfile @@ -5,7 +5,7 @@ CXXFLAGS = -std=c++20 $(COMMON_FLAGS) LDLIBS_aio = -lrt LDLIBS_alsa-simple = -lasound -LDLIBS_gl = -lglfw -lGL +LDLIBS_gl = -lglfw -lGL -lm LDLIBS_gl-3d = -lglfw -lGL -lm LDLIBS_gl-lighting = -lglfw -lGL -lm LDLIBS_gl-asteroids = -lglfw -lGL -lm -lasound diff --git a/src/gl-3d.c b/src/gl-3d.c @@ -38,6 +38,35 @@ static void draw_octahedron() glDrawElements(GL_TRIANGLE_FAN, 6, GL_UNSIGNED_BYTE, octahedron_indices + 6); } +float const duv = 1./32; + +struct vertex_t2v3 +{ + float texcoord[2]; + float vertex[3]; +} const plane_vertices[] = +{ + {{duv,duv}, {-.5,-.5,0.5}}, + {{1-duv,duv}, { .5,-.5,0}}, + {{duv,1-duv}, {-.5, .5,0}}, + {{1-duv,1-duv}, { .5, .5,0}}, +}; + +static void draw_plane() +{ + + glEnable(GL_TEXTURE_2D); + glEnable(GL_POINT_SPRITE); + glColor3f(1,1,1); + + glInterleavedArrays(GL_V3F, sizeof(struct vertex_t2v3), plane_vertices->vertex); + glPointSize(64); + glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); + glDrawArrays(GL_POINTS, 0, 4); + glDisable(GL_POINT_SPRITE); + glDisable(GL_TEXTURE_2D); +} + static void key_cb(GLFWwindow* w, int key, int /*scancode*/, int action, int /*mods*/) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) @@ -63,8 +92,27 @@ static void fb_resize_cb(GLFWwindow*, int w, int h) set_frustum_size(w, h); } +static struct rgb { + unsigned char r,g,b; +} tex[16*16*3]; + int main(int, char* argv[]) { + struct rgb grey = {32,32,32}; + for (unsigned i = 0; i < 16*16; ++i) + { + tex[i].r = i % 16 * 17; + tex[i].g = i / 16 * 17; + tex[i].b = 255-i; + tex[i] = grey; + }; + struct rgb black = {0,0,0}; + struct rgb green = {0,255,0}; + for (unsigned i = 0; i < 16; ++i) + { + tex[i] = tex[15+i*16] = black; + tex[i*16] = tex[i+15*16] = green; + }; assert(glfwInit()); GLFWwindow* window = glfwCreateWindow(DEFAULT_W, DEFAULT_H, argv[0], NULL, NULL); assert(window); @@ -73,10 +121,15 @@ int main(int, char* argv[]) glfwSetKeyCallback(window, key_cb); glfwSetFramebufferSizeCallback(window, fb_resize_cb); glfwSwapInterval(1); - glClearColor(0, 0, 0, 1); + glClearColor(0.2, 0.2, 0.2, 1); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); + glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -114,7 +167,7 @@ int main(int, char* argv[]) { glTranslated(sin(2*turns), cos(2*turns), sin(turns)); glRotated(180+deg, 0, 0, 1); - draw_octahedron(); + draw_plane(); } glfwSwapBuffers(window); diff --git a/src/gl.c b/src/gl.c @@ -2,6 +2,7 @@ #include <GLFW/glfw3.h> #include <assert.h> +#include <math.h> static void fb_resize_cb(GLFWwindow*, int w, int h) { @@ -21,8 +22,11 @@ int main(int, char* argv[]) while (!glfwWindowShouldClose(window)) { glfwPollEvents(); + double t = glfwGetTime() / 2; + t = t - floor(t); + t *= 3; glClear(GL_COLOR_BUFFER_BIT); - glRectd(-.5,-.5, .5, .5); + glRectd(t-2,-1, t-1, 1); glfwSwapBuffers(window); } glfwTerminate(); /* Implicit window destroy */ diff --git a/src/glob.c b/src/glob.c @@ -3,6 +3,12 @@ #include <stdlib.h> #include <string.h> +int errfunc(char const* epath, int eerrno) +{ + fprintf(stderr, "glob: %s %s\n", epath, strerror(eerrno)); + return 0; +} + static int cmpstringp(const void *p1, const void *p2) { return strcmp(* (char * const *) p1, * (char * const *) p2); @@ -18,7 +24,7 @@ int main(int argc, char* argv[]) for (int i = 1; i < argc; ++i) { - glob(argv[i], flags, NULL, &results); + glob(argv[i], flags, errfunc, &results); flags |= GLOB_APPEND; }