servicable pan-move-rotate
This commit is contained in:
42
src/main.cpp
42
src/main.cpp
@@ -4,6 +4,7 @@
|
|||||||
#include <glm/ext/matrix_clip_space.hpp>
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
#include <glm/ext/vector_float3.hpp>
|
#include <glm/ext/vector_float3.hpp>
|
||||||
|
#include <glm/matrix.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -82,27 +83,27 @@ static bool scroll_pressed = false;
|
|||||||
static double prev_cursor_x, prev_cursor_y;
|
static double prev_cursor_x, prev_cursor_y;
|
||||||
|
|
||||||
static double theta = 0.0; // angle of camera trans vect wrt x-z plane
|
static double theta = 0.0; // angle of camera trans vect wrt x-z plane
|
||||||
static double phi = 0.0; // angle of camera trans vect wrt x-axis
|
static double phi = glm::radians(270.0); // angle of camera trans vect wrt x-axis
|
||||||
|
|
||||||
static glm::vec3 camera_loc = glm::vec3(0, 0, 2);
|
|
||||||
static glm::vec3 focal_point = glm::vec3(0, 0, 0);
|
static glm::vec3 focal_point = glm::vec3(0, 0, 0);
|
||||||
|
static glm::vec3 camera_loc = glm::vec3(0, 0, -1);
|
||||||
static glm::vec3 up = glm::vec3(0, 1, 0);
|
static glm::vec3 up = glm::vec3(0, 1, 0);
|
||||||
static glm::mat4 camera_t = glm::lookAt(camera_loc, focal_point, up);
|
static glm::mat4 world_to_camera = glm::lookAt(camera_loc, focal_point, up);
|
||||||
|
|
||||||
static glm::vec4 strafe_x = camera_t[0];
|
|
||||||
static glm::vec4 strafe_y = camera_t[1];
|
|
||||||
|
|
||||||
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||||
float dx = (xpos - prev_cursor_x);
|
float dx = (xpos - prev_cursor_x);
|
||||||
float dy = (ypos - prev_cursor_y);
|
float dy = (ypos - prev_cursor_y);
|
||||||
strafe_x = camera_t[0];
|
|
||||||
strafe_y = camera_t[1];
|
glm::mat4 camera_to_world = glm::inverse(world_to_camera);
|
||||||
|
glm::vec4 strafe_x = camera_to_world[0];
|
||||||
|
glm::vec4 strafe_y = camera_to_world[1];
|
||||||
prev_cursor_x = xpos;
|
prev_cursor_x = xpos;
|
||||||
prev_cursor_y = ypos;
|
prev_cursor_y = ypos;
|
||||||
|
|
||||||
if (mouse_pressed) {
|
if (mouse_pressed) {
|
||||||
phi += dx / width * glm::radians(360.0);
|
phi += glm::radians(dx * (360/width));// * glm::radians(360.0);
|
||||||
theta += dy / height * glm::radians(360.0);
|
theta += glm::radians(dy * (360/height));// * glm::radians(360.0);
|
||||||
double len = glm::length(camera_loc - focal_point);
|
double len = glm::length(camera_loc - focal_point);
|
||||||
camera_loc.x = focal_point.x + (len * glm::cos(theta) * glm::cos(-phi));
|
camera_loc.x = focal_point.x + (len * glm::cos(theta) * glm::cos(-phi));
|
||||||
camera_loc.y = focal_point.y + (len * glm::sin(theta));
|
camera_loc.y = focal_point.y + (len * glm::sin(theta));
|
||||||
@@ -115,7 +116,7 @@ static void cursor_position_callback(GLFWwindow* window, double xpos, double ypo
|
|||||||
camera_loc += move_vec;
|
camera_loc += move_vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
camera_t = glm::lookAt(camera_loc, focal_point, up);
|
world_to_camera = glm::lookAt(camera_loc, focal_point, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
||||||
@@ -125,20 +126,15 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
|
|||||||
|
|
||||||
if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
|
if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
|
||||||
scroll_pressed = action == GLFW_PRESS;
|
scroll_pressed = action == GLFW_PRESS;
|
||||||
strafe_x = camera_t[0];
|
|
||||||
strafe_y = camera_t[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwGetCursorPos(window, &prev_cursor_x, &prev_cursor_y);
|
glfwGetCursorPos(window, &prev_cursor_x, &prev_cursor_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||||
glm::vec4 k = camera_t[2];
|
glm::vec3 k = camera_loc - focal_point;
|
||||||
float d = yoffset;
|
camera_loc -= k * (float)yoffset / 10.0f;
|
||||||
|
world_to_camera = glm::lookAt(camera_loc, focal_point, up);
|
||||||
camera_loc += k * (d / 10 * glm::max(glm::length(camera_loc), 1.0f));
|
|
||||||
|
|
||||||
camera_t = glm::lookAt(camera_loc, focal_point, up);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool glfw_setup() {
|
bool glfw_setup() {
|
||||||
@@ -187,7 +183,7 @@ int main() {
|
|||||||
glfwSwapBuffers(window); // front buffer is now back
|
glfwSwapBuffers(window); // front buffer is now back
|
||||||
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf)
|
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf)
|
||||||
|
|
||||||
/* body b;
|
body b;
|
||||||
assert(load_body(&b, "Icosphere.obj"));
|
assert(load_body(&b, "Icosphere.obj"));
|
||||||
b.shader = shader;
|
b.shader = shader;
|
||||||
b.pose = glm::translate(b.pose, glm::vec3(5, 5, 5));
|
b.pose = glm::translate(b.pose, glm::vec3(5, 5, 5));
|
||||||
@@ -195,7 +191,7 @@ int main() {
|
|||||||
|
|
||||||
body b2;
|
body b2;
|
||||||
create_new_sphere(&b2);
|
create_new_sphere(&b2);
|
||||||
b2.color = glm::vec4(0, 0.5, 0, 1);*/
|
b2.color = glm::vec4(0, 0.5, 0, 1);
|
||||||
|
|
||||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
// set_uniform(shader, "color", glm::vec4 { sin(time), sin(time + glm::radians(45.0f)), sin(time +
|
// set_uniform(shader, "color", glm::vec4 { sin(time), sin(time + glm::radians(45.0f)), sin(time +
|
||||||
@@ -213,11 +209,11 @@ int main() {
|
|||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
process_input();
|
process_input();
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
set_uniform(shader, "camera_t", camera_t);
|
set_uniform(shader, "camera_t", world_to_camera);
|
||||||
set_uniform(shader, "projection_t", projection_t);
|
set_uniform(shader, "projection_t", projection_t);
|
||||||
|
|
||||||
/*draw_body(b);
|
draw_body(b);
|
||||||
draw_body(b2);*/
|
draw_body(b2);
|
||||||
|
|
||||||
pthread_mutex_lock(&lock);
|
pthread_mutex_lock(&lock);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user