From 6b65556ef11e9acf79bbecdac8d205a973d5dc9b Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 27 Mar 2018 19:06:07 +0100 Subject: Add more vertex attributes (use a struct) --- frag.glsl | 3 ++- gltest.c | 21 +++++++++++++++------ nelem.h | 6 ++++++ vert.glsl | 5 +++++ 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 nelem.h diff --git a/frag.glsl b/frag.glsl index aa5ec0f..c48ee57 100644 --- a/frag.glsl +++ b/frag.glsl @@ -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); } diff --git a/gltest.c b/gltest.c index cb43383..5b1d4fc 100644 --- a/gltest.c +++ b/gltest.c @@ -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 }, diff --git a/nelem.h b/nelem.h new file mode 100644 index 0000000..4d9b96e --- /dev/null +++ b/nelem.h @@ -0,0 +1,6 @@ +#ifndef NELEM_H +#define NELEM_H + +#define NELEM(a) (sizeof (a) / sizeof (a)[0]) + +#endif // NELEM_H diff --git a/vert.glsl b/vert.glsl index 1730d35..2ee78d0 100644 --- a/vert.glsl +++ b/vert.glsl @@ -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; } -- cgit v1.2.3-54-g00ecf