forked from ssloy/tinyrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#include "geometry.h" | ||
|
||
template <> template <> vec<3,int> ::vec(const vec<3,float> &v) : x(int(v.x+.5)),y(int(v.y+.5)),z(int(v.z+.5)) {} | ||
template <> template <> vec<3,int> ::vec(const vec<3,float> &v) : x(int(v.x+.5f)),y(int(v.y+.5f)),z(int(v.z+.5f)) {} | ||
template <> template <> vec<3,float>::vec(const vec<3,int> &v) : x(v.x),y(v.y),z(v.z) {} | ||
template <> template <> vec<2,int> ::vec(const vec<2,float> &v) : x(int(v.x+.5)),y(int(v.y+.5)) {} | ||
template <> template <> vec<2,int> ::vec(const vec<2,float> &v) : x(int(v.x+.5f)),y(int(v.y+.5f)) {} | ||
template <> template <> vec<2,float>::vec(const vec<2,int> &v) : x(v.x),y(v.y) {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
struct FlatShader : public IShader { | ||
mat<3,3,float> varying_tri; | ||
|
||
virtual ~FlatShader() {} | ||
|
||
virtual Vec3i vertex(int iface, int nthvert) { | ||
Vec4f gl_Vertex = embed<4>(model->vert(iface, nthvert)); | ||
gl_Vertex = Projection*ModelView*gl_Vertex; | ||
varying_tri.set_col(nthvert, proj<3>(gl_Vertex/gl_Vertex[3])); | ||
gl_Vertex = Viewport*gl_Vertex; | ||
return proj<3>(gl_Vertex/gl_Vertex[3]); | ||
} | ||
|
||
virtual bool fragment(Vec3f bar, TGAColor &color) { | ||
Vec3f n = cross(varying_tri.col(1)-varying_tri.col(0),varying_tri.col(2)-varying_tri.col(0)).normalize(); | ||
float intensity = CLAMP(n*light_dir, 0.f, 1.f); | ||
color = TGAColor(255, 255, 255)*intensity; | ||
return false; | ||
} | ||
}; | ||
|
||
struct GouraudShader : public IShader { | ||
mat<3,3,float> varying_tri; | ||
Vec3f varying_ity; | ||
|
||
virtual ~GouraudShader() {} | ||
|
||
virtual Vec3i vertex(int iface, int nthvert) { | ||
Vec4f gl_Vertex = embed<4>(model->vert(iface, nthvert)); | ||
gl_Vertex = Projection*ModelView*gl_Vertex; | ||
varying_tri.set_col(nthvert, proj<3>(gl_Vertex/gl_Vertex[3])); | ||
|
||
varying_ity[nthvert] = CLAMP(model->normal(iface, nthvert)*light_dir, 0.f, 1.f); | ||
|
||
gl_Vertex = Viewport*gl_Vertex; | ||
return proj<3>(gl_Vertex/gl_Vertex[3]); | ||
} | ||
|
||
virtual bool fragment(Vec3f bar, TGAColor &color) { | ||
float intensity = varying_ity*bar; | ||
color = TGAColor(255, 255, 255)*intensity; | ||
return false; | ||
} | ||
}; | ||
|
||
|
||
struct ToonShader : public IShader { | ||
mat<3,3,float> varying_tri; | ||
Vec3f varying_ity; | ||
|
||
virtual ~ToonShader() {} | ||
|
||
virtual Vec3i vertex(int iface, int nthvert) { | ||
Vec4f gl_Vertex = embed<4>(model->vert(iface, nthvert)); | ||
gl_Vertex = Projection*ModelView*gl_Vertex; | ||
varying_tri.set_col(nthvert, proj<3>(gl_Vertex/gl_Vertex[3])); | ||
|
||
varying_ity[nthvert] = CLAMP(model->normal(iface, nthvert)*light_dir, 0.f, 1.f); | ||
|
||
gl_Vertex = Viewport*gl_Vertex; | ||
return proj<3>(gl_Vertex/gl_Vertex[3]); | ||
} | ||
|
||
virtual bool fragment(Vec3f bar, TGAColor &color) { | ||
float intensity = varying_ity*bar; | ||
if (intensity>.85) intensity = 1; | ||
else if (intensity>.60) intensity = .80; | ||
else if (intensity>.45) intensity = .60; | ||
else if (intensity>.30) intensity = .45; | ||
else if (intensity>.15) intensity = .30; | ||
color = TGAColor(255, 155, 0)*intensity; | ||
return false; | ||
} | ||
}; |