Compare commits
51 Commits
windows
...
0c589e4d27
Author | SHA1 | Date | |
---|---|---|---|
0c589e4d27 | |||
0165afab95 | |||
3fce829eca | |||
0cf21248f6 | |||
94afd17d65 | |||
fdbf1296de | |||
8cb94e8c95 | |||
884696feaa | |||
ff9e23255c | |||
bedd6c3ca0 | |||
f56e524d05 | |||
b989d74fca | |||
42d5def07e | |||
fefe273fce | |||
99f5cd3715 | |||
4e86d92987 | |||
6cef3efbbc | |||
431d723afc | |||
9d56515fe5 | |||
7f08e28a04 | |||
81c4e08e36 | |||
2c4f5fd641 | |||
3972553d36 | |||
e8057c97ff | |||
5b6092f9d4 | |||
09d715b9f7 | |||
5f69ed6434 | |||
4a40fe6e1a | |||
6ba8a0e3f6 | |||
8a044cbe86 | |||
c253fff7df | |||
97b34962f7 | |||
620adb20f9 | |||
507ba483b3 | |||
fc91f6662e | |||
2b0494a23d | |||
fec93b098b | |||
4a9f45b6ef | |||
fdb891d860 | |||
5bcfb18296 | |||
303b931fb7 | |||
69dbe5ae2f | |||
de6496ff81 | |||
807e0ce9d9 | |||
58e25b530b | |||
39f528d7ad | |||
6dc269ce13 | |||
9d5bb51463 | |||
c5d5536836 | |||
0d147adfe5 | |||
67ba331ba5 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
build/
|
||||
.idea/
|
14
.vscode/settings.json
vendored
14
.vscode/settings.json
vendored
@ -57,6 +57,18 @@
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"variant": "cpp",
|
||||
"codecvt": "cpp"
|
||||
"codecvt": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"ranges": "cpp",
|
||||
"list": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"bitset": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"regex": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"stop_token": "cpp"
|
||||
}
|
||||
}
|
118
CMakeLists.txt
118
CMakeLists.txt
@ -1,43 +1,83 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(CodingGame LANGUAGES C CXX)
|
||||
|
||||
# --- deps via vcpkg ---
|
||||
# (vcpkg installs decide static vs shared; no "SDL3-shared" component needed)
|
||||
find_package(SDL3 CONFIG REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW CONFIG REQUIRED)
|
||||
find_package(glm CONFIG REQUIRED)
|
||||
# ---------- Build-type defaults (only affects single-config generators like Ninja/Make) ----------
|
||||
if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
|
||||
endif()
|
||||
|
||||
# ---------- Dependencies ----------
|
||||
if (UNIX)
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
glm
|
||||
GIT_REPOSITORY https://github.com/g-truc/glm.git
|
||||
GIT_TAG bf71a834948186f4097caa076cd2663c69a10e1e # refs/tags/1.0.1
|
||||
)
|
||||
FetchContent_MakeAvailable(glm)
|
||||
|
||||
FetchContent_Declare(
|
||||
EnTT
|
||||
GIT_REPOSITORY https://github.com/skypjack/entt.git
|
||||
GIT_TAG d4014c74dc3793aba95ae354d6e23a026c2796db
|
||||
)
|
||||
FetchContent_MakeAvailable(EnTT)
|
||||
|
||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
elseif (MSVC) # vcpkg
|
||||
find_package(SDL3 CONFIG REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW CONFIG REQUIRED)
|
||||
find_package(glm CONFIG REQUIRED)
|
||||
find_package(EnTT CONFIG REQUIRED)
|
||||
endif()
|
||||
|
||||
# --- exe ---
|
||||
add_executable(CodingGame
|
||||
src/prelude.cpp
|
||||
src/file_manager.cpp
|
||||
src/shader.cpp
|
||||
src/block.cpp
|
||||
src/vertex.cpp
|
||||
src/texture.cpp
|
||||
src/model.cpp
|
||||
src/IO/parser.cpp
|
||||
src/IO/file_manager.cpp
|
||||
|
||||
src/window/window.cpp
|
||||
|
||||
src/renderer/debug.cpp
|
||||
src/renderer/basics.cpp
|
||||
src/renderer/mesh.cpp
|
||||
src/renderer/shader.cpp
|
||||
src/renderer/texture.cpp
|
||||
src/renderer/wavefront.cpp
|
||||
src/renderer/engine.cpp
|
||||
src/renderer/renderer.cpp
|
||||
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17)
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/src/shaders DESTINATION ${CMAKE_BINARY_DIR}/)
|
||||
file(COPY src/shaders DESTINATION ${CMAKE_BINARY_DIR}/)
|
||||
|
||||
target_include_directories(CodingGame PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/contrib
|
||||
)
|
||||
|
||||
target_link_libraries(CodingGame PRIVATE
|
||||
glm::glm
|
||||
SDL3::SDL3
|
||||
OpenGL::GL
|
||||
SDL3::SDL3 # vcpkg’s SDL3 target
|
||||
GLEW::GLEW
|
||||
glm::glm
|
||||
EnTT::EnTT
|
||||
)
|
||||
|
||||
# Debug flags per toolchain
|
||||
# ---------- Visibility (helps optimizer & smaller binaries on Release) ----------
|
||||
# Only affects non-Windows compilers
|
||||
set_target_properties(CodingGame PROPERTIES
|
||||
CXX_VISIBILITY_PRESET hidden
|
||||
VISIBILITY_INLINES_HIDDEN YES
|
||||
)
|
||||
|
||||
# ---------- Per-config flags ----------
|
||||
# Debug flags (your original intent, kept)
|
||||
if (MSVC)
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>)
|
||||
target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
|
||||
@ -45,8 +85,44 @@ else()
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>)
|
||||
endif()
|
||||
|
||||
# --- copy runtime DLLs next to the exe on Windows ---
|
||||
# (CMake 3.21+)
|
||||
# Release flags
|
||||
if (MSVC)
|
||||
# /O2: optimize speed, /GL: whole program opt (LTCG), /DNDEBUG: disable asserts
|
||||
target_compile_options(CodingGame PRIVATE
|
||||
$<$<CONFIG:Release>:/O2>
|
||||
$<$<CONFIG:Release>:/DNDEBUG>
|
||||
$<$<CONFIG:RelWithDebInfo>:/O2>
|
||||
)
|
||||
# Link-time codegen & extra linker opts for smaller/faster binaries
|
||||
target_link_options(CodingGame PRIVATE
|
||||
$<$<CONFIG:Release>:/LTCG /OPT:ICF /OPT:REF>
|
||||
$<$<CONFIG:RelWithDebInfo>:/LTCG /OPT:ICF /OPT:REF>
|
||||
)
|
||||
else()
|
||||
# GCC/Clang
|
||||
# -O3 for max opts, -ffast-math optional but can be risky; we keep it OFF by default.
|
||||
option(CODINGGAME_USE_MARCH_NATIVE "Enable -march=native on Release for this machine" ON)
|
||||
target_compile_options(CodingGame PRIVATE
|
||||
$<$<CONFIG:Release>:-O3>
|
||||
$<$<AND:$<CONFIG:Release>,$<BOOL:${CODINGGAME_USE_MARCH_NATIVE}>>:-march=native>
|
||||
$<$<CONFIG:Release>:-DNDEBUG>
|
||||
$<$<CONFIG:RelWithDebInfo>:-O3 -g>
|
||||
)
|
||||
# Linker: enable LTO when available; optionally strip symbols on non-Apple
|
||||
include(CheckIPOSupported) # IPO == LTO in CMake terms
|
||||
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_msg)
|
||||
if(ipo_supported)
|
||||
set_property(TARGET CodingGame PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
|
||||
set_property(TARGET CodingGame PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE)
|
||||
# -s strips symbols at link stage (use only for pure Release)
|
||||
target_link_options(CodingGame PRIVATE $<$<CONFIG:Release>:-s>)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# ---------- Windows: copy runtime DLLs ----------
|
||||
if (WIN32)
|
||||
add_custom_command(TARGET CodingGame POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
|
52
README.md
Normal file
52
README.md
Normal file
@ -0,0 +1,52 @@
|
||||
|
||||
|
||||
# Project Description
|
||||
|
||||
This is a basic future game engine for OpenGL 3D rendered games
|
||||
|
||||
## Building on Windows
|
||||
|
||||
In order to configure and run project on windows platform accomplish several steps.
|
||||
|
||||
### Configuring
|
||||
|
||||
```console
|
||||
cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_BUILD_TYPE=Release
|
||||
```
|
||||
|
||||
### Building
|
||||
|
||||
```console
|
||||
cmake --build build --config Release
|
||||
```
|
||||
|
||||
### Static Linking
|
||||
|
||||
For static linking you just need to modify the configure command as follows:
|
||||
|
||||
```console
|
||||
cmake -S . -B build -G "Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_BUILD_TYPE=Release
|
||||
```
|
||||
|
||||
## Multi-GPU Devices
|
||||
|
||||
If you want to use non-primary GPU on your device when launching the game specifically on Linux you should specify additional environment variables before running. For example in my case I have a hybrid gaming laptop with 2 GPUs AMD from CPU and NVIDIA discrete.
|
||||
|
||||
The run command in that case would look following:
|
||||
|
||||
```console
|
||||
__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia ./build/CodingGame
|
||||
```
|
||||
|
||||
## TODO List
|
||||
|
||||
### Optimizations
|
||||
|
||||
🚀 Summary of Speedups
|
||||
- Replace toElement / toMtlElement string comparisons with char-based switches.
|
||||
- Replace std::stoi with a custom fast Parser::TakeIndex.
|
||||
- Pre-reserve vectors for vertices, normals, texcoords, meshes.
|
||||
- Load whole file into memory before parsing (fastest for large OBJs).
|
||||
- Defer texture loading until after parsing.
|
||||
- Store material pointers in meshes → no runtime lookups in render.
|
||||
- Inline parsing functions.
|
25
WINDOWS.md
25
WINDOWS.md
@ -1,25 +0,0 @@
|
||||
|
||||
# Setup VCPKG
|
||||
|
||||
```console
|
||||
git clone https://github.com/microsoft/vcpkg C:\vcpkg
|
||||
C:\vcpkg\bootstrap-vcpkg.bat
|
||||
# install deps for 64-bit Windows
|
||||
C:\vcpkg\vcpkg install sdl3 glew glm --triplet x64-windows
|
||||
```
|
||||
|
||||
# Configure
|
||||
|
||||
```console
|
||||
cmake -S . -B build `
|
||||
-G "Visual Studio 17 2022" -A x64 `
|
||||
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake `
|
||||
-DVCPKG_TARGET_TRIPLET=x64-windows `
|
||||
-DCMAKE_BUILD_TYPE=Debug
|
||||
```
|
||||
|
||||
# Build
|
||||
|
||||
```console
|
||||
cmake --build build --config Debug
|
||||
```
|
@ -1,4 +1,4 @@
|
||||
# Blender 4.3.2 MTL File: 'untitled.blend'
|
||||
# Blender 4.3.2 MTL File: 'None'
|
||||
# www.blender.org
|
||||
|
||||
newmtl Grass_Bottom
|
||||
|
@ -2,25 +2,25 @@
|
||||
# www.blender.org
|
||||
mtllib grass_block.mtl
|
||||
o Cube
|
||||
v -1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 -1.000000
|
||||
v -0.389000 -0.389000 0.389000
|
||||
v -0.389000 0.389000 0.389000
|
||||
v -0.389000 -0.389000 -0.389000
|
||||
v -0.389000 0.389000 -0.389000
|
||||
v 0.389000 -0.389000 0.389000
|
||||
v 0.389000 0.389000 0.389000
|
||||
v 0.389000 -0.389000 -0.389000
|
||||
v 0.389000 0.389000 -0.389000
|
||||
vn -1.0000 -0.0000 -0.0000
|
||||
vn -0.0000 -0.0000 -1.0000
|
||||
vn 1.0000 -0.0000 -0.0000
|
||||
vn -0.0000 -0.0000 1.0000
|
||||
vn -0.0000 -1.0000 -0.0000
|
||||
vn -0.0000 1.0000 -0.0000
|
||||
vn -0.0000 -1.0000 -0.0000
|
||||
vt 0.000000 1.000000
|
||||
vt 1.000000 0.000000
|
||||
vt 0.000000 0.000000
|
||||
vt 1.000000 1.000000
|
||||
s 0
|
||||
s 1
|
||||
usemtl Grass_Side
|
||||
f 2/1/1 3/2/1 1/3/1
|
||||
f 4/1/2 7/2/2 3/3/2
|
||||
@ -31,8 +31,8 @@ f 4/1/2 8/4/2 7/2/2
|
||||
f 8/4/3 6/1/3 5/3/3
|
||||
f 6/4/4 2/1/4 1/3/4
|
||||
usemtl Grass_Top
|
||||
f 4/1/6 6/2/6 8/4/6
|
||||
f 4/1/6 2/3/6 6/2/6
|
||||
f 4/1/5 6/2/5 8/4/5
|
||||
f 4/1/5 2/3/5 6/2/5
|
||||
usemtl Grass_Bottom
|
||||
f 7/4/5 1/3/5 3/1/5
|
||||
f 7/4/5 5/2/5 1/3/5
|
||||
f 7/4/6 1/3/6 3/1/6
|
||||
f 7/4/6 5/2/6 1/3/6
|
||||
|
2
assets/plane.mtl
Normal file
2
assets/plane.mtl
Normal file
@ -0,0 +1,2 @@
|
||||
# Blender 4.3.2 MTL File: 'None'
|
||||
# www.blender.org
|
16
assets/plane.obj
Normal file
16
assets/plane.obj
Normal file
@ -0,0 +1,16 @@
|
||||
# Blender 4.3.2
|
||||
# www.blender.org
|
||||
mtllib plane.mtl
|
||||
o Plane
|
||||
v -5.000000 0.000000 5.000000
|
||||
v 5.000000 0.000000 5.000000
|
||||
v -5.000000 0.000000 -5.000000
|
||||
v 5.000000 0.000000 -5.000000
|
||||
vn -0.0000 1.0000 -0.0000
|
||||
vt 1.000000 0.000000
|
||||
vt 0.000000 1.000000
|
||||
vt 0.000000 0.000000
|
||||
vt 1.000000 1.000000
|
||||
s 0
|
||||
f 2/1/1 3/2/1 1/3/1
|
||||
f 2/1/1 4/4/1 3/2/1
|
12
assets/sphere.mtl
Normal file
12
assets/sphere.mtl
Normal file
@ -0,0 +1,12 @@
|
||||
# Blender 4.3.2 MTL File: 'None'
|
||||
# www.blender.org
|
||||
|
||||
newmtl Material.001
|
||||
Ns 250.000000
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.799999 0.715049 0.364110
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.500000
|
||||
d 1.000000
|
||||
illum 2
|
2537
assets/sphere.obj
Normal file
2537
assets/sphere.obj
Normal file
File diff suppressed because it is too large
Load Diff
20
include/IO/parser.h
Normal file
20
include/IO/parser.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef PARSER_H_
|
||||
#define PARSER_H_
|
||||
|
||||
// Very fast OBJ/MTL line parser
|
||||
class Parser {
|
||||
private:
|
||||
char* m_sv;
|
||||
public:
|
||||
Parser(char* sv) : m_sv(sv) {}
|
||||
public:
|
||||
void SkipSpaces();
|
||||
char* TakeWord();
|
||||
float TakeFloat();
|
||||
int TakeInt();
|
||||
bool TakeFaceIndices(int& vi, int& ti, int& ni);
|
||||
char* TakeUntil(char d);
|
||||
int TakeIndex(int baseCount);
|
||||
};
|
||||
|
||||
#endif // PARSER_H_
|
17
include/app/app.h
Normal file
17
include/app/app.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef APPLICATION_H_
|
||||
#define APPLICATION_H_
|
||||
|
||||
class IApplication {
|
||||
public:
|
||||
virtual ~IApplication() = default;
|
||||
|
||||
virtual void OnInit() {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnRender() {};
|
||||
virtual void OnShutdown() {};
|
||||
|
||||
virtual void OnEvent() {};
|
||||
virtual void OnWindowResized(const WindowResized& e) {};
|
||||
};
|
||||
|
||||
#endif // APPLICATION_H_
|
@ -1,16 +0,0 @@
|
||||
#ifndef BLOCK_H_
|
||||
#define BLOCK_H_
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Block {
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec4 m_color;
|
||||
public:
|
||||
Block(glm::vec3 position, glm::vec4 color);
|
||||
public:
|
||||
inline glm::vec3 Position() const { return m_position; }
|
||||
inline glm::vec4 Color() const { return m_color; }
|
||||
};
|
||||
|
||||
#endif
|
6
include/components/camera.h
Normal file
6
include/components/camera.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENTS_PLAYER_H_
|
||||
#define COMPONENTS_PLAYER_H_
|
||||
|
||||
struct camera {};
|
||||
|
||||
#endif // COMPONENTS_PLAYER_H_
|
26
include/components/light.h
Normal file
26
include/components/light.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef COMPONENTS_LIGHT_H_
|
||||
#define COMPONENTS_LIGHT_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "renderer/renderer.h"
|
||||
|
||||
struct light {
|
||||
friend class Renderer;
|
||||
public:
|
||||
enum LightType {
|
||||
DIRECTIONAL = 0,
|
||||
};
|
||||
LightType type;
|
||||
glm::vec3 color;
|
||||
float intensity;
|
||||
|
||||
light(LightType t, const glm::vec3& c, float i)
|
||||
: type(t), color(c), intensity(i),
|
||||
shadowMap(0), fbo(0), lightSpace(1.0f) {}
|
||||
private:
|
||||
unsigned int shadowMap;
|
||||
unsigned int fbo;
|
||||
glm::mat4 lightSpace;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_LIGHT_H_
|
11
include/components/mesh.h
Normal file
11
include/components/mesh.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef COMPONENTS_MESH_H_
|
||||
#define COMPONENTS_MESH_H_
|
||||
|
||||
#include <memory>
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
struct mesh {
|
||||
std::unique_ptr<Object> object;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_MESH_H_
|
12
include/components/transform.h
Normal file
12
include/components/transform.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef COMPONENTS_TRANSFORM_H_
|
||||
#define COMPONENTS_TRANSFORM_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct transform {
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 scale;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_TRANSFORM_H_
|
134
include/model.h
134
include/model.h
@ -1,134 +0,0 @@
|
||||
#ifndef MODEL_H_
|
||||
#define MODEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "shader.h"
|
||||
#include "texture.h"
|
||||
|
||||
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||
|
||||
class Vertex {
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec3 m_normal;
|
||||
glm::vec2 m_texCoord;
|
||||
public:
|
||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||
public:
|
||||
static void DefineAttrib();
|
||||
};
|
||||
|
||||
class FaceItem {
|
||||
private:
|
||||
unsigned int m_vIndex;
|
||||
unsigned int m_nIndex;
|
||||
unsigned int m_tIndex;
|
||||
public:
|
||||
FaceItem()
|
||||
: m_vIndex(0), m_nIndex(0), m_tIndex(0) {}
|
||||
FaceItem(unsigned int vI, unsigned int nI, unsigned int tI)
|
||||
: m_vIndex(vI), m_nIndex(nI), m_tIndex(tI) {}
|
||||
public:
|
||||
inline const unsigned int GetVertex() const { return m_vIndex; }
|
||||
inline const unsigned int GetNormal() const { return m_nIndex; }
|
||||
inline const unsigned int GetTex() const { return m_tIndex; }
|
||||
public:
|
||||
inline void SetVertex(unsigned int vIndex) { m_vIndex = vIndex; }
|
||||
inline void SetNorm(unsigned int nIndex) { m_nIndex = nIndex; }
|
||||
inline void SetTex(unsigned int tIndex) { m_tIndex = tIndex; }
|
||||
};
|
||||
|
||||
class Face {
|
||||
private:
|
||||
std::vector<FaceItem> m_items;
|
||||
public:
|
||||
Face()
|
||||
: m_items(std::vector<FaceItem>()) {}
|
||||
public:
|
||||
void PushItem(const FaceItem& item);
|
||||
public:
|
||||
inline const unsigned int GetSize() const { return m_items.size(); }
|
||||
inline const FaceItem& GetItem(unsigned int index) const { return m_items[index]; }
|
||||
};
|
||||
|
||||
class Material {
|
||||
private:
|
||||
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
||||
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
|
||||
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
|
||||
float m_shininess { 32.0f };
|
||||
|
||||
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
|
||||
public:
|
||||
Material() = default;
|
||||
Material(const Material& other) = default; // copy constructor
|
||||
Material& operator=(const Material& other) = default;
|
||||
public:
|
||||
inline const glm::vec3 GetAmbientColor() const { return m_ambient; }
|
||||
inline const glm::vec3 GetDiffuseColor() const { return m_diffuse; }
|
||||
inline const glm::vec3 GetSpecularColor() const { return m_specular; }
|
||||
inline const float GetSpecularWeight() const { return m_shininess; }
|
||||
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
|
||||
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
|
||||
public:
|
||||
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
|
||||
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
|
||||
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
|
||||
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
|
||||
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
|
||||
};
|
||||
|
||||
class Mesh {
|
||||
public: // TODO: abstract away
|
||||
unsigned int m_vao, m_vbo, m_ebo;
|
||||
std::vector<Vertex> m_vertexBuffer;
|
||||
std::vector<unsigned int> m_indexBuffer;
|
||||
public: // TODO: abstract away
|
||||
void Bind() { glBindVertexArray(m_vao); }
|
||||
void Unbind() { glBindVertexArray(0); }
|
||||
void Upload();
|
||||
public:
|
||||
std::string materialName;
|
||||
public:
|
||||
Mesh();
|
||||
public:
|
||||
void Render();
|
||||
};
|
||||
|
||||
class Object {
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
std::vector<glm::vec3> m_normals;
|
||||
std::vector<glm::vec2> m_texCoords;
|
||||
|
||||
std::vector<Mesh> m_meshes;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
||||
private:
|
||||
static inline int NormalizeIndex(const std::string &s, int baseCount);
|
||||
|
||||
private:
|
||||
Object();
|
||||
public:
|
||||
static Object LoadFile(const std::string& filename);
|
||||
|
||||
private:
|
||||
void LoadMaterials(const std::string& filename);
|
||||
private:
|
||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
||||
private:
|
||||
Mesh& GetLastMesh();
|
||||
void CreateNewMesh(const std::string& materialName);
|
||||
public:
|
||||
void Render(Shader& shader);
|
||||
};
|
||||
|
||||
#endif // MODEL_H_
|
@ -1,39 +0,0 @@
|
||||
#ifndef PRELUDE_H_
|
||||
#define PRELUDE_H_
|
||||
// #define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
#include "SDL3/SDL.h"
|
||||
|
||||
struct RenderContext {
|
||||
Uint64 time;
|
||||
Uint64 prev_time;
|
||||
bool program_failed = false;
|
||||
GLuint program = 0;
|
||||
GLint resolution_location = 0;
|
||||
GLint time_location = 0;
|
||||
bool pause = false;
|
||||
};
|
||||
|
||||
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader);
|
||||
|
||||
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader);
|
||||
|
||||
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program);
|
||||
|
||||
void reload_shaders(RenderContext*context);
|
||||
|
||||
// void key_callback(SDL_Window* window, int key, int scancode, int action, int mods);
|
||||
|
||||
void window_size_callback(SDL_Window* window, int width, int height);
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam);
|
||||
|
||||
void process_prelude(RenderContext *context);
|
||||
|
||||
#endif // PRELUDE_H_
|
18
include/renderer/basics.h
Normal file
18
include/renderer/basics.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef RENDERER_BASICS_H
|
||||
#define RENDERER_BASICS_H
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Vertex {
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec3 m_normal;
|
||||
glm::vec2 m_texCoord;
|
||||
public:
|
||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||
public:
|
||||
static void DefineAttrib();
|
||||
};
|
||||
|
||||
#endif // RENDERER_BASICS_H
|
14
include/renderer/debug.h
Normal file
14
include/renderer/debug.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef RENDERER_DEBUG_
|
||||
#define RENDERER_DEBUG_
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam);
|
||||
|
||||
#endif // RENDERER_DEBUG_
|
22
include/renderer/engine.h
Normal file
22
include/renderer/engine.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ENGINE_H_
|
||||
#define ENGINE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "window/window.h"
|
||||
#include "window/events/window.h"
|
||||
|
||||
#include "app/app.h"
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
static void Run(std::unique_ptr<IApplication> app);
|
||||
private:
|
||||
static std::unique_ptr<IApplication> s_app;
|
||||
static std::shared_ptr<Window> s_window;
|
||||
static bool s_running;
|
||||
};
|
||||
|
||||
|
||||
#endif // ENGINE_H_
|
42
include/renderer/material.h
Normal file
42
include/renderer/material.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef MATERIAL_H_
|
||||
#define MATERIAL_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "texture.h"
|
||||
|
||||
class Material {
|
||||
private:
|
||||
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
||||
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
|
||||
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
|
||||
float m_shininess { 32.0f };
|
||||
float m_opacity { 1.0f };
|
||||
int m_illum { 2 };
|
||||
|
||||
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
|
||||
public:
|
||||
Material() = default;
|
||||
Material(const Material& other) = default; // copy constructor
|
||||
Material& operator=(const Material& other) = default;
|
||||
public:
|
||||
inline const glm::vec3 GetAmbientColor() const { return m_ambient; }
|
||||
inline const glm::vec3 GetDiffuseColor() const { return m_diffuse; }
|
||||
inline const glm::vec3 GetSpecularColor() const { return m_specular; }
|
||||
inline const float GetSpecularWeight() const { return m_shininess; }
|
||||
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
|
||||
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
|
||||
inline const float GetOpacity() const { return m_opacity; }
|
||||
inline const int GetIllumination() const { return m_illum; }
|
||||
public:
|
||||
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
|
||||
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
|
||||
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
|
||||
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
|
||||
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
|
||||
inline void SetOpacity(float opacity) { m_opacity = opacity; }
|
||||
inline void SetIllumination(float illum) { m_illum = illum; }
|
||||
};
|
||||
|
||||
#endif // MATERIAL_H_
|
27
include/renderer/mesh.h
Normal file
27
include/renderer/mesh.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef MESH_H_
|
||||
#define MESH_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "renderer/basics.h"
|
||||
|
||||
class Mesh {
|
||||
public: // TODO: abstract away
|
||||
unsigned int m_vao, m_vbo, m_ebo;
|
||||
std::vector<Vertex> m_vertexBuffer;
|
||||
std::vector<unsigned int> m_indexBuffer;
|
||||
public: // TODO: abstract away
|
||||
void Bind() const { glBindVertexArray(m_vao); }
|
||||
void Unbind() { glBindVertexArray(0); }
|
||||
void Upload() const;
|
||||
public:
|
||||
std::string materialName;
|
||||
public:
|
||||
Mesh();
|
||||
public:
|
||||
void Render();
|
||||
};
|
||||
|
||||
#endif // MESH_H_
|
34
include/renderer/renderer.h
Normal file
34
include/renderer/renderer.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef RENDERER_H_
|
||||
#define RENDERER_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
#include "renderer/shader.h"
|
||||
|
||||
// TODO: make static or singleton
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer();
|
||||
|
||||
void Render(entt::registry& registry);
|
||||
void GenerateShadowMaps(entt::registry& registry);
|
||||
|
||||
void OnWindowResized(int w, int h);
|
||||
private:
|
||||
void ApplyLights(entt::registry& registry, Shader &shader);
|
||||
void UpdateView(entt::registry& registry, Shader &shader);
|
||||
void RenderScene(entt::registry& registry, Shader &shader);
|
||||
private:
|
||||
Shader m_shader;
|
||||
Shader m_depthShader;
|
||||
|
||||
// unsigned int m_depth_fbo;
|
||||
// unsigned int m_depthMap;
|
||||
|
||||
glm::mat4 m_model;
|
||||
glm::mat4 m_proj;
|
||||
glm::mat4 m_view;
|
||||
};
|
||||
|
||||
#endif // RENDERER_H_
|
@ -1,10 +1,8 @@
|
||||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
@ -4,14 +4,13 @@
|
||||
#include <memory>
|
||||
|
||||
class Texture {
|
||||
private:
|
||||
unsigned int m_id;
|
||||
private:
|
||||
public:
|
||||
Texture() {}
|
||||
Texture() : m_id(0) {}
|
||||
static std::unique_ptr<Texture> LoadFile(const std::string& filename);
|
||||
public:
|
||||
inline const unsigned int GetID() const { return m_id; }
|
||||
[[nodiscard]] unsigned int GetID() const { return m_id; }
|
||||
private:
|
||||
unsigned int m_id;
|
||||
};
|
||||
|
||||
#endif // TEXTURE_H_
|
50
include/renderer/wavefront.h
Normal file
50
include/renderer/wavefront.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef MODEL_H_
|
||||
#define MODEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "shader.h"
|
||||
#include "renderer/material.h"
|
||||
#include "renderer/mesh.h"
|
||||
|
||||
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||
|
||||
class Object {
|
||||
private:
|
||||
static inline int NormalizeIndex(int idx, int baseCount);
|
||||
|
||||
private:
|
||||
Object();
|
||||
public:
|
||||
~Object() = default;
|
||||
public:
|
||||
static Object* LoadFile(const std::string& filename);
|
||||
|
||||
private:
|
||||
void LoadMaterials(const std::filesystem::path& filename);
|
||||
private:
|
||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
||||
private:
|
||||
Mesh& GetLastMesh();
|
||||
void CreateNewMesh(const std::string& materialName);
|
||||
public:
|
||||
void Render(Shader& shader);
|
||||
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
std::vector<glm::vec3> m_normals;
|
||||
std::vector<glm::vec2> m_texCoords;
|
||||
|
||||
std::vector<Mesh> m_meshes;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
||||
};
|
||||
|
||||
#endif // MODEL_H_
|
@ -1,37 +0,0 @@
|
||||
#ifndef VERTEX_H_
|
||||
#define VERTEX_H_
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "block.h"
|
||||
|
||||
class Point {
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec3 m_normal;
|
||||
glm::vec4 m_color;
|
||||
public:
|
||||
Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color);
|
||||
};
|
||||
|
||||
class Vertices {
|
||||
private:
|
||||
std::vector<Point> m_items;
|
||||
std::vector<unsigned int> m_indices;
|
||||
|
||||
unsigned int m_vao;
|
||||
unsigned int m_vbo;
|
||||
unsigned int m_ebo;
|
||||
public:
|
||||
Vertices();
|
||||
public: // GPU
|
||||
void Bind();
|
||||
void Unbind();
|
||||
void Draw();
|
||||
void Upload();
|
||||
public:
|
||||
void PushVertex(const Point& point);
|
||||
void PushIndex(unsigned int index);
|
||||
};
|
||||
|
||||
#endif // VERTEX_H_
|
57
include/window/event.h
Normal file
57
include/window/event.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef EVENT_H_
|
||||
#define EVENT_H_
|
||||
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class EventDispatcher {
|
||||
using Type = std::type_index;
|
||||
using RawFn = std::function<void(const void*)>;
|
||||
|
||||
struct Slot { std::size_t id; RawFn fn; };
|
||||
|
||||
std::unordered_map<Type, std::vector<Slot>> subs_;
|
||||
std::size_t next_id_ = 1;
|
||||
|
||||
public:
|
||||
struct Handle {
|
||||
std::type_index type{typeid(void)};
|
||||
std::size_t id{0};
|
||||
explicit operator bool() const { return id != 0; }
|
||||
};
|
||||
|
||||
template<class E, class F>
|
||||
Handle Subscribe(F&& f) {
|
||||
auto& vec = subs_[Type(typeid(E))];
|
||||
Handle h{ Type(typeid(E)), next_id_++ };
|
||||
// Wrap strongly typed callback into type-erased RawFn
|
||||
RawFn wrapper = [fn = std::function<void(const E&)>(std::forward<F>(f))](const void* p){
|
||||
fn(*static_cast<const E*>(p));
|
||||
};
|
||||
vec.push_back(Slot{h.id, std::move(wrapper)});
|
||||
return h;
|
||||
}
|
||||
|
||||
// Unsubscribe with handle
|
||||
void Unsubscribe(const Handle& h) {
|
||||
auto it = subs_.find(h.type);
|
||||
if (it == subs_.end()) return;
|
||||
auto& vec = it->second;
|
||||
vec.erase(std::remove_if(vec.begin(), vec.end(),
|
||||
[&](const Slot& s){ return s.id == h.id; }),
|
||||
vec.end());
|
||||
}
|
||||
|
||||
// Publish immediately
|
||||
template<class E>
|
||||
void Dispatch(const E& e) const {
|
||||
auto it = subs_.find(Type(typeid(E)));
|
||||
if (it == subs_.end()) return;
|
||||
for (auto& slot : it->second) slot.fn(&e);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // EVENT_H_
|
7
include/window/events/window.h
Normal file
7
include/window/events/window.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
struct WindowResized { int w, h; };
|
||||
struct WindowCloseRequested {};
|
||||
|
||||
#endif // WINDOW_EVENTS_H_
|
51
include/window/window.h
Normal file
51
include/window/window.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef WINDOW_H_
|
||||
#define WINDOW_H_
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
#include "event.h"
|
||||
|
||||
#define ENGINE_GL_MAJOR_VERSION 4
|
||||
#define ENGINE_GL_MINOR_VERSION 6
|
||||
#define ENGINE_GL_MULTISAMPLE_BUFFERS 1
|
||||
#define ENGINE_GL_MULTISAMPLE_SAMPLES 8
|
||||
|
||||
#define DEFAULT_WIDTH 1024
|
||||
#define DEFAULT_HEIGHT 768
|
||||
|
||||
class Window : public EventDispatcher {
|
||||
friend class Engine;
|
||||
private:
|
||||
Window();
|
||||
Window(const char* title, int width, int height);
|
||||
~Window();
|
||||
|
||||
struct WindowDeleter {
|
||||
void operator()(Window* w) const { delete w; };
|
||||
};
|
||||
public:
|
||||
static std::shared_ptr<Window> GetInstance();
|
||||
|
||||
Window(Window&& window) noexcept;
|
||||
Window& operator=(Window&& window) noexcept;
|
||||
|
||||
Window(const Window& window) noexcept = delete;
|
||||
Window& operator=(const Window& window) noexcept = delete;
|
||||
public:
|
||||
[[nodiscard]] static inline int GetWidth() { return Window::GetInstance()->m_width; }
|
||||
[[nodiscard]] static inline int GetHeight() { return Window::GetInstance()->m_height; }
|
||||
private:
|
||||
void ProcessEvents();
|
||||
void SwapBuffers() const;
|
||||
void Destroy() const;
|
||||
private:
|
||||
static std::shared_ptr<Window> s_instance;
|
||||
|
||||
SDL_Window *m_handle;
|
||||
SDL_GLContext m_context;
|
||||
|
||||
int m_width;
|
||||
int m_height;
|
||||
};
|
||||
|
||||
#endif //WINDOW_H_
|
@ -1,4 +1,4 @@
|
||||
#include "file_manager.h"
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
123
src/IO/parser.cpp
Normal file
123
src/IO/parser.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include <charconv> // for std::from_chars (C++17+)
|
||||
#include <cstdlib> // for strtof (fallback)
|
||||
#include <cstring>
|
||||
|
||||
#include "IO/parser.h"
|
||||
|
||||
// Skip whitespace
|
||||
void Parser::SkipSpaces() {
|
||||
while (*m_sv == ' ' || *m_sv == '\t') ++m_sv;
|
||||
}
|
||||
|
||||
int Parser::TakeIndex(int baseCount) {
|
||||
if (!m_sv || *m_sv == '\0') return -1;
|
||||
|
||||
bool neg = (*m_sv == '-');
|
||||
if (neg) ++m_sv;
|
||||
|
||||
int idx = 0;
|
||||
while (*m_sv >= '0' && *m_sv <= '9') {
|
||||
idx = idx * 10 + (*m_sv - '0');
|
||||
++m_sv;
|
||||
}
|
||||
|
||||
if (neg) return baseCount + (-idx);
|
||||
return idx > 0 ? idx - 1 : -1;
|
||||
}
|
||||
|
||||
// Get next whitespace-delimited word
|
||||
char* Parser::TakeWord() {
|
||||
SkipSpaces();
|
||||
if (*m_sv == '\0' || *m_sv == '\n' || *m_sv == '\r') return nullptr;
|
||||
|
||||
char* start = m_sv;
|
||||
while (*m_sv && *m_sv != ' ' && *m_sv != '\t' && *m_sv != '\n' && *m_sv != '\r')
|
||||
++m_sv;
|
||||
|
||||
if (*m_sv) { *m_sv = '\0'; ++m_sv; }
|
||||
return start;
|
||||
}
|
||||
|
||||
// Parse a float quickly
|
||||
float Parser::TakeFloat() {
|
||||
SkipSpaces();
|
||||
if (*m_sv == '\0') return 0.0f;
|
||||
|
||||
#if __cpp_lib_to_chars >= 201611L
|
||||
float value = 0.0f;
|
||||
auto result = std::from_chars(m_sv, m_sv + std::strlen(m_sv), value);
|
||||
m_sv = const_cast<char*>(result.ptr);
|
||||
return value;
|
||||
#else
|
||||
char* end;
|
||||
float value = std::strtof(m_sv, &end);
|
||||
m_sv = end;
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Parse an integer quickly
|
||||
int Parser::TakeInt() {
|
||||
SkipSpaces();
|
||||
if (*m_sv == '\0') return 0;
|
||||
|
||||
#if __cpp_lib_to_chars >= 201611L
|
||||
int value = 0;
|
||||
auto result = std::from_chars(m_sv, m_sv + std::strlen(m_sv), value);
|
||||
m_sv = const_cast<char*>(result.ptr);
|
||||
return value;
|
||||
#else
|
||||
char* end;
|
||||
int value = static_cast<int>(std::strtol(m_sv, &end, 10));
|
||||
m_sv = end;
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Take everything until delimiter (mutates buffer)
|
||||
char* Parser::TakeUntil(char d) {
|
||||
SkipSpaces();
|
||||
if (*m_sv == '\0') return nullptr;
|
||||
|
||||
char* start = m_sv;
|
||||
while (*m_sv && *m_sv != d && *m_sv != '\n' && *m_sv != '\r')
|
||||
++m_sv;
|
||||
|
||||
if (*m_sv) { *m_sv = '\0'; ++m_sv; }
|
||||
return start;
|
||||
}
|
||||
|
||||
// Parser.h (or Parser.cpp)
|
||||
// Parse one face element at current position.
|
||||
// Accepts formats: "v", "v/t", "v//n", "v/t/n"
|
||||
// Returns true if a token was parsed, false if no more tokens on the line.
|
||||
bool Parser::TakeFaceIndices(int &vi, int &ti, int &ni) {
|
||||
SkipSpaces();
|
||||
if (*m_sv == '\0' || *m_sv == '\n' || *m_sv == '\r') {
|
||||
vi = ti = ni = 0; // sentinel raw value meaning "no token"
|
||||
return false;
|
||||
}
|
||||
|
||||
// parse vertex index (may be negative)
|
||||
vi = static_cast<int>(std::strtol(m_sv, &m_sv, 10));
|
||||
|
||||
ti = ni = 0; // 0 = not present (raw)
|
||||
if (*m_sv == '/') {
|
||||
++m_sv; // skip '/'
|
||||
// texcoord index (optional)
|
||||
if (*m_sv != '/' && *m_sv != ' ' && *m_sv != '\0' && *m_sv != '\n' && *m_sv != '\r') {
|
||||
ti = static_cast<int>(std::strtol(m_sv, &m_sv, 10));
|
||||
}
|
||||
if (*m_sv == '/') {
|
||||
++m_sv; // skip second '/'
|
||||
// normal index (optional)
|
||||
if (*m_sv != ' ' && *m_sv != '\0' && *m_sv != '\n' && *m_sv != '\r') {
|
||||
ni = static_cast<int>(std::strtol(m_sv, &m_sv, 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// At this point m_sv is either at whitespace, end, or next token char.
|
||||
// Do NOT mutate indices (leave them raw). Let NormalizeIndex handle conversion.
|
||||
return true;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
#include "block.h"
|
||||
|
||||
Block::Block(glm::vec3 position, glm::vec4 color) {
|
||||
this->m_position = position;
|
||||
this->m_color = m_color;
|
||||
}
|
229
src/main.bkp.cpp
229
src/main.bkp.cpp
@ -1,229 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "shader.h"
|
||||
#include "file_manager.h"
|
||||
#include "prelude.h"
|
||||
#include "block.h"
|
||||
#include "vertex.h"
|
||||
#include "model.h"
|
||||
|
||||
#define WIDTH 1024
|
||||
#define HEIGHT 768
|
||||
|
||||
int main() {
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow("OpenGL Test", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALWAYS_ON_TOP);
|
||||
|
||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (GLEW_OK != glewInit()) {
|
||||
fprintf(stderr, "Could not initialize GLEW!\n");
|
||||
SDL_GL_DestroyContext(glcontext);
|
||||
SDL_DestroyWindow(window);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDebugMessageCallback(MessageCallback, 0);
|
||||
|
||||
// brightness multipliers for faces
|
||||
const float FACE_BRIGHTNESS[6] = {
|
||||
1.0f, // front
|
||||
0.7f, // right
|
||||
0.5f, // back
|
||||
0.7f, // left
|
||||
1.2f, // top
|
||||
0.4f // bottom
|
||||
};
|
||||
|
||||
// position, normal, color
|
||||
glm::vec4 cubeColor = {1.0f, 0.5f, 0.31f, 1.0f};
|
||||
std::vector<Point> cubeVerts = {
|
||||
// front face (z = 0, normal = +Z)
|
||||
{ {-0.5f, -0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
|
||||
{ {0.5f, -0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
|
||||
{ {-0.5f, 0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
|
||||
|
||||
// back face (z = 1, normal = -Z)
|
||||
{ {-0.5f, -0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
|
||||
{ {0.5f, -0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
|
||||
{ {-0.5f, 0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
|
||||
|
||||
// left face (x = 0, normal = -X)
|
||||
{ {-0.5f, -0.5f, -0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {-0.5f, -0.5f, 0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {-0.5f, 0.5f, 0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {-0.5f, 0.5f, -0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
|
||||
|
||||
// right face (x = 1, normal = +X)
|
||||
{ {0.5f, -0.5f, -0.5f}, {1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {0.5f, -0.5f, 0.5f}, {1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, 0.5f}, {1.f, 0.f, 0.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, -0.5f}, {1.f, 0.f, 0.f}, cubeColor },
|
||||
|
||||
// top face (y = 1, normal = +Y)
|
||||
{ {-0.5f, 0.5f, 0.5f}, {0.f, 1.f, 0.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, 0.5f}, {0.f, 1.f, 0.f}, cubeColor },
|
||||
{ {0.5f, 0.5f, -0.5f}, {0.f, 1.f, 0.f}, cubeColor },
|
||||
{ {-0.5f, 0.5f, -0.5f}, {0.f, 1.f, 0.f}, cubeColor },
|
||||
|
||||
// bottom face (y = 0, normal = -Y)
|
||||
{ {-0.5f, -0.5f, 0.5f}, {0.f, -1.f, 0.f}, cubeColor },
|
||||
{ {0.5f, -0.5f, 0.5f}, {0.f, -1.f, 0.f}, cubeColor },
|
||||
{ {0.5f, -0.5f, -0.5f}, {0.f, -1.f, 0.f}, cubeColor },
|
||||
{ {-0.5f, -0.5f, -0.5f}, {0.f, -1.f, 0.f}, cubeColor },
|
||||
};
|
||||
|
||||
std::vector<unsigned int> cubeIndices = {
|
||||
0,1,2, 2,3,0, // front
|
||||
4,5,6, 6,7,4, // back
|
||||
8,9,10, 10,11,8, // left
|
||||
12,13,14, 14,15,12, // right
|
||||
16,17,18, 18,19,16, // top
|
||||
20,21,22, 22,23,20 // bottom
|
||||
};
|
||||
|
||||
Vertices vertices;
|
||||
for (auto &v : cubeVerts) vertices.PushVertex(v);
|
||||
for (auto i : cubeIndices) vertices.PushIndex(i);
|
||||
vertices.Upload();
|
||||
|
||||
Shader simpleShader;
|
||||
simpleShader.init(
|
||||
FileManager::read("./src/shaders/simple.vs"),
|
||||
FileManager::read("./src/shaders/simple.fs")
|
||||
);
|
||||
|
||||
int screenWidth = WIDTH, screenHeight = HEIGHT;
|
||||
|
||||
glm::vec3 cameraPosition(0.f, 0.f, 2.f);
|
||||
glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
||||
// glm::vec3 lightPosition(1.f, 3.5f, -2.f);
|
||||
glm::vec3 lightPosition = cameraPosition;
|
||||
|
||||
glm::mat4 model(1.f);
|
||||
|
||||
glm::mat4 view = glm::lookAt(
|
||||
cameraPosition,
|
||||
cameraPosition + cameraViewDirection,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
|
||||
glm::mat4 projection = glm::perspective(
|
||||
(float)M_PI_2,
|
||||
(float)screenWidth / (float)screenHeight,
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
|
||||
float angle = 3.45f;
|
||||
Uint64 lastTicks = SDL_GetTicks();
|
||||
|
||||
Object cube = Object::LoadFile("./assets/cube.obj");
|
||||
Object monkey = Object::LoadFile("./assets/monkey.obj");
|
||||
|
||||
bool paused = false;
|
||||
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
Uint64 currentTicks = SDL_GetTicks();
|
||||
float deltaTime = (currentTicks - lastTicks) / 1000.0f; // seconds
|
||||
|
||||
lastTicks = currentTicks;
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
quit = true;
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
int width, height;
|
||||
if (SDL_GetWindowSize(window, &width, &height)) {
|
||||
glViewport(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height);
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
switch (event.key.key) {
|
||||
case SDLK_SPACE:
|
||||
paused = !paused;
|
||||
break;
|
||||
// case SDLK_F5:
|
||||
// reload_shaders(&context);
|
||||
// break;
|
||||
default: break;
|
||||
};
|
||||
break;
|
||||
default: break;
|
||||
};
|
||||
}
|
||||
|
||||
// update rotation
|
||||
if (!paused) {
|
||||
angle += glm::radians(45.0f) * deltaTime; // 72° per second
|
||||
if (angle > glm::two_pi<float>()) {
|
||||
angle -= glm::two_pi<float>(); // keep value small
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout << "angle = " << angle << std::endl;
|
||||
|
||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Triangle render
|
||||
{
|
||||
simpleShader.use();
|
||||
|
||||
model = glm::rotate(
|
||||
glm::mat4(1.f),
|
||||
angle,
|
||||
glm::vec3(0.8f, -0.4f, 0.5f)
|
||||
);
|
||||
|
||||
// lightPosition -= glm::vec3(0.05f, 0.f, 0.f) * deltaTime;
|
||||
|
||||
simpleShader.setMat4("u_model", model);
|
||||
simpleShader.setMat4("u_view", view);
|
||||
simpleShader.setMat4("u_projection", projection);
|
||||
|
||||
simpleShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
simpleShader.setVec3("lightPos", lightPosition);
|
||||
simpleShader.setVec3("viewPos", cameraPosition);
|
||||
simpleShader.setFloat("ambientStrength", 0.2f);
|
||||
simpleShader.setFloat("specularStrength", 0.5f);
|
||||
|
||||
vertices.Draw();
|
||||
}
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
SDL_GL_DestroyContext(glcontext);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
return 0;
|
||||
}
|
311
src/main.cpp
311
src/main.cpp
@ -1,154 +1,111 @@
|
||||
#ifndef WIN32
|
||||
#define GLEW_STATIC
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <corecrt_math_defines.h>
|
||||
#endif
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/quaternion_geometric.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
#include "shader.h"
|
||||
#include "file_manager.h"
|
||||
#include "prelude.h"
|
||||
#include "block.h"
|
||||
#include "vertex.h"
|
||||
#include "model.h"
|
||||
#include "renderer/shader.h"
|
||||
#include "renderer/wavefront.h"
|
||||
#include "renderer/engine.h"
|
||||
#include "renderer/renderer.h"
|
||||
|
||||
#define WIDTH 1024
|
||||
#define HEIGHT 768
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
int main() {
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||
#include "components/transform.h"
|
||||
#include "components/light.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/mesh.h"
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
class Game : public IApplication {
|
||||
public:
|
||||
Game() {
|
||||
Object* lightObj = Object::LoadFile("./assets/sphere.obj");
|
||||
const auto lght = m_registry.create();
|
||||
m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||
m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||
m_registry.emplace<mesh>(lght, std::unique_ptr<Object>(lightObj));
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
||||
const auto cameraEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||
m_registry.emplace<camera>(cameraEntity);
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow("OpenGL Test", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALWAYS_ON_TOP);
|
||||
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||
const auto targetEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||
m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj));
|
||||
|
||||
SDL_SetWindowRelativeMouseMode(window, true);
|
||||
Object* cubeObj = Object::LoadFile("./assets/grass_block/grass_block.obj");
|
||||
const auto cubeEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||
m_registry.emplace<mesh>(cubeEntity, std::unique_ptr<Object>(cubeObj));
|
||||
|
||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (GLEW_OK != glewInit()) {
|
||||
fprintf(stderr, "Could not initialize GLEW!\n");
|
||||
SDL_GL_DestroyContext(glcontext);
|
||||
SDL_DestroyWindow(window);
|
||||
exit(1);
|
||||
Object* floorObj = Object::LoadFile("./assets/plane.obj");
|
||||
const auto floorEntt = m_registry.create();
|
||||
m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||
m_registry.emplace<mesh>(floorEntt, std::unique_ptr<Object>(floorObj));
|
||||
}
|
||||
~Game() override {}
|
||||
|
||||
std::cout << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl;
|
||||
std::cout << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl;
|
||||
std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl;
|
||||
void OnInit() override {
|
||||
std::cout << "Game initialized" << std::endl;
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDebugMessageCallback(MessageCallback, 0);
|
||||
m_angle = 3.45f;
|
||||
m_lastTicks = SDL_GetTicks();
|
||||
|
||||
Shader simpleShader;
|
||||
simpleShader.init(
|
||||
FileManager::read("./src/shaders/simple.vs"),
|
||||
FileManager::read("./src/shaders/simple.fs")
|
||||
);
|
||||
m_paused = false;
|
||||
|
||||
int screenWidth = WIDTH, screenHeight = HEIGHT;
|
||||
|
||||
glm::vec3 cameraPosition(0.f, 0.f, 2.f);
|
||||
// glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
||||
// glm::vec3 lightPosition(1.f, 3.5f, -2.f);
|
||||
glm::vec3 lightPosition(-5.f, 5.f, 5.f);
|
||||
|
||||
glm::mat4 model(1.f);
|
||||
|
||||
glm::mat4 projection = glm::perspective(
|
||||
(float)M_PI_2,
|
||||
(float)screenWidth / (float)screenHeight,
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
|
||||
float angle = 3.45f;
|
||||
Uint64 lastTicks = SDL_GetTicks();
|
||||
|
||||
// Object teapot = Object::LoadFile("./assets/kastrula/kastrula.obj");
|
||||
// Object bricks = Object::LoadFile("./assets/bricks/bricks.obj");
|
||||
Object lightSource = Object::LoadFile("./assets/cube.obj");
|
||||
Object target = Object::LoadFile("./assets/car/car.obj");
|
||||
|
||||
bool paused = false;
|
||||
|
||||
float yaw = -90.0f; // looking along -Z initially
|
||||
float pitch = 0.0f; // no vertical tilt
|
||||
m_yaw = -90.0f; // looking along -Z initially
|
||||
m_pitch = 0.0f; // no vertical tilt
|
||||
|
||||
// FPS tracking
|
||||
Uint64 startTicks = SDL_GetTicks();
|
||||
int frameCount = 0;
|
||||
m_startTicks = SDL_GetTicks();
|
||||
m_frameCount = 0;
|
||||
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
Uint64 currentTicks = SDL_GetTicks();
|
||||
float deltaTime = (currentTicks - lastTicks) / 1000.0f; // seconds
|
||||
|
||||
lastTicks = currentTicks;
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
quit = true;
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
int width, height;
|
||||
if (SDL_GetWindowSize(window, &width, &height)) {
|
||||
screenWidth = width;
|
||||
screenHeight = height;
|
||||
glViewport(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height);
|
||||
projection = glm::perspective(
|
||||
(float)M_PI_2,
|
||||
(float)screenWidth / (float)screenHeight,
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
m_renderer.GenerateShadowMaps(m_registry);
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
};
|
||||
|
||||
void OnWindowResized(const WindowResized& event) override {
|
||||
m_renderer.OnWindowResized(event.w, event.h);
|
||||
}
|
||||
|
||||
void OnUpdate() override {
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
float deltaTime = static_cast<float>(m_currentTicks - m_lastTicks) / 1000.0f; // seconds
|
||||
|
||||
m_lastTicks = m_currentTicks;
|
||||
|
||||
float mouseXRel, mouseYRel;
|
||||
Uint32 mouseState = SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel);
|
||||
SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel);
|
||||
|
||||
float sensitivity = 0.1f; // tweak as needed
|
||||
yaw += mouseXRel * sensitivity;
|
||||
pitch -= mouseYRel * sensitivity; // invert Y for typical FPS control
|
||||
m_yaw += mouseXRel * sensitivity;
|
||||
m_pitch -= mouseYRel * sensitivity; // invert Y for typical FPS control
|
||||
|
||||
// clamp pitch to avoid flipping
|
||||
if (pitch > 89.0f) pitch = 89.0f;
|
||||
if (pitch < -89.0f) pitch = -89.0f;
|
||||
// if (pitch > 89.0f) pitch = 89.0f;
|
||||
// if (pitch < -89.0f) pitch = -89.0f;
|
||||
m_pitch = glm::clamp(m_pitch, -89.0f, 89.0f);
|
||||
|
||||
// convert to direction vector
|
||||
glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
||||
cameraViewDirection.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
cameraViewDirection.y = sin(glm::radians(pitch));
|
||||
cameraViewDirection.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
cameraViewDirection.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
cameraViewDirection.y = sin(glm::radians(m_pitch));
|
||||
cameraViewDirection.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
cameraViewDirection = glm::normalize(cameraViewDirection);
|
||||
|
||||
glm::vec3 velocity(0.f);
|
||||
|
||||
const bool* state = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (state[SDL_SCANCODE_P]) paused = !paused;
|
||||
if (state[SDL_SCANCODE_P]) m_paused = !m_paused;
|
||||
|
||||
glm::vec3 front = glm::normalize(glm::vec3(cameraViewDirection.x, 0.f, cameraViewDirection.z));
|
||||
glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0.f, 1.f, 0.f)));
|
||||
@ -160,74 +117,100 @@ int main() {
|
||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||
|
||||
cameraPosition += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
|
||||
glm::mat4 view = glm::lookAt(
|
||||
cameraPosition,
|
||||
cameraPosition + cameraViewDirection,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
auto view = m_registry.view<camera, transform>();
|
||||
for (auto [cam, camTransform] : view.each()) {
|
||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
camTransform.rotation = cameraViewDirection;
|
||||
}
|
||||
|
||||
// update rotation
|
||||
if (!paused) {
|
||||
angle += glm::radians(45.0f) * deltaTime; // 72° per second
|
||||
if (angle > glm::two_pi<float>()) {
|
||||
angle -= glm::two_pi<float>(); // keep value small
|
||||
if (!m_paused) {
|
||||
m_angle += glm::radians(45.0f) * deltaTime; // 72° per second
|
||||
if (m_angle > glm::two_pi<float>()) {
|
||||
m_angle -= glm::two_pi<float>(); // keep value small
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout << "angle = " << angle << std::endl;
|
||||
// ---- Day-night simulation ----
|
||||
m_dayTime += deltaTime;
|
||||
if (m_dayTime > m_dayLength)
|
||||
m_dayTime -= m_dayLength; // loop every "day"
|
||||
|
||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
float dayProgress = m_dayTime / m_dayLength; // 0.0 -> 1.0
|
||||
float sunAngle = dayProgress * glm::two_pi<float>(); // radians through the sky
|
||||
|
||||
// Triangle render
|
||||
{
|
||||
simpleShader.use();
|
||||
// Compute sun direction (rotating around X axis)
|
||||
// At t=0.0 sun at east horizon, at π/2 overhead, at π west horizon
|
||||
glm::vec3 sunDir = glm::normalize(glm::vec3(0.0f, sin(sunAngle), cos(sunAngle)));
|
||||
|
||||
simpleShader.setMat4("u_view", view);
|
||||
simpleShader.setMat4("u_projection", projection);
|
||||
// Compute intensity: bright at noon, dim at dusk/dawn, dark at night
|
||||
float intensity = glm::max(sin(sunAngle), (double)0.0f); // 0 at night, 1 at noon
|
||||
intensity = glm::mix(0.05f, 1.5f, intensity); // keep some ambient even at night
|
||||
|
||||
simpleShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
simpleShader.setVec3("lightPos", lightPosition);
|
||||
simpleShader.setVec3("viewPos", cameraPosition);
|
||||
// Optional: tint color (warm at sunrise/sunset)
|
||||
glm::vec3 dayColor = glm::vec3(1.0f, 0.95f, 0.9f);
|
||||
glm::vec3 sunsetColor= glm::vec3(1.0f, 0.6f, 0.3f);
|
||||
float sunsetFactor = glm::clamp(1.0f - abs(sin(sunAngle)) * 2.0f, 0.0f, 1.0f);
|
||||
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
||||
|
||||
model = glm::mat4(1.f);
|
||||
model = glm::translate(model, lightPosition);
|
||||
|
||||
simpleShader.setMat4("u_model", model);
|
||||
|
||||
lightSource.Render(simpleShader);
|
||||
|
||||
// lightPosition -= glm::vec3(0.05f, 0.f, 0.f) * deltaTime;
|
||||
|
||||
model = glm::rotate(
|
||||
glm::mat4(1.f),
|
||||
angle,
|
||||
glm::vec3(0.f, -0.5f, 0.0f)
|
||||
) * 0.5f;
|
||||
|
||||
simpleShader.setMat4("u_model", model);
|
||||
|
||||
target.Render(simpleShader);
|
||||
// Update the directional light in the registry
|
||||
auto lightsView = m_registry.view<light, transform>();
|
||||
for (auto [entity, l, t] : lightsView.each()) {
|
||||
if (l.type == light::LightType::DIRECTIONAL) {
|
||||
// "position" for directional light often stores direction vector
|
||||
// If your system instead uses transform.rotation, adjust accordingly
|
||||
t.position = sunDir * 15.f; // use this as light direction
|
||||
l.color = sunColor;
|
||||
l.intensity = intensity;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
// auto rotateEntts = m_registry.view<transform, const mesh>();
|
||||
// for (auto [entity, transform, mesh] : rotateEntts.each()) {
|
||||
// // auto targetTransform = rotateEntts.get<transform>(entity);
|
||||
// if (!m_registry.all_of<light>(entity)) {
|
||||
// transform.rotation.y = m_angle;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
frameCount++;
|
||||
currentTicks = SDL_GetTicks();
|
||||
Uint64 elapsed = currentTicks - startTicks;
|
||||
void OnRender() override {
|
||||
m_renderer.Render(m_registry);
|
||||
|
||||
m_frameCount++;
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
Uint64 elapsed = m_currentTicks - m_startTicks;
|
||||
|
||||
if (elapsed >= 1000) { // one second passed
|
||||
double fps = (double)frameCount / (elapsed / 1000.0);
|
||||
double fps = static_cast<double>(m_frameCount) / (static_cast<double>(elapsed) / 1000.0);
|
||||
std::cout << "FPS: " << fps << std::endl;
|
||||
frameCount = 0;
|
||||
startTicks = currentTicks;
|
||||
m_frameCount = 0;
|
||||
m_startTicks = m_currentTicks;
|
||||
}
|
||||
}
|
||||
private:
|
||||
Renderer m_renderer;
|
||||
entt::registry m_registry;
|
||||
|
||||
SDL_GL_DestroyContext(glcontext);
|
||||
SDL_DestroyWindow(window);
|
||||
float m_angle;
|
||||
Uint64 m_lastTicks;
|
||||
|
||||
float m_dayTime = 0.0f; // accumulates time for day-night cycle
|
||||
float m_dayLength = 60.0f; // seconds per full day cycle
|
||||
|
||||
bool m_paused = false;
|
||||
|
||||
float m_yaw = -90.0f; // looking along -Z initially
|
||||
float m_pitch = 0.0f; // no vertical tilt
|
||||
|
||||
// FPS tracking
|
||||
Uint64 m_startTicks;
|
||||
int m_frameCount;
|
||||
|
||||
Uint64 m_currentTicks;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Engine::Run(std::make_unique<Game>());
|
||||
return 0;
|
||||
}
|
365
src/model.cpp
365
src/model.cpp
@ -1,365 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "model.h"
|
||||
|
||||
ObjElement toElement(const std::string &s) {
|
||||
if (s == "#") return ObjElement::OHASH;
|
||||
if (s == "mtllib") return ObjElement::MTLLIB;
|
||||
if (s == "usemtl") return ObjElement::USEMTL;
|
||||
if (s == "o") return ObjElement::O;
|
||||
if (s == "v") return ObjElement::V;
|
||||
if (s == "vn") return ObjElement::VN;
|
||||
if (s == "vt") return ObjElement::VT;
|
||||
if (s == "f") return ObjElement::F;
|
||||
return ObjElement::OUNKNOWN;
|
||||
}
|
||||
|
||||
MtlElement toMtlElement(const std::string &s) {
|
||||
if (s == "#") return MtlElement::MHASH;
|
||||
if (s == "newmtl") return MtlElement::NEWMTL;
|
||||
if (s == "Ns") return MtlElement::NS;
|
||||
if (s == "Ka") return MtlElement::KA;
|
||||
if (s == "Ks") return MtlElement::KS;
|
||||
if (s == "Kd") return MtlElement::KD;
|
||||
if (s == "Ni") return MtlElement::NI;
|
||||
if (s == "d") return MtlElement::D;
|
||||
if (s == "illum") return MtlElement::ILLUM;
|
||||
if (s == "map_Kd") return MtlElement::MAP_KD;
|
||||
if (s == "map_Ka") return MtlElement::MAP_KA;
|
||||
// if (s == "map_Ke") return MtlElement::MAP_KE;
|
||||
return MtlElement::MUNKNOWN;
|
||||
}
|
||||
|
||||
void Vertex::DefineAttrib()
|
||||
{
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
|
||||
void Face::PushItem(const FaceItem& item)
|
||||
{
|
||||
m_items.push_back(item);
|
||||
}
|
||||
|
||||
inline int Object::NormalizeIndex(const std::string &s, int baseCount) {
|
||||
if (s.empty()) return -1;
|
||||
int idx = std::stoi(s);
|
||||
if (idx > 0) return idx - 1;
|
||||
return baseCount + idx;
|
||||
}
|
||||
|
||||
Mesh::Mesh() {
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glGenBuffers(1, &m_vbo);
|
||||
glGenBuffers(1, &m_ebo);
|
||||
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
// VBO (vertex buffer)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
|
||||
// EBO (index buffer)
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
|
||||
Vertex::DefineAttrib();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
Object::Object() {
|
||||
m_vertices = std::vector<glm::vec3>();
|
||||
m_normals = std::vector<glm::vec3>();
|
||||
m_texCoords = std::vector<glm::vec2>();
|
||||
}
|
||||
|
||||
void Object::LoadMaterials(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
|
||||
std::string currentMaterialName;
|
||||
std::shared_ptr<Material> currentMaterial;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::istringstream iss(line);
|
||||
std::string prefix;
|
||||
iss >> prefix;
|
||||
switch(toMtlElement(prefix)) {
|
||||
case MtlElement::MHASH:
|
||||
{
|
||||
std::cout << "comment: " << line << std::endl;
|
||||
continue;
|
||||
}
|
||||
case MtlElement::NEWMTL:
|
||||
{
|
||||
if (currentMaterial) {
|
||||
m_materials.insert(std::make_pair(currentMaterialName, std::move(currentMaterial)));
|
||||
currentMaterial = nullptr;
|
||||
}
|
||||
std::string materialName;
|
||||
iss >> materialName;
|
||||
currentMaterialName = materialName;
|
||||
currentMaterial = std::make_shared<Material>();
|
||||
break;
|
||||
}
|
||||
case MtlElement::NS:
|
||||
{
|
||||
float weight;
|
||||
iss >> weight;
|
||||
currentMaterial->SetSpecularWeight(weight);
|
||||
}
|
||||
case MtlElement::KA:
|
||||
{
|
||||
float r, g, b;
|
||||
iss >> r >> g >> b;
|
||||
currentMaterial->SetAmbientColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
case MtlElement::KS:
|
||||
{
|
||||
float r, g, b;
|
||||
iss >> r >> g >> b;
|
||||
currentMaterial->SetSpecularColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
case MtlElement::KD:
|
||||
{
|
||||
float r, g, b;
|
||||
iss >> r >> g >> b;
|
||||
currentMaterial->SetDiffuseColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
case MtlElement::MAP_KD:
|
||||
{
|
||||
std::string texturePath;
|
||||
std::string part;
|
||||
while (iss >> part) {
|
||||
texturePath += part + " ";
|
||||
}
|
||||
texturePath = texturePath.substr(0, texturePath.size() - 1);
|
||||
currentMaterial->SetDiffuseTexture(Texture::LoadFile(texturePath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentMaterial) {
|
||||
// m_materials.insert(std::make_pair(currentMaterialName, std::move(currentMaterial)));
|
||||
AddMaterial(currentMaterialName, std::move(currentMaterial));
|
||||
}
|
||||
}
|
||||
|
||||
void Object::AddMaterial(std::string name, std::shared_ptr<Material> material)
|
||||
{
|
||||
m_materials.insert(std::make_pair(std::move(name), std::move(material)));
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> Object::GetMaterial(std::string name)
|
||||
{
|
||||
auto material = m_materials.find(name);
|
||||
if (material == m_materials.end()) return nullptr;
|
||||
return material->second;
|
||||
}
|
||||
|
||||
void Object::CreateNewMesh(const std::string& materialName)
|
||||
{
|
||||
Mesh mesh;
|
||||
mesh.materialName = materialName;
|
||||
m_meshes.push_back(mesh);
|
||||
}
|
||||
|
||||
Mesh& Object::GetLastMesh()
|
||||
{
|
||||
if (m_meshes.empty()) {
|
||||
auto material = std::make_shared<Material>();
|
||||
material->SetAmbientColor(glm::vec3(0.52f, 0.52f, 0.52f));
|
||||
// TODO: come up with name for a default material
|
||||
AddMaterial("", std::move(material));
|
||||
CreateNewMesh("");
|
||||
}
|
||||
return m_meshes.back();
|
||||
}
|
||||
|
||||
Object Object::LoadFile(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
|
||||
Object obj;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::istringstream iss(line);
|
||||
std::string prefix;
|
||||
iss >> prefix;
|
||||
switch(toElement(prefix)) {
|
||||
// comment
|
||||
case ObjElement::OHASH:
|
||||
{
|
||||
std::cout << "comment: " << line << std::endl;
|
||||
continue;
|
||||
}
|
||||
case ObjElement::MTLLIB:
|
||||
{
|
||||
std::string mtlFile;
|
||||
iss >> mtlFile;
|
||||
std::filesystem::path fullPath = filename;
|
||||
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
|
||||
obj.LoadMaterials(mtlPath.u8string());
|
||||
std::cout << "loaded mtl at '" << mtlPath << "' with "
|
||||
<< obj.m_materials.size() << " materials" << std::endl;
|
||||
break;
|
||||
}
|
||||
case ObjElement::USEMTL:
|
||||
{
|
||||
std::string materialName;
|
||||
iss >> materialName;
|
||||
auto& mesh = obj.GetLastMesh();
|
||||
if (mesh.materialName != materialName) {
|
||||
Mesh mesh;
|
||||
mesh.materialName = materialName;
|
||||
obj.m_meshes.push_back(mesh);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// object name I suppose
|
||||
case ObjElement::O:
|
||||
{
|
||||
obj.m_name = line.substr(2);
|
||||
break;
|
||||
}
|
||||
// vertex with its position
|
||||
case ObjElement::V:
|
||||
{
|
||||
float x, y, z, w;
|
||||
w = 1.0f;
|
||||
iss >> x >> y >> z;
|
||||
if (iss >> w) {
|
||||
x /= w;
|
||||
y /= w;
|
||||
z /= w;
|
||||
}
|
||||
obj.m_vertices.emplace_back(x, y, z);
|
||||
break;
|
||||
}
|
||||
case ObjElement::VN:
|
||||
{
|
||||
float x, y, z;
|
||||
iss >> x >> y >> z;
|
||||
obj.m_normals.emplace_back(x, y, z);
|
||||
break;
|
||||
}
|
||||
case ObjElement::VT:
|
||||
{
|
||||
float u, v;
|
||||
iss >> u >> v;
|
||||
obj.m_texCoords.emplace_back(u, 1.0f - v);
|
||||
break;
|
||||
}
|
||||
case ObjElement::F:
|
||||
{
|
||||
auto& mesh = obj.GetLastMesh();
|
||||
std::string token;
|
||||
|
||||
Face fv;
|
||||
|
||||
while (iss >> token) {
|
||||
std::string a, b, c;
|
||||
std::istringstream ref(token);
|
||||
|
||||
std::getline(ref, a, '/');
|
||||
std::getline(ref, b, '/');
|
||||
std::getline(ref, c, '/');
|
||||
|
||||
int vi = Object::NormalizeIndex(a, (int)obj.m_vertices.size());
|
||||
int ti = Object::NormalizeIndex(b, (int)obj.m_texCoords.size());
|
||||
int ni = Object::NormalizeIndex(c, (int)obj.m_normals.size());
|
||||
|
||||
glm::vec3 vert, norm;
|
||||
glm::vec2 texCoord;
|
||||
vert = obj.m_vertices[vi];
|
||||
if (ni >= 0) norm = obj.m_normals[ni];
|
||||
if (ti >= 0) texCoord = obj.m_texCoords[ti];
|
||||
|
||||
mesh.m_vertexBuffer.emplace_back(vert, norm, texCoord);
|
||||
mesh.m_indexBuffer.push_back(mesh.m_vertexBuffer.size() - 1);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Object name: " << obj.m_name << std::endl;
|
||||
std::cout << "Vertices count: " << obj.m_vertices.size() << std::endl;
|
||||
std::cout << "Normals count: " << obj.m_normals.size() << std::endl;
|
||||
std::cout << "TexCoords count: " << obj.m_texCoords.size() << std::endl;
|
||||
std::cout << "Meshes count: " << obj.m_meshes.size() << std::endl;
|
||||
std::cout << "Materials count: " << obj.m_materials.size() << std::endl;
|
||||
|
||||
// std::cout << "Vertex Buffer size: " << obj.m_vertexBuffer.size() << std::endl;
|
||||
// std::cout << "Index Buffer size: " << obj.m_indexBuffer.size() << std::endl;
|
||||
|
||||
file.close();
|
||||
|
||||
for (auto &mesh : obj.m_meshes) {
|
||||
mesh.Upload();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Object::Render(Shader& shader)
|
||||
{
|
||||
for (auto &mesh : m_meshes) {
|
||||
auto material = GetMaterial(mesh.materialName);
|
||||
|
||||
shader.setFloat("ambientStrength", 0.2f);
|
||||
shader.setFloat("specularStrength", material->GetSpecularWeight());
|
||||
shader.setVec3("ambientColor", material->GetAmbientColor());
|
||||
shader.setVec3("diffuseColor", material->GetDiffuseColor());
|
||||
shader.setVec3("specularColor", material->GetSpecularColor());
|
||||
|
||||
if (material->HasDiffuseTexture()) {
|
||||
shader.setBool("useTexture", true);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||
shader.setInt("diffuseTex", 0);
|
||||
} else {
|
||||
shader.setBool("useTexture", false);
|
||||
}
|
||||
|
||||
mesh.Render();
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::Upload()
|
||||
{
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_STATIC_DRAW);
|
||||
|
||||
// Upload indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::Render()
|
||||
{
|
||||
Bind();
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
||||
Unbind();
|
||||
}
|
190
src/prelude.cpp
190
src/prelude.cpp
@ -1,190 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "prelude.h"
|
||||
|
||||
// #define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
|
||||
#define SCREEN_WIDTH 1024
|
||||
#define SCREEN_HEIGHT 768
|
||||
|
||||
void panic_errno(const char *fmt, ...)
|
||||
{
|
||||
fprintf(stderr, "ERROR: ");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, ": %s\n", strerror(errno));
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *slurp_file(const char *file_path)
|
||||
{
|
||||
#define SLURP_FILE_PANIC panic_errno("Could not read file `%s`", file_path)
|
||||
FILE *f = fopen(file_path, "r");
|
||||
if (f == NULL) SLURP_FILE_PANIC;
|
||||
if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
long size = ftell(f);
|
||||
if (size < 0) SLURP_FILE_PANIC;
|
||||
|
||||
char *buffer = (char*)malloc(size + 1);
|
||||
if (buffer == NULL) SLURP_FILE_PANIC;
|
||||
|
||||
if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
fread(buffer, 1, size, f);
|
||||
if (ferror(f) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
buffer[size] = '\0';
|
||||
|
||||
if (fclose(f) < 0) SLURP_FILE_PANIC;
|
||||
|
||||
return buffer;
|
||||
#undef SLURP_FILE_PANIC
|
||||
}
|
||||
|
||||
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader)
|
||||
{
|
||||
*shader = glCreateShader(shader_type);
|
||||
glShaderSource(*shader, 1, &source, NULL);
|
||||
glCompileShader(*shader);
|
||||
|
||||
GLint compiled = 0;
|
||||
glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled);
|
||||
|
||||
if (!compiled) {
|
||||
GLchar message[1024];
|
||||
GLsizei message_size = 0;
|
||||
glGetShaderInfoLog(*shader, sizeof(message), &message_size, message);
|
||||
fprintf(stderr, "%.*s\n", message_size, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader)
|
||||
{
|
||||
char *source = slurp_file(file_path);
|
||||
bool err = compile_shader_source(source, shader_type, shader);
|
||||
free(source);
|
||||
return err;
|
||||
}
|
||||
|
||||
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
|
||||
{
|
||||
*program = glCreateProgram();
|
||||
|
||||
glAttachShader(*program, vert_shader);
|
||||
glAttachShader(*program, frag_shader);
|
||||
glLinkProgram(*program);
|
||||
|
||||
GLint linked = 0;
|
||||
glGetProgramiv(*program, GL_LINK_STATUS, &linked);
|
||||
if (!linked) {
|
||||
GLsizei message_size = 0;
|
||||
GLchar message[1024];
|
||||
|
||||
glGetProgramInfoLog(*program, sizeof(message), &message_size, message);
|
||||
fprintf(stderr, "Program Linking: %.*s\n", message_size, message);
|
||||
}
|
||||
|
||||
glDeleteShader(vert_shader);
|
||||
glDeleteShader(frag_shader);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
void reload_shaders(RenderContext* context)
|
||||
{
|
||||
glDeleteProgram(context->program);
|
||||
|
||||
context->program_failed = false;
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
GLuint vert = 0;
|
||||
if (!compile_shader_file("./main.vert", GL_VERTEX_SHADER, &vert)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
GLuint frag = 0;
|
||||
if (!compile_shader_file("./main.frag", GL_FRAGMENT_SHADER, &frag)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!link_program(vert, frag, &context->program)) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
context->program_failed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(context->program);
|
||||
|
||||
context->resolution_location = glGetUniformLocation(context->program, "resolution");
|
||||
context->time_location = glGetUniformLocation(context->program, "time");
|
||||
|
||||
printf("Successfully Reload the Shaders\n");
|
||||
}
|
||||
|
||||
void window_size_callback(SDL_Window* window, int width, int height)
|
||||
{
|
||||
(void) window;
|
||||
glViewport(
|
||||
width / 2 - SCREEN_WIDTH / 2,
|
||||
height / 2 - SCREEN_HEIGHT / 2,
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT);
|
||||
}
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam)
|
||||
{
|
||||
(void) source;
|
||||
(void) id;
|
||||
(void) length;
|
||||
(void) userParam;
|
||||
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||
type, severity, message);
|
||||
}
|
||||
|
||||
void process_prelude(RenderContext *context)
|
||||
{
|
||||
// glfwSetKeyCallback(window, key_callback);
|
||||
// glfwSetFramebufferSizeCallback(window, window_size_callback);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if (!context->program_failed) {
|
||||
glUniform2f(context->resolution_location,
|
||||
SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT);
|
||||
glUniform1f(context->time_location, context->time);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
int64_t cur_time = SDL_GetTicks();
|
||||
if (!context->pause) {
|
||||
context->time += cur_time - context->prev_time;
|
||||
}
|
||||
context->prev_time = cur_time;
|
||||
}
|
15
src/renderer/basics.cpp
Normal file
15
src/renderer/basics.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "renderer/basics.h"
|
||||
|
||||
void Vertex::DefineAttrib()
|
||||
{
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
24
src/renderer/debug.cpp
Normal file
24
src/renderer/debug.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "renderer/debug.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam)
|
||||
{
|
||||
(void) source;
|
||||
(void) id;
|
||||
(void) length;
|
||||
(void) userParam;
|
||||
std::cerr << "GL CALLBACK: " << (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "")
|
||||
<< " type = 0x" << type
|
||||
<< ", severity = 0x" << severity
|
||||
<< ", message = " << message << std::endl;
|
||||
// std::cerr << "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||
// (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
|
||||
// type, severity, message);
|
||||
}
|
42
src/renderer/engine.cpp
Normal file
42
src/renderer/engine.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include <memory>
|
||||
|
||||
#include "renderer/engine.h"
|
||||
#include "window/event.h"
|
||||
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
std::unique_ptr<IApplication> Engine::s_app = nullptr;
|
||||
std::shared_ptr<Window> Engine::s_window = nullptr;
|
||||
bool Engine::s_running = false;
|
||||
|
||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||
s_app = std::move(app);
|
||||
s_window = Window::GetInstance();
|
||||
s_running = true;
|
||||
|
||||
s_app->OnInit();
|
||||
|
||||
s_window->Subscribe<WindowCloseRequested>([](const WindowCloseRequested& e) {
|
||||
Engine::s_running = false;
|
||||
});
|
||||
|
||||
s_window->Subscribe<WindowResized>([](const WindowResized& e) {
|
||||
Engine::s_app->OnWindowResized(e);
|
||||
});
|
||||
|
||||
while (s_running) {
|
||||
s_window->ProcessEvents();
|
||||
|
||||
s_app->OnUpdate();
|
||||
|
||||
s_app->OnRender();
|
||||
|
||||
s_window->SwapBuffers();
|
||||
}
|
||||
|
||||
s_app->OnShutdown();
|
||||
|
||||
s_window->Destroy();
|
||||
s_app.reset();
|
||||
}
|
||||
|
46
src/renderer/mesh.cpp
Normal file
46
src/renderer/mesh.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "renderer/mesh.h"
|
||||
|
||||
Mesh::Mesh() {
|
||||
m_vao = 0;
|
||||
m_vbo = 0;
|
||||
m_ebo = 0;
|
||||
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glGenBuffers(1, &m_vbo);
|
||||
glGenBuffers(1, &m_ebo);
|
||||
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
// VBO (vertex buffer)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
|
||||
// EBO (index buffer)
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
|
||||
Vertex::DefineAttrib();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::Upload() const {
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_STATIC_DRAW);
|
||||
|
||||
// Upload indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::Render()
|
||||
{
|
||||
Bind();
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
||||
Unbind();
|
||||
}
|
197
src/renderer/renderer.cpp
Normal file
197
src/renderer/renderer.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#ifdef WIN32
|
||||
#include <corecrt_math_defines.h>
|
||||
#endif
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
|
||||
#include "renderer/renderer.h"
|
||||
#include "window/window.h"
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
#include "components/transform.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/light.h"
|
||||
#include "components/mesh.h"
|
||||
|
||||
Renderer::Renderer()
|
||||
{
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
static_cast<float>(Window::GetWidth()) / static_cast<float>(Window::GetHeight()),
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
|
||||
m_shader.init(
|
||||
FileManager::read("./src/shaders/simple.vs"),
|
||||
FileManager::read("./src/shaders/pbr.fs")
|
||||
);
|
||||
|
||||
m_depthShader.init(
|
||||
FileManager::read("./src/shaders/depth.vs"),
|
||||
FileManager::read("./src/shaders/depth.fs")
|
||||
);
|
||||
|
||||
m_model = glm::mat4(1.f);
|
||||
|
||||
m_shader.use();
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::OnWindowResized(int w, int h) {
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
static_cast<float>(w) / static_cast<float>(h),
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
m_depthShader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::ApplyLights(entt::registry& registry, Shader &shader) {
|
||||
auto lights = registry.view<light>();
|
||||
// TODO: Pass Lights Data to depth shader as well
|
||||
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||
size_t lightIndex = 0;
|
||||
for (auto entity : lights) {
|
||||
auto &l = registry.get<light>(entity);
|
||||
auto &transf = registry.get<transform>(entity);
|
||||
|
||||
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
||||
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||
shader.setVec3("lights[" + std::to_string(lightIndex) + "].color", l.color);
|
||||
shader.setFloat("lights[" + std::to_string(lightIndex) + "].intensity", l.intensity);
|
||||
shader.setMat4("lights[" + std::to_string(lightIndex) + "].lightSpace", l.lightSpace);
|
||||
shader.setInt("lights[" + std::to_string(lightIndex) + "].shadowMap", 10 + lightIndex);
|
||||
glActiveTexture(GL_TEXTURE10 + lightIndex);
|
||||
glBindTexture(GL_TEXTURE_2D, l.shadowMap);
|
||||
|
||||
++lightIndex;
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::UpdateView(entt::registry& registry, Shader &shader) {
|
||||
auto cam = registry.view<transform, camera>().back();
|
||||
auto camTransform = registry.get<transform>(cam);
|
||||
|
||||
m_view = glm::lookAt(
|
||||
camTransform.position,
|
||||
camTransform.position + camTransform.rotation,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
shader.setMat4("u_view", m_view);
|
||||
|
||||
shader.setVec3("viewPos", camTransform.position);
|
||||
}
|
||||
|
||||
void Renderer::RenderScene(entt::registry& registry, Shader &shader) {
|
||||
auto view = registry.view<transform, mesh>();
|
||||
|
||||
for (auto [entity, transf, mesh] : view.each()) {
|
||||
if (mesh.object == nullptr) {
|
||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (registry.all_of<light>(entity)) {
|
||||
auto &l = registry.get<light>(entity);
|
||||
shader.setBool("isLight", true);
|
||||
shader.setVec3("currentLightColor", l.color);
|
||||
} else {
|
||||
shader.setBool("isLight", false);
|
||||
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
||||
}
|
||||
|
||||
glm::mat4 rotation = glm::yawPitchRoll(transf.rotation.y, transf.rotation.x, transf.rotation.z);
|
||||
m_model = glm::translate(glm::mat4(1.f), transf.position) * rotation;
|
||||
|
||||
shader.setMat4("u_model", m_model);
|
||||
|
||||
mesh.object->Render(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::GenerateShadowMaps(entt::registry& registry) {
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
m_depthShader.use();
|
||||
|
||||
auto lights = registry.view<light>();
|
||||
|
||||
for (auto [lEntt, l] : lights.each()) {
|
||||
// TODO: support other light types when ready
|
||||
if (l.type != light::LightType::DIRECTIONAL) return;
|
||||
|
||||
glGenFramebuffers(1, &l.fbo);
|
||||
glGenTextures(1, &l.shadowMap);
|
||||
glBindTexture(GL_TEXTURE_2D, l.shadowMap);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,
|
||||
SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
||||
|
||||
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, l.fbo);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, l.shadowMap, 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::Render(entt::registry& registry) {
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
m_depthShader.use();
|
||||
|
||||
auto lights = registry.view<light, transform>();
|
||||
|
||||
for (auto [lEntt, l, t] : lights.each()) {
|
||||
// TODO: support other light types when ready
|
||||
if (l.type != light::LightType::DIRECTIONAL) return;
|
||||
|
||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
float near_plane = 0.1f, far_plane = 50.0f;
|
||||
glm::vec3 target = glm::vec3(0.0f, 0.5f, 0.0f);
|
||||
glm::mat4 lightView = glm::lookAt(t.position, target, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glm::mat4 lightProjection = glm::ortho(-6.0f, 6.0f, -6.0f, 6.0f, 1.0f, 20.0f);
|
||||
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
||||
|
||||
m_depthShader.setMat4("u_lightSpace", lightSpaceMatrix);
|
||||
l.lightSpace = lightSpaceMatrix;
|
||||
|
||||
glCullFace(GL_FRONT);
|
||||
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, l.fbo);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
RenderScene(registry, m_depthShader);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glCullFace(GL_BACK);
|
||||
}
|
||||
|
||||
// actual rendering
|
||||
|
||||
glViewport(0, 0, Window::GetWidth(), Window::GetHeight());
|
||||
|
||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
m_shader.use();
|
||||
|
||||
ApplyLights(registry, m_shader);
|
||||
UpdateView(registry, m_shader);
|
||||
|
||||
RenderScene(registry, m_shader);
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
#include "shader.h"
|
||||
#include <iostream>
|
||||
#include <GL/glew.h>
|
||||
#include "renderer/shader.h"
|
||||
|
||||
Shader::Shader()
|
||||
{
|
@ -1,7 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include "texture.h"
|
||||
#include "renderer/texture.h"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
@ -10,7 +11,7 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
|
||||
auto texture = std::make_unique<Texture>();
|
||||
|
||||
int w, h, c;
|
||||
unsigned char *data = stbi_load(filename.c_str(), &w, &h, &c, 0);
|
||||
unsigned char *data = stbi_load(filename.c_str(), &w, &h, &c, 4);
|
||||
if (!data) {
|
||||
std::cerr << "ERROR: Failed to load texture under '" << filename << "'" << std::endl;
|
||||
std::exit(1);
|
||||
@ -25,8 +26,7 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// TODO: automatically detect values for this function
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
std::cout << "Loaded texture under '" << filename << "' with size of " << sizeof(data) << " bytes" << std::endl;
|
491
src/renderer/wavefront.cpp
Normal file
491
src/renderer/wavefront.cpp
Normal file
@ -0,0 +1,491 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "IO/parser.h"
|
||||
#include "renderer/mesh.h"
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
#define DEFAULT_MATERIAL_NAME "default"
|
||||
|
||||
// ObjElement toElement(const std::string &s) {
|
||||
// if (s == "#") return ObjElement::OHASH;
|
||||
// if (s == "mtllib") return ObjElement::MTLLIB;
|
||||
// if (s == "usemtl") return ObjElement::USEMTL;
|
||||
// if (s == "o") return ObjElement::O;
|
||||
// if (s == "v") return ObjElement::V;
|
||||
// if (s == "vn") return ObjElement::VN;
|
||||
// if (s == "vt") return ObjElement::VT;
|
||||
// if (s == "f") return ObjElement::F;
|
||||
// return ObjElement::OUNKNOWN;
|
||||
// }
|
||||
|
||||
inline ObjElement toElement(const char* s) {
|
||||
switch (s[0]) {
|
||||
case '#': return ObjElement::OHASH;
|
||||
case 'm': if (strcmp(s, "mtllib") == 0) return ObjElement::MTLLIB; break;
|
||||
case 'u': if (strcmp(s, "usemtl") == 0) return ObjElement::USEMTL; break;
|
||||
case 'o': if (s[1] == '\0') return ObjElement::O; break;
|
||||
case 'v':
|
||||
if (s[1] == '\0') return ObjElement::V;
|
||||
if (s[1] == 'n' && s[2] == '\0') return ObjElement::VN;
|
||||
if (s[1] == 't' && s[2] == '\0') return ObjElement::VT;
|
||||
break;
|
||||
case 'f': if (s[1] == '\0') return ObjElement::F; break;
|
||||
}
|
||||
return ObjElement::OUNKNOWN;
|
||||
}
|
||||
|
||||
// MtlElement toMtlElement(const std::string &s) {
|
||||
// if (s == "#") return MtlElement::MHASH;
|
||||
// if (s == "newmtl") return MtlElement::NEWMTL;
|
||||
// if (s == "Ns") return MtlElement::NS;
|
||||
// if (s == "Ka") return MtlElement::KA;
|
||||
// if (s == "Ks") return MtlElement::KS;
|
||||
// if (s == "Kd") return MtlElement::KD;
|
||||
// if (s == "Ni") return MtlElement::NI;
|
||||
// if (s == "d") return MtlElement::D;
|
||||
// if (s == "illum") return MtlElement::ILLUM;
|
||||
// if (s == "map_Kd") return MtlElement::MAP_KD;
|
||||
// if (s == "map_Ka") return MtlElement::MAP_KA;
|
||||
// // if (s == "map_Ke") return MtlElement::MAP_KE;
|
||||
// return MtlElement::MUNKNOWN;
|
||||
// }
|
||||
|
||||
inline MtlElement toMtlElement(const char* s) {
|
||||
switch (s[0]) {
|
||||
case '#': return MtlElement::MHASH;
|
||||
case 'n':
|
||||
if (strcmp(s, "newmtl") == 0) return MtlElement::NEWMTL;
|
||||
break;
|
||||
case 'N':
|
||||
if (s[1] == 's' && s[2] == '\0') return MtlElement::NS;
|
||||
if (s[1] == 'i' && s[2] == '\0') return MtlElement::NI;
|
||||
break;
|
||||
case 'K':
|
||||
if (s[1] == 'a' && s[2] == '\0') return MtlElement::KA;
|
||||
if (s[1] == 's' && s[2] == '\0') return MtlElement::KS;
|
||||
if (s[1] == 'd' && s[2] == '\0') return MtlElement::KD;
|
||||
break;
|
||||
case 'd':
|
||||
if (s[1] == '\0') return MtlElement::D;
|
||||
break;
|
||||
case 'i':
|
||||
if (strcmp(s, "illum") == 0) return MtlElement::ILLUM;
|
||||
break;
|
||||
case 'm':
|
||||
if (strcmp(s, "map_Kd") == 0) return MtlElement::MAP_KD;
|
||||
if (strcmp(s, "map_Ka") == 0) return MtlElement::MAP_KA;
|
||||
// if (strcmp(s, "map_Ke") == 0) return MtlElement::MAP_KE;
|
||||
break;
|
||||
}
|
||||
return MtlElement::MUNKNOWN;
|
||||
}
|
||||
|
||||
inline int Object::NormalizeIndex(int idx, int baseCount) {
|
||||
// idx is the raw value returned by parser:
|
||||
// 0 -> means "not present" or invalid in our convention
|
||||
// >0 -> 1-based index -> convert to 0-based
|
||||
// <0 -> negative index -> relative to baseCount: baseCount + idx
|
||||
if (idx == 0) return -1; // absent / invalid
|
||||
if (idx > 0) return idx - 1; // 1-based -> 0-based
|
||||
return baseCount + idx; // negative -> count from end
|
||||
}
|
||||
|
||||
Object::Object() {
|
||||
m_vertices = std::vector<glm::vec3>();
|
||||
m_normals = std::vector<glm::vec3>();
|
||||
m_texCoords = std::vector<glm::vec2>();
|
||||
}
|
||||
|
||||
void Object::LoadMaterials(const std::filesystem::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open MTL file: " << filename << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string currentMaterialName;
|
||||
std::shared_ptr<Material> currentMaterial;
|
||||
|
||||
char line[1024]; // buffer per line
|
||||
|
||||
while (file.getline(line, sizeof(line))) {
|
||||
Parser p(line);
|
||||
char* prefix = p.TakeWord();
|
||||
if (!prefix) continue;
|
||||
|
||||
switch (toMtlElement(prefix)) {
|
||||
case MtlElement::MHASH: // comment
|
||||
continue;
|
||||
|
||||
case MtlElement::NEWMTL:
|
||||
{
|
||||
// If a material was being built, commit it first
|
||||
if (currentMaterial) {
|
||||
AddMaterial(currentMaterialName, std::move(currentMaterial));
|
||||
currentMaterial = nullptr;
|
||||
}
|
||||
|
||||
char* materialName = p.TakeWord();
|
||||
if (materialName) {
|
||||
currentMaterialName = materialName;
|
||||
currentMaterial = std::make_shared<Material>();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::NS: // specular weight
|
||||
{
|
||||
float weight = p.TakeFloat();
|
||||
if (currentMaterial) currentMaterial->SetSpecularWeight(weight);
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::KA: // ambient color
|
||||
{
|
||||
float r = p.TakeFloat();
|
||||
float g = p.TakeFloat();
|
||||
float b = p.TakeFloat();
|
||||
if (currentMaterial) currentMaterial->SetAmbientColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::KS: // specular color
|
||||
{
|
||||
float r = p.TakeFloat();
|
||||
float g = p.TakeFloat();
|
||||
float b = p.TakeFloat();
|
||||
if (currentMaterial) currentMaterial->SetSpecularColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::KD: // diffuse color
|
||||
{
|
||||
float r = p.TakeFloat();
|
||||
float g = p.TakeFloat();
|
||||
float b = p.TakeFloat();
|
||||
if (currentMaterial) currentMaterial->SetDiffuseColor(glm::vec3(r, g, b));
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::D: // opacity
|
||||
{
|
||||
float d = p.TakeFloat();
|
||||
if (currentMaterial) currentMaterial->SetOpacity(d);
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::ILLUM: // illumination model
|
||||
{
|
||||
int illum = p.TakeInt();
|
||||
if (currentMaterial) currentMaterial->SetIllumination(illum);
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::MAP_KD: // diffuse texture map
|
||||
{
|
||||
// take rest of line as texture path (can contain spaces)
|
||||
char* texPath = p.TakeUntil('\0');
|
||||
if (texPath && currentMaterial) {
|
||||
// trim trailing spaces
|
||||
size_t len = std::strlen(texPath);
|
||||
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
||||
texPath[--len] = '\0';
|
||||
|
||||
currentMaterial->SetDiffuseTexture(Texture::LoadFile(texPath));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MtlElement::MAP_KA: // ambient texture map
|
||||
{
|
||||
char* texPath = p.TakeUntil('\0');
|
||||
if (texPath && currentMaterial) {
|
||||
size_t len = std::strlen(texPath);
|
||||
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
||||
texPath[--len] = '\0';
|
||||
|
||||
// optional: handle ambient texture
|
||||
// currentMaterial->SetAmbientTexture(Texture::LoadFile(texPath));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// ignore unknown tokens
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Commit last material if pending
|
||||
if (currentMaterial) {
|
||||
AddMaterial(currentMaterialName, std::move(currentMaterial));
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Object::AddMaterial(std::string name, std::shared_ptr<Material> material)
|
||||
{
|
||||
m_materials.insert(std::make_pair(std::move(name), std::move(material)));
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> Object::GetMaterial(std::string name)
|
||||
{
|
||||
auto material = m_materials.find(name);
|
||||
if (material == m_materials.end()) return nullptr;
|
||||
return material->second;
|
||||
}
|
||||
|
||||
void Object::CreateNewMesh(const std::string& materialName)
|
||||
{
|
||||
Mesh mesh;
|
||||
mesh.materialName = materialName;
|
||||
m_meshes.push_back(mesh);
|
||||
}
|
||||
|
||||
Mesh& Object::GetLastMesh()
|
||||
{
|
||||
if (m_meshes.empty()) {
|
||||
auto material = std::make_shared<Material>();
|
||||
material->SetAmbientColor(glm::vec3(0.52f, 0.52f, 0.52f));
|
||||
AddMaterial(DEFAULT_MATERIAL_NAME, std::move(material));
|
||||
CreateNewMesh(DEFAULT_MATERIAL_NAME);
|
||||
}
|
||||
return m_meshes.back();
|
||||
}
|
||||
|
||||
Object* Object::LoadFile(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open OBJ file: " << filename << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
Object* obj = new Object();
|
||||
char line[1024]; // static buffer for each line (enough for OBJ lines)
|
||||
|
||||
while (file.getline(line, sizeof(line))) {
|
||||
Parser p(line);
|
||||
char* prefix = p.TakeWord();
|
||||
if (!prefix) continue;
|
||||
|
||||
switch (toElement(prefix)) {
|
||||
case ObjElement::OHASH: // comment
|
||||
continue;
|
||||
|
||||
case ObjElement::MTLLIB:
|
||||
{
|
||||
char* mtlFile = p.TakeWord();
|
||||
if (mtlFile) {
|
||||
std::filesystem::path fullPath = filename;
|
||||
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
|
||||
obj->LoadMaterials(mtlPath);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::USEMTL:
|
||||
{
|
||||
char* materialName = p.TakeWord();
|
||||
if (materialName) {
|
||||
auto& mesh = obj->GetLastMesh();
|
||||
if (mesh.materialName != materialName) {
|
||||
Mesh newMesh;
|
||||
newMesh.materialName = materialName;
|
||||
obj->m_meshes.push_back(newMesh);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::O: // object name
|
||||
{
|
||||
char* name = p.TakeWord();
|
||||
if (name) obj->m_name = name;
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::V: // vertex
|
||||
{
|
||||
float x = p.TakeFloat();
|
||||
float y = p.TakeFloat();
|
||||
float z = p.TakeFloat();
|
||||
float w = p.TakeFloat();
|
||||
|
||||
if (w != 0.0f && w != 1.0f) {
|
||||
x /= w; y /= w; z /= w;
|
||||
}
|
||||
obj->m_vertices.emplace_back(x, y, z);
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::VN: // normal
|
||||
{
|
||||
float x = p.TakeFloat();
|
||||
float y = p.TakeFloat();
|
||||
float z = p.TakeFloat();
|
||||
obj->m_normals.emplace_back(x, y, z);
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::VT: // texcoord
|
||||
{
|
||||
float u = p.TakeFloat();
|
||||
float v = p.TakeFloat();
|
||||
obj->m_texCoords.emplace_back(u, 1.0f - v);
|
||||
break;
|
||||
}
|
||||
|
||||
case ObjElement::F: // face
|
||||
{
|
||||
auto& mesh = obj->GetLastMesh();
|
||||
int raw_vi, raw_ti, raw_ni;
|
||||
|
||||
while (p.TakeFaceIndices(raw_vi, raw_ti, raw_ni)) {
|
||||
// Convert raw OBJ indices to 0-based / -1 sentinel
|
||||
int vi = Object::NormalizeIndex(raw_vi, (int)obj->m_vertices.size());
|
||||
int ti = Object::NormalizeIndex(raw_ti, (int)obj->m_texCoords.size());
|
||||
int ni = Object::NormalizeIndex(raw_ni, (int)obj->m_normals.size());
|
||||
|
||||
if (vi < 0) {
|
||||
// malformed token (no vertex) — skip
|
||||
continue;
|
||||
}
|
||||
|
||||
glm::vec3 vert = obj->m_vertices[vi];
|
||||
glm::vec3 norm(0.0f);
|
||||
glm::vec2 texCoord(0.0f);
|
||||
|
||||
if (ni >= 0) norm = obj->m_normals[ni];
|
||||
if (ti >= 0) texCoord = obj->m_texCoords[ti];
|
||||
|
||||
mesh.m_vertexBuffer.emplace_back(vert, norm, texCoord);
|
||||
mesh.m_indexBuffer.push_back(mesh.m_vertexBuffer.size() - 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// ignore unknown tokens
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Object name: " << obj->m_name << std::endl;
|
||||
std::cout << "Vertices count: " << obj->m_vertices.size() << std::endl;
|
||||
std::cout << "Normals count: " << obj->m_normals.size() << std::endl;
|
||||
std::cout << "TexCoords count: " << obj->m_texCoords.size() << std::endl;
|
||||
std::cout << "Meshes count: " << obj->m_meshes.size() << std::endl;
|
||||
std::cout << "Materials count: " << obj->m_materials.size() << std::endl;
|
||||
|
||||
file.close();
|
||||
|
||||
for (auto &mesh : obj->m_meshes) {
|
||||
mesh.Upload();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
// void Object::Render(Shader& shader)
|
||||
// {
|
||||
// for (auto &mesh : m_meshes) {
|
||||
// auto material = GetMaterial(mesh.materialName);
|
||||
|
||||
// shader.setFloat("ambientStrength", 0.2f);
|
||||
// shader.setFloat("shininess", material->GetSpecularWeight());
|
||||
// shader.setFloat("opacity", material->GetOpacity());
|
||||
// shader.setBool("useSpecular", material->GetIllumination() >= 2);
|
||||
// shader.setFloat("specularStrength", 1.0f);
|
||||
// shader.setVec3("ambientColor", material->GetAmbientColor());
|
||||
// shader.setVec3("diffuseColor", material->GetDiffuseColor());
|
||||
// shader.setVec3("specularColor", material->GetSpecularColor());
|
||||
|
||||
// if (material->HasDiffuseTexture()) {
|
||||
// shader.setBool("useTexture", true);
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||
// shader.setInt("diffuseTex", 0);
|
||||
// } else {
|
||||
// shader.setBool("useTexture", false);
|
||||
// }
|
||||
|
||||
// mesh.Render();
|
||||
// }
|
||||
// }
|
||||
|
||||
void Object::Render(Shader& shader)
|
||||
{
|
||||
for (auto &mesh : m_meshes)
|
||||
{
|
||||
auto material = GetMaterial(mesh.materialName);
|
||||
|
||||
// --- Basic material properties ---
|
||||
shader.setFloat("opacity", material->GetOpacity());
|
||||
|
||||
// Albedo (base color)
|
||||
shader.setVec3("albedo", material->GetDiffuseColor());
|
||||
|
||||
// Metallic and roughness (defaults)
|
||||
shader.setFloat("metallic", 0.8f);
|
||||
shader.setFloat("roughness", 0.5f);
|
||||
shader.setFloat("ao", 1.0f); // default ambient occlusion if none
|
||||
|
||||
// --- Optional textures ---
|
||||
int texUnit = 0;
|
||||
|
||||
// Albedo texture
|
||||
if (material->HasDiffuseTexture()) {
|
||||
shader.setBool("useAlbedoMap", true);
|
||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||
shader.setInt("albedoTex", texUnit++);
|
||||
} else {
|
||||
shader.setBool("useAlbedoMap", false);
|
||||
}
|
||||
|
||||
// Metallic texture
|
||||
// if (material->HasMetallicTexture()) {
|
||||
if (false) {
|
||||
shader.setBool("useMetallicMap", true);
|
||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||
// glBindTexture(GL_TEXTURE_2D, material->GetMetallicTexture()->GetID());
|
||||
shader.setInt("metallicTex", texUnit++);
|
||||
} else {
|
||||
shader.setBool("useMetallicMap", false);
|
||||
}
|
||||
|
||||
// Roughness texture
|
||||
// if (material->HasRoughnessTexture()) {
|
||||
if (false) {
|
||||
shader.setBool("useRoughnessMap", true);
|
||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||
// glBindTexture(GL_TEXTURE_2D, material->GetRoughnessTexture()->GetID());
|
||||
shader.setInt("roughnessTex", texUnit++);
|
||||
} else {
|
||||
shader.setBool("useRoughnessMap", false);
|
||||
}
|
||||
|
||||
// AO texture
|
||||
// if (material->HasAoTexture()) {
|
||||
if (false) {
|
||||
shader.setBool("useAoMap", true);
|
||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||
// glBindTexture(GL_TEXTURE_2D, material->GetAoTexture()->GetID());
|
||||
shader.setInt("aoTex", texUnit++);
|
||||
} else {
|
||||
shader.setBool("useAoMap", false);
|
||||
}
|
||||
|
||||
// --- Render mesh ---
|
||||
mesh.Render();
|
||||
}
|
||||
}
|
||||
|
||||
|
6
src/shaders/depth.fs
Normal file
6
src/shaders/depth.fs
Normal file
@ -0,0 +1,6 @@
|
||||
#version 410 core
|
||||
|
||||
void main()
|
||||
{
|
||||
// gl_FragDepth = gl_FragCoord.z;
|
||||
}
|
13
src/shaders/depth.vs
Normal file
13
src/shaders/depth.vs
Normal file
@ -0,0 +1,13 @@
|
||||
#version 410 core
|
||||
|
||||
// Input vertex attributes
|
||||
layout (location = 0) in vec3 position; // Vertex position in local space (model space)
|
||||
|
||||
// Uniforms for transformation matrices
|
||||
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
||||
uniform mat4 u_lightSpace;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = u_lightSpace * u_model * vec4(position, 1.0);
|
||||
}
|
203
src/shaders/pbr.fs
Normal file
203
src/shaders/pbr.fs
Normal file
@ -0,0 +1,203 @@
|
||||
#version 410 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 vertexPos; // must be world-space position
|
||||
in vec3 vertexNormal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
|
||||
// Lights
|
||||
struct Light {
|
||||
int type; // 0 = directional (shadowed), other = non-directional (no shadow)
|
||||
vec3 position; // for directional: encode light direction (see note)
|
||||
vec3 color;
|
||||
float intensity;
|
||||
mat4 lightSpace;
|
||||
sampler2D shadowMap;
|
||||
};
|
||||
#define MAX_LIGHTS 10
|
||||
uniform int lightsCount;
|
||||
uniform Light lights[MAX_LIGHTS];
|
||||
|
||||
uniform bool isLight;
|
||||
uniform vec3 currentLightColor;
|
||||
|
||||
// Material parameters
|
||||
uniform vec3 albedo;
|
||||
uniform float metallic;
|
||||
uniform float roughness;
|
||||
uniform float ao;
|
||||
|
||||
uniform sampler2D albedoTex;
|
||||
uniform sampler2D metallicTex;
|
||||
uniform sampler2D roughnessTex;
|
||||
uniform sampler2D aoTex;
|
||||
uniform bool useAlbedoMap;
|
||||
uniform bool useMetallicMap;
|
||||
uniform bool useRoughnessMap;
|
||||
uniform bool useAoMap;
|
||||
|
||||
uniform float opacity;
|
||||
|
||||
#define PI 3.14159265359
|
||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Improved ShadowCalculation: returns [0,1] shadow factor (1 = fully in shadow)
|
||||
float ShadowCalculation(sampler2D shadowMap, mat4 lightSpace, vec3 N, vec3 L)
|
||||
{
|
||||
// Transform fragment position to light's clip / NDC space
|
||||
vec4 fragPosLightSpace = lightSpace * vec4(vertexPos, 1.0);
|
||||
|
||||
// perspective divide
|
||||
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
|
||||
// to [0,1]
|
||||
projCoords = projCoords * 0.5 + 0.5;
|
||||
|
||||
// if outside the light's orthographic frustum (xy outside or z > 1), consider unshadowed
|
||||
if (projCoords.z > 1.0) {
|
||||
return 0.0;
|
||||
}
|
||||
if (projCoords.x < 0.0 || projCoords.x > 1.0 || projCoords.y < 0.0 || projCoords.y > 1.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float currentDepth = projCoords.z;
|
||||
// basic bias (slope-dependent)
|
||||
float bias = max(0.001 * (1.0 - dot(N, L)), 0.0005);
|
||||
|
||||
// PCF (3x3)
|
||||
float shadow = 0.0;
|
||||
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
|
||||
for (int x = -1; x <= 1; ++x) {
|
||||
for (int y = -1; y <= 1; ++y) {
|
||||
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
|
||||
shadow += (currentDepth - bias) > pcfDepth ? 1.0 : 0.0;
|
||||
}
|
||||
}
|
||||
shadow /= 9.0;
|
||||
|
||||
// clamp to [0,1]
|
||||
shadow = clamp(shadow, 0.0, 1.0);
|
||||
return shadow;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// PBR helpers (unchanged)
|
||||
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
||||
{
|
||||
float a = roughness * roughness;
|
||||
float a2 = a * a;
|
||||
float NdotH = max(dot(N, H), 0.0);
|
||||
float NdotH2 = NdotH * NdotH;
|
||||
|
||||
float num = a2;
|
||||
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||
denom = PI * denom * denom;
|
||||
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
float GeometrySchlickGGX(float NdotV, float roughness)
|
||||
{
|
||||
float r = roughness + 1.0;
|
||||
float k = (r * r) / 8.0;
|
||||
|
||||
float num = NdotV;
|
||||
float denom = NdotV * (1.0 - k) + k;
|
||||
return num / denom;
|
||||
}
|
||||
|
||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
||||
{
|
||||
float ggx1 = GeometrySchlickGGX(max(dot(N,L),0.0), roughness);
|
||||
float ggx2 = GeometrySchlickGGX(max(dot(N,V),0.0), roughness);
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
||||
{
|
||||
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Main
|
||||
void main()
|
||||
{
|
||||
if (isLight) {
|
||||
vec3 emissive = currentLightColor * 10.0;
|
||||
FragColor = vec4(emissive, 1.0);
|
||||
return;
|
||||
}
|
||||
|
||||
vec3 N = normalize(vertexNormal);
|
||||
vec3 V = normalize(viewPos - vertexPos);
|
||||
|
||||
vec3 baseColor = useAlbedoMap ? texture(albedoTex, TexCoords).rgb : albedo;
|
||||
float metal = useMetallicMap ? texture(metallicTex, TexCoords).r : metallic;
|
||||
float rough = useRoughnessMap ? texture(roughnessTex, TexCoords).r : roughness;
|
||||
float aoValue = useAoMap ? texture(aoTex, TexCoords).r : ao;
|
||||
|
||||
vec3 F0 = mix(vec3(0.04), baseColor, metal);
|
||||
|
||||
vec3 Lo = vec3(0.0);
|
||||
|
||||
// Loop over all lights
|
||||
for (int i = 0; i < lightsCount; i++)
|
||||
{
|
||||
// compute light vector L depending on type
|
||||
vec3 L;
|
||||
if (lights[i].type == 0) {
|
||||
// directional light: convention here is that lights[i].position stores the direction
|
||||
// *towards* the light (for example, for sun direction you may upload -sunDir).
|
||||
// Adjust according to your CPU-side convention.
|
||||
L = normalize(lights[i].position); // expect this to be a direction
|
||||
} else {
|
||||
// point / spot style light: position is world-space point
|
||||
L = normalize(lights[i].position - vertexPos);
|
||||
}
|
||||
|
||||
vec3 H = normalize(V + L);
|
||||
|
||||
float NDF = DistributionGGX(N, H, rough);
|
||||
float G = GeometrySmith(N, V, L, rough);
|
||||
vec3 F = fresnelSchlick(max(dot(H,V),0.0), F0);
|
||||
|
||||
// compute shadow only for directional (type==0) lights that have shadow maps
|
||||
float shadow_i = 0.0;
|
||||
if (lights[i].type == 0) {
|
||||
shadow_i = ShadowCalculation(lights[i].shadowMap, lights[i].lightSpace, N, L);
|
||||
}
|
||||
|
||||
vec3 numerator = NDF * G * F;
|
||||
float denominator = 4.0 * max(dot(N,V),0.0) * max(dot(N,L),0.0) + 0.001;
|
||||
vec3 specular = numerator / denominator;
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = vec3(1.0) - kS;
|
||||
kD *= 1.0 - metal;
|
||||
|
||||
float NdotL = max(dot(N,L), 0.0);
|
||||
|
||||
vec3 radiance = lights[i].color * lights[i].intensity;
|
||||
|
||||
// Apply shadow_i only to this light's contribution:
|
||||
// when shadow_i == 1.0 -> this light contributes 0
|
||||
// when shadow_i == 0.0 -> full contribution
|
||||
vec3 contrib = (kD * baseColor / PI + specular) * radiance * NdotL * (1.0 - shadow_i);
|
||||
|
||||
Lo += contrib;
|
||||
}
|
||||
|
||||
// Ambient (unshadowed by design)
|
||||
vec3 ambient = vec3(0.03) * baseColor * aoValue;
|
||||
|
||||
vec3 color = ambient + Lo;
|
||||
|
||||
// HDR tonemapping + gamma
|
||||
color = color / (color + vec3(1.0));
|
||||
color = pow(color, vec3(1.0/2.2));
|
||||
|
||||
FragColor = vec4(color, opacity);
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
#version 410 core
|
||||
|
||||
// Output color of the fragment (pixel)
|
||||
out vec4 FragColor; // RGBA color for the fragment, where A is the alpha (opacity)
|
||||
|
||||
in vec3 vertexPos;
|
||||
in vec3 vertexNormal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
// From Object Renderer
|
||||
|
||||
uniform vec3 ambientColor;
|
||||
uniform vec3 diffuseColor;
|
||||
uniform vec3 specularColor;
|
||||
|
||||
uniform float ambientStrength;
|
||||
uniform float specularStrength;
|
||||
|
||||
uniform sampler2D diffuseTex;
|
||||
uniform bool useTexture;
|
||||
|
||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
||||
|
||||
void main()
|
||||
{
|
||||
// Lighting vectors
|
||||
vec3 lightDir = normalize(lightPos - vertexPos);
|
||||
vec3 norm = normalize(vertexNormal);
|
||||
vec3 viewDir = normalize(viewPos - vertexPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
// Phong components
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * specularColor;
|
||||
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * diffuseColor;
|
||||
|
||||
vec3 ambient = ambientStrength * ambientColor;
|
||||
|
||||
vec3 texColor = (useTexture)
|
||||
? texture(diffuseTex, TexCoords).rgb
|
||||
: diffuseColor;
|
||||
|
||||
vec3 result = (ambient + diffuse + specular) * texColor;
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
@ -9,6 +9,7 @@ layout (location = 2) in vec2 texCoord; // Vertex texture uv
|
||||
out vec3 vertexPos;
|
||||
out vec3 vertexNormal;
|
||||
out vec2 TexCoords;
|
||||
out vec4 fragPosLightSpace;
|
||||
|
||||
// Uniforms for transformation matrices
|
||||
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
||||
@ -25,5 +26,7 @@ void main()
|
||||
|
||||
TexCoords = texCoord;
|
||||
|
||||
// fragPosLightSpace = u_lightSpace * vec4(vertexPos, 1.0);
|
||||
|
||||
gl_Position = u_projection * u_view * vec4(vertexPos, 1.0);
|
||||
}
|
151
src/vertex.cpp
151
src/vertex.cpp
@ -1,151 +0,0 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "vertex.h"
|
||||
|
||||
#define BLOCK_SIZE 0.5f
|
||||
|
||||
Point::Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color)
|
||||
{
|
||||
m_position = position;
|
||||
m_normal = normal;
|
||||
m_color = color;
|
||||
}
|
||||
|
||||
Vertices::Vertices()
|
||||
{
|
||||
m_items = std::vector<Point>();
|
||||
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glGenBuffers(1, &m_vbo);
|
||||
glGenBuffers(1, &m_ebo);
|
||||
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
// VBO (vertex buffer)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// EBO (index buffer)
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// Call this after you finish adding vertices (or call it each time after PushBlock)
|
||||
void Vertices::Upload()
|
||||
{
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_items.size() * sizeof(Point), m_items.data(), GL_DYNAMIC_DRAW);
|
||||
|
||||
// Upload indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), m_indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// void Vertices::PushBlock(const Block& block)
|
||||
// {
|
||||
// // 1 face
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 2 face
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 3 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
|
||||
// // 4 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
|
||||
// // 5 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 6 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 7 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 8 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 9 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position(), block.Color());
|
||||
|
||||
// // 10 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color());
|
||||
|
||||
// // 11 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, 0.f), block.Color());
|
||||
|
||||
// // 12 face
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, -BLOCK_SIZE), block.Color());
|
||||
// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color());
|
||||
// }
|
||||
|
||||
void Vertices::PushVertex(const Point& point)
|
||||
{
|
||||
m_items.push_back(point);
|
||||
}
|
||||
|
||||
void Vertices::PushIndex(unsigned int index)
|
||||
{
|
||||
m_indices.push_back(index);
|
||||
}
|
||||
|
||||
void Vertices::Bind()
|
||||
{
|
||||
glBindVertexArray(m_vao);
|
||||
}
|
||||
|
||||
void Vertices::Unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Vertices::Draw()
|
||||
{
|
||||
Bind();
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indices.size()), GL_UNSIGNED_INT, 0);
|
||||
Unbind();
|
||||
}
|
140
src/window/window.cpp
Normal file
140
src/window/window.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "window/window.h"
|
||||
#include "window/events/window.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "renderer/debug.h"
|
||||
|
||||
std::shared_ptr<Window> Window::s_instance = nullptr;
|
||||
|
||||
Window::Window(const char* title, int width, int height) {
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, ENGINE_GL_MAJOR_VERSION);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, ENGINE_GL_MINOR_VERSION);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, ENGINE_GL_MULTISAMPLE_BUFFERS);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, ENGINE_GL_MULTISAMPLE_SAMPLES);
|
||||
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
|
||||
m_handle = SDL_CreateWindow(title, m_width, m_height, SDL_WINDOW_OPENGL|SDL_WINDOW_ALWAYS_ON_TOP|SDL_WINDOW_RESIZABLE);
|
||||
|
||||
if (!m_handle) {
|
||||
std::cerr << "Failed to create window" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
||||
|
||||
m_context = SDL_GL_CreateContext(m_handle);
|
||||
|
||||
if (!SDL_GL_MakeCurrent(m_handle, m_context)) {
|
||||
std::cerr << "SDL_GL_MakeCurrent failed: " << SDL_GetError() << "\n";
|
||||
SDL_DestroyWindow(m_handle);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (GLEW_OK != glewInit()) {
|
||||
std::cerr << "Could not initialize GLEW!" << std::endl;
|
||||
SDL_GL_DestroyContext(m_context);
|
||||
SDL_DestroyWindow(m_handle);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
std::cout << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl;
|
||||
std::cout << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl;
|
||||
std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl;
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glDebugMessageCallback(MessageCallback, nullptr);
|
||||
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
}
|
||||
|
||||
Window::Window() : Window("OpenGL Test", DEFAULT_WIDTH, DEFAULT_HEIGHT) {}
|
||||
|
||||
std::shared_ptr<Window> Window::GetInstance() {
|
||||
if (!s_instance) {
|
||||
s_instance = std::shared_ptr<Window>(new Window(), WindowDeleter{});
|
||||
}
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
Window::Window(Window&& window) noexcept
|
||||
: m_handle(window.m_handle), m_context(window.m_context), m_width(window.m_width), m_height(window.m_height)
|
||||
{
|
||||
window.m_handle = nullptr;
|
||||
window.m_context = (SDL_GLContext)nullptr;
|
||||
window.m_width = 0;
|
||||
window.m_height = 0;
|
||||
}
|
||||
|
||||
Window& Window::operator=(Window&& window) noexcept
|
||||
{
|
||||
if (this == &window) return *this;
|
||||
|
||||
// Destroy();
|
||||
|
||||
this->m_handle = window.m_handle;
|
||||
this->m_context = window.m_context;
|
||||
this->m_width = window.m_width;
|
||||
this->m_height = window.m_height;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Window::ProcessEvents() {
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
Dispatch(WindowCloseRequested());
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
Dispatch(WindowCloseRequested());
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
int width, height;
|
||||
if (SDL_GetWindowSizeInPixels(m_handle, &width, &height)) {
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
glViewport(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height);
|
||||
Dispatch(WindowResized{ m_width, m_height });
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void Window::SwapBuffers() const {
|
||||
SDL_GL_SwapWindow(m_handle);
|
||||
}
|
||||
|
||||
Window::~Window() {
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void Window::Destroy() const {
|
||||
if (m_context)
|
||||
SDL_GL_DestroyContext(m_context);
|
||||
if (m_handle)
|
||||
SDL_DestroyWindow(m_handle);
|
||||
}
|
||||
|
Reference in New Issue
Block a user