tofu

Making something with OpenGL in Go
Log | Files | Refs

commit a725c6588d5bdc8bd850bd3d90d48590da3f8f83
parent b2d0180669048d007691ba434db948042d6adbc7
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 29 Nov 2024 08:38:58 +0900

read mtl file

Diffstat:
Mcmd/sample/fragment.glsl | 1+
Mcmd/sample/main.go | 6+++---
Mobject.go | 35+++++++++++++++++++++++++++++++----
3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl @@ -57,6 +57,7 @@ void main() { for (int i = 0; i < PointLights.length(); i++) { fcol += vec4(calcPointLight(PointLights[i], Cube, CamPos), 0); } + float x; } vec3 calcPointLight(PointLight l, Material m, vec3 camPos) { diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -308,7 +308,7 @@ func (app *App) Update() error { teapotUniforms.Trans.View = view teapotUniforms.Trans.Projection = projection teapotUniforms.Trans.Model = tofu.Translate(tofu.Vec3{0, 0, 0}). - Mul(tofu.Scale(0.06)) + Mul(tofu.Scale(0.3)) teapotUniforms.CamPos = camera.Pos if err := app.teapotProgram.SetUniforms(); err != nil { log.Printf("teapotprogram.setuniforms(): %v", err) @@ -327,7 +327,7 @@ func (app *App) Update() error { for _, pl := range pointLights { lightUniforms.Trans.Model = tofu.Translate(pl.Pos). Mul(tofu.Scale(0.3)) - lightUniforms.LightCol = pl.Specular + lightUniforms.LightCol = tofu.Vec3{1, 1, 1}.Sub(tofu.Vec3{1, 1, 1}.Sub(pl.Specular).MulF(0.3)) if err := app.lightProgram.SetUniforms(); err != nil { log.Printf("lightprogram.setuniforms(): %v", err) } @@ -376,7 +376,7 @@ func main() { 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") - teapotPath := filepath.Join(filepath.Dir(f), "teapot.obj") + teapotPath := filepath.Join(filepath.Dir(f), "backpack/backpack.obj") teapotFpath := filepath.Join(filepath.Dir(f), "teapot.glsl") teapot, err = tofu.DecodeObject(teapotPath) diff --git a/object.go b/object.go @@ -2,21 +2,30 @@ package tofu import ( "bufio" + "errors" "fmt" "log" "math" "os" + "path/filepath" "strconv" "strings" "github.com/go-gl/gl/v3.3-core/gl" ) +type ErrUnsupported string + +func (e ErrUnsupported) Error() string { + return string(e) + " unsupported, ignoring" +} + type Object struct { Vertices []Vec3 TexCoords []Vec2 Normals []Vec3 Faces []Face + Materials map[string]*Material vao *VAO } @@ -30,8 +39,13 @@ func DecodeObject(filename string) (*Object, error) { s := bufio.NewScanner(f) nl := 1 for s.Scan() { - if err := decodeLine(obj, s.Text()); err != nil { - return nil, fmt.Errorf("%s:%d %s", filename, nl, err) + if err := decodeObjectLine(obj, s.Text(), filename); err != nil { + var perr ErrUnsupported + if errors.As(err, &perr) { + //log.Printf("%s:%d %v", filename, nl, perr) + } else { + return nil, fmt.Errorf("%s:%d %s", filename, nl, err) + } } nl++ } @@ -41,7 +55,7 @@ func DecodeObject(filename string) (*Object, error) { return obj, nil } -func decodeLine(obj *Object, line string) error { +func decodeObjectLine(obj *Object, line string, filename string) error { if len(line) == 0 || line[0] == '#' { return nil } @@ -66,8 +80,21 @@ func decodeLine(obj *Object, line string) error { if err := obj.addFace(sf[1:]); err != nil { return fmt.Errorf("addFace: %v", err) } + case "mtllib": + if len(sf) < 2 { + return fmt.Errorf("no mtllib file specified") + } + m, err := decodeMaterial(filepath.Join(filepath.Dir(filename), sf[1])) + if err != nil { + return fmt.Errorf("decode mtl file: %v", err) + } + if obj.Materials == nil { + obj.Materials = make(map[string]*Material) + } + obj.Materials[m.Name] = m + log.Printf("mtl: %+v", m) default: - log.Printf("unsupported: %s, ignoring", t) + return ErrUnsupported(t) } return nil }