diff options
-rw-r--r-- | examples/pugl_gl3_demo.c | 23 | ||||
-rw-r--r-- | shaders/header_330.glsl | 2 | ||||
-rw-r--r-- | shaders/rect.vert | 7 |
3 files changed, 23 insertions, 9 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); diff --git a/shaders/header_330.glsl b/shaders/header_330.glsl index 5ae7f43..1bcdf05 100644 --- a/shaders/header_330.glsl +++ b/shaders/header_330.glsl @@ -1 +1,3 @@ #version 330 core + +#define UBO(qualifiers) layout(std140) diff --git a/shaders/rect.vert b/shaders/rect.vert index 337f105..ef8c8cc 100644 --- a/shaders/rect.vert +++ b/shaders/rect.vert @@ -1,7 +1,10 @@ /* The vertex shader is trivial, but forwards scaled UV coordinates (in pixels) to the fragment shader for drawing the border. */ -uniform mat4 u_projection; +UBO(binding = 0) uniform UniformBufferObject +{ + mat4 projection; +} ubo; layout(location = 0) in vec2 v_position; layout(location = 1) in vec2 v_origin; @@ -22,7 +25,7 @@ main() v_origin[0], v_origin[1], 0.0, 1.0); // clang-format on - mat4 MVP = u_projection * m; + mat4 MVP = ubo.projection * m; f_uv = v_position * v_size; f_size = v_size; |