tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 9360873ea55df80f683402e47f5f79bb55efe620
parent 0cea544387e27db1adc96b54e3d63ad74f8b010c
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 13 Nov 2024 09:28:42 +0900

lighting maps

Diffstat:
Mcmd/sample/fragment.glsl | 12++++++------
Mcmd/sample/main.go | 46++++++++++++++++++++++++++--------------------
Mcmd/sample/vertex.glsl | 4+++-
Mobject.go | 5+++--
Mshader.go | 2+-
5 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl @@ -1,9 +1,8 @@ #version 330 core struct Material { - vec3 ambient; - vec3 diffuse; - vec3 specular; + sampler2D diffuse; + sampler2D specular; float shiness; }; @@ -16,6 +15,7 @@ struct Light { in vec3 fnormal; in vec3 fpos; +in vec2 texPos; uniform vec3 camPos; uniform vec3 objCol; @@ -25,16 +25,16 @@ uniform Light light; out vec4 fcol; void main() { - vec3 ambient = material.ambient * light.ambient; + vec3 ambient = vec3(texture(material.diffuse, texPos)) * light.ambient; vec3 lightDir = normalize(light.position - fpos); float diff = max(dot(fnormal, lightDir), 0.0); - vec3 diffuse = diff * material.diffuse * light.diffuse; + vec3 diffuse = diff * vec3(texture(material.diffuse, texPos)) * light.diffuse; vec3 viewDir = normalize(camPos - fpos); vec3 reflectDir = reflect(-lightDir, fnormal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); - vec3 specular = material.specular * spec * light.specular; + vec3 specular = vec3(texture(material.specular, texPos)) * spec * light.specular; fcol = vec4(ambient + diffuse + specular, 1.0); } diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -111,24 +111,20 @@ var object = tofu.Object{ tofu.Vec2{1.0, 0.0}, tofu.Vec2{0.0, 0.0}, tofu.Vec2{0.0, 1.0}, - tofu.Vec2{1.0, 1.0}, - tofu.Vec2{1.0, 0.0}, - tofu.Vec2{0.0, 0.0}, - tofu.Vec2{0.0, 1.0}, }, Faces: []tofu.Face{ - {V: [3]int{0, 1, 2}, N: [3]int{0, 0, 0}}, - {V: [3]int{0, 2, 3}, N: [3]int{0, 0, 0}}, - {V: [3]int{0, 1, 5}, N: [3]int{1, 1, 1}}, - {V: [3]int{0, 5, 4}, N: [3]int{1, 1, 1}}, - {V: [3]int{1, 2, 6}, N: [3]int{2, 2, 2}}, - {V: [3]int{1, 6, 5}, N: [3]int{2, 2, 2}}, - {V: [3]int{2, 3, 7}, N: [3]int{3, 3, 3}}, - {V: [3]int{2, 7, 6}, N: [3]int{3, 3, 3}}, - {V: [3]int{3, 0, 4}, N: [3]int{4, 4, 4}}, - {V: [3]int{3, 4, 7}, N: [3]int{4, 4, 4}}, - {V: [3]int{4, 5, 6}, N: [3]int{5, 5, 5}}, - {V: [3]int{4, 6, 7}, N: [3]int{5, 5, 5}}, + {V: [3]int{0, 1, 2}, N: [3]int{0, 0, 0}, T: [3]int{0, 1, 2}}, + {V: [3]int{0, 2, 3}, N: [3]int{0, 0, 0}, T: [3]int{0, 2, 3}}, + {V: [3]int{0, 1, 5}, N: [3]int{1, 1, 1}, T: [3]int{0, 1, 2}}, + {V: [3]int{0, 5, 4}, N: [3]int{1, 1, 1}, T: [3]int{0, 2, 3}}, + {V: [3]int{1, 2, 6}, N: [3]int{2, 2, 2}, T: [3]int{0, 1, 2}}, + {V: [3]int{1, 6, 5}, N: [3]int{2, 2, 2}, T: [3]int{0, 2, 3}}, + {V: [3]int{2, 3, 7}, N: [3]int{3, 3, 3}, T: [3]int{0, 1, 2}}, + {V: [3]int{2, 7, 6}, N: [3]int{3, 3, 3}, T: [3]int{0, 2, 3}}, + {V: [3]int{3, 0, 4}, N: [3]int{4, 4, 4}, T: [3]int{0, 1, 2}}, + {V: [3]int{3, 4, 7}, N: [3]int{4, 4, 4}, T: [3]int{0, 2, 3}}, + {V: [3]int{4, 5, 6}, N: [3]int{5, 5, 5}, T: [3]int{0, 1, 2}}, + {V: [3]int{4, 6, 7}, N: [3]int{5, 5, 5}, T: [3]int{0, 2, 3}}, }, } @@ -143,6 +139,8 @@ type App struct { camera *tofu.Camera termination bool startedAt time.Time + texture *tofu.Texture + specularMap *tofu.Texture } func (app *App) Update() error { @@ -163,7 +161,6 @@ func (app *App) Update() error { float32(math.Sin(float64(now*2.0))/2 + 1), float32(math.Sin(float64(now*0.7))/2 + 1), float32(math.Sin(float64(now*1.3))/2 + 1)} - matCol := tofu.Vec3{1, 1, 1} app.program.Use() app.program.SetVec3("light.position", lightPos) app.program.SetVec3("light.ambient", lightCol.MulF(0.2)) @@ -174,9 +171,8 @@ func (app *App) Update() error { app.program.SetMat4("view", view) app.program.SetMat4("projection", projection) app.program.SetVec3("camPos", camera.Pos) - app.program.SetVec3("material.ambient", matCol.MulF(0.4)) - app.program.SetVec3("material.diffuse", matCol.MulF(1.0)) - app.program.SetVec3("material.specular", matCol.MulF(1.0)) + app.program.SetTexture("material.diffuse", app.texture) + app.program.SetTexture("material.specular", app.specularMap) app.program.SetFloat32("material.shiness", 32) model := tofu.Translate(tofu.Vec3{0, 0, 0}). Mul(tofu.Rotate(now, tofu.Vec3{math.Sqrt2 / 2, -math.Sqrt2 / 2, 0})) @@ -216,6 +212,8 @@ func main() { vpath := filepath.Join(filepath.Dir(f), "vertex.glsl") fpath := filepath.Join(filepath.Dir(f), "fragment.glsl") lightFpath := filepath.Join(filepath.Dir(f), "light_fragment.glsl") + texturePath := filepath.Join(filepath.Dir(f), "container2.png") + specularMapPath := filepath.Join(filepath.Dir(f), "container2_specular.png") app.program, err = tofu.NewProgram(vpath, fpath) if err != nil { @@ -225,6 +223,14 @@ func main() { if err != nil { log.Fatalf("create light shader program: %v", err) } + app.texture, err = tofu.NewTexture(texturePath) + if err != nil { + log.Fatalf("NewTesture: %v", err) + } + app.specularMap, err = tofu.NewTexture(specularMapPath) + if err != nil { + log.Fatalf("NewTesture: %v", err) + } camera = tofu.NewCamera() camera.MoveTo(tofu.Vec3{0, 0, 3}) diff --git a/cmd/sample/vertex.glsl b/cmd/sample/vertex.glsl @@ -1,14 +1,16 @@ #version 330 core layout (location = 0) in vec3 pos; layout (location = 1) in vec3 col; -layout (location = 2) in vec2 vtexCoord; +layout (location = 2) in vec2 vTexPos; layout (location = 3) in vec3 normal; uniform mat4 projection, view, model; out vec3 fnormal; out vec3 fpos; +out vec2 texPos; void main() { gl_Position = projection * view * model * vec4(pos, 1.0); fnormal = mat3(transpose(inverse(model))) * normal; fpos = vec3(model * vec4(pos, 1.0)); + texPos = vTexPos; } diff --git a/object.go b/object.go @@ -18,6 +18,7 @@ type Object struct { type Face struct { V [3]int N [3]int + T [3]int } const objectStride = 11 @@ -33,8 +34,8 @@ func (obj Object) data() []float32 { data[objectStride*(3*i+j)+3] = float32(r) / 0xffff data[objectStride*(3*i+j)+4] = float32(g) / 0xffff data[objectStride*(3*i+j)+5] = float32(b) / 0xffff - data[objectStride*(3*i+j)+6] = obj.TexCoords[f.V[j]][0] - data[objectStride*(3*i+j)+7] = obj.TexCoords[f.V[j]][1] + data[objectStride*(3*i+j)+6] = obj.TexCoords[f.T[j]][0] + data[objectStride*(3*i+j)+7] = obj.TexCoords[f.T[j]][1] data[objectStride*(3*i+j)+8] = obj.Normals[f.N[j]][0] data[objectStride*(3*i+j)+9] = obj.Normals[f.N[j]][1] data[objectStride*(3*i+j)+10] = obj.Normals[f.N[j]][2] diff --git a/shader.go b/shader.go @@ -116,7 +116,7 @@ func (p *Program) SetMat4(name string, val Mat4) error { return nil } -func (p *Program) SetTexture(t *Texture, name string) error { +func (p *Program) SetTexture(name string, t *Texture) error { p.Use() gl.ActiveTexture(t.unit) gl.BindTexture(gl.TEXTURE_2D, t.id)