コード例 #1
0
ファイル: shader.c プロジェクト: yanbin-ha/Corange
void shader_program_set_texture(shader_program* p, char* name, int index, asset_hndl t) {

    GLint location = glGetUniformLocation(shader_program_handle(p), name);
    if ( location == -1) {
        warning("Shader has no uniform called '%s'", name);
    } else {
        glActiveTexture(GL_TEXTURE0 + index);
        glBindTexture(texture_type(asset_hndl_ptr(&t)), texture_handle(asset_hndl_ptr(&t)));
        glUniform1i(location, index);
    }

}
コード例 #2
0
ファイル: animated_object.c プロジェクト: AntonioCS/Corange
void animated_object_update(animated_object* ao, float timestep) {
  
  ao->animation_time += timestep;
  animation* animation = asset_hndl_ptr(ao->animation);
  
  if (animation == NULL) { return; }
  
  if (ao->pose == NULL) {
    error("Animated object needs skeleton loaded with 'animated_object_load_skeleton'.");
  }
  
  float time_diff = animation->end_time - animation->start_time;
  float timepoint = animation->start_time + fmod(ao->animation_time, time_diff);
  
  skeleton* frame0 = animation->frames[0];
  skeleton* frame1 = animation->frames[animation->num_frames-1];
  float frame0_time = 0;
  float frame1_time = 999999;
  
  for(int i = 0; i < animation->num_frames; i++) {
    if ((timepoint > animation->frame_times[i]) && (frame0_time < animation->frame_times[i])) {
      frame0 = animation->frames[i];
      frame0_time = animation->frame_times[i];
    }
    
    if ((timepoint < animation->frame_times[i]) && (frame1_time > animation->frame_times[i])) {
      frame1 = animation->frames[i];
      frame1_time = animation->frame_times[i];
    }
  }
  
  float amount = (timepoint - frame0_time) / (frame1_time - frame0_time);
  amount = 1-amount;
  
  skeleton* skel = asset_hndl_ptr(ao->skeleton);
  
  if (skel->num_bones != frame0->num_bones) { error("Animation has a different number of bones to skeleton"); }
  if (skel->num_bones != frame1->num_bones) { error("Animation has a different number of bones to skeleton"); }
  
  for(int i = 0; i < skel->num_bones; i++) {
    vec3 position = vec3_smoothstep(frame0->bones[i]->position, frame1->bones[i]->position, amount);
    mat4 rotation = mat4_smoothstep(frame0->bones[i]->rotation, frame1->bones[i]->rotation, amount);
    
    ao->pose->bones[i]->position = position;
    ao->pose->bones[i]->rotation = rotation;
  }
  
}
コード例 #3
0
ファイル: scotland.c プロジェクト: AntonioCS/Corange
static void toggle_freecam(ui_button* b, SDL_Event event) {
  
  if (event.type == SDL_MOUSEBUTTONDOWN) {
    
    if (ui_button_contains_position(b, vec2_new(event.motion.x, event.motion.y))) {
      b->pressed = true;
    }
  
  } else if (event.type == SDL_MOUSEBUTTONUP) {
    
    if (b->pressed) {
      b->pressed = false;
      
      freecam = !freecam;
      
      camera* cam = entity_get("camera");
      landscape* world = entity_get("world");
      
      vec3 cam_dir = vec3_normalize(vec3_sub(cam->target, cam->position));
      float height = terrain_height(asset_hndl_ptr(world->terrain), vec2_new(cam->position.x, cam->position.z));
      cam->position.y = height + 1;
      cam->target = vec3_add(cam->position, cam_dir);
    }
  }
}
コード例 #4
0
ファイル: material.c プロジェクト: aadarshasubedi/Corange
static void material_generate_programs(material* m) {
  
  for(int i = 0; i < m->num_entries; i++) {
  
    material_entry* me = m->entries[i];
    me->program = shader_program_new();
    
    bool attached = false;
    for(int j = 0; j < me->num_items; j++) {
      
      if (me->types[j] == mat_item_shader) {
        asset_hndl ah = me->items[j].as_asset;
        
        shader_program_attach_shader(me->program, asset_hndl_ptr(&ah));
        attached = true;
      }
      
    }
    
    if (attached) { shader_program_link(me->program); }
    
  }
  
  SDL_GL_CheckError();
    
}
コード例 #5
0
ファイル: animated_object.c プロジェクト: AntonioCS/Corange
void animated_object_load_skeleton(animated_object* ao, asset_hndl ah) {
  if(ao->pose != NULL) {
    skeleton_delete(ao->pose);
  }
  ao->skeleton = ah;
  ao->pose = skeleton_copy(asset_hndl_ptr(ao->skeleton));
}
コード例 #6
0
ファイル: lang.c プロジェクト: ghosthamlet/Corange
char* S(char* id) {
  if (!asset_hndl_isnull(&curr_lang)) {
    return lang_get(asset_hndl_ptr(&curr_lang), id);
  } else {
    error("Current Language hasn't been set!");
    return NULL;
  }
}
コード例 #7
0
ファイル: scotland.c プロジェクト: AntonioCS/Corange
void scotland_update() {
  
  camera* cam = entity_get("camera");
  light* sun = entity_get("sun");
  static_object* skydome = entity_get("skydome");
  landscape* world = entity_get("world");
  
  sun_orbit += frame_time() * 0.01;
  
  sun->position.x = 512 + sin(sun_orbit) * 512;
  sun->position.y = cos(sun_orbit) * 512;
  sun->position.z = 512;
  sun->target = vec3_new(512, 0, 512);
  
  if (w_held || s_held) {
    
    vec3 cam_dir = vec3_normalize(vec3_sub(cam->target, cam->position));
    float speed = 0.5;
    if (!freecam) speed = 0.05;
    if (w_held) {
      cam->position = vec3_add(cam->position, vec3_mul(cam_dir, speed));
    }
    if (s_held) {
      cam->position = vec3_sub(cam->position, vec3_mul(cam_dir, speed));
    }
    
    if (!freecam) {
      float height = terrain_height(asset_hndl_ptr(world->terrain), vec2_new(cam->position.x, cam->position.z));
      cam->position.y = height + 1;
    }
    
    cam->target = vec3_add(cam->position, cam_dir);
  }
  
  Uint8 keystate = SDL_GetMouseState(NULL, NULL);
  if(keystate & SDL_BUTTON(1)){
  
    float a1 = -(float)mouse_x * 0.005;
    float a2 = (float)mouse_y * 0.005;
    
    vec3 cam_dir = vec3_normalize(vec3_sub(cam->target, cam->position));
    
    cam_dir.y += -a2;
    vec3 side_dir = vec3_normalize(vec3_cross(cam_dir, vec3_new(0,1,0)));
    cam_dir = vec3_add(cam_dir, vec3_mul(side_dir, -a1));
    cam_dir = vec3_normalize(cam_dir);
    
    cam->target = vec3_add(cam->position, cam_dir);
  }
  
  mouse_x = 0;
  mouse_y = 0;
  
  ui_button* framerate = ui_elem_get("framerate");
  ui_button_set_label(framerate, frame_rate_string());
}
コード例 #8
0
void animated_object_update(animated_object* ao, float timestep) {
  
  ao->animation_time += timestep;
  
  animation* a = asset_hndl_ptr(&ao->animation);
  if (a == NULL) { return; }
  
  if (ao->pose != NULL) {
    frame_delete(ao->pose);
    ao->pose = NULL;
  }
  
  ao->pose = animation_sample(a, ao->animation_time);
  frame_gen_transforms(ao->pose);
  
}
コード例 #9
0
ファイル: scotland.c プロジェクト: AntonioCS/Corange
static int load_resources(void* unused) {
  
  folder_load(P("./resources/terrain/"));
  folder_load(P("./resources/vegetation/"));
  
  static_object* skydome = entity_new("skydome", static_object);
  skydome->renderable = asset_hndl_new_load(P("./resources/terrain/skydome.obj"));
  ((renderable*)asset_hndl_ptr(skydome->renderable))->material = asset_hndl_new_load(P("$CORANGE/shaders/skydome.mat"));
  skydome->position = vec3_new(512, 0, 512);
  skydome->scale = vec3_new(1024, 1024, 1024);
  
  landscape* world = entity_new("world", landscape);
  world->terrain = asset_hndl_new_load(P("./resources/terrain/heightmap.raw"));
  world->normalmap = asset_hndl_new_load(P("./resources/terrain/normalsmap.dds"));
  world->colormap = asset_hndl_new_load(P("./resources/terrain/colormap.dds"));
  world->attributemap = asset_hndl_new_load(P("./resources/terrain/attributemap.dds"));

  landscape_set_textures(world, asset_hndl_new_load(P("./resources/terrain/surface.dds")), 
                                asset_hndl_new_load(P("./resources/terrain/surface_bump.dds")), 
                                asset_hndl_new_load(P("./resources/terrain/surface_far.dds")), 
                                asset_hndl_new_load(P("./resources/terrain/surface_far_bump.dds")));
  
  vegetation_init();
  //vegetation_add_type(asset_get("./resources/terrain/heightmap.raw"), 
  //                    asset_get("./resources/vegetation/grass.obj"), 
  //                    4.0);
  
  ui_button* loading = ui_elem_get("loading");
  ui_spinner* load_spinner = ui_elem_get("load_spinner");
  ui_button* framerate = ui_elem_get("framerate");
  ui_button* wireframe = ui_elem_get("wireframe");
  ui_button* freecam = ui_elem_get("freecam");
  loading->active = false;
  load_spinner->active = false;
  framerate->active = true;
  wireframe->active = true;
  freecam->active = true;
  
  loading_resources = false;
  return 1;
}
コード例 #10
0
ファイル: teapot.c プロジェクト: aadarshasubedi/Corange
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;
  
}
コード例 #11
0
void animated_object_load_skeleton(animated_object* ao, asset_hndl ah) {
  if(ao->pose != NULL) { frame_delete(ao->pose); }
  ao->skeleton = ah;
  ao->pose = frame_copy(((skeleton*)asset_hndl_ptr(&ao->skeleton))->rest_pose);
  frame_gen_transforms(ao->pose);
}
コード例 #12
0
ファイル: ui_rectangle.c プロジェクト: juankprada/Corange
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);

}
コード例 #13
0
ファイル: casset.c プロジェクト: marioux/Corange
asset* asset_get(fpath path) {
  asset_hndl ah = asset_hndl_new(path);
  return asset_hndl_ptr(&ah);
}