Skip to content

Commit

Permalink
feat: move Shader to a folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzxzzk115 committed Nov 2, 2023
1 parent eea4d7e commit 43d2403
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
29 changes: 1 addition & 28 deletions Renderer/Application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "VGL/FrameBuffer.h"
#include "VGL/Matrix4.h"
#include "VGL/VGL.h"
#include "VGL/Shaders/Gouraud.h"

#include <raylib-cpp.hpp>

Expand All @@ -34,34 +35,6 @@ void DrawFrameBuffer()
}
}

struct GouraudShader : public VGLShaderBase
{
int BindDiffuseTextureSlot;
int BindNormalTextureSlot;

Vector3Float UniformLightDirection;
Matrix4 UniformMVP;
Matrix4 UniformMVPIT;

std::vector<Vector2Float> VaryingUVs {3};

virtual void vert(int faceIndex, int vertexIndexInFace, Vector3Float& gl_Position) override
{
VaryingUVs[vertexIndexInFace] = TargetMesh->GetUV(faceIndex, vertexIndexInFace);
gl_Position = UniformMVP * gl_Position;
}

virtual bool frag(Vector3Float bc, VGL::Color& gl_FragColor) override
{
Vector2Float uv = VaryingUVs[0] * bc.X + VaryingUVs[1] * bc.Y + VaryingUVs[2] * bc.Z;
Vector3Float n = (UniformMVPIT * sample2D(BindNormalTextureSlot, uv.X, uv.Y).XYZ()).Normalized();
Vector3Float l = (UniformMVP * UniformLightDirection).Normalized();
float intensity = std::max(0.0f, n * l);
gl_FragColor = sample2D(BindDiffuseTextureSlot, uv.X, uv.Y) * intensity;
return false;
}
};

int main()
{
raylib::Window window(ScreenWidth, ScreenHeight, "VSoftRenderer");
Expand Down
1 change: 1 addition & 0 deletions Renderer/VGL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_library(${TARGET_NAME} src/Line.cpp
src/Texture2D.cpp
src/VGL.cpp
src/Matrix3.cpp
src/ShadersCompile.cpp
)

set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD 17)
Expand Down
40 changes: 40 additions & 0 deletions Renderer/VGL/include/VGL/Shaders/Gouraud.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Gouraud.h
//
// Created or modified by Kexuan Zhang on 2023/11/2 23:04.
//

#pragma once

#include "VGL/VGL.h"

namespace VGL
{
struct GouraudShader : public VGLShaderBase
{
int BindDiffuseTextureSlot;
int BindNormalTextureSlot;

Vector3Float UniformLightDirection;
Matrix4 UniformMVP;
Matrix4 UniformMVPIT;

std::vector<Vector2Float> VaryingUVs {3};

virtual void vert(int faceIndex, int vertexIndexInFace, Vector3Float& gl_Position) override
{
VaryingUVs[vertexIndexInFace] = TargetMesh->GetUV(faceIndex, vertexIndexInFace);
gl_Position = UniformMVP * gl_Position;
}

virtual bool frag(Vector3Float bc, VGL::Color& gl_FragColor) override
{
Vector2Float uv = VaryingUVs[0] * bc.X + VaryingUVs[1] * bc.Y + VaryingUVs[2] * bc.Z;
Vector3Float n = (UniformMVPIT * sample2D(BindNormalTextureSlot, uv.X, uv.Y).XYZ()).Normalized();
Vector3Float l = (UniformMVP * UniformLightDirection).Normalized();
float intensity = std::max(0.0f, n * l);
gl_FragColor = sample2D(BindDiffuseTextureSlot, uv.X, uv.Y) * intensity;
return false;
}
};
} // namespace VGL
7 changes: 7 additions & 0 deletions Renderer/VGL/src/ShadersCompile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// ShadersCompile.cpp
//
// Created or modified by Kexuan Zhang on 2023/11/2 23:07.
//

#include "VGL/Shaders/Gouraud.h"

0 comments on commit 43d2403

Please sign in to comment.