Пример #1
0
int main(int argc, char **argv) {
  
  #ifdef _WIN32
    FILE* ctt = fopen("CON", "w" );
    FILE* fout = freopen( "CON", "w", stdout );
    FILE* ferr = freopen( "CON", "w", stderr );
  #endif
  
  corange_init("../../assets_core");
  
  graphics_viewport_set_title("Teapot");
  graphics_viewport_set_size(1280, 720);
  
  camera* cam = entity_new("camera", camera);
  cam->position = vec3_new(5, 5, 5);
  cam->target =  vec3_new(0, 0, 0);
  
  teapot_shader = asset_hndl_new_load(P("./assets/teapot.mat"));
  teapot_object = asset_hndl_new_load(P("./assets/teapot.obj"));
  
  int running = 1;
  SDL_Event e = {0};
  
  while(running) {
    
    frame_begin();
    
    camera* cam = entity_get("camera");
    
    while(SDL_PollEvent(&e)) {
      switch(e.type){
      case SDL_KEYDOWN:
      case SDL_KEYUP:
        if (e.key.keysym.sym == SDLK_ESCAPE) { running = 0; }
        if (e.key.keysym.sym == SDLK_PRINTSCREEN) { graphics_viewport_screenshot(); }
        if (e.key.keysym.sym == SDLK_r &&
            e.key.keysym.mod == KMOD_LCTRL) {
            asset_reload_all();
        }
        break;
      case SDL_QUIT:
        running = 0;
        break;
      }
      camera_control_orbit(cam, e);
      ui_event(e);
    }
    
    ui_update();
    
    glClearColor(0.25, 0.25, 0.25, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    shader_program* shader = material_first_program(asset_hndl_ptr(&teapot_shader));
    shader_program_enable(shader);
    shader_program_set_mat4(shader, "world", mat4_id());
    shader_program_set_mat4(shader, "view", camera_view_matrix(cam));
    shader_program_set_mat4(shader, "proj", camera_proj_matrix(cam));
      
    shader_program_set_texture(shader, "cube_beach", 0, asset_hndl_new_load(P("$CORANGE/water/cube_sea.dds")));
    shader_program_set_vec3(shader, "camera_direction", camera_direction(cam));
    
    renderable* r = asset_hndl_ptr(&teapot_object);
    
    for(int i=0; i < r->num_surfaces; i++) {
      
      renderable_surface* s = r->surfaces[i];
      
      int mentry_id = min(i, ((material*)asset_hndl_ptr(&r->material))->num_entries-1);
      material_entry* me = material_get_entry(asset_hndl_ptr(&r->material), mentry_id);
      
      glBindBuffer(GL_ARRAY_BUFFER, s->vertex_vbo);
      
      shader_program_enable_attribute(shader, "vPosition",  3, 18, (void*)0);
      shader_program_enable_attribute(shader, "vNormal",    3, 18, (void*)(sizeof(float) * 3));
      //shader_program_enable_attribute(shader, "vTangent",   3, 18, (void*)(sizeof(float) * 6));
      //shader_program_enable_attribute(shader, "vBinormal",  3, 18, (void*)(sizeof(float) * 9));
      //shader_program_enable_attribute(shader, "vTexcoord",  2, 18, (void*)(sizeof(float) * 12));
      
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->triangle_vbo);
        glDrawElements(GL_TRIANGLES, s->num_triangles * 3, GL_UNSIGNED_INT, (void*)0);
      
      shader_program_disable_attribute(shader, "vPosition");
      shader_program_disable_attribute(shader, "vNormal");
      //shader_program_disable_attribute(shader, "vTangent");
      //shader_program_disable_attribute(shader, "vBinormal");
      //shader_program_disable_attribute(shader, "vTexcoord");
      
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

    }
  
    shader_program_disable(shader);
    
    glDisable(GL_DEPTH_TEST);
    ui_render();
    
    graphics_swap();
    
    frame_end();
  }
  
  corange_finish();
  
  return 0;
  
}
Пример #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);

}