commit 7d04a43ae723f5734b13cb24d735286a4a19fa7a
parent 257173c8f68001994832c224635f4f7ca273951c
Author: William Casarin <jb55@jb55.com>
Date: Sat, 27 Oct 2018 02:35:56 -0700
resizable ui elements
Diffstat:
10 files changed, 78 insertions(+), 28 deletions(-)
diff --git a/etc/shaders/ui.v.glsl b/etc/shaders/ui.v.glsl
@@ -5,14 +5,18 @@ in vec3 color;
out vec3 v_color;
-uniform mat4 view;
+uniform mat4 mvp;
+uniform vec2 uipos;
+uniform vec2 uisize;
uniform sampler2D texture1;
void main()
{
- vec4 v4_pos = vec4(position * 0.2, 1.0);
- gl_Position = v4_pos;
+ vec2 v2_pos = uipos + uisize * (position.xy - vec2(0.5, 0.5)) - (1.0 - uisize);
+ vec4 v4_pos = vec4(v2_pos, 0.0, 1.0) ;
+ gl_Position = mvp * v4_pos;
+
v_color = color + position;
}
diff --git a/src/event.c b/src/event.c
@@ -4,7 +4,7 @@
#include "input.h"
#include "game.h"
-void process_events(float *camera, struct input *input) {
+void process_events(struct ui *ui, float *camera, struct input *input) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
@@ -17,6 +17,7 @@ void process_events(float *camera, struct input *input) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
handle_resize(camera, event.window.data1, event.window.data2);
+ resize_ui(ui, event.window.data1, event.window.data2);
break;
}
break;
diff --git a/src/event.h b/src/event.h
@@ -4,7 +4,8 @@
#include "gl.h"
#include "input.h"
+#include "ui.h"
-void process_events(float *camera, struct input *input);
+void process_events(struct ui *ui, float *camera, struct input *input);
#endif /* PA_EVENT_H */
diff --git a/src/fbo.c b/src/fbo.c
@@ -1,10 +1,12 @@
#include <assert.h>
#include "fbo.h"
+#include "util.h"
void create_fbo(struct fbo *fbo, int width, int height) {
glGenFramebuffers(1, &fbo->handle);
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
+ check_gl();
fbo->width = width;
fbo->height = height;
@@ -16,12 +18,19 @@ int fbo_attach_renderbuffer(struct fbo *fbo,
GLuint *rbo = &fbo->attachments[fbo->n_attachments++];
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
+ check_gl();
glGenRenderbuffers(1, rbo);
+ check_gl();
glBindRenderbuffer(GL_RENDERBUFFER, *rbo);
+ check_gl();
glRenderbufferStorage(GL_RENDERBUFFER, internalformat, fbo->width, fbo->height);
+ check_gl();
glBindRenderbuffer(GL_RENDERBUFFER, 0);
+ check_gl();
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, *rbo);
+ check_gl();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_gl();
return *rbo;
}
@@ -31,35 +40,48 @@ int fbo_attach_texture(struct fbo *fbo, GLenum attachment) {
GLuint *texture = &fbo->attachments[fbo->n_attachments++];
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
+ check_gl();
glGenTextures(1, texture);
+ check_gl();
glBindTexture(GL_TEXTURE_2D, *texture);
+ check_gl();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbo->width, fbo->height, 0, GL_RGB,
GL_UNSIGNED_BYTE, NULL);
+ check_gl();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ check_gl();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ check_gl();
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, *texture,
0);
+ check_gl();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_gl();
return *texture;
}
void fbo_check(struct fbo *fbo) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
+ check_gl();
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_gl();
}
void delete_fbo(struct fbo *fbo) {
glDeleteFramebuffers(1, &fbo->handle);
+ check_gl();
}
void bind_fbo(struct fbo *fbo) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
+ check_gl();
}
void unbind_fbo(struct fbo *fbo) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ check_gl();
}
diff --git a/src/gl.h b/src/gl.h
@@ -40,6 +40,7 @@ void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose,
void glUseProgram(GLuint program);
void glUniform1f(GLint location, GLfloat v0);
+void glUniform2f(GLint location, GLfloat v0, GLfloat v1);
void glGetProgramiv(GLuint program, GLenum pname, GLint *params);
void glDeleteProgram( GLuint program);
@@ -85,6 +86,9 @@ void glFramebufferRenderbuffer( GLenum target,
GLenum attachment,
GLenum renderbuffertarget,
GLuint renderbuffer);
+
+
+
#endif /* POLYADVENT_GL_H */
diff --git a/src/main.c b/src/main.c
@@ -44,7 +44,7 @@ int main(void)
u32 last = SDL_GetTicks();
while (1) {
- process_events(game.test_resources.camera_persp, &game.input);
+ process_events(&game.ui, game.test_resources.camera_persp, &game.input);
u32 ticks = SDL_GetTicks();
update(&game, ticks-last);
diff --git a/src/mat4.c b/src/mat4.c
@@ -161,25 +161,25 @@ mat4 *mat4_perspective(float fov, float aspect, float near,
mat4 *mat4_ortho(float left, float right, float bottom, float top, float near,
float far, mat4 *dest)
{
- float lr = 1.0 / (left - right);
- float bt = 1.0 / (bottom - top);
- float nf = 1.0 / (near - far);
- dest[0] = -2 * lr;
+ float rl = 1.0 / (right - left);
+ float tb = 1.0 / (top - bottom);
+ float fn = 1.0 / (far - near);
+ dest[0] = 2.0 * rl;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
dest[4] = 0;
- dest[5] = -2 * bt;
+ dest[5] = 2.0 * tb;
dest[6] = 0;
dest[7] = 0;
dest[8] = 0;
dest[9] = 0;
- dest[10] = 2 * nf;
+ dest[10] = -2.0 * fn;
dest[11] = 0;
- dest[12] = (left + right) * lr;
- dest[13] = (top + bottom) * bt;
- dest[14] = (far + near) * nf;
- dest[15] = 1;
+ dest[12] = (left + right) * rl;
+ dest[13] = (top + bottom) * tb;
+ dest[14] = (far + near) * fn;
+ dest[15] = 1.0;
return dest;
}
diff --git a/src/render.c b/src/render.c
@@ -154,6 +154,7 @@ recalc_normals(GLint nm_uniform, mat4 *model_view, mat4 *normal) {
void render (struct game *game) {
float adjust = game->test_resources.light_intensity[0];
+ glEnable(GL_DEPTH_TEST);
glClearColor( 0.5294f * adjust, 0.8078f * adjust, 0.9216f * adjust, 1.0f ); //clear background screen to black
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
check_gl();
@@ -216,7 +217,7 @@ void render (struct game *game) {
check_gl();
}
- render_ui(&game->ui);
+ render_ui(&game->ui, view);
//player
// y tho
diff --git a/src/ui.c b/src/ui.c
@@ -37,12 +37,23 @@ static void create_quad(struct geometry *geom)
check_gl();
}
-void render_ui(struct ui *ui) {
+void render_ui(struct ui *ui, float *view) {
+ static float mvp[MAT4_ELEMS];
+ glDisable(GL_DEPTH_TEST);
glUseProgram(ui->shader.handle);
check_gl();
+ float uipos[2] = {0.01, 0.01};
+ float uisize[2] = {0.2, 0.2};
+
+ mat4_multiply(ui->ortho, view, mvp);
+
// setup camera for shader
- glUniformMatrix4fv(ui->view_uniform, 1, 0, ui->camera);
+ /* glUniform2f(ui->uipos_uniform, uipos[0] * uisize[0], uipos[1] * uisize[1]); */
+ glUniform2f(ui->uipos_uniform, uipos[0], uipos[1]);
+ glUniform2f(ui->uisize_uniform, uisize[0], uisize[1]);
+
+ glUniformMatrix4fv(ui->mvp_uniform, 1, 0, ui->ortho);
check_gl();
// render quad
@@ -53,13 +64,15 @@ void render_ui(struct ui *ui) {
void resize_ui(struct ui *ui, int width, int height) {
float left = 0;
- float right = width;
+ float right = 1.0;
+ float bottom = 1.0;
float top = 0;
- float bottom = height;
- float near = -100;
- float far = 100;
+ float near = 0.0;
+ float far = 1.0;
+
+ printf("%d %d\n", width, height);
- mat4_ortho(left, right, bottom, top, near, far, ui->camera);
+ mat4_ortho(left, right, bottom, top, near, far, ui->ortho);
}
@@ -82,7 +95,9 @@ void create_ui(struct ui *ui, int width, int height) {
GLuint program = ui->shader.handle;
- ui->view_uniform = glGetUniformLocation(program, "view");
+ ui->mvp_uniform = glGetUniformLocation(program, "mvp");
+ ui->uipos_uniform = glGetUniformLocation(program, "uipos");
+ ui->uisize_uniform = glGetUniformLocation(program, "uisize");
/* ui->attrs.normal = (gpu_addr)glGetAttribLocation(program, "normal"); */
ui->attrs.position = (gpu_addr)glGetAttribLocation(program, "position");
diff --git a/src/ui.h b/src/ui.h
@@ -10,13 +10,15 @@ struct ui {
struct gpu_program shader;
struct geometry quad;
struct attributes attrs;
- GLint view_uniform;
- float camera[MAT4_ELEMS];
+ GLint mvp_uniform;
+ GLint uipos_uniform;
+ GLint uisize_uniform;
+ float ortho[MAT4_ELEMS];
};
void create_ui(struct ui *ui, int width, int height);
void resize_ui(struct ui *ui, int width, int height);
-void render_ui(struct ui *ui);
+void render_ui(struct ui *ui, float *camera);
#endif /* POLYADVENT_UI_H */