From 11800b6179458eb962cd1862e4053efd7f28c2f4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 4 Apr 2020 13:36:46 +0200 Subject: Shader Demo: Use a UBO --- examples/pugl_gl3_demo.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/pugl_gl3_demo.c b/examples/pugl_gl3_demo.c index 6f7ed91..7b2a49f 100644 --- a/examples/pugl_gl3_demo.c +++ b/examples/pugl_gl3_demo.c @@ -53,6 +53,11 @@ static const int defaultWidth = 512; static const int defaultHeight = 512; +typedef struct +{ + mat4 projection; +} RectUniforms; + typedef struct { PuglTestOptions opts; @@ -65,7 +70,6 @@ typedef struct GLuint vbo; GLuint instanceVbo; GLuint ibo; - GLint u_projection; unsigned framesDrawn; int quit; } PuglTestApp; @@ -112,13 +116,12 @@ onExpose(PuglView* view) glUseProgram(app->drawRect.program); glBindVertexArray(app->vao); - // Set projection matrix uniform - glUniformMatrix4fv(app->u_projection, 1, GL_FALSE, (const GLfloat*)&proj); - for (size_t i = 0; i < app->numRects; ++i) { moveRect(&app->rects[i], i, app->numRects, width, height, time); } + glBufferData(GL_UNIFORM_BUFFER, sizeof(proj), &proj, GL_STREAM_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, (GLsizeiptr)(app->numRects * sizeof(Rect)), @@ -276,9 +279,15 @@ setupGl(PuglTestApp* app) return PUGL_FAILURE; } - // Get location of rectangle shader uniforms - app->u_projection = - glGetUniformLocation(app->drawRect.program, "u_projection"); + // Get location of rectangle shader uniform block + const GLuint globalsIndex = glGetUniformBlockIndex(app->drawRect.program, + "UniformBufferObject"); + + // Generate/bind a uniform buffer for setting rectangle properties + GLuint uboHandle; + glGenBuffers(1, &uboHandle); + glBindBuffer(GL_UNIFORM_BUFFER, uboHandle); + glBindBufferBase(GL_UNIFORM_BUFFER, globalsIndex, uboHandle); // Generate/bind a VAO to track state glGenVertexArrays(1, &app->vao); -- cgit v1.2.1