diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-03-27 19:06:07 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-03-27 19:21:57 +0100 |
commit | 6b65556ef11e9acf79bbecdac8d205a973d5dc9b (patch) | |
tree | 146e5d1773dd8f5ffef77e4574b26496aee6a2ad | |
parent | 4329c4e47c43dd34a106b5c8158f06728780629a (diff) | |
download | faqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.tar.gz faqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.tar.xz faqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.zip |
Add more vertex attributes (use a struct)
-rw-r--r-- | frag.glsl | 3 | ||||
-rw-r--r-- | gltest.c | 21 | ||||
-rw-r--r-- | nelem.h | 6 | ||||
-rw-r--r-- | vert.glsl | 5 |
4 files changed, 28 insertions, 7 deletions
@@ -2,8 +2,9 @@ // SPDX-License-Identifier: MIT #version 330 core out vec4 color; +in vec3 vcolor; void main() { - color = vec4(1.0f, 0.5f, 0.2f, 1.0f); + color = vec4(vcolor, 1.0); } @@ -11,6 +11,7 @@ #include "linmath.h" #include "loadgl.h" #include "shaders.h" +#include "nelem.h" enum { WIDTH = 800, @@ -35,11 +36,15 @@ int main(int argc, char **argv) int ret; GLuint ebo, vbo, vao, prog; GLint ucolor; - vec3 verts[] = { - { 0.5f, 0.5f, 0.0f }, - { 0.5f, -0.5f, 0.0f }, - { -0.5f, -0.5f, 0.0f }, - { -0.5f, 0.5f, 0.0f }, + 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, @@ -77,8 +82,12 @@ int main(int argc, char **argv) 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, 3, GL_FLOAT, GL_FALSE, sizeof *verts, 0); + 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, shader_vert_data, shader_vert_size }, @@ -0,0 +1,6 @@ +#ifndef NELEM_H +#define NELEM_H + +#define NELEM(a) (sizeof (a) / sizeof (a)[0]) + +#endif // NELEM_H @@ -2,8 +2,13 @@ // SPDX-License-Identifier: MIT #version 330 core layout (location = 0) in vec3 pos; +layout (location = 1) in vec3 color; +layout (location = 2) in vec2 uv; + +out vec3 vcolor; void main() { gl_Position = vec4(pos, 1.0); + vcolor = color; } |