Skip to content

Commit 0d7ab02

Browse files
v1.2 stage2
+ Add cubemap and skybox example(including split texture and total texture) + Add special models, i.e. cube + Add more ctor for models. + fix bugs + Adjust mouse dragging(still have some bugs in singular position) + Add or change some tests.
1 parent bfccca1 commit 0d7ab02

36 files changed

+597
-62
lines changed

Resources/Configs/SkyboxConfig.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# skybox credit: https://assetstore.unity.com/packages/2d/textures-materials/sky/fantasy-skybox-free-18353
2+
option = split
3+
[paths]
4+
sucrose = ../../../../../Resources/Models/Sucrose/Sucrose.pmx
5+
sucrose_vertex_shader = ../../../../../Shaders/SkyboxReflect.vert
6+
sucrose_fragment_shader = ../../../../../Shaders/SkyboxReflect.frag
7+
split_skybox_dir = ../../../../../Resources/Models/Skybox/split/skybox.jpg
8+
skybox = ../../../../../Resources/Models/Skybox/FS000_Day_01.png
9+
skybox_vertex_shader= ../../../../../Shaders/Skybox.vert
10+
skybox_fragment_shader = ../../../../../Shaders/Skybox.frag
11+
12+
# for debug
13+
cube = ../../../../../Resources/Models/Cube/CubeWithNormal.obj
14+
15+
[window]
16+
name = Skybox Test
17+
width = 800
18+
height = 600
603 KB
Loading
454 KB
Loading
588 KB
Loading
274 KB
Loading
723 KB
Loading
525 KB
Loading
338 KB
Loading
462 KB
Loading

Resources/Models/Skybox/test.png

583 Bytes
Loading

Shaders/Skybox.frag

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
4+
in vec3 TexCoords;
5+
6+
uniform samplerCube skybox;
7+
8+
void main()
9+
{
10+
FragColor = texture(skybox, TexCoords);
11+
}

Shaders/Skybox.vert

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
4+
out vec3 TexCoords;
5+
6+
uniform mat4 model;
7+
uniform mat4 projection;
8+
uniform mat4 view;
9+
10+
void main()
11+
{
12+
TexCoords = (aPos - vec3(0.5, 0.5, 0.5)) * 2; // aPos is in [0,1]
13+
gl_Position = projection * view * model * vec4(aPos, 1.0);
14+
}

Shaders/SkyboxReflect.frag

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#version 330 core
2+
3+
in vec3 position;
4+
in vec3 normal;
5+
in vec2 texCoords;
6+
7+
uniform sampler2D diffuseTexture1;
8+
uniform samplerCube skybox;
9+
uniform vec3 cameraPos;
10+
11+
out vec4 FragColor;
12+
13+
void main()
14+
{
15+
vec3 incident = normalize(position - cameraPos);
16+
vec3 reflection = reflect(incident, normalize(normal));
17+
FragColor = vec4(texture(skybox, reflection).rgb, 1.0) * 0.5 +
18+
vec4(texture(diffuseTexture1, texCoords).rgb, 1.0) * 0.5;
19+
return;
20+
}

Shaders/SkyboxReflect.vert

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 330 core
2+
3+
layout(location = 0) in vec3 aPos;
4+
layout(location = 1) in vec3 aNormal;
5+
layout(location = 2) in vec2 aTexture;
6+
7+
uniform mat4 model;
8+
uniform mat4 view;
9+
uniform mat4 projection;
10+
11+
out vec3 position;
12+
out vec3 normal;
13+
out vec2 texCoords;
14+
15+
void main()
16+
{
17+
normal = mat3(transpose(inverse(model))) * aNormal;
18+
position = vec3(model * vec4(aPos, 1.0));
19+
texCoords = aTexture;
20+
gl_Position = projection * view * model * vec4(aPos, 1.0);
21+
return;
22+
}

examples/BasicSettings.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ void SetMVP(float width, float height, float near, float far,
2525
return;
2626
}
2727

28+
void SetProjection(float width, float height, float near, float far,
29+
Core::Camera& camera, Core::Shader& shader)
30+
{
31+
auto projectionMat = glm::perspective(glm::radians(camera.fov),
32+
width / height, near, far);
33+
shader.SetMat4("projection", projectionMat);
34+
return;
35+
}
36+
2837
inline void SetBasicKeyBindings(Core::MainWindow& mainWindow, Core::Camera& camera)
2938
{
3039
mainWindow.BindKeyPressing<GLFW_KEY_W>([&camera, &mainWindow]() {
@@ -85,6 +94,7 @@ inline void SetBasicButtonBindings(Core::MainWindow& mainWindow, Core::Camera& c
8594
});
8695

8796
static bool lastNotDrag = false;
97+
static float initCameraY = camera.GetPosition().y;
8898
mainWindow.BindMouseButtonPressing<GLFW_MOUSE_BUTTON_RIGHT>([&mainWindow, &camera]() {
8999
const auto [xPos, yPos] = mainWindow.GetCursorPos();
90100
static float lastxPos = xPos, lastyPos = yPos;
@@ -96,10 +106,14 @@ inline void SetBasicButtonBindings(Core::MainWindow& mainWindow, Core::Camera& c
96106
return;
97107
}
98108

109+
glm::vec3 yAxis = glm::cross(camera.GetGaze(), {0, 1, 0});
110+
if (glm::all(glm::epsilonEqual(yAxis, glm::zero<glm::vec3>(), glm::vec3{ 1e-4f })))
111+
yAxis = { 1,0,0 };
112+
99113
camera.RotateAroundCenter(camera.mouseSensitivity * xOffset, { 0, 1, 0 },
100-
{ 0, 10, 0 });
101-
camera.RotateAroundCenter(camera.mouseSensitivity * yOffset, { 1, 0, 0 },
102-
{ 0, 10, 0 });
114+
{ 0, initCameraY, 0 });
115+
camera.RotateAroundCenter(camera.mouseSensitivity * yOffset, yAxis,
116+
{ 0, initCameraY, 0 });
103117
});
104118
mainWindow.BindMouseButtonReleasing<GLFW_MOUSE_BUTTON_RIGHT>([]() { lastNotDrag = true; });
105119
}

examples/ShadowMap.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#include "BasicSettings.h"
2-
31
#include "FrameworkCore/Core_All.h"
42
#include "Utility/IO/IniFile.h"
5-
#include "Utility/IO/IOExtension.h"
63

4+
using namespace OpenGLFramework;
75
int main()
86
{
97
std::filesystem::path configPath = "../../../../../Resources/Configs/ShadowMapConfig.ini";

examples/SkyBox.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "BasicSettings.h"
2+
3+
#include "FrameworkCore/Core_All.h"
4+
#include "Utility/IO/IniFile.h"
5+
6+
int main()
7+
{
8+
std::filesystem::path configPath = "../../../../../Resources/Configs/SkyboxConfig.ini";
9+
IOExtension::IniFile file{ configPath };
10+
11+
auto& pathsSection = file.rootSection.GetSubsection("paths")->get();
12+
auto& windowSection = file.rootSection.GetSubsection("window")->get();
13+
std::string windowName = windowSection("name");
14+
unsigned int width = std::stoi(windowSection("width")),
15+
height = std::stoi(windowSection("height"));
16+
17+
[[maybe_unused]] auto& contextManager = Core::ContextManager::GetInstance();
18+
Core::MainWindow mainWindow{ width, height, windowName.c_str() };
19+
20+
auto skyBox = file.rootSection("option") == "split" ?
21+
Core::SkyBoxTexture{ pathsSection("split_skybox_dir") } :
22+
Core::SkyBoxTexture{ pathsSection("skybox"),
23+
Core::SkyBoxTexture::TextureSegmentType::HorizontalLeft
24+
};
25+
Core::Shader skyBoxShader{
26+
pathsSection("skybox_vertex_shader"),
27+
pathsSection("skybox_fragment_shader")
28+
};
29+
Core::BasicTriRenderModel cube = Core::Cube::GetBasicTriRenderModel();
30+
cube.transform.scale = { 10, 10, 10 };
31+
cube.transform.Translate({ -0.5,-0.5,-0.5 });
32+
33+
Core::BasicTriRenderModel sucroseModel{ pathsSection("sucrose") };
34+
Core::Shader sucroseShader{
35+
pathsSection("sucrose_vertex_shader"),
36+
pathsSection("sucrose_fragment_shader")
37+
};
38+
sucroseModel.transform.scale = { 0.1, 0.1, 0.1 };
39+
40+
float near = 0.1f, far = 100.0f;
41+
42+
Core::Camera frontCamera{ { 0, 1, 3.5}, {0, 1, 0}, {0, 0, -1} };
43+
auto initialViewMatTranslation = frontCamera.GetViewMatrix()[3];
44+
SetBasicKeyBindings(mainWindow, frontCamera);
45+
SetBasicButtonBindings(mainWindow, frontCamera);
46+
47+
auto BindSkybox = [&skyBox](int textureBeginID, Core::Shader& shader)
48+
{
49+
glActiveTexture(GL_TEXTURE0 + textureBeginID);
50+
shader.SetInt("skybox", textureBeginID);
51+
glBindTexture(GL_TEXTURE_CUBE_MAP, skyBox.GetID());
52+
};
53+
54+
mainWindow.Register([&]() {
55+
sucroseShader.Activate();
56+
SetMVP(static_cast<float>(width), static_cast<float>(height),
57+
static_cast<float>(near), static_cast<float>(far),
58+
sucroseModel, frontCamera, sucroseShader);
59+
sucroseShader.SetVec3("cameraPos", frontCamera.GetPosition());
60+
sucroseModel.Draw(sucroseShader, BindSkybox, nullptr);
61+
});
62+
63+
mainWindow.Register([&]() {
64+
glDepthFunc(GL_LEQUAL);
65+
skyBoxShader.Activate();
66+
skyBoxShader.SetMat4("model", cube.transform.GetModelMatrix());
67+
auto viewMat = glm::mat4{ glm::mat3{ frontCamera.GetViewMatrix() } };
68+
viewMat[3] = initialViewMatTranslation;
69+
skyBoxShader.SetMat4("view", viewMat);
70+
SetProjection(width, height, near, far, frontCamera, skyBoxShader);
71+
cube.Draw(skyBoxShader, BindSkybox, nullptr);
72+
glDepthFunc(GL_LESS);
73+
});
74+
75+
mainWindow.MainLoop({ 0.0, 0.0, 0.0, 0.0 });
76+
return 0;
77+
}

examples/SoftShadow.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ Core::MainWindow LoadMainWindow(IOExtension::IniFile<std::unordered_map>& file)
225225
return { width, height, windowName.c_str() };
226226
}
227227

228+
void Increment(auto& it) { it++; }
229+
228230
int main()
229231
{
230232
std::filesystem::path configPath = "../../../../../Resources/Configs/SoftShadowConfig.ini";
@@ -249,17 +251,17 @@ int main()
249251
SetBasicButtonBindings(mainWindow, normalCamera);
250252

251253
auto lightSetter = SetLightPosition(lightSpaceCamera);
252-
mainWindow.Register(std::bind<void(IterType&, int)>(
253-
std::advance<IterType, int>, lightSetter.begin(), 1)
254+
mainWindow.Register(std::bind(
255+
Increment<IterType>, lightSetter.begin())
254256
);
255257
int option = 1;
256258
auto shadowOptionSetter = SetShadowOption(option);
257-
mainWindow.Register(std::bind<void(IterType&, int)>(
258-
std::advance<IterType, int>, shadowOptionSetter.begin(), 1)
259+
mainWindow.Register(std::bind(
260+
Increment<IterType>, shadowOptionSetter.begin())
259261
);
260262
auto basicInfoShow = ShowBasicInfo(mainWindow);
261-
mainWindow.Register(std::bind<void(IterType&, int)>(
262-
std::advance<IterType, int>, basicInfoShow.begin(), 1)
263+
mainWindow.Register(std::bind(
264+
Increment<IterType>, basicInfoShow.begin())
263265
);
264266

265267
mainWindow.Register(std::bind(RenderShadowMap,

examples/xmake.lua

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1-
target("BlinnPhong")
2-
set_kind("binary")
3-
add_includedirs("../src/")
4-
add_headerfiles("./*.h")
5-
add_deps("OpenGLFramework")
6-
add_files("BlinnPhong.cpp")
7-
8-
target("ShadowMap")
9-
set_kind("binary")
10-
add_includedirs("../src/")
11-
add_headerfiles("./*.h")
12-
add_deps("OpenGLFramework")
13-
add_files("ShadowMap.cpp")
1+
for _, file in ipairs(os.files("./*.cpp")) do
142

15-
target("SoftShadow")
3+
target(path.basename(file))
164
set_kind("binary")
175
add_includedirs("../src/")
18-
add_headerfiles("./*.h")
6+
add_headerfiles("*.h")
197
add_deps("OpenGLFramework")
20-
add_files("SoftShadow.cpp")
8+
add_files(file)
219

22-
target("BasicFilters")
23-
set_kind("binary")
24-
add_includedirs("../src/")
25-
add_headerfiles("./*.h")
26-
add_deps("OpenGLFramework")
27-
add_files("BasicFilters.cpp")
10+
end
2811

2912
includes("complex-examples")

src/FrameworkCore/Camera.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Camera
8181
}
8282

8383
glm::vec3 GetPosition() const { return position_; };
84+
glm::vec3 GetGaze() const { return gaze_; }
85+
glm::vec3 GetUp() const { return up_; }
8486
private:
8587
// Note that this sequence is deliberate, so that up will be initialized after front.
8688
glm::vec3 position_;

src/FrameworkCore/Core_All.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
#include "FrameworkCore/Shader.h"
55
#include "FrameworkCore/Camera.h"
66
#include "FrameworkCore/Framebuffer.h"
7+
#include "FrameworkCore/SkyboxTexture.h"
8+
#include "FrameworkCore/SpecialModels/SpecialModel.h"

src/FrameworkCore/Mesh.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ BasicTriMesh::BasicTriMesh(const aiMesh* mesh): vertices(mesh->mNumVertices),
2828
return;
2929
}
3030

31+
BasicTriMesh::BasicTriMesh(std::vector<glm::vec3> init_vertices,
32+
std::vector<glm::ivec3> init_triangles) :
33+
vertices{std::move(init_vertices)}, triangles{std::move(init_triangles)}
34+
{};
35+
3136
std::vector<glm::vec3> BasicTriMesh::GetRealTriNormals()
3237
{
3338
std::vector<glm::vec3> normals(triangles.size());
@@ -200,6 +205,16 @@ BasicTriRenderMesh::BasicTriRenderMesh(const aiMesh* mesh,
200205
return;
201206
};
202207

208+
BasicTriRenderMesh::BasicTriRenderMesh(BasicTriMesh mesh,
209+
const std::vector<glm::vec3>& init_normals) :
210+
BasicTriMesh{ std::move(mesh) }, verticesAttributes(init_normals.size())
211+
{
212+
for (int i = 0; i < verticesAttributes.size(); i++)
213+
verticesAttributes[i].normalCoord = init_normals[i];
214+
SetupRenderResource_();
215+
return;
216+
};
217+
203218
void BasicTriRenderMesh::ReleaseRenderResources_()
204219
{
205220
glDeleteBuffers(1, &VBO);

src/FrameworkCore/Mesh.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class BasicTriMesh
3838
std::vector<glm::vec3> vertices;
3939
std::vector<glm::ivec3> triangles;
4040
BasicTriMesh(const aiMesh* mesh);
41+
BasicTriMesh(std::vector<glm::vec3> init_vertices,
42+
std::vector<glm::ivec3> init_triangles);
4143
std::vector<glm::vec3> GetRealTriNormals();
4244
std::vector<glm::vec3> GetRealVertexNormals();
4345
};
@@ -49,6 +51,8 @@ class BasicTriRenderMesh : public BasicTriMesh
4951
std::vector<std::reference_wrapper<Texture>> diffuseTextureRefs;
5052
std::vector<std::reference_wrapper<Texture>> specularTextureRefs;
5153

54+
BasicTriRenderMesh(BasicTriMesh mesh,
55+
const std::vector<glm::vec3>& init_normals);
5256
BasicTriRenderMesh(const aiMesh* mesh, const aiMaterial* material,
5357
TexturePool& texturePool, const std::filesystem::path& rootPath);
5458
BasicTriRenderMesh(const BasicTriRenderMesh&) = delete;

src/FrameworkCore/Model.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ BasicTriModel::BasicTriModel(const std::filesystem::path& modelPath)
1313
{
1414
Assimp::Importer importer;
1515
const aiScene* model = importer.ReadFile(modelPath.string(),
16-
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs
16+
aiProcess_Triangulate | aiProcess_FlipUVs
1717
| aiProcess_JoinIdenticalVertices);
1818
if (model == nullptr || (model->mFlags & AI_SCENE_FLAGS_INCOMPLETE) != 0 ||
1919
model->mRootNode == nullptr) [[unlikely]]
@@ -25,6 +25,10 @@ BasicTriModel::BasicTriModel(const std::filesystem::path& modelPath)
2525
return;
2626
}
2727

28+
BasicTriModel::BasicTriModel(std::vector<BasicTriMesh> init_meshes):
29+
meshes{std::move(init_meshes)}
30+
{};
31+
2832
void BasicTriModel::LoadResources_(const aiScene* model)
2933
{
3034
LoadResourcesDecorator_(model,
@@ -54,12 +58,17 @@ void BasicTriRenderModel::LoadResources_(const aiScene* model,
5458
stbi_set_flip_vertically_on_load(false);
5559
}
5660

61+
BasicTriRenderModel::BasicTriRenderModel(std::vector<BasicTriRenderMesh>
62+
init_meshes) : meshes{ std::move(init_meshes) }
63+
{ };
64+
5765
BasicTriRenderModel::BasicTriRenderModel(const std::filesystem::path& modelPath,
5866
bool textureNeedFlip)
5967
{
6068
Assimp::Importer importer;
6169
const aiScene* model = importer.ReadFile(modelPath.string(),
62-
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
70+
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs
71+
| aiProcess_JoinIdenticalVertices);
6372
if (model == nullptr || (model->mFlags & AI_SCENE_FLAGS_INCOMPLETE) != 0 ||
6473
model->mRootNode == nullptr) [[unlikely]]
6574
{

0 commit comments

Comments
 (0)