aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2018-03-27 19:06:07 +0100
committerTomasz Kramkowski <tk@the-tk.com>2018-03-27 19:21:57 +0100
commit6b65556ef11e9acf79bbecdac8d205a973d5dc9b (patch)
tree146e5d1773dd8f5ffef77e4574b26496aee6a2ad
parent4329c4e47c43dd34a106b5c8158f06728780629a (diff)
downloadfaqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.tar.gz
faqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.tar.xz
faqe-6b65556ef11e9acf79bbecdac8d205a973d5dc9b.zip
Add more vertex attributes (use a struct)
-rw-r--r--frag.glsl3
-rw-r--r--gltest.c21
-rw-r--r--nelem.h6
-rw-r--r--vert.glsl5
4 files changed, 28 insertions, 7 deletions
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;
}