Selaa lähdekoodia

Update README.md hellog3n example

Daniel Salvadori 6 vuotta sitten
vanhempi
commit
105a8ae869
1 muutettua tiedostoa jossa 88 lisäystä ja 46 poistoa
  1. 88 46
      README.md

+ 88 - 46
README.md

@@ -86,58 +86,100 @@ Install the development files of OpenAL and Vorbis using [Homebrew](https://brew
 
   The code below is a basic "hello world" application 
   ([hellog3n](https://github.com/g3n/demos/tree/master/hellog3n))
-  that shows a blue torus.
-  You can download and install `hellog3n` via:
-    
-    go get -u github.com/g3n/demos/hellog3n
-
-  For more complex demos please see the [G3N demo program](https://github.com/g3n/g3nd).
+  that shows a blue torus and a button that when clicked makes the torus red:
 
   ```Go
-  package main
-
-  import (
-      "github.com/g3n/engine/util/application"
-      "github.com/g3n/engine/geometry"
-      "github.com/g3n/engine/material"
-      "github.com/g3n/engine/math32"
-      "github.com/g3n/engine/graphic"
-      "github.com/g3n/engine/light"
-  )
-
-  func main() {
-
-      app, _ := application.Create(application.Options{
-          Title:  "Hello G3N",
-          Width:  800,
-          Height: 600,
-      })
-
-      // Create a blue torus and add it to the scene
-      geom := geometry.NewTorus(1, .4, 12, 32, math32.Pi*2)
-      mat := material.NewPhong(math32.NewColor("DarkBlue"))
-      torusMesh := graphic.NewMesh(geom, mat)
-      app.Scene().Add(torusMesh)
-
-      // Add lights to the scene
-      ambientLight := light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.8)
-      app.Scene().Add(ambientLight)
-      pointLight := light.NewPoint(&math32.Color{1, 1, 1}, 5.0)
-      pointLight.SetPosition(1, 0, 2)
-      app.Scene().Add(pointLight)
-
-      // Add an axis helper to the scene
-      axis := graphic.NewAxisHelper(0.5)
-      app.Scene().Add(axis)
-
-      app.CameraPersp().SetPosition(0, 0, 3)
-      app.Run()
-  }
+package main
+
+import (
+	"github.com/g3n/engine/app"
+	"github.com/g3n/engine/camera"
+	"github.com/g3n/engine/camera/control"
+	"github.com/g3n/engine/core"
+	"github.com/g3n/engine/geometry"
+	"github.com/g3n/engine/gls"
+	"github.com/g3n/engine/graphic"
+	"github.com/g3n/engine/gui"
+	"github.com/g3n/engine/light"
+	"github.com/g3n/engine/material"
+	"github.com/g3n/engine/math32"
+	"github.com/g3n/engine/renderer"
+	"github.com/g3n/engine/window"
+	"time"
+)
+
+func main() {
+
+	// Create application and scene
+	a := app.App()
+	scene := core.NewNode()
+
+	// Set the scene to be managed by the gui manager
+	gui.Manager().Set(scene)
+
+	// Create perspective camera
+	cam := camera.NewPerspective(65, 1, 0.01, 1000)
+	cam.SetPosition(0, 0, 3)
+	scene.Add(cam)
+
+	// Set up orbit control for the camera
+	control.NewOrbitControl(cam)
+
+	// Set up callback to update viewport and camera aspect ratio when the window is resized
+	onResize := func(evname string, ev interface{}) {
+		// Get framebuffer size and update viewport accordingly
+		width, height := a.GetSize()
+		a.Gls().Viewport(0, 0, int32(width), int32(height))
+		// Update the camera's aspect ratio
+		cam.SetAspect(float32(width) / float32(height))
+	}
+	a.Subscribe(window.OnWindowSize, onResize)
+	onResize("", nil)
+
+	// Create a blue torus and add it to the scene
+	geom := geometry.NewTorus(1, .4, 12, 32, math32.Pi*2)
+	mat := material.NewPhong(math32.NewColor("DarkBlue"))
+	mesh := graphic.NewMesh(geom, mat)
+	scene.Add(mesh)
+
+	// Create and add a button to the scene
+	btn := gui.NewButton("Make Red")
+	btn.SetPosition(100, 40)
+	btn.SetSize(40, 40)
+	btn.Subscribe(gui.OnClick, func(name string, ev interface{}) {
+		mat.SetColor(math32.NewColor("DarkRed"))
+	})
+	scene.Add(btn)
+
+	// Create and add lights to the scene
+	scene.Add(light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.8))
+	pointLight := light.NewPoint(&math32.Color{1, 1, 1}, 5.0)
+	pointLight.SetPosition(	1, 0, 2)
+	scene.Add(pointLight)
+
+	// Create and add an axis helper to the scene
+	scene.Add(graphic.NewAxisHelper(0.5))
+
+	// Set background color to gray
+	a.Gls().ClearColor(0.5, 0.5, 0.5, 1.0)
+
+	// Run the application
+	a.Run(func(renderer *renderer.Renderer, deltaTime time.Duration) {
+		a.Gls().Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
+		renderer.Render(scene, cam)
+	})
+}
   ```
 
   <p align="center">
     <img style="float: right;" src="https://github.com/g3n/demos/blob/master/hellog3n/screenshot.png" alt="hellog3n Screenshot"/>
-  </p>
+  </p>    
+  
+  You can download and install `hellog3n` via:
+    
+    go get -u github.com/g3n/demos/hellog3n
+
+  For more complex demos please see the [G3N demo program](https://github.com/g3n/g3nd).
 
   ## Documentation