Beispiel #1
0
void camera_control_joyorbit(camera* c, float timestep) {
  
  if (joystick_count() == 0) return;
  
  SDL_Joystick* mainstick = joystick_get(0);
  int x_move = SDL_JoystickGetAxis(mainstick, 0);
  int y_move = SDL_JoystickGetAxis(mainstick, 1);
  
  /* Dead Zone */
  if (abs(x_move) < 10000) { x_move = 0; };
  if (abs(y_move) < 10000) { y_move = 0; };
  
  float a1 = (x_move / 32768.0) * -0.05;
  float a2 = (y_move / 32768.0) * 0.05;
  
  vec3 translation = c->target;
  c->position = vec3_sub(c->position, translation);
  c->target = vec3_sub(c->target, translation);
  
  c->position = mat3_mul_vec3(mat3_rotation_y( a1 ), c->position );
  vec3 axis = vec3_normalize(vec3_cross( vec3_sub(c->position, c->target) , vec3_new(0,1,0) ));
  c->position = mat3_mul_vec3(mat3_rotation_axis_angle(axis, a2 ), c->position );
  
  c->position = vec3_add(c->position, translation);
  c->target = vec3_add(c->target, translation);

}
Beispiel #2
0
void camera_control_orbit(camera* c, SDL_Event e) {
  
  float a1 = 0;
  float a2 = 0;
  vec3 axis;
  
  vec3 translation = c->target;
  c->position = vec3_sub(c->position, translation);
  c->target = vec3_sub(c->target, translation);
  
  switch(e.type) {
    
    case SDL_MOUSEMOTION:
      if (e.motion.state & SDL_BUTTON(1)) {
        a1 = e.motion.xrel * -0.005;
        a2 = e.motion.yrel * 0.005;
        c->position = mat3_mul_vec3(mat3_rotation_y( a1 ), c->position );
        axis = vec3_normalize(vec3_cross( vec3_sub(c->position, c->target) , vec3_new(0,1,0) ));
        c->position = mat3_mul_vec3(mat3_rotation_angle_axis(a2, axis), c->position );
      }
    break;
    
    case SDL_MOUSEWHEEL:
      c->position = vec3_add(c->position, vec3_mul(vec3_normalize(c->position), -e.wheel.y));
    break;

  }
  
  c->position = vec3_add(c->position, translation);
  c->target = vec3_add(c->target, translation);

}
Beispiel #3
0
void camera_control_orbit(camera* c, SDL_Event e) {
  
  float a1 = 0;
  float a2 = 0;
  vec3 axis;
  
  vec3 translation = c->target;
  c->position = vec3_sub(c->position, translation);
  c->target = vec3_sub(c->target, translation);
  
  switch(e.type) {
    
    case SDL_MOUSEMOTION:
      if (e.motion.state & SDL_BUTTON(1)) {
        a1 = e.motion.xrel * -0.005;
        a2 = e.motion.yrel * 0.005;
        c->position = mat3_mul_vec3(mat3_rotation_y( a1 ), c->position );
        axis = vec3_normalize(vec3_cross( vec3_sub(c->position, c->target) , vec3_new(0,1,0) ));
        c->position = mat3_mul_vec3(mat3_rotation_axis_angle(axis, a2 ), c->position );
      }
    break;
    
    case SDL_MOUSEBUTTONDOWN:
      if (e.button.button == SDL_BUTTON_WHEELUP) {
        c->position = vec3_sub(c->position, vec3_normalize(c->position));
      }
      if (e.button.button == SDL_BUTTON_WHEELDOWN) {
        c->position = vec3_add(c->position, vec3_normalize(c->position));
      }
    break;

  }
  
  c->position = vec3_add(c->position, translation);
  c->target = vec3_add(c->target, translation);

}
void update_game(Game* G)
{
    float delta_time = (float)get_delta_time(G->timer);
    int ii;

    _control_camera(G, delta_time);
    set_view_matrix(G->graphics, mat4_inverse(transform_get_matrix(G->camera)));

    /* Dynamic Lights */
    if(G->dynamic_lights) {
        G->sun_light.position = mat3_mul_vector(vec3_create(5,5,0), mat3_rotation_y((float)get_running_time(G->timer)*0.5f));
        G->light_transform += delta_time;
        for(ii=0;ii<NUM_LIGHTS;++ii) {
            if(ii % 2)
                G->lights[ii].position.z = sinf((G->light_transform + ii * 1.0f)/2.0f) * 10.0f;
            else
                G->lights[ii].position.x = sinf((G->light_transform + ii * 1.0f)/2.0f) * 10.0f;
        }
    }

    add_light(G->graphics, G->sun_light);
    for(ii=0;ii<NUM_LIGHTS;++ii) {
        add_light(G->graphics, G->lights[ii]);
    }
    render_scene(G->scene, G->graphics);

    G->tap_timer += delta_time;

    /* Calculate FPS */
    G->fps_time += delta_time;
    G->fps_count++;

    if(G->fps_time >= 1.0f) {
        G->fps = G->fps_count/G->fps_time;
        system_log("FPS: %f\n", G->fps);
        G->fps_time -= 1.0f;
        G->fps_count = 0;
    }
    {
        int width, height;
        float scale = 50.0f;
        float x = -G->width/2.0f;
        float y = G->height/2.0f-scale;
        char buffer[256] = {0};
        // FPS
        sprintf(buffer, "FPS: %.2f", G->fps);
        add_string(G->ui, x, y, scale, buffer);
        y -= scale;
        // Renderer
        switch(renderer_type(G->graphics)) {
        case kForward: add_string(G->ui, x, y, scale, "Forward renderer"); break;
        case kLightPrePass: add_string(G->ui, x, y, scale, "Deferred Lighting"); break;
        case kDeferred: add_string(G->ui, x, y, scale, "Deferred Shading"); break;
        default: assert(!"Invalid renderer"); break;
        }
        y -= scale;
        // Resolution
        graphics_size(G->graphics, &width, &height);
        sprintf(buffer, "%dx%d", width, height);
        add_string(G->ui, x, y, scale, buffer);
    }
}