[CORE][CHG] SkyBox Test

This commit is contained in:
Linloir 2022-12-18 10:49:07 +08:00
parent 12f32f5c07
commit 957713cda7
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
17 changed files with 122 additions and 14 deletions

View File

@ -125,6 +125,7 @@
<ClCompile Include="scrolllistwidget.cpp" />
<ClCompile Include="shader.cpp" />
<ClCompile Include="sidebar.cpp" />
<ClCompile Include="skybox.cpp" />
<ClCompile Include="texture.cpp" />
<ClCompile Include="vao.cpp" />
<ClCompile Include="vbo.cpp" />
@ -153,6 +154,7 @@
<QtMoc Include="roundedcornerwidget.h" />
<QtMoc Include="scrolllistwidget.h" />
<QtMoc Include="modelthumbnailwidget.h" />
<ClInclude Include="skybox.h" />
<ClInclude Include="texture.h" />
<ClInclude Include="utils.h" />
<ClInclude Include="vbo.h" />
@ -172,6 +174,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
</ExcludedFromBuild>
</None>
<None Include="skyboxfragmentshader.glsl" />
<None Include="skyboxvertexshader.glsl" />
<None Include="terrainfragmentshader.glsl" />
<None Include="terrainvertexshader.glsl" />
<None Include="vertexshader.glsl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -168,6 +168,9 @@
<ClCompile Include="illuminer.cpp">
<Filter>Source Files\OpenGL Abstractions</Filter>
</ClCompile>
<ClCompile Include="skybox.cpp">
<Filter>Source Files\OpenGL Abstractions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="camera.h">
@ -212,6 +215,9 @@
<ClInclude Include="illuminer.h">
<Filter>Header Files\OpenGL Abstractions</Filter>
</ClInclude>
<ClInclude Include="skybox.h">
<Filter>Header Files\OpenGL Abstractions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<QtMoc Include="mainwindow.h">
@ -264,5 +270,17 @@
<None Include="vertexshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="skyboxfragmentshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="skyboxvertexshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="terrainfragmentshader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="terrainvertexshader.glsl">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
</Project>

View File

@ -2,6 +2,10 @@
<qresource prefix="/shaders">
<file>fragmentshader.glsl</file>
<file>vertexshader.glsl</file>
<file>skyboxvertexshader.glsl</file>
<file>terrainfragmentshader.glsl</file>
<file>terrainvertexshader.glsl</file>
<file>skyboxfragmentshader.glsl</file>
</qresource>
<qresource prefix="/fonts">
<file>font_awesome_6_regular_free.otf</file>

View File

@ -31,6 +31,8 @@ SceneViewer::SceneViewer(QWidget* parent)
// Copy the shaders to the temp folder
extractShaderResorce("vertexshader.glsl");
extractShaderResorce("fragmentshader.glsl");
extractShaderResorce("skyboxvertexshader.glsl");
extractShaderResorce("skyboxfragmentshader.glsl");
}
SceneViewer::~SceneViewer() {
@ -76,7 +78,19 @@ void SceneViewer::initializeGL() {
vertexShader.dispose();
fragmentShader.dispose();
_skyShader.ensureInitialized();
Logger::info("Sky Shader initialized");
VertexShader skyVertexShader("./temp/shaders/skyboxvertexshader.glsl");
FragmentShader skyFragmentShader("./temp/shaders/skyboxfragmentshader.glsl");
_skyShader.attachShader(skyVertexShader);
_skyShader.attachShader(skyFragmentShader);
skyVertexShader.dispose();
skyFragmentShader.dispose();
// Test Code Start
_sky = new SkyBox("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\SkyBoxes");
_dirLight = new DirLight();
Model* model = new Model("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\Models\\backpack\\backpack.obj");
@ -140,6 +154,12 @@ void SceneViewer::paintGL() {
}
_shaderProgram.unbind();
_skyShader.bind();
_skyShader.setUniform("view", glm::mat4(glm::mat3(view)));
_skyShader.setUniform("projection", projection);
_sky->render();
_skyShader.unbind();
}
void SceneViewer::mousePressEvent(QMouseEvent* event) {

View File

@ -28,7 +28,7 @@ private:
// Shader program for objects
ShaderProgram _shaderProgram = ShaderProgram::empty();
ShaderProgram _skyShader = ShaderProgram::empty();
skybox* sky;
SkyBox* _sky;
// Main camera
Camera _camera;
float _cameraMovementSpeed = 0.02f;

View File

@ -1,13 +1,12 @@
#include "utils.h"
#include "skybox.h"
#include "shader.h"
#include "camera.h"
#include <STBImage/stb_image.h>
#include <vector>
#include <string>
skybox::skybox(std::string path){
#include "skybox.h"
#include "shader.h"
#include "camera.h"
SkyBox::SkyBox(std::string path){
faces.clear();
faces.push_back(path + "/right.jpg");
faces.push_back(path + "/left.jpg");
@ -27,7 +26,7 @@ skybox::skybox(std::string path){
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
}
void skybox::render() {
void SkyBox::render() {
OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
// skybox cube
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(skyboxVAO);
@ -38,7 +37,7 @@ void skybox::render() {
OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LESS); // set depth function back to default
}
unsigned int skybox::loadCubemap(std::vector<std::string> faces)
unsigned int SkyBox::loadCubemap(std::vector<std::string> faces)
{
unsigned int textureID;
OPENGL_FUNCTIONS->glGenTextures(1, &textureID);

View File

@ -1,17 +1,17 @@
#pragma once
#include <vector>
#include <string>
#include "utils.h"
#include "shader.h"
#include "camera.h"
#include <vector>
#include <string>
class skybox {
class SkyBox {
public:
std::vector<std::string> faces;
skybox(std::string path);
SkyBox(std::string path);
unsigned int cubemapTexture, skyboxVAO, skyboxVBO;
void render();
private:

View File

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}

View File

@ -0,0 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = aPos;
vec4 pos = projection * view * vec4(aPos, 1.0);
gl_Position = pos.xyww;
gl_Position.y = -gl_Position.y;
}

View File

@ -0,0 +1,13 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture sampler
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}

View File

@ -0,0 +1,22 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

View File

Before

Width:  |  Height:  |  Size: 723 KiB

After

Width:  |  Height:  |  Size: 723 KiB

View File

Before

Width:  |  Height:  |  Size: 274 KiB

After

Width:  |  Height:  |  Size: 274 KiB

View File

Before

Width:  |  Height:  |  Size: 462 KiB

After

Width:  |  Height:  |  Size: 462 KiB

View File

Before

Width:  |  Height:  |  Size: 588 KiB

After

Width:  |  Height:  |  Size: 588 KiB

View File

Before

Width:  |  Height:  |  Size: 525 KiB

After

Width:  |  Height:  |  Size: 525 KiB

View File

Before

Width:  |  Height:  |  Size: 338 KiB

After

Width:  |  Height:  |  Size: 338 KiB