commit 74f1e1e3c90c4b3d58abd348221d98b543f4f55a
parent e189458839b984fd4ea4f16eadc8b1ef69aadcf3
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 9 Nov 2024 16:59:10 +0900
lighting
Diffstat:
4 files changed, 56 insertions(+), 19 deletions(-)
diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl
@@ -1,8 +1,17 @@
#version 330 core
+in vec3 fnormal;
+in vec3 fpos;
uniform vec3 lightCol;
+uniform vec3 lightPos;
uniform vec3 objCol;
out vec4 fcol;
void main() {
- fcol = vec4(objCol * lightCol, 1.0);
+ float ambientStrength = 0.1;
+ vec3 ambient = ambientStrength * lightCol;
+ vec3 dir = normalize(lightPos - fpos);
+ float diff = max(dot(fnormal, dir), 0.0);
+ vec3 diffuse = diff * lightCol;
+ fcol = vec4(objCol * (ambient + diffuse), 1.0);
+ fcol = vec4(1.0, 0.0, 0.0, 1.0);
}
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -113,6 +113,20 @@ var object = tofu.Object{
color.RGBA{0, 0, 255, 255},
color.RGBA{255, 255, 0, 255},
},
+ Normals: []tofu.Vec3{
+ {0.0, 0.0, -1.0},
+ {0.0, 0.0, -1.0},
+ {1.0, 0.0, 0.0},
+ {1.0, 0.0, 0.0},
+ {0.0, -1.0, 0.0},
+ {0.0, -1.0, 0.0},
+ {-1.0, 0.0, 0.0},
+ {-1.0, 0.0, 0.0},
+ {0.0, 1.0, 0.0},
+ {0.0, 1.0, 0.0},
+ {0.0, 0.0, 1.0},
+ {0.0, 0.0, 1.0},
+ },
TexCoords: []tofu.Vec2{
tofu.Vec2{1.0, 1.0},
tofu.Vec2{1.0, 0.0},
@@ -203,11 +217,12 @@ func main() {
projection := tofu.Perspective(80*math.Pi/180, 800/600, 0.1, 100)
program.Use()
+ program.SetVec3("lightPos", tofu.Vec3{3, 3, 0})
program.SetMat4("view", view)
program.SetMat4("projection", projection)
- model := tofu.Translate(tofu.Vec3{0, 0, 0}).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
+ model := tofu.Translate(tofu.Vec3{0, 0, 0})
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
program.SetMat4("model", model)
program.SetVec3("lightCol", lightCol)
program.SetVec3("objCol", objCol)
@@ -216,9 +231,9 @@ func main() {
lightProgram.Use()
lightProgram.SetMat4("view", view)
lightProgram.SetMat4("projection", projection)
- model = tofu.Translate(tofu.Vec3{3, 3, 0}).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
+ model = tofu.Translate(tofu.Vec3{3, 3, 0})
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
lightProgram.SetMat4("model", model)
lightProgram.SetVec3("lightCol", lightCol)
gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil)
diff --git a/cmd/sample/vertex.glsl b/cmd/sample/vertex.glsl
@@ -2,8 +2,13 @@
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 col;
layout (location = 2) in vec2 vtexCoord;
+layout (location = 3) in vec3 normal;
uniform mat4 projection, view, model;
+out vec3 fnormal;
+out vec3 fpos;
void main() {
gl_Position = projection * view * model * vec4(pos, 1.0);
+ fnormal = normal;
+ fpos = vec3(model * vec4(pos, 1.0));
}
diff --git a/object.go b/object.go
@@ -10,22 +10,28 @@ type Object struct {
Vertices []Vec3
Colors []color.Color
TexCoords []Vec2
+ Normals []Vec3
Faces [][3]uint32
vao *VAO
}
+const objectStride = 11
+
func (obj Object) data() []float32 {
- data := make([]float32, 8 * len(obj.Vertices))
+ data := make([]float32, objectStride * len(obj.Vertices))
for i := 0; i < len(obj.Vertices); i++ {
- data[8 * i] = obj.Vertices[i][0]
- data[8 * i + 1] = obj.Vertices[i][1]
- data[8 * i + 2] = obj.Vertices[i][2]
+ data[objectStride * i] = obj.Vertices[i][0]
+ data[objectStride * i + 1] = obj.Vertices[i][1]
+ data[objectStride * i + 2] = obj.Vertices[i][2]
r, g, b, _ := obj.Colors[i].RGBA()
- data[8 * i + 3] = float32(r)/0xffff
- data[8 * i + 4] = float32(g)/0xffff
- data[8 * i + 5] = float32(b)/0xffff
- data[8 * i + 6] = obj.TexCoords[i][0]
- data[8 * i + 7] = obj.TexCoords[i][1]
+ data[objectStride * i + 3] = float32(r)/0xffff
+ data[objectStride * i + 4] = float32(g)/0xffff
+ data[objectStride * i + 5] = float32(b)/0xffff
+ data[objectStride * i + 6] = obj.TexCoords[i][0]
+ data[objectStride * i + 7] = obj.TexCoords[i][1]
+ data[objectStride * i + 8] = obj.Normals[i][0]
+ data[objectStride * i + 9] = obj.Normals[i][1]
+ data[objectStride * i + 10] = obj.Normals[i][2]
}
return data
}
@@ -55,6 +61,7 @@ const (
UniformVertex = iota
UniformColor
UniformTexCoords
+ UniformNormal
)
func newVAO(obj *Object) *VAO {
@@ -64,9 +71,10 @@ func newVAO(obj *Object) *VAO {
vao.bind()
data := obj.data()
vao.setData(data)
- vao.setAttribute(UniformVertex, 3, 8, 0)
- vao.setAttribute(UniformColor, 3, 8, 3)
- vao.setAttribute(UniformTexCoords, 2, 8, 6)
+ vao.setAttribute(UniformVertex, 3, objectStride, 0)
+ vao.setAttribute(UniformColor, 3, objectStride, 3)
+ vao.setAttribute(UniformTexCoords, 2, objectStride, 6)
+ vao.setAttribute(UniformNormal, 3, objectStride, 8)
fdata := obj.faceData()
vao.setFaces(fdata)
return vao