tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 945bde8fa5846fd1a03ad201dfb53d04a41387d2
parent 035d49ecfa6e82c604b05af5f2e2496e35d10245
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon, 18 Nov 2024 10:33:18 +0900

fixing bug

Diffstat:
Mcmd/sample/fragment.glsl | 22+++++++++++-----------
Mcmd/sample/main.go | 53+++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 52 insertions(+), 23 deletions(-)

diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl @@ -14,7 +14,7 @@ struct Light { vec3 specular; float Kc, Kl, Kq; - float cutOff; + float cutOff, outerCutOff; }; in vec3 fnormal; @@ -35,7 +35,7 @@ void main() { vec3 viewDir, specular; vec3 reflectDir; float diff, spec, dist, attenuation; - float theta; + float epsilon, theta, intensity; fcol = vec4(0, 0, 0, 1); diffuse = vec3(0, 0, 0); @@ -48,15 +48,15 @@ void main() { lightDir = normalize(light.pos - fpos); theta = dot(lightDir, normalize(-light.dir)); - if (theta > light.cutOff) { - diff = max(dot(fnormal, lightDir), 0.0); - diffuse = diff * vec3(texture(material.diffuse, texPos)) * light.diffuse; - - viewDir = normalize(camPos - fpos); - reflectDir = reflect(-lightDir, fnormal); - spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); - specular = vec3(texture(material.specular, texPos)) * spec * light.specular; - } + epsilon = light.cutOff - light.outerCutOff; + intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0); + diff = max(dot(fnormal, lightDir), 0.0); + diffuse = diff * vec3(texture(material.diffuse, texPos)) * light.diffuse * intensity; + + viewDir = normalize(camPos - fpos); + reflectDir = reflect(-lightDir, fnormal); + spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); + specular = vec3(texture(material.specular, texPos)) * spec * light.specular * intensity; fcol += vec4(ambient + diffuse + specular, 0) * attenuation; diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -120,7 +120,6 @@ var object = &tofu.Object{ } var cubePositions = []tofu.Vec3{ - {0, 0, 0}, {4.24, 0.94, -1.94}, {0.79, 2.40, 2.87}, {-0.64, -1.68, 2.79}, @@ -136,20 +135,22 @@ var cubePositions = []tofu.Vec3{ var alpha float32 = 0.2 type App struct { - program *tofu.Program - lightProgram *tofu.Program - cursorChan chan tofu.Cursor - cur tofu.Cursor - keyChan chan struct{} - camera *tofu.Camera - termination bool - startedAt time.Time - texture *tofu.Texture - specularMap *tofu.Texture + program *tofu.Program + lightProgram *tofu.Program + teapotProgram *tofu.Program + cursorChan chan tofu.Cursor + cur tofu.Cursor + keyChan chan struct{} + camera *tofu.Camera + termination bool + startedAt time.Time + texture *tofu.Texture + specularMap *tofu.Texture } func (app *App) Update() error { now := float32(time.Since(app.startedAt).Seconds()) + now = 0 processInput(app) if app.termination { return tofu.Termination @@ -181,7 +182,8 @@ func (app *App) Update() error { app.program.SetVec3("sun.specular", sunCol) app.program.SetVec3("light.pos", lightPos) app.program.SetVec3("light.dir", tofu.Vec3{0, 0, 0}.Sub(lightPos)) - app.program.SetFloat32("light.cutOff", float32(math.Cos(12.5*math.Pi/180))) + app.program.SetFloat32("light.cutOff", float32(math.Cos(25*math.Pi/180))) + app.program.SetFloat32("light.outerCutOff", float32(math.Cos(35*math.Pi/180))) app.program.SetVec3("light.ambient", lightCol.MulF(0.1)) app.program.SetVec3("light.diffuse", lightCol.MulF(0.9)) app.program.SetVec3("light.specular", lightCol) @@ -194,6 +196,28 @@ func (app *App) Update() error { app.program.SetMat4("model", model) object.Draw() } + app.teapotProgram.Use() + app.teapotProgram.SetMat4("view", view) + app.teapotProgram.SetMat4("projection", projection) + app.teapotProgram.SetMat4("model", tofu.Translate(tofu.Vec3{0, 0, 0}).Mul(tofu.Scale(0.3))) + app.teapotProgram.SetVec3("camPos", camera.Pos) + app.teapotProgram.SetVec3("material.diffuse", tofu.Vec3{0.3, 0.4, 0.4}) + app.teapotProgram.SetVec3("material.specular", tofu.Vec3{0.3, 0.4, 0.4}) + app.teapotProgram.SetFloat32("material.shiness", 0) + app.teapotProgram.SetVec3("sun.dir", sunDir) + app.teapotProgram.SetVec3("sun.ambient", sunCol.MulF(0.5)) + app.teapotProgram.SetVec3("sun.diffuse", sunCol.MulF(0.3)) + app.teapotProgram.SetVec3("sun.specular", sunCol) + app.teapotProgram.SetVec3("light.pos", lightPos) + app.teapotProgram.SetVec3("light.dir", tofu.Vec3{0, 0, 0}.Sub(lightPos)) + app.teapotProgram.SetFloat32("light.outerCutOff", float32(math.Cos(35*math.Pi/180))) + app.teapotProgram.SetFloat32("light.cutOff", float32(math.Cos(25*math.Pi/180))) + app.teapotProgram.SetVec3("light.ambient", lightCol.MulF(0.4)) + app.teapotProgram.SetVec3("light.diffuse", lightCol.MulF(0.9)) + app.teapotProgram.SetVec3("light.specular", lightCol) + app.teapotProgram.SetFloat32("light.Kc", 1.0) + app.teapotProgram.SetFloat32("light.Kl", 0.0009) + app.teapotProgram.SetFloat32("light.Kq", 0.00032) teapot.Draw() app.lightProgram.Use() @@ -232,6 +256,7 @@ func main() { texturePath := filepath.Join(filepath.Dir(f), "container2.png") specularMapPath := filepath.Join(filepath.Dir(f), "container2_specular.png") teapotPath := filepath.Join(filepath.Dir(f), "teapot.obj") + teapotFpath := filepath.Join(filepath.Dir(f), "teapot.glsl") tf, err := os.Open(teapotPath) if err != nil { @@ -252,6 +277,10 @@ func main() { if err != nil { log.Fatalf("create light shader program: %v", err) } + app.teapotProgram, err = tofu.NewProgram(vpath, teapotFpath) + if err != nil { + log.Fatalf("create teapot shader program: %v", err) + } app.texture, err = tofu.NewTexture(texturePath) if err != nil { log.Fatalf("NewTesture: %v", err)