Skip to content

Instantly share code, notes, and snippets.

@eliquious
Created November 2, 2020 05:24
Show Gist options
  • Save eliquious/db7597a9c440f764463062ab4270f08f to your computer and use it in GitHub Desktop.
Save eliquious/db7597a9c440f764463062ab4270f08f to your computer and use it in GitHub Desktop.
G3N testing
package main
import (
"time"
"github.com/g3n/engine/app"
"github.com/g3n/engine/camera"
"github.com/g3n/engine/core"
"github.com/g3n/engine/gls"
"github.com/g3n/engine/light"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/renderer"
"github.com/g3n/engine/util/logger"
"github.com/g3n/engine/window"
)
func main() {
a := new(App)
a.Application = app.App()
// Creates application logger
a.log = logger.New("G3ND", nil)
a.log.AddWriter(logger.NewConsole(false))
a.log.SetFormat(logger.FTIME | logger.FMICROS)
a.log.SetLevel(logger.DEBUG)
// Create scenes
a.scene = core.NewNode()
// Create camera and orbit control
width, height := a.GetSize()
aspect := float32(width) / float32(height)
a.camera = camera.New(aspect)
a.scene.Add(a.camera) // Add camera to scene (important for audio demos)
a.orbit = camera.NewOrbitControl(a.camera)
// Create and add ambient light to scene
a.ambLight = light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.5)
a.scene.Add(a.ambLight)
// Sets the default window resize event handler
a.Subscribe(window.OnWindowSize, func(evname string, ev interface{}) { a.OnWindowResize() })
a.OnWindowResize()
// Subscribe to key events
a.Subscribe(window.OnKeyDown, func(evname string, ev interface{}) {
kev := ev.(*window.KeyEvent)
if kev.Key == window.KeyEscape { // ESC terminates the program
a.Exit()
}
})
// Setup application
a.Setup()
// Run application
a.Run()
}
// App contains the application state
type App struct {
*app.Application // Embedded standard application object
log *logger.Logger // Application logger
scene *core.Node // Scene rendered
// demoScene *core.Node // Scene populated by individual demos
ambLight *light.Ambient // Scene ambient light
// Camera and orbit control
camera *camera.Camera // Camera
orbit *camera.OrbitControl // Orbit control
// Terrain model
terrain *TerrainModel
}
// Setup sets up the scene
func (a *App) Setup() {
// Destroy all objects in demo scene and GUI
a.scene.DisposeChildren(true)
// Clear subscriptions with ID (every subscribe called by demos should use the app address as ID so we can unsubscribe here)
a.UnsubscribeAllID(a)
// Clear all custom cursors and reset current cursor
a.DisposeAllCustomCursors()
a.SetCursor(window.ArrowCursor)
// Set default background color
a.Gls().ClearColor(0.6, 0.6, 0.6, 1.0)
// Reset renderer z-sorting flag
a.Renderer().SetObjectSorting(true)
// Reset ambient light
a.ambLight.SetColor(&math32.Color{1.0, 1.0, 1.0})
a.ambLight.SetIntensity(0.5)
// Reset camera
a.camera.SetPosition(0, 0, 5)
a.camera.UpdateSize(5)
a.camera.LookAt(&math32.Vector3{0, 0, 0}, &math32.Vector3{0, 1, 0})
a.camera.SetProjection(camera.Perspective)
// Set up orbit control for the camera
// camera.NewOrbitControl(a.camera)
a.orbit.Reset()
a.terrain = &TerrainModel{}
a.terrain.Start(a)
}
// Run runs the application render loop
func (a *App) Run() {
a.Application.Run(a.Update)
}
// Update updates the scene
func (a *App) Update(rend *renderer.Renderer, deltaTime time.Duration) {
// Clear the color, depth, and stencil buffers
a.Gls().Clear(gls.COLOR_BUFFER_BIT | gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT) // TODO maybe do inside renderer, and allow customization
// Update the current running demo if any
if a.terrain != nil {
a.terrain.Update(a, deltaTime)
}
// Render scene
err := rend.Render(a.scene, a.camera)
if err != nil {
panic(err)
}
// Update GUI timers
// gui.Manager().TimerManager.ProcessTimers()
}
// AmbLight returns the default scene ambient light
func (a *App) AmbLight() *light.Ambient {
return a.ambLight
}
// Log returns the application logger
func (a *App) Log() *logger.Logger {
return a.log
}
// Scene returns the current application 3D scene
func (a *App) Scene() *core.Node {
return a.scene
}
// Camera returns the current application camera
func (a *App) Camera() *camera.Camera {
return a.camera
}
// Orbit returns the current camera orbit control
func (a *App) Orbit() *camera.OrbitControl {
return a.orbit
}
// OnWindowResize is default handler for window resize events.
func (a *App) OnWindowResize() {
// Get framebuffer size and set the viewport accordingly
width, height := a.GetFramebufferSize()
a.Gls().Viewport(0, 0, int32(width), int32(height))
// Set camera aspect ratio
a.camera.SetAspect(float32(width) / float32(height))
}
package main
import (
"time"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/util/helper"
)
// TerrainModel is your test object. You can store state here.
// By convention and to avoid conflict with other demo/tests name it
// using your test category and name.
type TerrainModel struct {
grid *helper.Grid // Pointer to a GridHelper created in 'Start'
}
// Start will be called once when the test is selected from the G3ND list.
// 'a' is a pointer to the G3ND application.
// It allows access to several methods such as a.Scene(), which returns the current scene,
// a.DemoPanel(), a.Camera(), a.Window() among others.
// You can build your scene adding your objects to the a.Scene()
func (t *TerrainModel) Start(a *App) {
// Show axis helper
ah := helper.NewAxes(1.0)
a.Scene().Add(ah)
// Creates a grid helper and saves its pointer in the test state
t.grid = helper.NewGrid(50, 1, &math32.Color{0.4, 0.4, 0.4})
a.Scene().Add(t.grid)
// Changes the camera position
a.Camera().SetPosition(0, 4, 10)
a.Camera().LookAt(math32.NewVector3(0, 0, 0), math32.NewVector3(0, 1, 0))
}
// Update method will be called at every frame
// You can animate your objects here.
func (t *TerrainModel) Update(a *App, deltaTime time.Duration) {
// Rotate the grid, just for show.
// rps := float32(deltaTime.Seconds()) * 2 * math32.Pi
// t.grid.RotateY(rps * 0.05)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment