summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-08-29 07:11:23 +0200
committerBad Diode <bd@badd10de.dev>2022-08-29 07:11:23 +0200
commit6218601074e7070ccd5ded69a79f313d83a17cf6 (patch)
tree2add001a9c447db2879e148cbea3acd71e08a460
parentbc552363659403d0dd77f671b7b2a1c62c5fc204 (diff)
downloadogl-monotext-6218601074e7070ccd5ded69a79f313d83a17cf6.tar.gz
ogl-monotext-6218601074e7070ccd5ded69a79f313d83a17cf6.zip
Add initial working example of instanced text drawing
-rw-r--r--src/main.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 7241409..2a901a3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -208,6 +208,26 @@ render(Context *ctx) {
208 glEnableVertexAttribArray(1); 208 glEnableVertexAttribArray(1);
209 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)(2 * sizeof(f32))); 209 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)(2 * sizeof(f32)));
210 210
211 // NOTE: This should be a dynamic array of offsets and index into the
212 // texture atlas.
213 struct Letter {
214 f32 x;
215 f32 y;
216 f32 idx;
217 };
218 struct Letter letters[] = {
219 {-0.25, 0.0, 0},
220 {0.0, 0.0, 1},
221 {0.25, 0.0, 2},
222 };
223 u32 offset_vbo;
224 glGenBuffers(1, &offset_vbo);
225 glBindBuffer(GL_ARRAY_BUFFER, offset_vbo);
226 glBufferData(GL_ARRAY_BUFFER, sizeof(letters), letters, GL_STATIC_DRAW);
227 glEnableVertexAttribArray(2);
228 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(f32), (void*)0);
229 glVertexAttribDivisor(2, 1);
230
211 // Element buffer object setup. 231 // Element buffer object setup.
212 u32 ebo; 232 u32 ebo;
213 glGenBuffers(1, &ebo); 233 glGenBuffers(1, &ebo);
@@ -243,14 +263,14 @@ render(Context *ctx) {
243"#version 330 core\n" 263"#version 330 core\n"
244"layout (location = 0) in vec2 position;\n" 264"layout (location = 0) in vec2 position;\n"
245"layout (location = 1) in vec2 tex_coords;\n" 265"layout (location = 1) in vec2 tex_coords;\n"
246// TODO: layout (location = 2) in u32 index; 266"layout (location = 2) in vec3 offset;\n"
247"out vec2 tex;\n" 267"out vec2 tex;\n"
248"void main() {\n" 268"void main() {\n"
269" float idx = offset.z;\n"
249" float N = 3;\n" 270" float N = 3;\n"
250" float idx = 2;\n"
251" float m = 1.0 / N;\n" 271" float m = 1.0 / N;\n"
252" float k = m * idx;\n" 272" float k = m * idx;\n"
253" gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" 273" gl_Position = vec4(position / 8 + offset.xy, 0.0, 1.0);\n"
254" tex = tex_coords * vec2(m, 1.0) + vec2(k, 0.0);\n" 274" tex = tex_coords * vec2(m, 1.0) + vec2(k, 0.0);\n"
255"}"; 275"}";
256 276
@@ -288,7 +308,9 @@ render(Context *ctx) {
288 // // glBlendEquation(GL_FUNC_ADD); 308 // // glBlendEquation(GL_FUNC_ADD);
289 // 309 //
290 // glBindTexture(GL_TEXTURE_2D, text_tex); // TODO: Bind the texture atlas. 310 // glBindTexture(GL_TEXTURE_2D, text_tex); // TODO: Bind the texture atlas.
291 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 311 // TODO: Send the number of instances as a uniform or see if you can query
312 // it from the GLSL for N. In this case N == 3.
313 glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, 3);
292 // glDisable(GL_BLEND); 314 // glDisable(GL_BLEND);
293 315
294 glfwSwapBuffers(ctx->window); 316 glfwSwapBuffers(ctx->window);