Add files via upload
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Camera.cpp
|
||||
*
|
||||
* Created on: Feb 11, 2020
|
||||
* Author: MihailRis
|
||||
*/
|
||||
|
||||
#include "Camera.h"
|
||||
#include "Window.h"
|
||||
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
Camera::Camera(vec3 position, float fov) : position(position), fov(fov), rotation(1.0f) {
|
||||
updateVectors();
|
||||
}
|
||||
|
||||
void Camera::updateVectors(){
|
||||
front = vec3(rotation * vec4(0,0,-1,1));
|
||||
right = vec3(rotation * vec4(1,0,0,1));
|
||||
up = vec3(rotation * vec4(0,1,0,1));
|
||||
}
|
||||
|
||||
void Camera::rotate(float x, float y, float z){
|
||||
rotation = glm::rotate(rotation, z, vec3(0,0,1));
|
||||
rotation = glm::rotate(rotation, y, vec3(0,1,0));
|
||||
rotation = glm::rotate(rotation, x, vec3(1,0,0));
|
||||
|
||||
updateVectors();
|
||||
}
|
||||
|
||||
mat4 Camera::getProjection(){
|
||||
float aspect = (float)Window::width / (float)Window::height;
|
||||
return glm::perspective(fov, aspect, 0.1f, 1500.0f);
|
||||
}
|
||||
|
||||
mat4 Camera::getView(){
|
||||
return glm::lookAt(position, position+front, up);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Camera.h
|
||||
*
|
||||
* Created on: Feb 11, 2020
|
||||
* Author: MihailRis
|
||||
*/
|
||||
|
||||
#ifndef WINDOW_CAMERA_H_
|
||||
#define WINDOW_CAMERA_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
using namespace glm;
|
||||
|
||||
class Camera {
|
||||
void updateVectors();
|
||||
public:
|
||||
vec3 front;
|
||||
vec3 up;
|
||||
vec3 right;
|
||||
|
||||
vec3 position;
|
||||
float fov;
|
||||
mat4 rotation;
|
||||
Camera(vec3 position, float fov);
|
||||
|
||||
void rotate(float x, float y, float z);
|
||||
|
||||
mat4 getProjection();
|
||||
mat4 getView();
|
||||
};
|
||||
|
||||
#endif /* WINDOW_CAMERA_H_ */
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "Events.h"
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <string.h>
|
||||
|
||||
bool* Events::_keys;
|
||||
uint* Events::_frames;
|
||||
uint Events::_current = 0;
|
||||
float Events::deltaX = 0.0f;
|
||||
float Events::deltaY = 0.0f;
|
||||
float Events::x = 0.0f;
|
||||
float Events::y = 0.0f;
|
||||
bool Events::_cursor_locked = false;
|
||||
bool Events::_cursor_started = false;
|
||||
|
||||
#define _MOUSE_BUTTONS 1024
|
||||
|
||||
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
|
||||
if (Events::_cursor_started){
|
||||
Events::deltaX += xpos-Events::x;
|
||||
Events::deltaY += ypos-Events::y;
|
||||
}
|
||||
else {
|
||||
Events::_cursor_started = true;
|
||||
}
|
||||
Events::x = xpos;
|
||||
Events::y = ypos;
|
||||
}
|
||||
|
||||
void mouse_button_callback(GLFWwindow* window, int button, int action, int mode){
|
||||
if (action == GLFW_PRESS){
|
||||
Events::_keys[_MOUSE_BUTTONS+button] = true;
|
||||
Events::_frames[_MOUSE_BUTTONS+button] = Events::_current;
|
||||
}
|
||||
else if (action == GLFW_RELEASE){
|
||||
Events::_keys[_MOUSE_BUTTONS+button] = false;
|
||||
Events::_frames[_MOUSE_BUTTONS+button] = Events::_current;
|
||||
}
|
||||
}
|
||||
|
||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
|
||||
if (action == GLFW_PRESS){
|
||||
Events::_keys[key] = true;
|
||||
Events::_frames[key] = Events::_current;
|
||||
}
|
||||
else if (action == GLFW_RELEASE){
|
||||
Events::_keys[key] = false;
|
||||
Events::_frames[key] = Events::_current;
|
||||
}
|
||||
}
|
||||
|
||||
void window_size_callback(GLFWwindow* window, int width, int height){
|
||||
glViewport(0,0, width, height);
|
||||
Window::width = width;
|
||||
Window::height = height;
|
||||
}
|
||||
|
||||
int Events::initialize(){
|
||||
GLFWwindow* window = Window::window;
|
||||
_keys = new bool[1032];
|
||||
_frames = new uint[1032];
|
||||
|
||||
memset(_keys, false, 1032*sizeof(bool));
|
||||
memset(_frames, 0, 1032*sizeof(uint));
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||
glfwSetWindowSizeCallback(window, window_size_callback);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Events::pressed(int keycode){
|
||||
if (keycode < 0 || keycode >= _MOUSE_BUTTONS)
|
||||
return false;
|
||||
return _keys[keycode];
|
||||
}
|
||||
|
||||
bool Events::jpressed(int keycode){
|
||||
if (keycode < 0 || keycode >= _MOUSE_BUTTONS)
|
||||
return false;
|
||||
return _keys[keycode] && _frames[keycode] == _current;
|
||||
}
|
||||
|
||||
bool Events::clicked(int button){
|
||||
int index = _MOUSE_BUTTONS+button;
|
||||
return _keys[index];
|
||||
}
|
||||
|
||||
bool Events::jclicked(int button){
|
||||
int index = _MOUSE_BUTTONS+button;
|
||||
return _keys[index] && _frames[index] == _current;
|
||||
}
|
||||
|
||||
void Events::toogleCursor(){
|
||||
_cursor_locked = !_cursor_locked;
|
||||
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
||||
}
|
||||
|
||||
void Events::pullEvents(){
|
||||
_current++;
|
||||
deltaX = 0.0f;
|
||||
deltaY = 0.0f;
|
||||
glfwPollEvents();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
#include "Window.h"
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
class Events {
|
||||
public:
|
||||
static bool* _keys;
|
||||
static uint* _frames;
|
||||
static uint _current;
|
||||
static float deltaX;
|
||||
static float deltaY;
|
||||
static float x;
|
||||
static float y;
|
||||
static bool _cursor_locked;
|
||||
static bool _cursor_started;
|
||||
|
||||
static int initialize();
|
||||
static void pullEvents();
|
||||
|
||||
static bool pressed(int keycode);
|
||||
static bool jpressed(int keycode);
|
||||
|
||||
static bool clicked(int button);
|
||||
static bool jclicked(int button);
|
||||
|
||||
static void toogleCursor();
|
||||
};
|
||||
|
||||
#endif /* WINDOW_EVENTS_H_ */
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "Window.h"
|
||||
|
||||
GLFWwindow* Window::window;
|
||||
int Window::width = 0;
|
||||
int Window::height = 0;
|
||||
|
||||
int Window::initialize(int width, int height, const char* title){
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
|
||||
//glfwWindowHint(GLFW_SAMPLES, 2);
|
||||
|
||||
window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
||||
if (window == nullptr){
|
||||
std::cerr << "Failed to create GLFW Window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK){
|
||||
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
glViewport(0,0, width, height);
|
||||
|
||||
Window::width = width;
|
||||
Window::height = height;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Window::setCursorMode(int mode){
|
||||
glfwSetInputMode(window, GLFW_CURSOR, mode);
|
||||
}
|
||||
|
||||
void Window::terminate(){
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
bool Window::isShouldClose(){
|
||||
return glfwWindowShouldClose(window);
|
||||
}
|
||||
|
||||
void Window::setShouldClose(bool flag){
|
||||
glfwSetWindowShouldClose(window, flag);
|
||||
}
|
||||
|
||||
void Window::swapBuffers(){
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef WINDOW_WINDOW_H_
|
||||
#define WINDOW_WINDOW_H_
|
||||
|
||||
class GLFWwindow;
|
||||
|
||||
class Window {
|
||||
public:
|
||||
static int width;
|
||||
static int height;
|
||||
static GLFWwindow* window; // не лучшее решение делать window публичным
|
||||
static int initialize(int width, int height, const char* title);
|
||||
static void terminate();
|
||||
|
||||
static void setCursorMode(int mode);
|
||||
static bool isShouldClose();
|
||||
static void setShouldClose(bool flag);
|
||||
static void swapBuffers();
|
||||
};
|
||||
|
||||
#endif /* WINDOW_WINDOW_H_ */
|
||||
Reference in New Issue
Block a user