diff options
author | Bad Diode <bd@badd10de.dev> | 2022-08-29 07:11:23 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-08-29 07:11:23 +0200 |
commit | bc552363659403d0dd77f671b7b2a1c62c5fc204 (patch) | |
tree | 9179ba0bb353ed82eb7a39516bb84cda28b6350d | |
parent | 294cc9a5b17b4a84d3fc25fb0a269b4c37cc5a9f (diff) | |
download | ogl-monotext-bc552363659403d0dd77f671b7b2a1c62c5fc204.tar.gz ogl-monotext-bc552363659403d0dd77f671b7b2a1c62c5fc204.zip |
Add texture subsetting for texture atlas
Note that in the shader N is the number of elements within the texture
and idx is the index of the element we want to submit (starting at 0).
-rw-r--r-- | src/main.c | 26 |
1 files changed, 15 insertions, 11 deletions
@@ -179,7 +179,6 @@ render(Context *ctx) { | |||
179 | glfwGetFramebufferSize(ctx->window, &ctx->win_width, &ctx->win_height); | 179 | glfwGetFramebufferSize(ctx->window, &ctx->win_width, &ctx->win_height); |
180 | glViewport(0, 0, ctx->win_width, ctx->win_height); | 180 | glViewport(0, 0, ctx->win_width, ctx->win_height); |
181 | 181 | ||
182 | |||
183 | // TODO: Only need to be done once on the initialization function. | 182 | // TODO: Only need to be done once on the initialization function. |
184 | // Prepare quad rendering. | 183 | // Prepare quad rendering. |
185 | f32 vertices[] = { | 184 | f32 vertices[] = { |
@@ -217,19 +216,19 @@ render(Context *ctx) { | |||
217 | 216 | ||
218 | // Texture atlas. | 217 | // Texture atlas. |
219 | u8 texture[] = { | 218 | u8 texture[] = { |
220 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 219 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
221 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 220 | 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, |
222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 221 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
223 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 222 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
224 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 223 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
225 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 224 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
226 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 225 | 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, |
227 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | 226 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
228 | }; | 227 | }; |
229 | u32 tex_id; | 228 | u32 tex_id; |
230 | glGenTextures(1, &tex_id); | 229 | glGenTextures(1, &tex_id); |
231 | glBindTexture(GL_TEXTURE_2D, tex_id); | 230 | glBindTexture(GL_TEXTURE_2D, tex_id); |
232 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 16, 8, 0, GL_RED, | 231 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 8 * 3, 8, 0, GL_RED, |
233 | GL_UNSIGNED_BYTE, &texture); | 232 | GL_UNSIGNED_BYTE, &texture); |
234 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 233 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 234 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
@@ -244,10 +243,15 @@ render(Context *ctx) { | |||
244 | "#version 330 core\n" | 243 | "#version 330 core\n" |
245 | "layout (location = 0) in vec2 position;\n" | 244 | "layout (location = 0) in vec2 position;\n" |
246 | "layout (location = 1) in vec2 tex_coords;\n" | 245 | "layout (location = 1) in vec2 tex_coords;\n" |
246 | // TODO: layout (location = 2) in u32 index; | ||
247 | "out vec2 tex;\n" | 247 | "out vec2 tex;\n" |
248 | "void main() {\n" | 248 | "void main() {\n" |
249 | " float N = 3;\n" | ||
250 | " float idx = 2;\n" | ||
251 | " float m = 1.0 / N;\n" | ||
252 | " float k = m * idx;\n" | ||
249 | " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" | 253 | " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" |
250 | " tex = tex_coords;\n" | 254 | " tex = tex_coords * vec2(m, 1.0) + vec2(k, 0.0);\n" |
251 | "}"; | 255 | "}"; |
252 | 256 | ||
253 | // Fragment shader. | 257 | // Fragment shader. |