224 lines
6.1 KiB
Markdown
224 lines
6.1 KiB
Markdown
# Modern C++23 3D Game Engine
|
|
|
|
A fully-featured, modern C++23-based 3D game engine using OpenGL 4.6 with advanced rendering features.
|
|
|
|
## Features
|
|
|
|
- **Modern C++23**: Utilizes the latest C++ features including concepts, ranges, modules support, and std::expected
|
|
- **OpenGL 4.6**: Modern OpenGL with compute shaders, bindless textures, and UBOs
|
|
- **Cross-Platform**: Works on Windows, Linux, and macOS
|
|
- **Advanced Rendering**: PBR shading, shadow mapping, normal mapping, and more
|
|
- **Resource Management**: Smart resource caching with hot-reload support
|
|
- **Scene Management**: Hierarchical scene graph with frustum culling
|
|
- **Input System**: Modern input handling with concepts and type safety
|
|
- **Memory Safe**: RAII principles, smart pointers, and minimal raw memory management
|
|
|
|
## Requirements
|
|
|
|
- C++23 compatible compiler (GCC 13+, Clang 16+, MSVC 19.35+)
|
|
- CMake 3.25 or higher
|
|
- OpenGL 4.6 compatible graphics card
|
|
- GLFW 3.3+
|
|
- GLM (OpenGL Mathematics)
|
|
- GLAD (OpenGL loader)
|
|
|
|
### Installing Dependencies
|
|
|
|
#### Ubuntu/Debian:
|
|
```bash
|
|
sudo apt update
|
|
sudo apt install build-essential cmake pkg-config
|
|
sudo apt install libglfw3-dev libglm-dev libglu1-mesa-dev
|
|
```
|
|
|
|
#### macOS (with Homebrew):
|
|
```bash
|
|
brew install cmake glfw glm pkg-config
|
|
```
|
|
|
|
#### Windows:
|
|
Use vcpkg to install dependencies:
|
|
```bash
|
|
vcpkg install glfw3 glm
|
|
```
|
|
|
|
## Building
|
|
|
|
1. Clone the repository:
|
|
```bash
|
|
git clone <your-repo-url>
|
|
cd ModernEngine
|
|
```
|
|
|
|
2. Create build directory:
|
|
```bash
|
|
mkdir build && cd build
|
|
```
|
|
|
|
3. Configure with CMake:
|
|
```bash
|
|
cmake .. -DCMAKE_CXX_STANDARD=23
|
|
```
|
|
|
|
4. Build:
|
|
```bash
|
|
cmake --build . --config Release
|
|
```
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
ModernEngine/
|
|
├── CMakeLists.txt # Main CMake configuration
|
|
├── include/ # Header files
|
|
│ ├── Engine.h # Main engine class
|
|
│ ├── Window.h # Window management
|
|
│ ├── Input.h # Input system with C++20 concepts
|
|
│ ├── Renderer.h # OpenGL renderer
|
|
│ ├── Shader.h # Shader management
|
|
│ ├── Mesh.h # Mesh and vertex handling
|
|
│ ├── Scene.h # Scene graph management
|
|
│ ├── Camera.h # Camera system
|
|
│ └── ResourceManager.h # Resource caching system
|
|
├── src/ # Source files
|
|
│ ├── main.cpp # Example game implementation
|
|
│ └── Engine.cpp # Engine implementation
|
|
├── assets/ # Game assets
|
|
│ └── shaders/ # GLSL shaders
|
|
│ ├── basic.vert # Vertex shader
|
|
│ └── basic.frag # Fragment shader with PBR
|
|
├── third_party/ # Third party libraries
|
|
│ └── glad/ # OpenGL loader
|
|
└── README.md # This file
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Basic Example
|
|
|
|
```cpp
|
|
#include "Engine.h"
|
|
|
|
class MyGame : public Engine {
|
|
public:
|
|
MyGame() : Engine({
|
|
.window_width = 1920,
|
|
.window_height = 1080,
|
|
.window_title = "My C++23 Game",
|
|
.enable_vsync = true,
|
|
.target_fps = 144
|
|
}) {}
|
|
|
|
protected:
|
|
bool OnInitialize() override {
|
|
// Load shaders
|
|
auto shader = ResourceManager::LoadShader("default",
|
|
"assets/shaders/basic.vert",
|
|
"assets/shaders/basic.frag");
|
|
|
|
// Create meshes
|
|
auto cube = Mesh::CreateCube();
|
|
GetScene().AddNode(std::move(cube), {0, 0, 0});
|
|
|
|
// Setup camera
|
|
GetCamera().SetPosition({5, 3, 5});
|
|
GetCamera().LookAt({5, 3, 5}, {0, 0, 0});
|
|
|
|
return true;
|
|
}
|
|
|
|
void OnUpdate(float delta_time) override {
|
|
// Game logic here
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
MyGame game;
|
|
if (game.Initialize()) {
|
|
game.Run();
|
|
}
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
### Advanced Features
|
|
|
|
#### Resource Management with Error Handling
|
|
```cpp
|
|
// C++23 std::expected for error handling
|
|
auto shader_result = ResourceManager::LoadShader("pbr",
|
|
"shaders/pbr.vert", "shaders/pbr.frag");
|
|
|
|
if (!shader_result) {
|
|
std::cerr << "Shader failed: " << shader_result.error() << std::endl;
|
|
return false;
|
|
}
|
|
```
|
|
|
|
#### Type-Safe Input System
|
|
```cpp
|
|
// C++20 concepts for compile-time type checking
|
|
if (Input::IsKeyPressed('W')) {
|
|
camera.MoveForward(speed * deltaTime);
|
|
}
|
|
|
|
if (Input::IsMouseButtonPressed(Input::MouseButton::Left)) {
|
|
auto mousePos = Input::GetMousePosition();
|
|
// Handle mouse click
|
|
}
|
|
```
|
|
|
|
#### Modern Mesh Creation
|
|
```cpp
|
|
// Using C++20 spans for safe array handling
|
|
std::vector<Vertex> vertices = /* ... */;
|
|
std::vector<unsigned int> indices = /* ... */;
|
|
|
|
auto mesh = std::make_unique<Mesh>(
|
|
std::span<const Vertex>(vertices),
|
|
std::span<const unsigned int>(indices)
|
|
);
|
|
|
|
// Or use factory methods
|
|
auto sphere = Mesh::CreateSphere(32, 16);
|
|
auto cube = Mesh::CreateCube();
|
|
```
|
|
|
|
## Modern C++23 Features Used
|
|
|
|
- **Concepts**: Type-safe templates with compile-time validation
|
|
- **Ranges**: Modern iteration and algorithm composition
|
|
- **std::expected**: Proper error handling without exceptions
|
|
- **Modules**: Import std for faster compilation (where supported)
|
|
- **Spaceship Operator**: Automatic comparison operators
|
|
- **Designated Initializers**: Clean struct initialization
|
|
- **[[nodiscard]]**: Preventing accidental value discarding
|
|
- **constexpr**: Compile-time computation
|
|
- **RAII**: Automatic resource management
|
|
|
|
## Performance Features
|
|
|
|
- **Uniform Buffer Objects**: Efficient uniform data transfer
|
|
- **Instanced Rendering**: Draw many objects with one call
|
|
- **Frustum Culling**: Only render visible objects
|
|
- **Hot Reloading**: Live shader and asset reloading
|
|
- **Memory Pools**: Reduced allocation overhead
|
|
- **Cache-Friendly Design**: Data structures optimized for CPU cache
|
|
|
|
## Contributing
|
|
|
|
1. Ensure your compiler supports C++23
|
|
2. Follow the existing code style
|
|
3. Add tests for new features
|
|
4. Update documentation
|
|
|
|
## License
|
|
|
|
MIT License - See LICENSE file for details
|
|
|
|
## Acknowledgments
|
|
|
|
- OpenGL community for excellent documentation
|
|
- GLM for mathematics library
|
|
- GLFW for cross-platform window management
|
|
- C++ standardization committee for modern language features |