Skip to content

Commit

Permalink
camera2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lin0qlin committed Dec 6, 2024
1 parent b3878b4 commit 449086a
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 46 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ set(SOURCES
object.cpp
renderer.cpp
shader.cpp
camera.cpp
controls.cpp
fpscontrols.cpp
navigationcontrols.cpp
)

# 添加可执行文件
Expand All @@ -103,6 +107,10 @@ target_sources(ChessGame PRIVATE
object.h
renderer.h
shader.h
camera.h
controls.h
fpscontrols.h
navigationcontrols.h
)

# 链接库
Expand Down
62 changes: 62 additions & 0 deletions camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "camera.h"
#include "renderer.h"


Camera::Camera(float width, float height) : position(4.0f, 5.0f, 10.0f), horizontalAngle(3.14), verticalAngle(-0.4), FoV(45)
{
computeMatrices(width, height);
}


void Camera::computeMatrices(float width, float height)
{

glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);

// Right vector
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);

// Up vector : perpendicular to both direction and right
glm::vec3 up = glm::cross( right, direction );


// Projection matrix : 45&deg; Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
projectionMatrix = glm::perspective(glm::radians(FoV), width/height, 0.1f, 100.0f);
// Camera matrix
viewMatrix = glm::lookAt(
position, // Camera is here
position+direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
);

}

void Camera::setFoV(float newFoV)
{
FoV = newFoV;
}

const glm::mat4 &Camera::getViewMatrix() const
{
return viewMatrix;
}

const glm::mat4 &Camera::getProjectionMatrix() const
{
return projectionMatrix;
}

void Camera::Bind(Shader *shader)
{
shader->setUniform3fv("camPosition", position);
}


34 changes: 34 additions & 0 deletions camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef CAMERA_H
#define CAMERA_H

#include "glm/glm.hpp"
#include "glm/gtx/transform.hpp"
#include "shader.h"

class Camera
{
public:
Camera(float width, float height);
void computeMatrices(float width, float height);

glm::vec3 position;

float horizontalAngle;

float verticalAngle;

void setFoV(float newFoV);

const glm::mat4 &getViewMatrix() const;

const glm::mat4 &getProjectionMatrix() const;

void Bind(Shader *shader);

private:
float FoV;
glm::mat4 viewMatrix;
glm::mat4 projectionMatrix;
};

#endif // CAMERA_H
17 changes: 17 additions & 0 deletions controls.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "controls.h"

Controls::Controls(GLFWwindow* window, Camera *camera):m_Camera(camera),m_Window(window), speed(6), mouseSpeed(0.05)
{
glfwGetWindowSize(window, &width, &height);
}

void Controls::setSpeed(float newSpeed)
{
speed = newSpeed;
}

void Controls::setMouseSpeed(float newMouseSpeed)
{
mouseSpeed = newMouseSpeed;
}

26 changes: 26 additions & 0 deletions controls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef CONTROLS_H
#define CONTROLS_H

#include "camera.h"
#include <GLFW/glfw3.h>
#include "shader.h"

class Controls
{
public:
Controls(GLFWwindow* window, Camera *camera);
void setSpeed(float newSpeed);

void setMouseSpeed(float newMouseSpeed);
virtual void update(float deltaTime, Shader *shader)=0;

protected:
Camera *m_Camera;
float speed;
float mouseSpeed;
int width,height;

GLFWwindow* m_Window;
};

#endif // CONTROLS_H
82 changes: 82 additions & 0 deletions fpscontrols.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "fpscontrols.h"

FPSControls::FPSControls(GLFWwindow *window, Camera *camera):Controls(window, camera)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}

void FPSControls::update(float deltaTime, Shader *shader)
{
double xpos, ypos;
glfwGetCursorPos(m_Window, &xpos, &ypos);


glfwSetCursorPos(m_Window, width/2, height/2);




m_Camera->horizontalAngle+= mouseSpeed * deltaTime * float( width/2 - xpos );
m_Camera->verticalAngle += mouseSpeed * deltaTime * float(height/2 - ypos );

glm::vec3 direction(
cos(m_Camera->verticalAngle) * sin(m_Camera->horizontalAngle),
sin(m_Camera->verticalAngle),
cos(m_Camera->verticalAngle) * cos(m_Camera->horizontalAngle)
);

// Right vector
glm::vec3 right = glm::vec3(
sin(m_Camera->horizontalAngle - 3.14f/2.0f),
0,
cos(m_Camera->horizontalAngle - 3.14f/2.0f)
);

// Up vector : perpendicular to both direction and right
glm::vec3 up = glm::cross( right, direction );


// Move forward
if (glfwGetKey(m_Window, GLFW_KEY_UP ) == GLFW_PRESS){
m_Camera->position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(m_Window, GLFW_KEY_DOWN ) == GLFW_PRESS){
m_Camera->position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(m_Window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
m_Camera->position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(m_Window, GLFW_KEY_LEFT ) == GLFW_PRESS){
m_Camera->position -= right * deltaTime * speed;
}
// Move forward
if (glfwGetKey(m_Window, GLFW_KEY_W ) == GLFW_PRESS){
m_Camera->position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(m_Window, GLFW_KEY_S ) == GLFW_PRESS){
m_Camera->position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(m_Window, GLFW_KEY_D ) == GLFW_PRESS){
m_Camera->position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(m_Window, GLFW_KEY_A ) == GLFW_PRESS){
m_Camera->position -= right * deltaTime * speed;
}
// go up
if (glfwGetKey(m_Window, GLFW_KEY_SPACE ) == GLFW_PRESS){
m_Camera->position += up * deltaTime * speed;
}
// go down
if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS){
m_Camera->position -= up * deltaTime * speed;
}

shader->setUniform3fv("ambiantLight", glm::vec3(0.1,0.1,0.1));

}
13 changes: 13 additions & 0 deletions fpscontrols.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef FPSCONTROLS_H
#define FPSCONTROLS_H

#include "controls.h"

class FPSControls : public Controls
{
public:
FPSControls(GLFWwindow* window, Camera *camera);
void update(float deltaTime, Shader* shader);
};

#endif // FPSCONTROLS_H
50 changes: 35 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "renderer.h"
#include "shader.h"
#include <vector>
#include <ctime>
#include "renderer.h"
#include "shader.h"
#include "camera.h"
#include "navigationcontrols.h"


ChessBoard chessBoard;
std::vector<ChessPiece> chessPieces;
bool isWhiteTurn = true;
Renderer renderer;
Camera* camera;
NavigationControls* controls;

void setupOpenGL() {
glEnable(GL_DEPTH_TEST); // 启用深度测试
glClearColor(0.0f, 0.0f, 0.3f, 0.1f); // background
// glEnable(GL_LIGHTING);
// glEnable(GL_LIGHT0);


// 设置投影矩阵为正交投影
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluOrtho2D(0.0, 8.0, 0.0, 8.0); // 设置为适合棋盘的范围(0-8)
gluPerspective(45.0, 1.0, 1.0, 100.0); // 透视投影 (45°视角, 宽高比为1, 近平面1, 远平面100)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4.0, 10.0, 20.0, // 相机位置 (x, y, z)
4.0, 0.0, 0.0, // 相机观察点 (中心位置)
0.0, 1.0, 0.0); // 相机的向上向量
glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // 深灰色背景
// // 设置投影矩阵为正交投影
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// // gluOrtho2D(0.0, 8.0, 0.0, 8.0); // 设置为适合棋盘的范围(0-8)
// gluPerspective(45.0, 1.0, 1.0, 100.0); // 透视投影 (45°视角, 宽高比为1, 近平面1, 远平面100)
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// gluLookAt(4.0, 10.0, 20.0, // 相机位置 (x, y, z)
// 4.0, 0.0, 0.0, // 相机观察点 (中心位置)
// 0.0, 1.0, 0.0); // 相机的向上向量

}


Expand Down Expand Up @@ -133,9 +139,17 @@ void gameLoop(GLFWwindow* window) {
}
*/



void initializeCamera(GLFWwindow* window, float width, float height) {
camera = new Camera(width, height);
controls = new NavigationControls(window, camera);
}


void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderer.render(chessBoard, chessPieces);
renderer.render(chessBoard, chessPieces, camera->getViewMatrix(), camera->getProjectionMatrix());
glfwSwapBuffers(glfwGetCurrentContext());
}

Expand All @@ -154,16 +168,22 @@ int main() {

setupOpenGL(); // 设置 OpenGL 状态
initializeGame(); // 初始化棋盘和棋子
srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子
initializeCamera(window, 800, 800); // 初始化摄像机
// srand(static_cast<unsigned int>(time(0))); // 初始化随机数种子

// 主循环
while (!glfwWindowShouldClose(window)) {
// gameLoop(window); // 处理用户输入和游戏逻辑
float deltaTime = 0.016f; // 简化的时间步长,实际可用时间函数计算
controls->update(deltaTime, nullptr); // 更新摄像机控制
render(); // 渲染棋盘和棋子
glfwSwapBuffers(window); // 刷新窗口
glfwPollEvents(); // 处理窗口事件
}

// 清理
delete camera;
delete controls;
glfwDestroyWindow(window);
glfwTerminate();
return 0;
Expand Down
Loading

0 comments on commit 449086a

Please sign in to comment.