Ejemplo n.º 1
0
Archivo: gx.c Proyecto: LWSS/gx
void render_game(struct GameMemory *memory, struct Renderer *renderer, uint32 screen_width, uint32 screen_height)
{
    struct GameState *game_state = (struct GameState *)memory->game_memory;
    struct RenderBuffer *render_buffer = (struct RenderBuffer *)memory->render_memory;

    float aspect_ratio = (float)screen_width / (float)screen_height;
    mat4 projection_matrix = mat4_orthographic(-game_state->camera.zoom/2.0f, game_state->camera.zoom/2.0f,
                                               -game_state->camera.zoom/2.0f / aspect_ratio, game_state->camera.zoom/2.0f / aspect_ratio,
                                               0.0f, 1.0f);
    mat4 view_matrix = mat4_look_at(vec3_new(game_state->camera.position.x, game_state->camera.position.y, 0.0f), vec3_new(game_state->camera.position.x, game_state->camera.position.y, -1.0f), vec3_new(0, 1, 0));
    mat4 view_projection_matrix = mat4_mul(projection_matrix, view_matrix);

    update_ubo(renderer->camera_ubo, sizeof(mat4), &view_projection_matrix);

    bind_program(renderer->line_program);
    draw_world_line_buffer(renderer, render_buffer, renderer->line_program);
    bind_program(0);

    bind_program(renderer->quad_program);
    draw_world_quad_buffer(&renderer->sprite_batch, render_buffer);
    bind_program(0);

    draw_buildings(game_state, renderer);
    draw_ships(game_state, renderer);
    draw_projectiles(game_state, renderer);

    view_projection_matrix = mat4_orthographic(0, screen_width, screen_height, 0, 0, 1);
    update_ubo(renderer->camera_ubo, sizeof(mat4), &view_projection_matrix);

    bind_program(renderer->quad_program);
    draw_screen_quad_buffer(&renderer->sprite_batch, render_buffer);
    bind_program(0);

    draw_frame_text_buffer(renderer, render_buffer);
}
Ejemplo n.º 2
0
void ui_rectangle_render(ui_rectangle* r) {

    if(!r->active) {
        return;
    }

    int width = graphics_viewport_width();
    int height = graphics_viewport_height();

    asset_hndl mat = asset_hndl_new_load(P("$CORANGE/shaders/ui.mat"));
    shader_program* program_ui = material_get_entry(asset_hndl_ptr(&mat), 0)->program;

    shader_program_enable(program_ui);
    shader_program_set_mat4(program_ui, "world", mat4_id());
    shader_program_set_mat4(program_ui, "view", mat4_id());
    shader_program_set_mat4(program_ui, "proj", mat4_orthographic(0, width, height, 0, -1, 1));

    glEnable(GL_BLEND);
    glBlendFunc(r->blend_src, r->blend_dst);

    if (asset_hndl_isnull(&r->texture)) {
        shader_program_set_texture(program_ui, "diffuse", 0, asset_hndl_new_load(P("$CORANGE/textures/white.dds")));
    } else {
        shader_program_set_texture(program_ui, "diffuse", 0, r->texture);
    }

    shader_program_set_texture(program_ui, "random",  1, asset_hndl_new_load(P("$CORANGE/textures/random.dds")));

    shader_program_set_float(program_ui, "time", r->time);
    shader_program_set_float(program_ui, "glitch", r->glitch);

    float rect_colors[] = {
        r->color.x, r->color.y, r->color.z, r->color.w,
        r->color.x, r->color.y, r->color.z, r->color.w,
        r->color.x, r->color.y, r->color.z, r->color.w,
        r->color.x, r->color.y, r->color.z, r->color.w,
        r->color.x, r->color.y, r->color.z, r->color.w,
        r->color.x, r->color.y, r->color.z, r->color.w
    };

    float rect_positions[] = {
        r->top_left.x,     r->top_left.y,
        r->top_left.x,     r->bottom_right.y,
        r->bottom_right.x, r->bottom_right.y,
        r->top_left.x,     r->top_left.y,
        r->bottom_right.x, r->top_left.y,
        r->bottom_right.x, r->bottom_right.y,
    };

    float rect_texcoords[12];

    if (r->texture_tile) {

        float width = r->bottom_right.x - r->top_left.x;
        float height = r->bottom_right.y - r->top_left.y;

        rect_texcoords[0]  = 0;
        rect_texcoords[1]  = height / r->texture_height;
        rect_texcoords[2]  = width  / r->texture_width;
        rect_texcoords[3]  = height / r->texture_height;
        rect_texcoords[4]  = width  / r->texture_width;
        rect_texcoords[5]  = 0;
        rect_texcoords[6]  = 0;
        rect_texcoords[7]  = height / r->texture_height;
        rect_texcoords[8]  = 0;
        rect_texcoords[9]  = 0;
        rect_texcoords[10] = width  / r->texture_width;
        rect_texcoords[11] = 0;

    } else {

        rect_texcoords[0]  = 0;
        rect_texcoords[1]  = 0;
        rect_texcoords[2]  = 1;
        rect_texcoords[3]  = 0;
        rect_texcoords[4]  = 1;
        rect_texcoords[5]  = 1;
        rect_texcoords[6]  = 0;
        rect_texcoords[7]  = 0;
        rect_texcoords[8]  = 0;
        rect_texcoords[9]  = 1;
        rect_texcoords[10] = 1;
        rect_texcoords[11] = 1;

    }

    shader_program_enable_attribute(program_ui, "vPosition", 2, 2, rect_positions);
    shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, rect_texcoords);
    shader_program_enable_attribute(program_ui, "vColor", 4, 4, rect_colors);

    glDrawArrays(GL_TRIANGLES, 0, 6);

    shader_program_disable_attribute(program_ui, "vPosition");
    shader_program_disable_attribute(program_ui, "vTexcoord");
    shader_program_disable_attribute(program_ui, "vColor");

    if (r->border_size > 0) {

        glLineWidth(r->border_size);

        float border_colors[] = {
            r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w,
            r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w,
            r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w,
            r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w,
            r->border_color.x, r->border_color.y, r->border_color.z, r->border_color.w
        };

        float border_positions[] = {
            r->top_left.x,     r->top_left.y,
            r->bottom_right.x, r->top_left.y,
            r->bottom_right.x, r->bottom_right.y,
            r->top_left.x,     r->bottom_right.y,
            r->top_left.x,     r->top_left.y
        };

        float border_texcoord[] = {
            0, 0,  0, 0,  0, 0,  0, 0,  0, 0
        };

        shader_program_enable_attribute(program_ui, "vPosition", 2, 2, border_positions);
        shader_program_enable_attribute(program_ui, "vTexcoord", 2, 2, border_texcoord);
        shader_program_enable_attribute(program_ui, "vColor", 4, 4, border_colors);

        glDrawArrays(GL_LINE_STRIP, 0, 5);

        shader_program_disable_attribute(program_ui, "vPosition");
        shader_program_disable_attribute(program_ui, "vTexcoord");
        shader_program_disable_attribute(program_ui, "vColor");

        glLineWidth(1);
    }

    glDisable(GL_BLEND);

    shader_program_disable(program_ui);

}