From d44bfe02649fa818e229085e090ec8aef18f2e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Sun, 19 May 2019 21:18:30 +0200 Subject: [PATCH] Add vertex type and offsetof to OpenGL triangle --- examples/simple.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index 474a85f9..428ecd81 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -30,17 +30,20 @@ #include "linmath.h" #include +#include #include -static const struct +typedef struct Vertex { - float x, y; - float r, g, b; -} vertices[3] = + vec2 pos; + vec3 col; +} Vertex; + +static const Vertex vertices[3] = { - { -0.6f, -0.4f, 1.f, 0.f, 0.f }, - { 0.6f, -0.4f, 0.f, 1.f, 0.f }, - { 0.f, 0.6f, 0.f, 0.f, 1.f } + { { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } }, + { { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } }, + { { 0.f, 0.6f }, { 0.f, 0.f, 1.f } } }; static const char* vertex_shader_text = @@ -123,10 +126,10 @@ int main(void) glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, - sizeof(vertices[0]), (void*) 0); + sizeof(Vertex), (void*) offsetof(Vertex, pos)); glEnableVertexAttribArray(vcol_location); glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, - sizeof(vertices[0]), (void*) (sizeof(float) * 2)); + sizeof(Vertex), (void*) offsetof(Vertex, col)); while (!glfwWindowShouldClose(window)) {