mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9014720f8c | |||
| cf0fe21e52 | |||
| b219ee830d |
28
build.sh
Executable file
28
build.sh
Executable file
@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
if [[ $# -ne 1 ]]; then
|
||||
echo -e "1) Build Debug mode\n2) Build Release mode"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
mode=$1
|
||||
if [[ $mode -eq 1 ]]; then
|
||||
|
||||
./switch-physx-mode.sh 1
|
||||
go build .
|
||||
echo "Debug build finished"
|
||||
|
||||
elif [[ $mode -eq 2 ]]; then
|
||||
|
||||
./switch-physx-mode.sh 2
|
||||
go build -tags "nmage_release,physx_release" .
|
||||
echo "Release build finished"
|
||||
|
||||
else
|
||||
|
||||
echo "Unknown build option. Please select 1 for a Debug build or 2 for a Release build"
|
||||
exit 1
|
||||
|
||||
fi
|
||||
@ -1,4 +1,4 @@
|
||||
//go:build !release
|
||||
//go:build !nmage_release
|
||||
|
||||
package consts
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
//go:build release
|
||||
//go:build nmage_release
|
||||
|
||||
package consts
|
||||
|
||||
|
||||
@ -172,6 +172,7 @@ func createWindow(title string, x, y, width, height int32, flags WindowFlags, re
|
||||
return nil, err
|
||||
}
|
||||
|
||||
win.SDLWin.GLSwap()
|
||||
return win, err
|
||||
}
|
||||
|
||||
|
||||
3
go.mod
3
go.mod
@ -9,5 +9,8 @@ require github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6
|
||||
require (
|
||||
github.com/bloeys/assimp-go v0.4.4
|
||||
github.com/bloeys/gglm v0.43.0
|
||||
github.com/bloeys/physx-go v0.2.0
|
||||
github.com/inkyblackness/imgui-go/v4 v4.6.0
|
||||
)
|
||||
|
||||
// replace github.com/bloeys/physx-go => ../physx-go
|
||||
|
||||
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ github.com/bloeys/assimp-go v0.4.4 h1:Yn5e/RpE0Oes0YMBy8O7KkwAO4R/RpgrZPJCt08dVI
|
||||
github.com/bloeys/assimp-go v0.4.4/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0=
|
||||
github.com/bloeys/gglm v0.43.0 h1:ZpOghR3PHfpkigTDh+FqxLsF0gN8CD6s/bWoei6LyxI=
|
||||
github.com/bloeys/gglm v0.43.0/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk=
|
||||
github.com/bloeys/physx-go v0.2.0 h1:zsjskoPFlH7m8hKnlCsJ4hVKe8ys2s1fLEhcNDfofS4=
|
||||
github.com/bloeys/physx-go v0.2.0/go.mod h1:YNt5tX4T8d/4RdldvpWc77DeHM4jqlIunUQUi/rqSKA=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8Ii9rROdgWxQuGoJ9WDXxfk=
|
||||
|
||||
30
main.go
30
main.go
@ -13,9 +13,11 @@ import (
|
||||
"github.com/bloeys/nmage/logging"
|
||||
"github.com/bloeys/nmage/materials"
|
||||
"github.com/bloeys/nmage/meshes"
|
||||
"github.com/bloeys/nmage/physics/physx"
|
||||
"github.com/bloeys/nmage/renderer/rend3dgl"
|
||||
"github.com/bloeys/nmage/timing"
|
||||
nmageimgui "github.com/bloeys/nmage/ui/imgui"
|
||||
"github.com/bloeys/physx-go/pgo"
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
@ -65,6 +67,7 @@ var (
|
||||
type OurGame struct {
|
||||
Win *engine.Window
|
||||
ImGUIInfo nmageimgui.ImguiInfo
|
||||
Px *physx.PhysX
|
||||
}
|
||||
|
||||
type TransformComp struct {
|
||||
@ -215,6 +218,33 @@ func (g *OurGame) Init() {
|
||||
//Lights
|
||||
simpleMat.SetUnifVec3("lightPos1", lightPos1)
|
||||
simpleMat.SetUnifVec3("lightColor1", lightColor1)
|
||||
|
||||
// Setup physx
|
||||
px, err := physx.NewPhysx(physx.PhysXCreationOptions{
|
||||
TypicalObjectLength: 1,
|
||||
TypicalObjectSpeed: 9.81,
|
||||
|
||||
EnableVisualDebugger: pgo.PvdSupported,
|
||||
VisualDebuggerHost: "127.0.0.1",
|
||||
VisualDebuggerPort: 5425,
|
||||
VisualDebuggerTimeoutMillis: 10_000,
|
||||
VisualDebuggerTransmitConstraints: true,
|
||||
VisualDebuggerTransmitContacts: true,
|
||||
VisualDebuggerTransmitSceneQueries: true,
|
||||
|
||||
SceneGravity: gglm.NewVec3(0, -9.81, 0),
|
||||
|
||||
SceneCPUDispatcherThreads: 2,
|
||||
SceneContactHandler: g.PhysxContactHandler,
|
||||
})
|
||||
if err != nil {
|
||||
logging.ErrLog.Fatalln("Failed to create PhysX. Err:", err)
|
||||
}
|
||||
g.Px = px
|
||||
}
|
||||
|
||||
func (g *OurGame) PhysxContactHandler(pgo.ContactPairHeader) {
|
||||
|
||||
}
|
||||
|
||||
func (g *OurGame) Update() {
|
||||
|
||||
86
physics/physx/physx.go
Executable file
86
physics/physx/physx.go
Executable file
@ -0,0 +1,86 @@
|
||||
package physx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
"github.com/bloeys/nmage/logging"
|
||||
"github.com/bloeys/physx-go/pgo"
|
||||
)
|
||||
|
||||
type PhysX struct {
|
||||
Foundation *pgo.Foundation
|
||||
Physics *pgo.Physics
|
||||
Scene *pgo.Scene
|
||||
}
|
||||
|
||||
type PhysXCreationOptions struct {
|
||||
|
||||
// Good defaults are length=1 (1m sizes), and speed=9.81 (speed of gravity)
|
||||
TypicalObjectLength float32
|
||||
// Good defaults are length=1 (1m sizes), and speed=9.81 (speed of gravity)
|
||||
TypicalObjectSpeed float32
|
||||
|
||||
// If EnableVisualDebugger=true then all VisualDebuggerXYZ variables must be set
|
||||
EnableVisualDebugger bool
|
||||
VisualDebuggerHost string
|
||||
// Default port is 5425
|
||||
VisualDebuggerPort int
|
||||
VisualDebuggerTimeoutMillis int
|
||||
VisualDebuggerTransmitConstraints bool
|
||||
VisualDebuggerTransmitContacts bool
|
||||
VisualDebuggerTransmitSceneQueries bool
|
||||
|
||||
SceneGravity *gglm.Vec3
|
||||
// Number of internal PhysX threads that do work.
|
||||
// If this is zero then all work is done on the thread that calls simulate
|
||||
SceneCPUDispatcherThreads uint32
|
||||
// Gets called when two objects collide
|
||||
SceneContactHandler func(cph pgo.ContactPairHeader)
|
||||
}
|
||||
|
||||
func NewPhysx(options PhysXCreationOptions) (px *PhysX, err error) {
|
||||
|
||||
if options.EnableVisualDebugger && !pgo.PvdSupported {
|
||||
return nil, errors.New("can not enable PhysX visual debugger (PVD) because physx-go is in release mode. Please build without the 'physx_release' tag")
|
||||
}
|
||||
|
||||
logging.InfoLog.Printf("Initializing PhysX. Worker threads: %d. PVD supported: %v\n", options.SceneCPUDispatcherThreads, pgo.PvdSupported)
|
||||
|
||||
// Setup foundation, pvd, and physics
|
||||
px = &PhysX{}
|
||||
px.Foundation = pgo.CreateFoundation()
|
||||
|
||||
ts := pgo.NewTolerancesScale(options.TypicalObjectLength, options.TypicalObjectSpeed)
|
||||
if options.EnableVisualDebugger {
|
||||
|
||||
pvdTr := pgo.DefaultPvdSocketTransportCreate(options.VisualDebuggerHost, options.VisualDebuggerPort, options.VisualDebuggerTimeoutMillis)
|
||||
pvd := pgo.CreatePvd(px.Foundation)
|
||||
if !pvd.Connect(pvdTr, pgo.PvdInstrumentationFlag_eALL) {
|
||||
return nil, errors.New("failed to connect to PhysX Visual Debugger. Is it running? Did you pass correct visual debugger host/port (default port is 5425)?")
|
||||
}
|
||||
|
||||
px.Physics = pgo.CreatePhysics(px.Foundation, ts, false, pvd)
|
||||
|
||||
} else {
|
||||
px.Physics = pgo.CreatePhysics(px.Foundation, ts, false, nil)
|
||||
}
|
||||
|
||||
// Setup scene
|
||||
sd := pgo.NewSceneDesc(ts)
|
||||
sd.SetGravity(pgo.NewVec3(options.SceneGravity.X(), options.SceneGravity.Y(), options.SceneGravity.Z()))
|
||||
sd.SetCpuDispatcher(pgo.DefaultCpuDispatcherCreate(options.SceneCPUDispatcherThreads, nil).ToCpuDispatcher())
|
||||
sd.SetOnContactCallback(options.SceneContactHandler)
|
||||
|
||||
px.Scene = px.Physics.CreateScene(sd)
|
||||
if options.EnableVisualDebugger {
|
||||
|
||||
scenePvdClient := px.Scene.GetScenePvdClient()
|
||||
scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_CONSTRAINTS, options.VisualDebuggerTransmitConstraints)
|
||||
scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_CONTACTS, options.VisualDebuggerTransmitContacts)
|
||||
scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_SCENEQUERIES, options.VisualDebuggerTransmitSceneQueries)
|
||||
scenePvdClient.Release()
|
||||
}
|
||||
|
||||
return px, nil
|
||||
}
|
||||
38
switch-physx-mode.sh
Executable file
38
switch-physx-mode.sh
Executable file
@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
if [[ $# -ne 1 ]]; then
|
||||
echo -e "1) Checked mode\n2) Release mode"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
mode=$1
|
||||
physxBinDir="../physx/physx/bin/win.x86_64.vc142.mt"
|
||||
physxCBinDir="../physx-c/x64"
|
||||
if [[ $mode -eq 1 ]]; then
|
||||
|
||||
physxCheckedBinDir="$physxBinDir/checked"
|
||||
cp "$physxCheckedBinDir/PhysX_64.dll" "$physxCheckedBinDir/PhysXCommon_64.dll" "$physxCheckedBinDir/PhysXFoundation_64.dll" .
|
||||
|
||||
physxCCheckedBinDir="$physxCBinDir/Checked"
|
||||
cp "$physxCCheckedBinDir/physx-c.dll" .
|
||||
|
||||
echo "Switched PhysX to Checked mode"
|
||||
|
||||
elif [[ $mode -eq 2 ]]; then
|
||||
|
||||
physxReleaseBinDir="$physxBinDir/release"
|
||||
cp "$physxReleaseBinDir/PhysX_64.dll" "$physxReleaseBinDir/PhysXCommon_64.dll" "$physxReleaseBinDir/PhysXFoundation_64.dll" .
|
||||
|
||||
physxCReleaseBinDir="$physxCBinDir/Release"
|
||||
cp "$physxCReleaseBinDir/physx-c.dll" .
|
||||
|
||||
echo "Switched PhysX to Release mode"
|
||||
|
||||
else
|
||||
|
||||
echo "Unknown mode. Please select 1 or 2"
|
||||
exit 1
|
||||
|
||||
fi
|
||||
Reference in New Issue
Block a user