summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-04 19:04:04 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-04 19:04:43 -0500
commita04565b7796fdfcff0b652644a0983145abbba87 (patch)
tree45d5ce88fe1a9915632211d765d07e3c83ce0166
parent9c96427ba61f17d4cb91235bef2a77483a0278bf (diff)
small modern gl geometry api
-rw-r--r--gl.c85
-rw-r--r--main.c78
-rw-r--r--math.c14
3 files changed, 148 insertions, 29 deletions
diff --git a/gl.c b/gl.c
index 023b852..41e29e5 100644
--- a/gl.c
+++ b/gl.c
@@ -26,6 +26,7 @@
do(ENABLEVERTEXATTRIBARRAY, EnableVertexAttribArray)\
do(DISABLEVERTEXATTRIBARRAY, DisableVertexAttribArray)\
do(GENVERTEXARRAYS, GenVertexArrays)\
+ do(DELETEVERTEXARRAYS, DeleteVertexArrays)\
do(BINDVERTEXARRAY, BindVertexArray)\
do(UNIFORM1F, Uniform1f)\
do(UNIFORM2F, Uniform2f)\
@@ -125,3 +126,87 @@ static GLint gl_uniform_loc(GLuint program, char const *uniform) {
return loc;
}
+typedef struct {
+ v2 pos;
+ v4 color;
+} GLSimpleVertex;
+typedef struct {
+ GLSimpleVertex v1, v2, v3;
+} GLSimpleTriangle;
+
+static GLSimpleTriangle *gl_geometry_triangles;
+static GLuint gl_geometry_program;
+static GLuint gl_geometry_v_pos;
+static GLuint gl_geometry_v_color;
+static GLint gl_geometry_u_window_size;
+static GLuint gl_geometry_vbo, gl_geometry_vao;
+
+static void gl_geometry_init(void) {
+ char const *vshader_code = "#version 110\n\
+ attribute vec2 v_pos;\n\
+ attribute vec4 v_color;\n\
+ varying vec4 color;\n\
+ uniform vec2 window_size;\n\
+ void main() {\n\
+ float x = v_pos.x / window_size.x * 2.0 - 1.0;\n\
+ float y = 1.0 - v_pos.y / window_size.y * 2.0;\n\
+ gl_Position = vec4(x, y, 0.0, 1.0);\n\
+ color = v_color;\n\
+ }\n\
+ ";
+ char const *fshader_code = "#version 110\n\
+ varying vec4 color;\n\
+ void main() {\n\
+ gl_FragColor = color;\n\
+ }\n\
+ ";
+
+ gl_geometry_program = gl_compile_and_link_shaders(vshader_code, fshader_code);
+ gl_geometry_v_pos = gl_attrib_loc(gl_geometry_program, "v_pos");
+ gl_geometry_v_color = gl_attrib_loc(gl_geometry_program, "v_color");
+ gl_geometry_u_window_size = gl_uniform_loc(gl_geometry_program, "window_size");
+
+ glGenBuffers(1, &gl_geometry_vbo);
+ if (gl_version_major >= 3)
+ glGenVertexArrays(1, &gl_geometry_vao);
+}
+
+static void gl_geometry_rect(Rect r, u32 color_rgba) {
+ v4 color = rgba_u32_to_v4(color_rgba);
+
+ v2 p1 = r.pos;
+ v2 p2 = v2_add(r.pos, V2(0, r.size.y));
+ v2 p3 = v2_add(r.pos, V2(r.size.x, r.size.y));
+ v2 p4 = p3;
+ v2 p5 = v2_add(r.pos, V2(r.size.x, 0));
+ v2 p6 = p1;
+ GLSimpleTriangle triangle = {
+ {p1, color},
+ {p2, color},
+ {p3, color}
+ };
+ arr_add(gl_geometry_triangles, triangle);
+ triangle.v1.pos = p4;
+ triangle.v2.pos = p5;
+ triangle.v3.pos = p6;
+ arr_add(gl_geometry_triangles, triangle);
+}
+
+static void gl_geometry_draw(Ted *ted) {
+ size_t ntriangles = arr_len(gl_geometry_triangles);
+
+ if (gl_version_major >= 3)
+ glBindVertexArray(gl_geometry_vao);
+ glBindBuffer(GL_ARRAY_BUFFER, gl_geometry_vbo);
+ glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(ntriangles * sizeof(GLSimpleTriangle)), gl_geometry_triangles, GL_STREAM_DRAW);
+ glVertexAttribPointer(gl_geometry_v_pos, 2, GL_FLOAT, 0, sizeof(GLSimpleVertex), (void *)offsetof(GLSimpleVertex, pos));
+ glEnableVertexAttribArray(gl_geometry_v_pos);
+ glVertexAttribPointer(gl_geometry_v_color, 4, GL_FLOAT, 0, sizeof(GLSimpleVertex), (void *)offsetof(GLSimpleVertex, color));
+ glEnableVertexAttribArray(gl_geometry_v_color);
+
+ glUseProgram(gl_geometry_program);
+ glUniform2f(gl_geometry_u_window_size, ted->window_width, ted->window_height);
+ glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(3 * ntriangles));
+
+ arr_clear(gl_geometry_triangles);
+}
diff --git a/main.c b/main.c
index 4e1ce89..49f4730 100644
--- a/main.c
+++ b/main.c
@@ -19,13 +19,10 @@ no_warn_end
#if _WIN32
#include <shellapi.h>
#endif
-#include "gl.c"
+
#include "unicode.h"
-#include "text.c"
#include "util.c"
-#define MATH_GL
-#include "math.c"
#if _WIN32
#include "filesystem-win.c"
#elif __unix__
@@ -33,14 +30,20 @@ no_warn_end
#else
#error "Unrecognized operating system."
#endif
+#include "arr.c"
+#define MATH_GL
+#include "math.c"
+#include "text.h"
#include "command.h"
#include "colors.h"
#include "time.c"
#include "ted.h"
+#include "gl.c"
+#include "text.c"
+
#include "string32.c"
-#include "arr.c"
#include "syntax.c"
#include "buffer.c"
#include "ted.c"
@@ -278,32 +281,34 @@ int main(int argc, char **argv) {
}
}
#endif
-
+
+ gl_geometry_init();
+
SDL_GL_SetSwapInterval(1); // vsync
-
- char const *vshader_code = "#version 110\n\
-attribute vec2 v_pos;\n\
-attribute vec4 v_color;\n\
-varying vec4 color;\n\
-uniform vec2 window_size;\n\
-void main() {\n\
- float x = v_pos.x / window_size.x * 2.0 - 1.0;\n\
- float y = 1.0 - v_pos.y / window_size.y * 2.0;\n\
- gl_Position = vec4(x, y, 0.0, 1.0);\n\
- color = v_color;\n\
-}\n\
-";
- char const *fshader_code = "#version 110\n\
-varying vec4 color;\n\
-void main() {\n\
- gl_FragColor = color;\n\
-}\n\
-";
+ #if 0
float vertices[][6] = {
{0, 0, 1, 0, 0, 1},
{50, 0, 0, 1, 0, 1},
{0, 50, 0, 0, 1, 1},
};
+ char const *vshader_code = "#version 110\n\
+ attribute vec2 v_pos;\n\
+ attribute vec4 v_color;\n\
+ varying vec4 color;\n\
+ uniform vec2 window_size;\n\
+ void main() {\n\
+ float x = v_pos.x / window_size.x * 2.0 - 1.0;\n\
+ float y = 1.0 - v_pos.y / window_size.y * 2.0;\n\
+ gl_Position = vec4(x, y, 0.0, 1.0);\n\
+ color = v_color;\n\
+ }\n\
+ ";
+ char const *fshader_code = "#version 110\n\
+ varying vec4 color;\n\
+ void main() {\n\
+ gl_FragColor = color;\n\
+ }\n\
+ ";
GLuint program = gl_compile_and_link_shaders(vshader_code, fshader_code);
GLuint v_pos = gl_attrib_loc(program, "v_pos");
GLuint v_color = gl_attrib_loc(program, "v_color");
@@ -341,6 +346,29 @@ void main() {\n\
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
}
+ #endif
+
+ while (1) {
+ int w, h;
+ SDL_GetWindowSize(window, &w, &h);
+ ted->window_width = (float)w;
+ ted->window_height = (float)h;
+
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) return 0;
+
+ glViewport(0, 0, w, h);
+ glClearColor(0,0,0,1);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ for (int i = 0; i < 20; ++i) {
+ gl_geometry_rect(rect(V2(0, 10*(float)i), V2(100, 100)), 0xffffffff >> i);
+
+ gl_geometry_draw(ted);
+ }
+
+ SDL_GL_SwapWindow(window);
+ }
return 0;
diff --git a/math.c b/math.c
index 1443267..00735cf 100644
--- a/math.c
+++ b/math.c
@@ -772,10 +772,16 @@ static void gl_color2f(float v, float a) {
}
static void rgba_u32_to_floats(u32 rgba, float floats[4]) {
- floats[0] = (float)(rgba >> 24) / 255.f;
- floats[1] = (float)(rgba >> 16) / 255.f;
- floats[2] = (float)(rgba >> 8) / 255.f;
- floats[3] = (float)(rgba >> 0) / 255.f;
+ floats[0] = (float)((rgba >> 24) & 0xFF) / 255.f;
+ floats[1] = (float)((rgba >> 16) & 0xFF) / 255.f;
+ floats[2] = (float)((rgba >> 8) & 0xFF) / 255.f;
+ floats[3] = (float)((rgba >> 0) & 0xFF) / 255.f;
+}
+
+static v4 rgba_u32_to_v4(u32 rgba) {
+ float c[4];
+ rgba_u32_to_floats(rgba, c);
+ return V4(c[0], c[1], c[2], c[3]);
}
// color is 0xRRGGBBAA