Skip to content

Commit e7f1bd4

Browse files
committed
remove "default" shader
1 parent f2e0dbd commit e7f1bd4

File tree

10 files changed

+31
-131
lines changed

10 files changed

+31
-131
lines changed

examples/bowser.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ func main() {
3838

3939
// create a rendering context
4040
context := NewContext(width*scale, height*scale)
41+
context.ClearColorBufferWith(Black)
4142

4243
// create transformation matrix and light direction
4344
aspect := float64(width) / float64(height)
4445
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
4546
light := V(0.75, 0.25, 1).Normalize()
46-
color := White
4747

4848
// render
49-
context.ClearColor = Black
50-
context.Shader = NewDefaultShader(matrix, light, eye, color)
51-
context.ClearColorBuffer()
52-
context.ClearDepthBuffer()
49+
shader := NewPhongShader(matrix, light, eye)
50+
shader.ObjectColor = HexColor("FFD34E")
51+
shader.DiffuseColor = Gray(0.9)
52+
shader.SpecularColor = Gray(0.25)
53+
shader.SpecularPower = 100
54+
context.Shader = shader
5355
start := time.Now()
5456
context.DrawMesh(mesh)
5557
fmt.Println(time.Since(start))

examples/capsule.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ func main() {
4848
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
4949

5050
// render
51-
context.ClearColor = White
52-
shader := NewDefaultShader(matrix, light, eye, Black)
51+
shader := NewPhongShader(matrix, light, eye)
5352
shader.Texture = texture
5453
context.Shader = shader
55-
context.ClearColorBuffer()
5654
start := time.Now()
5755
context.DrawMesh(mesh)
5856
fmt.Println(time.Since(start))

examples/dragon.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ func main() {
6262

6363
// create a rendering context
6464
context := NewContext(width*scale, height*scale)
65-
context.ClearColor = background
65+
context.ClearColorBufferWith(background)
6666

6767
// create transformation matrix and light direction
6868
aspect := float64(width) / float64(height)
6969
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
7070

7171
// render
72-
context.ClearColorBuffer()
73-
context.Shader = NewDefaultShader(matrix, light, eye, color)
72+
shader := NewPhongShader(matrix, light, eye)
73+
shader.ObjectColor = color
74+
context.Shader = shader
7475
done = timed("rendering mesh")
7576
context.DrawMesh(mesh)
7677
done()

examples/hello.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func main() {
3333
light := V(-2, 0, 1).Normalize()
3434
color := Color{0.5, 1, 0.65, 1}
3535

36-
context.Shader = NewDefaultShader(matrix, light, eye, color)
36+
shader := NewPhongShader(matrix, light, eye)
37+
shader.ObjectColor = color
38+
context.Shader = shader
3739
context.DrawMesh(mesh)
3840

3941
SavePNG("out.png", context.Image())

examples/magica.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func main() {
7171
shader := NewPhongShader(matrix, light, eye)
7272
shader.AmbientColor = Gray(0.4)
7373
shader.DiffuseColor = Gray(0.9)
74-
shader.SpecularColor = Gray(0)
74+
shader.SpecularPower = 0
7575
context.Shader = shader
7676
done = timed("rendering triangles")
7777
context.DrawTriangles(mesh.Triangles)

examples/shapes.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,27 @@ func main() {
5151

5252
// create a rendering context
5353
context := NewContext(width*scale, height*scale)
54+
context.ClearColorBufferWith(Black)
5455

5556
// create transformation matrix and light direction
5657
aspect := float64(width) / float64(height)
5758
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
5859

5960
// render
60-
context.ClearColorBufferWith(Black)
61-
context.Shader = NewDefaultShader(matrix, light, eye, color)
61+
shader := NewPhongShader(matrix, light, eye)
62+
shader.ObjectColor = color
63+
context.Shader = shader
6264
start := time.Now()
6365
context.DrawMesh(mesh)
6466
fmt.Println(time.Since(start))
6567

6668
mesh, _ = LoadSTL("examples/sphere.stl")
6769
mesh.SmoothNormals()
6870
mesh.Transform(Scale(V(2.5, 2.5, 2.5)))
69-
context.Shader = NewDefaultShader(matrix, light, eye, HexColor("FFFF9D").Alpha(0.65))
71+
shader = NewPhongShader(matrix, light, eye)
72+
shader.ObjectColor = HexColor("FFFF9D").Alpha(0.65)
73+
shader.SpecularPower = 0
74+
context.Shader = shader
7075
context.DrawMesh(mesh)
7176
context.Wireframe = true
7277
context.DepthBias = -0.00001

examples/square.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
5252

5353
// render
54-
shader := NewDefaultShader(matrix, light, eye, color)
54+
shader := NewTextureShader(matrix, texture)
5555
shader.Texture = texture
5656
start := time.Now()
5757
context.Shader = shader

examples/teapot.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ func main() {
4343
// create transformation matrix and light direction
4444
aspect := float64(width) / float64(height)
4545
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
46-
// h := 0.6
47-
// w := h * float64(width) / float64(height)
48-
// matrix := LookAt(eye, center, up).Orthographic(-w, w, -h, h, -10, 10)
4946
light := V(0, 1, 1).Normalize()
50-
color := HexColor("#B9121B")
5147

5248
// render
5349
start := time.Now()
54-
context.Shader = NewDefaultShader(matrix, light, eye, color)
50+
shader := NewPhongShader(matrix, light, eye)
51+
shader.ObjectColor = HexColor("#B9121B")
52+
shader.SpecularColor = Gray(0.25)
53+
shader.SpecularPower = 64
54+
context.Shader = shader
5555
context.DrawMesh(mesh)
5656
fmt.Println(time.Since(start))
5757

examples/test1.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

shader.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,6 @@ type Shader interface {
77
Fragment(Vertex) Color
88
}
99

10-
type DefaultShader struct {
11-
Matrix Matrix
12-
Light Vector
13-
Camera Vector
14-
Color Color
15-
Texture Texture
16-
}
17-
18-
func NewDefaultShader(matrix Matrix, light, camera Vector, color Color) *DefaultShader {
19-
return &DefaultShader{matrix, light, camera, color, nil}
20-
}
21-
22-
func (shader *DefaultShader) Vertex(v Vertex) Vertex {
23-
v.Output = shader.Matrix.MulPositionW(v.Position)
24-
return v
25-
}
26-
27-
func (shader *DefaultShader) Fragment(v Vertex) Color {
28-
color := shader.Color
29-
if color == Discard {
30-
color = v.Color
31-
}
32-
if shader.Texture != nil {
33-
color = shader.Texture.BilinearSample(v.Texture.X, v.Texture.Y)
34-
}
35-
diffuse := math.Max(v.Normal.Dot(shader.Light), 0)
36-
specular := 0.0
37-
if diffuse > 0 {
38-
camera := shader.Camera.Sub(v.Position).Normalize()
39-
specular = math.Max(camera.Dot(shader.Light.Negate().Reflect(v.Normal)), 0)
40-
specular = math.Pow(specular, 50)
41-
}
42-
light := Clamp(diffuse+specular, 0.1, 1)
43-
return color.MulScalar(light).Alpha(color.A)
44-
}
45-
4610
// SolidColorShader renders with a single, solid color.
4711
type SolidColorShader struct {
4812
Matrix Matrix
@@ -119,7 +83,7 @@ func (shader *PhongShader) Fragment(v Vertex) Color {
11983
}
12084
diffuse := math.Max(v.Normal.Dot(shader.LightDirection), 0)
12185
light = light.Add(shader.DiffuseColor.MulScalar(diffuse))
122-
if diffuse > 0 {
86+
if diffuse > 0 && shader.SpecularPower > 0 {
12387
camera := shader.CameraPosition.Sub(v.Position).Normalize()
12488
reflected := shader.LightDirection.Negate().Reflect(v.Normal)
12589
specular := math.Max(camera.Dot(reflected), 0)

0 commit comments

Comments
 (0)