GithubHelp home page GithubHelp logo

silky / quatfirstpersoncamera Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gamedevtech/quatfirstpersoncamera

0.0 2.0 0.0 93 KB

Public domain single-file C implementation of first-person quaternion camera used in such video game genres like FPS.

C 100.00%

quatfirstpersoncamera's Introduction

Description: Single-file C implementation of first-person quaternion camera used in such video game genres like FPS.

License: Public domain

Language: C

Dependencies: libm (can be omitted if custom QFPC_SIN and QFPC_COS are defined)

Example for SDL2

#define QFPC_IMPLEMENTATION
#include "qfpc.h"

int main()
{
  /* Application init */
  
  /* float cam_pos[3] init */
  /* float cam_rot[4] init */

  SDL_ShowCursor(0);
  int win_w, win_h;
  SDL_GetWindowSize(sdl_window, &win_w, &win_h);
  SDL_WarpMouseInWindow(sdl_window, win_w/2, win_h/2);
  
  while(1)
  {
    SDL_PumpEvents();
    int mouse_x, mouse_y;
    SDL_GetMouseState(&mouse_x, &mouse_y);
    const Uint8 * key = SDL_GetKeyboardState(NULL);
    SDL_WarpMouseInWindow(sdl_window, win_w/2, win_h/2);
    
    quatFirstPersonCamera(
      cam_pos, cam_rot,
      0.1f, 0.05f,
      win_w/2, win_h/2,
      mouse_x, mouse_y,
      key[SDL_SCANCODE_W],
      key[SDL_SCANCODE_A],
      key[SDL_SCANCODE_S],
      key[SDL_SCANCODE_D],
      key[SDL_SCANCODE_E],
      key[SDL_SCANCODE_Q]
    );

    /* Now cam_pos and cam_rot can be passed to a shader */

    /* The rest of application */
  }
}

Example for GLFW3

#define QFPC_IMPLEMENTATION
#include "qfpc.h"

int main()
{
  /* Application init */
  
  /* float cam_pos[3] init */
  /* float cam_rot[4] init */

  glfwSetInputMode(glfw_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  int win_w, win_h;
  glfwGetWindowSize(glfw_window, &win_w, &win_h);
  for(int i = 0; i < 2; ++i)
  {
    glfwPollEvents();
    glfwSetCursorPos(glfw_window, win_w/2, win_h/2);
  }

  while(!glfwWindowShouldClose(glfw_window))
  {
    glfwPollEvents();
    double mouse_x, mouse_y;
    glfwGetCursorPos(glfw_window, &mouse_x, &mouse_y);
    glfwSetCursorPos(glfw_window, win_w/2, win_h/2);

    quatFirstPersonCamera(
      cam_pos, cam_rot,
      0.1f, 0.05f,
      win_w/2, win_h/2,
      mouse_x, mouse_y,
      glfwGetKey(glfw_window, GLFW_KEY_W),
      glfwGetKey(glfw_window, GLFW_KEY_A),
      glfwGetKey(glfw_window, GLFW_KEY_S),
      glfwGetKey(glfw_window, GLFW_KEY_D),
      glfwGetKey(glfw_window, GLFW_KEY_E),
      glfwGetKey(glfw_window, GLFW_KEY_Q)
    );

    /* Now cam_pos and cam_rot can be passed to a shader */

    /* The rest of application */
  }
}

quatfirstpersoncamera's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.