diff options
author | David Robillard <d@drobilla.net> | 2020-04-04 13:36:46 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-04-04 13:36:46 +0200 |
commit | 11800b6179458eb962cd1862e4053efd7f28c2f4 (patch) | |
tree | 1b239515e3c36e11c04b9da1861aa65eff0d6afd /examples/pugl_gl3_demo.c | |
parent | ecc281c56a8c3c297cfd2f0b6d4671b2000efd9b (diff) | |
download | pugl-11800b6179458eb962cd1862e4053efd7f28c2f4.tar.gz pugl-11800b6179458eb962cd1862e4053efd7f28c2f4.tar.bz2 pugl-11800b6179458eb962cd1862e4053efd7f28c2f4.zip |
Shader Demo: Use a UBO
Diffstat (limited to 'examples/pugl_gl3_demo.c')
-rw-r--r-- | examples/pugl_gl3_demo.c | 23 |
1 files changed, 16 insertions, 7 deletions
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 @@ -55,6 +55,11 @@ static const int defaultHeight = 512; typedef struct { + mat4 projection; +} RectUniforms; + +typedef struct +{ PuglTestOptions opts; PuglWorld* world; PuglView* view; @@ -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); |