polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 257173c8f68001994832c224635f4f7ca273951c
parent dc809dd3b27dd35c37e5edde3ac3d43d09bd8ab8
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 27 Oct 2018 01:06:35 -0700

ui working

Diffstat:
Aetc/shaders/ui.f.glsl | 11+++++++++++
Aetc/shaders/ui.v.glsl | 18++++++++++++++++++
Msrc/game.c | 10+++++++++-
Msrc/game.h | 4+++-
Msrc/geometry.c | 30+++++++++++++++++++++---------
Msrc/main.c | 3+--
Msrc/render.c | 1+
Msrc/ui.c | 71+++++++++++++++++++++++++++++++++++++++++++----------------------------
Msrc/ui.h | 5++++-
9 files changed, 111 insertions(+), 42 deletions(-)

diff --git a/etc/shaders/ui.f.glsl b/etc/shaders/ui.f.glsl @@ -0,0 +1,11 @@ +#version 300 es + +precision mediump float; + +in vec3 v_color; +out vec4 fragmentColor; + +void main() { + // fragmentColor = vec4(color + diffuse, 1.0); + fragmentColor = vec4(v_color, 1.0); +} diff --git a/etc/shaders/ui.v.glsl b/etc/shaders/ui.v.glsl @@ -0,0 +1,18 @@ +#version 300 es + +in vec3 position; +in vec3 color; + +out vec3 v_color; + +uniform mat4 view; + +uniform sampler2D texture1; + +void main() +{ + vec4 v4_pos = vec4(position * 0.2, 1.0); + gl_Position = v4_pos; + + v_color = color + position; +} diff --git a/src/game.c b/src/game.c @@ -4,6 +4,8 @@ #include "ply.h" #include "model.h" #include "terrain.h" +#include "render.h" +#include "util.h" #include <assert.h> mat4 *cam_init = (float[16]){ @@ -21,7 +23,10 @@ static void camera_update(struct node *node) { mat4_multiply(persp, mat, mat); } -void game_init(struct game *game) { +void game_init(struct game *game, int width, int height) { + init_gl(&game->test_resources, width, height); + check_gl(); + struct resources *res = &game->test_resources; mat4 *mvp = res->test_mvp; struct node *root = &res->root; @@ -46,6 +51,9 @@ void game_init(struct game *game) { .scale = 1.0 }; + create_ui(&game->ui, width, height); + check_gl(); + terrain_init(terrain); terrain->entity.model.shading = SHADING_TERRAIN; terrain->size = size; diff --git a/src/game.h b/src/game.h @@ -9,6 +9,7 @@ #include "shader.h" #include "entity.h" #include "terrain.h" +#include "ui.h" #define PLAYER_HEIGHT 1.73 @@ -54,12 +55,13 @@ struct resources { struct game { int counter; int seed; + struct ui ui; struct resources test_resources; struct input input; struct terrain terrain; }; -void game_init(struct game *game); +void game_init(struct game *game, int width, int height); void should_update(struct game *game); #endif /* PA_GAME_H */ diff --git a/src/geometry.c b/src/geometry.c @@ -26,21 +26,32 @@ destroy_buffer_geometry(struct geometry *geom) { void render_geometry(struct geometry *geom, struct attributes *attrs) { bind_vbo(&geom->vbos.vertex, attrs->position); - bind_vbo(&geom->vbos.normal, attrs->normal); - if (geom->vbos.color.handle) + check_gl(); + if (geom->vbos.normal.handle) { + bind_vbo(&geom->vbos.normal, attrs->normal); + check_gl(); + } + if (geom->vbos.color.handle) { bind_vbo(&geom->vbos.color, attrs->color); + check_gl(); + } bind_ibo(&geom->vbos.index); + check_gl(); glDrawElements(GL_TRIANGLES, geom->num_indices, /* count */ GL_UNSIGNED_INT, /* type */ (void*)0 /* element array buffer offset */ ); + check_gl(); } void init_geometry(struct geometry *geom) { geom->colors = NULL; + geom->vbos.color.handle = 0; + geom->normals = NULL; + geom->vbos.normal.handle = 0; } void @@ -48,7 +59,7 @@ make_buffer_geometry(struct geometry *geom) { // VBOs assert(geom->vertices); - assert(geom->normals); + /* assert(geom->normals); */ assert(geom->indices); assert(geom->num_indices >= 1); @@ -62,12 +73,13 @@ make_buffer_geometry(struct geometry *geom) { /* printf("making normal buffer\n"); */ // cube normals - make_vertex_buffer( - GL_ARRAY_BUFFER, - geom->normals, - geom->num_verts * 3 * (int)sizeof(*geom->normals), - &geom->vbos.normal - ); + if (geom->normals != NULL) + make_vertex_buffer( + GL_ARRAY_BUFFER, + geom->normals, + geom->num_verts * 3 * (int)sizeof(*geom->normals), + &geom->vbos.normal + ); // vertex colors if (geom->colors != NULL) diff --git a/src/main.c b/src/main.c @@ -38,8 +38,7 @@ int main(void) SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GL_CreateContext(window); - init_gl(&game.test_resources, width, height); - game_init(&game); + game_init(&game, width, height); check_gl(); u32 last = SDL_GetTicks(); diff --git a/src/render.c b/src/render.c @@ -216,6 +216,7 @@ void render (struct game *game) { check_gl(); } + render_ui(&game->ui); //player // y tho diff --git a/src/ui.c b/src/ui.c @@ -4,6 +4,7 @@ #include "buffer.h" #include "geometry.h" #include "util.h" +#include "common.h" // v1------v0 @@ -12,46 +13,41 @@ // | | // v2------v3 -static const GLfloat quad_vertices[] = { +static GLfloat quad_vertices[] = { 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, // v0-v1-v2-v3 front }; -static const GLfloat quad_normals[] = { +static GLfloat quad_normals[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front }; -static const GLushort quad_indices[] = {0, 1, 2, 0, 2, 3}; - -static void -render_quad(struct geometry *geom, struct attributes *attrs) { - bind_vbo(&geom->vbos.vertex, attrs->position); - bind_vbo(&geom->vbos.normal, attrs->normal); - - bind_ibo(&geom->vbos.index); - - glDrawElements( - GL_TRIANGLES, - ARRAY_SIZE(quad_indices), /* count */ - GL_UNSIGNED_SHORT, /* type */ - (void*)0 /* element array buffer offset */ - ); - - //glDisableVertexAttribArray(resources->attributes.position); -} +static GLuint quad_indices[] = {0, 1, 2, 0, 2, 3}; static void create_quad(struct geometry *geom) { - + init_geometry(geom); + geom->indices = quad_indices; + geom->vertices = quad_vertices; + geom->colors = quad_normals; + geom->num_indices = ARRAY_SIZE(quad_indices); + geom->num_verts = ARRAY_SIZE(quad_vertices); + make_buffer_geometry(geom); + check_gl(); } void render_ui(struct ui *ui) { glUseProgram(ui->shader.handle); - - // render quad + check_gl(); // setup camera for shader + glUniformMatrix4fv(ui->view_uniform, 1, 0, ui->camera); + check_gl(); + + // render quad + render_geometry(&ui->quad, &ui->attrs); + check_gl(); } @@ -60,8 +56,8 @@ void resize_ui(struct ui *ui, int width, int height) { float right = width; float top = 0; float bottom = height; - float near = 0; - float far = 2; + float near = -100; + float far = 100; mat4_ortho(left, right, bottom, top, near, far, ui->camera); } @@ -70,10 +66,29 @@ void resize_ui(struct ui *ui, int width, int height) { void create_ui(struct ui *ui, int width, int height) { struct shader vertex; struct shader fragment; + int ok = 0; + + create_quad(&ui->quad); + check_gl(); + + ok = make_shader(GL_VERTEX_SHADER, SHADER("ui.v.glsl"), &vertex); + assert(ok && "ui vertex shader"); + + ok = make_shader(GL_FRAGMENT_SHADER, SHADER("ui.f.glsl"), &fragment); + assert(ok && "ui fragment shader"); + + ok = make_program(&vertex, &fragment, &ui->shader); + assert(ok && "ui program"); + + GLuint program = ui->shader.handle; + + ui->view_uniform = glGetUniformLocation(program, "view"); - make_shader(GL_VERTEX_SHADER, "ui.v.glsl", &vertex); - make_shader(GL_FRAGMENT_SHADER, "ui.f.glsl", &vertex); - make_program(&vertex, &fragment, &ui->shader); + /* ui->attrs.normal = (gpu_addr)glGetAttribLocation(program, "normal"); */ + ui->attrs.position = (gpu_addr)glGetAttribLocation(program, "position"); + ui->attrs.color = (gpu_addr)glGetAttribLocation(program, "color"); + check_gl(); resize_ui(ui, width, height); + check_gl(); } diff --git a/src/ui.h b/src/ui.h @@ -9,11 +9,14 @@ struct ui { struct gpu_program shader; struct geometry quad; + struct attributes attrs; + GLint view_uniform; float camera[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 *); +void render_ui(struct ui *ui); + #endif /* POLYADVENT_UI_H */