Browse Source

smooth camera

master
alistair 2 years ago
parent
commit
6b78727a39
  1. 2
      data/levels/init.json
  2. 6
      data/shaders/test.frag
  3. 1
      src/entity.h
  4. 4
      src/frametime.h
  5. 36
      src/main.cpp
  6. 17
      src/player.cpp

2
data/levels/init.json

@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
{
"draw": {"model": "orb"},
"physics": {
"position": [8,0,0],
"position": [8,2,0],
"orientation": [0,0,0]
}
}

6
data/shaders/test.frag

@ -12,9 +12,9 @@ uniform vec3 cameraPosition; @@ -12,9 +12,9 @@ uniform vec3 cameraPosition;
uniform samplerCube skybox;
uniform bool flatshading = true;
uniform vec3 fogcolour = vec3(0.9, 0.9, 1.0);
uniform float fogdistance = 20;
uniform float fogheight = 0.1;
uniform vec3 fogcolour = vec3(1.0, 0.9, 1.0);
uniform float fogdistance = 10;
uniform float fogheight = 0.9;
float gamma = 2.2;

1
src/entity.h

@ -19,6 +19,7 @@ struct entity { @@ -19,6 +19,7 @@ struct entity {
struct physics_model {
glm::vec3 pos;
glm::vec3 velocity;
glm::mat4 orientation;
};

4
src/frametime.h

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
#pragma once
extern float framestep;

36
src/main.cpp

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
//Using SDL, SDL OpenGL, standard IO, and, strings
#include <SDL2/SDL_mouse.h>
#include <cstdint>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
@ -33,9 +34,9 @@ @@ -33,9 +34,9 @@
#include "drawing.h"
#include "level.h"
#include "player.h"
#include "frametime.h"
float framestep = 0.0;
class SDLGLGLWindow {
@ -120,7 +121,7 @@ class SDLGLGLWindow { @@ -120,7 +121,7 @@ class SDLGLGLWindow {
SDL_GL_CONTEXT_PROFILE_CORE );
gWindow = SDL_CreateWindow( "Tutorial", SDL_WINDOWPOS_UNDEFINED,
gWindow = SDL_CreateWindow( "ponder orb", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
@ -222,6 +223,7 @@ class SDLGLGLWindow { @@ -222,6 +223,7 @@ class SDLGLGLWindow {
int initGL() {
bool success = true;
float cubeVertices[] = {
// vertices normals textures tangent
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
@ -478,10 +480,13 @@ int main(int argc, char **argv) { @@ -478,10 +480,13 @@ int main(int argc, char **argv) {
std::cout << "Rendering now\n";
float last_frametime = SDL_GetTicks();
//While application is running
while( !quit )
{
bool shademode = true;
const float camera_speed = 5.4f;
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
@ -490,9 +495,24 @@ int main(int argc, char **argv) { @@ -490,9 +495,24 @@ int main(int argc, char **argv) {
{
quit = true;
}
if (e.type == SDL_KEYUP) {
switch (e.key.keysym.scancode) {
case SDL_SCANCODE_W:
cont.player_data->move(0, glm::vec3(0,0,1));
break;
case SDL_SCANCODE_S:
cont.player_data->move(0, glm::vec3(0,0,-1));
break;
case SDL_SCANCODE_A:
cont.player_data->move(0, glm::vec3(-1,0,0));
break;
case SDL_SCANCODE_D:
cont.player_data->move(0, glm::vec3(1,0,0));
break;
}
}
if (e.type == SDL_KEYDOWN) {
bool shademode = true;
const float camera_speed = 0.4f;
switch (e.key.keysym.scancode) {
case SDL_SCANCODE_X:
if ((shademode = !shademode)) {
@ -544,8 +564,12 @@ int main(int argc, char **argv) { @@ -544,8 +564,12 @@ int main(int argc, char **argv) {
}
}
}
cont.update_camera();
float this_frametime = SDL_GetTicks() / 1000.0;
framestep = this_frametime - last_frametime;
last_frametime = this_frametime;
cont.player_data->update();
cont.update_camera();
cont.render();
cont.swap_window();

17
src/player.cpp

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
#include "player.h"
#include <glm/geometric.hpp>
#include "player.h"
#include "frametime.h"
namespace player {
@ -53,6 +54,7 @@ namespace player { @@ -53,6 +54,7 @@ namespace player {
}
void player::update() {
physics.pos += framestep * physics.velocity;
cam->pos = physics.pos;
}
@ -66,17 +68,18 @@ namespace player { @@ -66,17 +68,18 @@ namespace player {
direction = glm::normalize(direction);
auto left_direction = glm::cross(cam->front, cam->up);
amount += velocity * direction.x * left_direction;
amount += velocity * direction.y * cam->up;
amount += velocity * direction.z * cam->front;
amount += direction.x * left_direction;
amount += direction.y * cam->up;
amount += direction.z * cam->front;
amount = velocity * glm::normalize(amount);
if (move_mode == movement::WALKING) {
amount *= glm::vec3(1,0,1);
}
physics.pos += amount;
cam->pos = physics.pos;
physics.velocity = amount;
}

Loading…
Cancel
Save