Created
April 3, 2019 12:58
-
-
Save roxlu/b5b324c071a0a74840112b9f5cb89bb1 to your computer and use it in GitHub Desktop.
Very rough veresion of turn table camera.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Cam::init(const vec3& p, const vec3& t, const vec3& u, float winWidth, float winHeight) { | |
win_width = winWidth; | |
win_height = winHeight; | |
dir = t - p; | |
pos = p; | |
target = t; | |
up = u; | |
pm.perspective(60.0f, winWidth / winHeight, 0.1f, 100.0f); | |
lm.lookat(pos, target, up); | |
} | |
void Cam::update() { | |
lm.lookat(target - (dir * zoom), target, up); | |
mat4 trans; | |
trans.translate(pan.x, pan.y, 0.0); | |
mat4 rot; | |
rot.rotateY(yaw); | |
rot.rotateX(pitch); | |
vm = lm * trans * rot; | |
} | |
void Cam::onMouseDown(float x, float y) { | |
screen_to_world = pm * vm; | |
screen_to_world.invert(); | |
mouse_down_pos = screenToWorld(x, y); | |
is_down = true; | |
} | |
void Cam::onMouseMove(float x, float y) { | |
if (false == is_down) { | |
return; | |
} | |
vec4 mouse_move = screenToWorld(x, y); | |
pan.x = mouse_move.x - mouse_down_pos.x; | |
pan.y = mouse_move.y - mouse_down_pos.y; | |
} | |
void Cam::onMouseUp() { | |
is_down = false; | |
} | |
vec4 Cam::screenToWorld(float x, float y) { | |
float nx = 2.0 * x / win_width - 1.0; | |
float ny = -2.0 * y / win_height + 1.0; | |
vec4 world_pos(nx, ny, -1.0f, 1.0f); | |
return screen_to_world * world_pos; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cam { | |
public: | |
void init(const vec3& pos, const vec3& target, const vec3& up, float winWidth, float winHeight); | |
void update(); | |
void onMouseDown(float x, float y); | |
void onMouseMove(float x, float y); | |
void onMouseUp(); | |
vec4 screenToWorld(float x, float y); | |
public: | |
float win_width = 0.0f; | |
float win_height = 0.0f; | |
float zoom = 1.0f; | |
vec3 pos; | |
vec3 target; | |
vec3 up; | |
vec3 dir; | |
vec2 pan; | |
mat4 pm; /* Projection Matrix. */ | |
mat4 lm; /* Lookat matrix */ | |
mat4 vm; /* View matrix. */ | |
float yaw = 0.0f; /* Around Y-axis. */ | |
float pitch = 0.0f; /* Around X-axis. */ | |
mat4 screen_to_world; /* Inverted projection matrix. */ | |
vec4 mouse_down_pos; | |
bool is_down = false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment