Skip to content

Commit

Permalink
refactor: rename property name. update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zzxzzk115 committed Nov 2, 2023
1 parent dc78ae1 commit b76f1ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,26 @@
```c++
struct GouraudShader : public VGLShaderBase
{
int TextureSlot;
Vector3Float LightDirection;
int BindTextureSlot;

Matrix4 MVP;
Vector3Float UniformLightDirection;
Matrix4 UniformMVP;

Vector3Float VaryingIntensity; // written by vertex shader, read by fragment shader
Vector3Float VaryingIntensity;
std::vector<Vector2Float> VaryingUVs {3};

virtual Vector3Float vert(Vertex& vertex, int vertexIndexInFace) override
virtual void vert(Vertex& vertex, int vertexIndexInFace, Vector3Float& gl_Position) override
{
VaryingIntensity[vertexIndexInFace] = std::max(0.0f, LightDirection * vertex.Normal);
VaryingIntensity[vertexIndexInFace] = std::max(0.0f, UniformLightDirection * vertex.Normal);
VaryingUVs[vertexIndexInFace] = vertex.UV;
Vector3Float glVertex = MVP * VGLShaderBase::vert(vertex, vertexIndexInFace);
return glVertex;
gl_Position = UniformMVP * vertex.Position;
}

virtual bool frag(Vector3Float bc, VSoftRenderer::Color& color) override
virtual bool frag(Vector3Float bc, VGL::Color& gl_FragColor) override
{
float intensity = VaryingIntensity * bc;
Vector2Float uv = VaryingUVs[0] * bc.X + VaryingUVs[1] * bc.Y + VaryingUVs[2] * bc.Z;
color = sample2D(TextureSlot, uv.X, uv.Y) * intensity;
gl_FragColor = sample2D(BindTextureSlot, uv.X, uv.Y) * intensity;
return false;
}
};
Expand Down Expand Up @@ -90,9 +89,9 @@
glBindTexture(0, texture);

GouraudShader shader = {};
shader.MVP = mvp;
shader.LightDirection = light.GetDirection();
shader.TextureSlot = 0;
shader.UniformMVP = mvp;
shader.UniformLightDirection = light.GetDirection();
shader.BindTextureSlot = 0;

glBindShader(0, &shader);

Expand Down
25 changes: 12 additions & 13 deletions Renderer/Application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,26 @@ void DrawFrameBuffer()

struct GouraudShader : public VGLShaderBase
{
int TextureSlot;
Vector3Float LightDirection;
int BindTextureSlot;

Matrix4 MVP;
Vector3Float UniformLightDirection;
Matrix4 UniformMVP;

Vector3Float VaryingIntensity; // written by vertex shader, read by fragment shader
Vector3Float VaryingIntensity;
std::vector<Vector2Float> VaryingUVs {3};

virtual Vector3Float vert(Vertex& vertex, int vertexIndexInFace) override
virtual void vert(Vertex& vertex, int vertexIndexInFace, Vector3Float& gl_Position) override
{
VaryingIntensity[vertexIndexInFace] = std::max(0.0f, LightDirection * vertex.Normal);
VaryingIntensity[vertexIndexInFace] = std::max(0.0f, UniformLightDirection * vertex.Normal);
VaryingUVs[vertexIndexInFace] = vertex.UV;
Vector3Float glVertex = MVP * VGLShaderBase::vert(vertex, vertexIndexInFace);
return glVertex;
gl_Position = UniformMVP * vertex.Position;
}

virtual bool frag(Vector3Float bc, VGL::Color& color) override
virtual bool frag(Vector3Float bc, VGL::Color& gl_FragColor) override
{
float intensity = VaryingIntensity * bc;
Vector2Float uv = VaryingUVs[0] * bc.X + VaryingUVs[1] * bc.Y + VaryingUVs[2] * bc.Z;
color = sample2D(TextureSlot, uv.X, uv.Y) * intensity;
gl_FragColor = sample2D(BindTextureSlot, uv.X, uv.Y) * intensity;
return false;
}
};
Expand Down Expand Up @@ -197,9 +196,9 @@ int main()
glBindTexture(0, texture);

GouraudShader shader = {};
shader.MVP = mvp;
shader.LightDirection = light.GetDirection();
shader.TextureSlot = 0;
shader.UniformMVP = mvp;
shader.UniformLightDirection = light.GetDirection();
shader.BindTextureSlot = 0;

glBindShader(0, &shader);

Expand Down
4 changes: 2 additions & 2 deletions Renderer/VGL/include/VGL/VGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace VGL
{
virtual ~VGLShaderBase() = default;

virtual Vector3Float vert(Vertex& vertex, int vertexIndexInFace);
virtual bool frag(Vector3Float bc, Color& color) = 0;
virtual void vert(Vertex& vertex, int vertexIndexInFace, Vector3Float& gl_Position) = 0;
virtual bool frag(Vector3Float bc, Color& gl_FragColor) = 0;

Color sample2D(int textureSlot, float u, float v);
};
Expand Down
9 changes: 3 additions & 6 deletions Renderer/VGL/src/VGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace VGL
{
VGLState g_vglState = {};

Vector3Float VGLShaderBase::vert(Vertex& vertex, int vertexIndexInFace)
{
return vertex.Position;
}

Color VGLShaderBase::sample2D(int textureSlot, float u, float v)
{
if (g_vglState.TextureMap.count(textureSlot) == 0)
Expand Down Expand Up @@ -97,7 +92,9 @@ namespace VGL
throw std::runtime_error("No shader bound!");
}

screenCoords[v] = g_vglState.ViewportMatrix * g_vglState.UsingShader->vert(vertex, v);
Vector3Float position;
g_vglState.UsingShader->vert(vertex, v, position);
screenCoords[v] = g_vglState.ViewportMatrix * position;
}
Triangle3D::DrawInterpolated(screenCoords, g_vglState.UsingShader);
}
Expand Down

0 comments on commit b76f1ac

Please sign in to comment.