3
社区成员
发帖
与我相关
我的任务
分享
头文件CandyRenderEngine/Transform.h
//===========================================================
// 文件: Transform.h
// 路径: CandyRenderEngine/Transform.h
// 说明: 变换组件头文件
//===========================================================
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Transform {
public:
Transform();
void SetPosition(const glm::vec3& position);
void SetPosition(float x, float y, float z);
void Translate(const glm::vec3& delta);
void Translate(float x, float y, float z);
glm::vec3 GetPosition() const { return m_Position; }
void SetRotation(const glm::vec3& rotation);
void SetRotation(float pitch, float yaw, float roll);
void Rotate(const glm::vec3& delta);
void Rotate(float pitch, float yaw, float roll);
glm::vec3 GetRotation() const { return m_Rotation; }
void SetScale(const glm::vec3& scale);
void SetScale(float uniform);
void SetScale(float x, float y, float z);
void Scale(const glm::vec3& delta);
void Scale(float factor);
glm::vec3 GetScale() const { return m_Scale; }
glm::mat4 GetModelMatrix() const;
glm::vec3 GetForward() const { return m_Forward; }
glm::vec3 GetRight() const { return m_Right; }
glm::vec3 GetUp() const { return m_Up; }
void Reset();
private:
void UpdateVectors();
glm::vec3 m_Position;
glm::vec3 m_Rotation;
glm::vec3 m_Scale;
glm::vec3 m_Forward;
glm::vec3 m_Right;
glm::vec3 m_Up;
};
//===========================================================
// 文件: Texture.h
// 路径: CandyRenderEngine/Texture.h
// 说明: 纹理管理头文件
//===========================================================
#pragma once
#include <glad/glad.h>
#include <string>
class Texture
{
public:
Texture();
explicit Texture(const std::string& path);
Texture(const std::string& path, const std::string& type);
~Texture();
bool LoadFromFile(const std::string& path);
void Bind(unsigned int slot = 0) const;
void Unbind() const;
GLuint GetID() const { return m_ID; }
int GetWidth() const { return m_Width; }
int GetHeight() const { return m_Height; }
const std::string& GetPath() const { return m_Path; }
const std::string& GetType() const { return m_Type; }
void SetType(const std::string& type) { m_Type = type; }
private:
GLuint m_ID;
int m_Width;
int m_Height;
std::string m_Path;
std::string m_Type;
};
//===========================================================
// 文件: Shader.h
// 路径: CandyRenderEngine/Core/Shader.h
// 说明: 着色器管理头文件
//===========================================================
#pragma once
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string>
#include <unordered_map>
class Shader {
public:
Shader();
~Shader();
bool LoadFromFile(const std::string& vertexPath, const std::string& fragmentPath);
bool LoadFromSource(const std::string& vertexSource, const std::string& fragmentSource);
void Use() const;
void Unuse() const;
// Uniform 设置
void SetBool(const std::string& name, bool value) const;
void SetInt(const std::string& name, int value) const;
void SetFloat(const std::string& name, float value) const;
void SetVec2(const std::string& name, const glm::vec2& value) const;
void SetVec3(const std::string& name, const glm::vec3& value) const;
void SetVec4(const std::string& name, const glm::vec4& value) const;
void SetMat2(const std::string& name, const glm::mat2& mat) const;
void SetMat3(const std::string& name, const glm::mat3& mat) const;
void SetMat4(const std::string& name, const glm::mat4& mat) const;
GLuint GetID() const { return m_ID; }
bool IsValid() const { return m_ID != 0; }
private:
GLuint CompileShader(GLenum type, const std::string& source);
bool LinkProgram(GLuint vertexShader, GLuint fragmentShader);
bool CheckCompileErrors(GLuint shader, const std::string& type);
bool CheckLinkErrors(GLuint program);
GLint GetUniformLocation(const std::string& name) const;
std::string ReadFile(const std::string& filePath);
GLuint m_ID;
mutable std::unordered_map<std::string, GLint> m_UniformLocationCache;
};
//===========================================================
// 文件: Scene.h
// 路径: CandyRenderEngine/Scene.h
// 说明: 场景管理头文件
//===========================================================
#pragma once
#include <vector>
#include <memory>
#include <string>
#include <glm/glm.hpp>
#include "Model.h"
#include "Camera.h"
#include "Light.h"
class Shader;
class Scene {
public:
Scene();
explicit Scene(const std::string& name);
~Scene();
void SetName(const std::string& name) { m_Name = name; }
const std::string& GetName() const { return m_Name; }
void AddModel(std::shared_ptr<Model> model);
void RemoveModel(Model* model);
void RemoveModel(size_t index);
void ClearModels();
size_t GetModelCount() const { return m_Models.size(); }
Model* GetModel(size_t index) const;
Model* FindModelByName(const std::string& name) const;
void AddLight(std::shared_ptr<Light> light);
void RemoveLight(Light* light);
void ClearLights();
size_t GetLightCount() const { return m_Lights.size(); }
Light* GetLight(size_t index) const;
void SetMainCamera(std::shared_ptr<Camera> camera);
Camera* GetMainCamera() const { return m_MainCamera.get(); }
void Render(Shader& shader) const;
void Update(float deltaTime);
void Clear();
private:
std::string m_Name;
std::vector<std::shared_ptr<Model>> m_Models;
std::vector<std::shared_ptr<Light>> m_Lights;
std::shared_ptr<Camera> m_MainCamera;
float m_CurrentTime;
};
//===========================================================
// 文件: Renderer.h
// 路径: CandyRenderEngine/Renderer.h
// 说明: 主渲染器头文件
//===========================================================
#pragma once
#include "pch.h"
// 前向声明
class Camera;
class Transform;
class Renderer {
public:
Renderer();
~Renderer();
bool Initialize(HWND parentHwnd, int width, int height);
void Shutdown();
void RenderFrame();
void Resize(int width, int height);
// 相机控制
void ProcessMouseMovement(float xOffset, float yOffset);
void ProcessMouseScroll(float yOffset);
void ProcessPan(float xOffset, float yOffset);
void ResetCamera();
void FitView();
// 模型变换
void SetModelRotation(float x, float y, float z);
void SetModelScale(float scale);
void SetModelPosition(float x, float y, float z);
// 渲染设置
void SetWireframeMode(bool enabled);
void ShowGrid(bool show);
// 获取信息
void GetCameraPosition(float& x, float& y, float& z);
//
bool GetWireframeMode() const { return m_WireframeMode; }
bool IsGridVisible() const { return m_ShowGrid; }
private:
void CreateCube();
void CreateGrid();
void CompileShaders();
HWND hwnd = nullptr;
HDC hdc = nullptr;
HGLRC hglrc = nullptr;
bool initialized = false;
// OpenGL 对象
GLuint shaderProgram = 0;
GLuint gridProgram = 0;
GLuint VAO = 0;
GLuint VBO = 0;
GLuint EBO = 0;
GLuint gridVAO = 0;
GLuint gridVBO = 0;
// 相机和变换(指针,前向声明即可)
Camera* m_Camera;
Transform* m_ModelTransform;
// 渲染标志
bool m_WireframeMode = false;
bool m_ShowGrid = true;
};
//===========================================================
// 文件: pch.h
// 路径: CandyRenderEngine/pch.h
// 说明: 预编译头文件
//===========================================================
#pragma once
// Windows 头文件
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// 标准库头文件
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <memory>
#include <unordered_map>
#include <algorithm>
#include <cmath>
// OpenGL 头文件(GLAD 必须第一个)
#include <glad/glad.h>
// GLM 数学库
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//===========================================================
// 文件: Model.h
// 路径: CandyRenderEngine/Model.h
// 说明: 模型管理头文件,负责加载和管理多个 Mesh
// 依赖: Mesh.h, Shader.h, Transform.h
//===========================================================
#pragma once
#include <vector>
#include <string>
#include <memory>
#include <glm/glm.hpp>
#include "Mesh.h"
#include "Transform.h"
// 前置声明
class Shader;
class Model {
public:
Model();
explicit Model(const std::string& path);
~Model();
// ===== 加载模型 =====
bool LoadModel(const std::string& path);
// ===== 渲染 =====
void Draw(Shader& shader) const;
// ===== 网格管理 =====
void AddMesh(std::shared_ptr<Mesh> mesh);
void ClearMeshes();
size_t GetMeshCount() const { return m_Meshes.size(); }
Mesh* GetMesh(size_t index) const;
// ===== 变换 =====
Transform& GetTransform() { return m_Transform; }
const Transform& GetTransform() const { return m_Transform; }
void SetPosition(const glm::vec3& position);
void SetRotation(const glm::vec3& rotation);
void SetScale(const glm::vec3& scale);
// ===== 模型信息 =====
const std::string& GetName() const { return m_Name; }
void SetName(const std::string& name) { m_Name = name; }
// ===== 包围盒 =====
void CalculateBoundingBox();
glm::vec3 GetMinBounds() const { return m_MinBounds; }
glm::vec3 GetMaxBounds() const { return m_MaxBounds; }
glm::vec3 GetCenter() const { return (m_MinBounds + m_MaxBounds) * 0.5f; }
float GetRadius() const;
// ===== 可见性 =====
void SetVisible(bool visible) { m_Visible = visible; }
bool IsVisible() const { return m_Visible; }
private:
std::vector<std::shared_ptr<Mesh>> m_Meshes;
Transform m_Transform;
std::string m_Name;
bool m_Visible;
// 包围盒
glm::vec3 m_MinBounds;
glm::vec3 m_MaxBounds;
};
//===========================================================
// 文件: Mesh.h
// 路径: CandyRenderEngine/Mesh.h
// 说明: 网格数据管理头文件
//===========================================================
#pragma once
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <vector>
#include <string>
#include <memory>
#include "Texture.h"
// 顶点结构体
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoords;
glm::vec3 Tangent;
glm::vec3 Bitangent;
Vertex() : Position(0.0f), Normal(0.0f), TexCoords(0.0f), Tangent(0.0f), Bitangent(0.0f) {}
Vertex(const glm::vec3& pos, const glm::vec3& norm, const glm::vec2& tex)
: Position(pos), Normal(norm), TexCoords(tex), Tangent(0.0f), Bitangent(0.0f) {
}
};
class Shader;
class Mesh {
public:
Mesh();
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices);
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices,
const std::vector<std::shared_ptr<Texture>>& textures);
~Mesh();
void Draw(Shader& shader) const;
void SetVertices(const std::vector<Vertex>& vertices);
void SetIndices(const std::vector<unsigned int>& indices);
void SetTextures(const std::vector<std::shared_ptr<Texture>>& textures);
const std::vector<Vertex>& GetVertices() const { return m_Vertices; }
const std::vector<unsigned int>& GetIndices() const { return m_Indices; }
const std::vector<std::shared_ptr<Texture>>& GetTextures() const { return m_Textures; }
size_t GetVertexCount() const { return m_Vertices.size(); }
size_t GetIndexCount() const { return m_Indices.size(); }
size_t GetTextureCount() const { return m_Textures.size(); }
void Clear();
private:
void SetupMesh();
std::vector<Vertex> m_Vertices;
std::vector<unsigned int> m_Indices;
std::vector<std::shared_ptr<Texture>> m_Textures;
GLuint m_VAO;
GLuint m_VBO;
GLuint m_EBO;
bool m_IsInitialized;
};
//===========================================================
// 文件: Material.h
// 路径: CandyRenderEngine/Material.h
// 说明: 材质系统头文件,管理物体的材质属性
// 依赖: GLM, Texture.h
//===========================================================
#pragma once
#include <glm/glm.hpp>
#include <memory>
#include <string>
#include <unordered_map>
class Texture;
struct MaterialProperties {
glm::vec3 Ambient; // 环境光反射
glm::vec3 Diffuse; // 漫反射
glm::vec3 Specular; // 高光反射
float Shininess; // 光泽度
float Opacity; // 不透明度
float Metallic; // 金属度 (PBR)
float Roughness; // 粗糙度 (PBR)
MaterialProperties()
: Ambient(0.2f, 0.2f, 0.2f)
, Diffuse(0.8f, 0.8f, 0.8f)
, Specular(0.5f, 0.5f, 0.5f)
, Shininess(32.0f)
, Opacity(1.0f)
, Metallic(0.0f)
, Roughness(0.5f) {
}
};
class Shader;
class Material {
public:
Material();
explicit Material(const MaterialProperties& properties);
~Material();
// ===== 材质属性 =====
void SetProperties(const MaterialProperties& properties);
const MaterialProperties& GetProperties() const { return m_Properties; }
void SetAmbient(const glm::vec3& ambient) { m_Properties.Ambient = ambient; }
void SetDiffuse(const glm::vec3& diffuse) { m_Properties.Diffuse = diffuse; }
void SetSpecular(const glm::vec3& specular) { m_Properties.Specular = specular; }
void SetShininess(float shininess) { m_Properties.Shininess = shininess; }
void SetMetallic(float metallic) { m_Properties.Metallic = metallic; }
void SetRoughness(float roughness) { m_Properties.Roughness = roughness; }
// ===== 纹理管理 =====
void SetTexture(const std::string& type, std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> GetTexture(const std::string& type) const;
void ClearTextures();
// ===== 应用到着色器 =====
void Apply(Shader& shader) const;
// ===== 名称 =====
void SetName(const std::string& name) { m_Name = name; }
const std::string& GetName() const { return m_Name; }
private:
MaterialProperties m_Properties;
std::unordered_map<std::string, std::shared_ptr<Texture>> m_Textures;
std::string m_Name;
};
//===========================================================
// 文件: Light.h
// 路径: CandyRenderEngine/Light.h
// 说明: 光源管理头文件,支持方向光、点光源、聚光灯
//===========================================================
#pragma once
#include <glm/glm.hpp>
enum class LightType {
Directional, // 方向光(太阳)
Point, // 点光源(灯泡)
Spot // 聚光灯(手电筒)
};
class Light {
public:
Light();
explicit Light(LightType type);
// ===== 光源属性 =====
void SetType(LightType type) { m_Type = type; }
LightType GetType() const { return m_Type; }
void SetPosition(const glm::vec3& position) { m_Position = position; }
glm::vec3 GetPosition() const { return m_Position; }
void SetDirection(const glm::vec3& direction) { m_Direction = glm::normalize(direction); }
glm::vec3 GetDirection() const { return m_Direction; }
void SetColor(const glm::vec3& color) { m_Color = color; }
glm::vec3 GetColor() const { return m_Color; }
void SetIntensity(float intensity) { m_Intensity = intensity; }
float GetIntensity() const { return m_Intensity; }
// ===== 点光源/聚光灯特有 =====
void SetRange(float range) { m_Range = range; }
float GetRange() const { return m_Range; }
void SetAttenuation(float constant, float linear, float quadratic);
void GetAttenuation(float& constant, float& linear, float& quadratic) const;
// ===== 聚光灯特有 =====
void SetSpotAngle(float innerAngle, float outerAngle);
float GetInnerAngle() const { return m_InnerAngle; }
float GetOuterAngle() const { return m_OuterAngle; }
// ===== 启用/禁用 =====
void SetEnabled(bool enabled) { m_Enabled = enabled; }
bool IsEnabled() const { return m_Enabled; }
private:
LightType m_Type;
glm::vec3 m_Position;
glm::vec3 m_Direction;
glm::vec3 m_Color;
float m_Intensity;
float m_Range;
float m_ConstantAttenuation;
float m_LinearAttenuation;
float m_QuadraticAttenuation;
float m_InnerAngle;
float m_OuterAngle;
bool m_Enabled;
};
//===========================================================
// 文件: Frustum.h
// 路径: CandyRenderEngine/Frustum.h
// 说明: 视锥体头文件,用于视锥剔除
// 依赖: GLM, BoundingBox.h
//===========================================================
#pragma once
#include <glm/glm.hpp>
#include "BoundingBox.h"
class Frustum {
public:
Frustum();
// 从视图投影矩阵更新视锥体平面
void Update(const glm::mat4& viewProjection);
// 检查包围盒是否在视锥体内
bool IsBoxVisible(const BoundingBox& box) const;
// 检查球体是否在视锥体内
bool IsSphereVisible(const glm::vec3& center, float radius) const;
// 检查点是否在视锥体内
bool IsPointVisible(const glm::vec3& point) const;
// 获取平面(用于调试)
const glm::vec4& GetPlane(int index) const { return m_Planes[index]; }
// 平面索引: 0=左, 1=右, 2=下, 3=上, 4=近, 5=远
enum Plane {
PLANE_LEFT = 0,
PLANE_RIGHT,
PLANE_BOTTOM,
PLANE_TOP,
PLANE_NEAR,
PLANE_FAR,
PLANE_COUNT
};
private:
glm::vec4 m_Planes[6];
};
//===========================================================
// 文件: framework.h
// 路径: CandyRenderEngine/framework.h
// 说明: Windows 基础头文件
// 依赖: Windows SDK
//===========================================================
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
#include <windows.h>
//===========================================================
// 文件: Export.h
// 路径: CandyRenderEngine/Export.h
// 说明: DLL 导出函数声明
//===========================================================
#pragma once
#include "pch.h"
#ifdef RENDERENGINE_EXPORTS
#define RENDER_API __declspec(dllexport)
#else
#define RENDER_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
// 基础渲染
RENDER_API bool Initialize(HWND hwnd, int width, int height);
RENDER_API void Shutdown();
RENDER_API void RenderFrame();
RENDER_API void Resize(int width, int height);
// 相机控制
RENDER_API void ProcessMouseMovement(float xOffset, float yOffset);
RENDER_API void ProcessMouseScroll(float yOffset);
RENDER_API void ProcessPan(float xOffset, float yOffset);
RENDER_API void ResetCamera();
RENDER_API void FitView();
// 模型变换
RENDER_API void SetModelRotation(float x, float y, float z);
RENDER_API void SetModelScale(float scale);
RENDER_API void SetModelPosition(float x, float y, float z);
// 渲染设置
RENDER_API void SetWireframeMode(bool enabled);
RENDER_API void ShowGrid(bool show);
// 获取信息
RENDER_API void GetCameraPosition(float* x, float* y, float* z);
RENDER_API bool GetWireframeMode();
RENDER_API bool IsGridVisible();
#ifdef __cplusplus
}
#endif
//===========================================================
// 文件: Camera.h
// 路径: CandyRenderEngine/Core/Camera.h
// 说明: 相机系统头文件,支持平移、旋转、缩放(滚轮)功能
// 依赖: GLM 数学库
//===========================================================
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// 相机移动方向枚举
enum class CameraMovement {
FORWARD, // 向前
BACKWARD, // 向后
LEFT, // 向左
RIGHT, // 向右
UP, // 向上
DOWN // 向下
};
class Camera {
public:
// 构造函数(位置、朝上方向、偏航角、俯仰角)
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 5.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
float yaw = -90.0f, float pitch = 0.0f);
// ===== 获取矩阵 =====
glm::mat4 GetViewMatrix() const;
glm::mat4 GetProjectionMatrix(float aspectRatio) const;
// ===== 键盘/WASD 移动 =====
void ProcessKeyboard(CameraMovement direction, float deltaTime);
// ===== 鼠标旋转(第一人称/轨道控制)=====
void ProcessMouseMovement(float xOffset, float yOffset, bool constrainPitch = true);
// ===== 鼠标滚轮缩放 =====
void ProcessMouseScroll(float yOffset);
// ===== 触摸/手势操作(用于 CAD)=====
void ProcessPan(float xOffset, float yOffset); // 平移视图
void ProcessRotate(float angleDelta); // 旋转视图
void ProcessPinch(float zoomDelta); // 双指缩放
// ===== 轨道控制(围绕目标点旋转)=====
void OrbitRotate(float xOffset, float yOffset); // 围绕目标点旋转
void OrbitZoom(float zoomDelta); // 拉近/拉远目标点
void OrbitPan(float xOffset, float yOffset); // 平移目标点
// ===== 设置目标点(用于轨道相机)=====
void SetTarget(const glm::vec3& target);
glm::vec3 GetTarget() const { return m_Target; }
// ===== 重置相机 =====
void Reset();
// ===== 设置相机参数 =====
void SetPosition(const glm::vec3& position);
void SetYaw(float yaw);
void SetPitch(float pitch);
void SetZoom(float zoom);
void SetSpeed(float moveSpeed, float rotateSpeed, float zoomSpeed);
// ===== Getters =====
glm::vec3 GetPosition() const { return m_Position; }
glm::vec3 GetFront() const { return m_Front; }
glm::vec3 GetUp() const { return m_Up; }
glm::vec3 GetRight() const { return m_Right; }
float GetYaw() const { return m_Yaw; }
float GetPitch() const { return m_Pitch; }
float GetZoom() const { return m_Zoom; }
// ===== 开关控制 =====
void EnableOrbitMode(bool enable) { m_OrbitMode = enable; }
bool IsOrbitMode() const { return m_OrbitMode; }
private:
void UpdateCameraVectors(); // 根据欧拉角更新方向向量
// 相机基础参数
glm::vec3 m_Position;
glm::vec3 m_Front;
glm::vec3 m_Up;
glm::vec3 m_Right;
glm::vec3 m_WorldUp;
// 轨道模式参数
glm::vec3 m_Target; // 围绕的目标点
float m_Radius; // 到目标点的距离
bool m_OrbitMode; // 是否使用轨道模式
// 欧拉角
float m_Yaw;
float m_Pitch;
// 缩放(透视相机的视野)
float m_Zoom;
// 移动速度
float m_MoveSpeed;
float m_RotateSpeed;
float m_ZoomSpeed;
// 移动标志(用于键盘连续移动)
bool m_Keys[6];
};
//===========================================================
// 文件: BoundingBox.h
// 路径: CandyRenderEngine/BoundingBox.h
// 说明: 包围盒头文件,用于碰撞检测和视锥剔除
// 依赖: GLM
//===========================================================
#pragma once
#include <glm/glm.hpp>
#include <vector>
class BoundingBox {
public:
BoundingBox();
BoundingBox(const glm::vec3& min, const glm::vec3& max);
// ===== 包围盒计算 =====
void Merge(const BoundingBox& other);
void Merge(const glm::vec3& point);
void Reset();
// ===== 变换 =====
BoundingBox Transform(const glm::mat4& matrix) const;
// ===== 相交测试 =====
bool Intersects(const BoundingBox& other) const;
bool Contains(const glm::vec3& point) const;
// ===== 视锥剔除 =====
bool IsOnFrustum(const glm::vec4 planes[6]) const;
// ===== 获取包围盒属性 =====
glm::vec3 GetMin() const { return m_Min; }
glm::vec3 GetMax() const { return m_Max; }
glm::vec3 GetCenter() const { return (m_Min + m_Max) * 0.5f; }
glm::vec3 GetSize() const { return m_Max - m_Min; }
float GetRadius() const;
float GetVolume() const;
// ===== 判断是否有效 =====
bool IsValid() const { return m_Min.x <= m_Max.x && m_Min.y <= m_Max.y && m_Min.z <= m_Max.z; }
// ===== 8个顶点 =====
void GetCorners(glm::vec3 corners[8]) const;
private:
glm::vec3 m_Min;
glm::vec3 m_Max;
};