g3n / engine

Go 3D Game Engine (http://g3n.rocks)
https://discord.gg/NfaeVr8zDg
BSD 2-Clause "Simplified" License
2.8k stars 295 forks source link

Raycasting with .obj files #216

Closed Innoviox closed 2 years ago

Innoviox commented 3 years ago

Raycasting to a Node loaded from a .obj loaded as shown in the g3nd demos leads to errors.

Load the object like this (path redacted):

dec, _ := obj.Decode("[...]/tile.obj", "")
group, _ := dec.NewGroup()
a.Scene().Add(group)

The error is on raycaster.go, line 347:

mat := m.GetMaterial(i).GetMaterial()

Maybe because the materials aren't being loaded correctly?

Full traceback:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x426c085]

goroutine 1 [running, locked to thread]:
github.com/g3n/engine/experimental/collision.(*Raycaster).RaycastMesh.func2(0x3b842742bb83126f, 0x3e6d9168bffcf923, 0xc0010bed3e75cb36, 0x3e8f607c3e3851ec, 0xc0010bed, 0x0)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/experimental/collision/raycaster.go:347 +0x145
github.com/g3n/engine/geometry.(*Geometry).ReadFaces(0xc0002465b0, 0xc0000db958)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/geometry/geometry.go:291 +0x2b3
github.com/g3n/engine/experimental/collision.(*Raycaster).RaycastMesh(0xc00007a000, 0xc0001d6c00, 0xc0000dba88)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/experimental/collision/raycaster.go:345 +0x3d9
github.com/g3n/engine/experimental/collision.(*Raycaster).intersectObject(0xc00007a000, 0x44b7c80, 0xc0001d6c00, 0xc0000dba88, 0x1)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/experimental/collision/raycaster.go:116 +0x16f
github.com/g3n/engine/experimental/collision.(*Raycaster).intersectObject(0xc00007a000, 0x44b7900, 0xc0000fc000, 0xc0000dba88, 0x3f800001)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/experimental/collision/raycaster.go:125 +0x115
github.com/g3n/engine/experimental/collision.(*Raycaster).IntersectObjects(0xc00007a000, 0xc00106c040, 0x3, 0x4, 0x1, 0xc0000000b8, 0xb804006d9b, 0x41f5140)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/experimental/collision/raycaster.go:95 +0x9c
github.com/g3n/g3nd/demos/other.(*Raycast).onMouse(0xc0000100e0, 0xc0000a6460, 0x4372d80, 0xc00011e070)
    [...]/g3nd/demos/other/raycast.go:63 +0x11e
github.com/g3n/g3nd/demos/other.(*Raycast).Start.func1(0x442f122, 0xd, 0x4372d80, 0xc00011e070)
    [...]/g3nd/demos/other/raycast.go:46 +0x47
github.com/g3n/engine/core.(*Dispatcher).Dispatch(0xc00011e008, 0x442f122, 0xd, 0x4372d80, 0xc00011e070, 0x444a5c8)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/core/dispatcher.go:109 +0xbb
github.com/g3n/engine/window.Init.func3(0xc0001ea000, 0x0, 0x1, 0x0)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/window/glfw.go:325 +0x105
github.com/go-gl/glfw/v3.2/glfw.goMouseButtonCB(0x4f57740, 0x100000000, 0xc000000000)
    [...]/go/pkg/mod/github.com/go-gl/glfw@v0.0.0-20190409004039-e6da0acd62b1/v3.2/glfw/input.go:264 +0x6d
github.com/go-gl/glfw/v3.2/glfw._cgoexpwrap_e91ead9994e1_goMouseButtonCB(0x4f57740, 0x100000000, 0x3ed242ed00000000)
    _cgo_gotypes.go:2047 +0x45
github.com/go-gl/glfw/v3.2/glfw._Cfunc_glfwPollEvents()
    _cgo_gotypes.go:1135 +0x45
github.com/go-gl/glfw/v3.2/glfw.PollEvents()
    [...]/go/pkg/mod/github.com/go-gl/glfw@v0.0.0-20190409004039-e6da0acd62b1/v3.2/glfw/window.go:778 +0x25
github.com/g3n/engine/window.(*GlfwWindow).PollEvents(...)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/window/glfw.go:433
github.com/g3n/engine/app.(*Application).Run(0xc00007e300, 0xc0000dbf20)
    [...]/go/pkg/mod/github.com/g3n/engine@v0.1.1-0.20200214161420-db7282a2ba23/app/app-desktop.go:90 +0x96
github.com/g3n/g3nd/app.(*App).Run(0xc0000a6460)
    [...]/g3nd/app/app.go:614 +0xba
main.main()
    [...]/g3nd/main.go:24 +0x2a
Innoviox commented 3 years ago

I'm not sure how the GetMaterial method works. My two materials are start=0, count=30 and start=30, count=30. The logic in GetMaterial returns if:

if gmat.start >= vpos && gmat.start+gmat.count <= vpos

So, for material one, this will return if vpos is <= 0 but >= 30? and for the second if vpos is <= 30 but >= 60?

(for reference, this is the obj that I am trying to load: tile.zip)

Innoviox commented 3 years ago

I think line 210 of graphic.go should be switched to:

if gmat.start <= vpos && gmat.start+gmat.count >= vpos {

This now returns the right material. I'm going to submit a PR.