feat(viewport): add camera class
This commit is contained in:
@@ -25,6 +25,7 @@ qt_add_executable(${AppExec}
|
||||
src/gui/Interface.cpp
|
||||
src/gui/viewport/Viewport.cpp
|
||||
src/gui/viewport/ViewportGLWidget.cpp
|
||||
src/gui/viewport/GLCamera.cpp
|
||||
src/gui/network/NetworkGraphicsView.cpp
|
||||
src/gui/network/NetworkGraphicsScene.cpp
|
||||
src/gui/network/Network.cpp
|
||||
|
||||
134
src/gui/viewport/GLCamera.cpp
Normal file
134
src/gui/viewport/GLCamera.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "gui/viewport/GLCamera.h"
|
||||
#include <glm/ext/quaternion_geometric.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/geometric.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <iostream>
|
||||
#include <glm/ext/quaternion_trigonometric.hpp>
|
||||
#include <glm/ext/quaternion_float.hpp>
|
||||
|
||||
void printMatrix(const glm::mat4& matrix) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
std::cout << matrix[i][j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
void printVec(const glm::vec3& vector) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
std::cout << vector[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void GLCamera::changeCenter(float x, float y, float z)
|
||||
{
|
||||
camCenter_.x += x;
|
||||
camCenter_.y += y;
|
||||
camCenter_.z += z;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
void GLCamera::setCenter(float x, float y, float z)
|
||||
{
|
||||
camCenter_.x = x;
|
||||
camCenter_.y = y;
|
||||
camCenter_.z = z;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
glm::vec3 GLCamera::getUp()
|
||||
{
|
||||
return glm::normalize(glm::cross(getForward(), getRight()));
|
||||
}
|
||||
|
||||
|
||||
GLCamera::GLCamera()
|
||||
: GLCamera(0.0f, 0.0f, 10.0f)
|
||||
{
|
||||
}
|
||||
|
||||
GLCamera::GLCamera(float posX, float posY, float posZ)
|
||||
{
|
||||
setPos(posX, posY, posZ);
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
glm::mat4 GLCamera::getViewMatrix()
|
||||
{
|
||||
return viewMatrix_;
|
||||
}
|
||||
|
||||
void GLCamera::setPos(float x, float y, float z)
|
||||
{
|
||||
camPos_.x = x;
|
||||
camPos_.y = y;
|
||||
camPos_.z = z;
|
||||
}
|
||||
|
||||
void GLCamera::movePos(float x, float y, float z)
|
||||
{
|
||||
camPos_.x += x;
|
||||
camPos_.y += y;
|
||||
camPos_.z += z;
|
||||
}
|
||||
|
||||
|
||||
void GLCamera::rotateAroundCenter(float angle, glm::vec3 axis)
|
||||
{
|
||||
glm::mat4 rotMatrix = glm::rotate(glm::mat4(1.0f), angle, axis);
|
||||
|
||||
axis = glm::normalize(axis);
|
||||
// build a quaternion for this rotation
|
||||
glm::quat q = glm::angleAxis(angle, axis);
|
||||
// rotate the position vector
|
||||
camPos_ = q * camPos_;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
void GLCamera::changeRadius(float delta)
|
||||
{
|
||||
glm::vec3 centerDir = glm::normalize(camPos_-camCenter_);
|
||||
camPos_+=centerDir*delta;
|
||||
|
||||
viewMatrix_ = glm::lookAt(
|
||||
camPos_,
|
||||
camCenter_,
|
||||
camUp_
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
glm::vec3 GLCamera::getRight()
|
||||
{
|
||||
return glm::cross(getForward(), camUp_);
|
||||
};
|
||||
|
||||
glm::vec3 GLCamera::getForward()
|
||||
{
|
||||
return glm::normalize(camCenter_-camPos_);
|
||||
};
|
||||
|
||||
28
src/gui/viewport/GLCamera.h
Normal file
28
src/gui/viewport/GLCamera.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class GLCamera
|
||||
{
|
||||
public:
|
||||
GLCamera();
|
||||
GLCamera(float posX, float posY, float posZ);
|
||||
glm::mat4 getViewMatrix();
|
||||
void setPos(float x, float y, float z);
|
||||
void movePos(float x, float y, float z);
|
||||
void rotateAroundCenter(float angle, glm::vec3 axis);
|
||||
void changeRadius(float delta);
|
||||
void changeCenter(float x, float y, float z);
|
||||
void setCenter(float x, float y, float z);
|
||||
|
||||
glm::vec3 getForward();
|
||||
glm::vec3 getRight();
|
||||
glm::vec3 getUp();
|
||||
|
||||
|
||||
private:
|
||||
glm::mat4 viewMatrix_{1.0f};
|
||||
glm::vec3 camPos_{0.0f,0.0f,0.0f};
|
||||
glm::vec3 camCenter_{0.0f,0.0f,0.0f};
|
||||
glm::vec3 camUp_{0.0f,1.0f,0.0f};
|
||||
};
|
||||
@@ -14,10 +14,14 @@ void ViewportGLWidget::initializeGL()
|
||||
std::cout << "format: " << (fmt.renderableType() == QSurfaceFormat::OpenGL ? "true" : "false") << "\n";
|
||||
std::cout << "hello\n";
|
||||
|
||||
// init loop
|
||||
QTimer* loopTimer = new QTimer(this);
|
||||
connect(loopTimer, &QTimer::timeout, this, QOverload<>::of(&QOpenGLWidget::update));
|
||||
loopTimer->start(16);
|
||||
|
||||
// init camera
|
||||
camera_ = GLCamera();
|
||||
|
||||
|
||||
|
||||
glGenVertexArrays(1, &vao);
|
||||
@@ -119,8 +123,6 @@ void ViewportGLWidget::paintGL()
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
angle_+=0.01;
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
|
||||
glm::mat4 projMatrix = glm::perspective(
|
||||
@@ -131,11 +133,14 @@ void ViewportGLWidget::paintGL()
|
||||
);
|
||||
|
||||
|
||||
glm::mat4 viewMatrix = glm::lookAt(
|
||||
glm::vec3(sin(angle_)*5, 1, cos(angle_)*5),
|
||||
glm::vec3(0,0,0),
|
||||
glm::vec3(0,1,0)
|
||||
);
|
||||
// glm::mat4 viewMatrix = glm::lookAt(
|
||||
// glm::vec3(sin(angle_)*5, 1, cos(angle_)*5),
|
||||
// glm::vec3(0,0,0),
|
||||
// glm::vec3(0,1,0)
|
||||
// );
|
||||
camera_.rotateAroundCenter(0.01, glm::vec3(0,1,0));
|
||||
glm::mat4 viewMatrix = camera_.getViewMatrix();
|
||||
|
||||
|
||||
GLint projMLoc = glGetUniformLocation(shaderProgram, "uProj");
|
||||
glUniformMatrix4fv(projMLoc, 1, GL_FALSE, glm::value_ptr(projMatrix));
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <QOpenGLWidget>
|
||||
#include <iostream>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include "gui/viewport/GLCamera.h"
|
||||
|
||||
class ViewportGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_2_Core
|
||||
{
|
||||
@@ -11,7 +12,7 @@ public:
|
||||
QSize sizeHint() const override { return QSize(-1, -1); }
|
||||
GLuint vao;
|
||||
GLuint shaderProgram;
|
||||
float angle_ = 0.0f;
|
||||
GLCamera camera_;
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
|
||||
Reference in New Issue
Block a user