main.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This is a minimum G3N application showing how to create a window,
  5. // a scene, add some 3D objects to the scene and render it.
  6. // For more complete demos please see: https://github.com/g3n/g3nd
  7. package main
  8. import (
  9. "github.com/g3n/engine/camera"
  10. "github.com/g3n/engine/core"
  11. "github.com/g3n/engine/geometry"
  12. "github.com/g3n/engine/gls"
  13. "github.com/g3n/engine/graphic"
  14. "github.com/g3n/engine/light"
  15. "github.com/g3n/engine/material"
  16. "github.com/g3n/engine/math32"
  17. "github.com/g3n/engine/renderer"
  18. "github.com/g3n/engine/window"
  19. "math"
  20. "runtime"
  21. )
  22. func main() {
  23. // Creates window and OpenGL context
  24. win, err := window.New("glfw", 800, 600, "Hello G3N", false)
  25. if err != nil {
  26. panic(err)
  27. }
  28. // OpenGL functions must be executed in the same thread where
  29. // the context was created (by window.New())
  30. runtime.LockOSThread()
  31. // Create OpenGL state
  32. gs, err := gls.New()
  33. if err != nil {
  34. panic(err)
  35. }
  36. // Creates scene for 3D objects
  37. scene := core.NewNode()
  38. // Adds white ambient light to the scene
  39. ambLight := light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.5)
  40. scene.Add(ambLight)
  41. // Adds a perspective camera to the scene
  42. width, height := win.GetSize()
  43. aspect := float32(width) / float32(height)
  44. camera := camera.NewPerspective(65, aspect, 0.01, 1000)
  45. camera.SetPosition(0, 0, 5)
  46. // Add an axis helper
  47. axis := graphic.NewAxisHelper(2)
  48. scene.Add(axis)
  49. // Creates a wireframe sphere positioned at the center of the scene
  50. geom := geometry.NewSphere(2, 16, 16, 0, math.Pi*2, 0, math.Pi)
  51. mat := material.NewStandard(math32.NewColor(1, 1, 1))
  52. mat.SetSide(material.SideDouble)
  53. mat.SetWireframe(true)
  54. sphere := graphic.NewMesh(geom, mat)
  55. scene.Add(sphere)
  56. // Creates a renderer and adds default shaders
  57. rend := renderer.NewRenderer(gs)
  58. err = rend.AddDefaultShaders()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Sets window background color
  63. gs.ClearColor(0, 0, 0, 1.0)
  64. // Render loop
  65. for !win.ShouldClose() {
  66. // Clear buffers
  67. gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
  68. // Rotates the sphere a bit around the Z axis (up)
  69. sphere.AddRotationY(0.005)
  70. // Render the scene using the specified camera
  71. rend.Render(scene, camera)
  72. // Update window and checks for I/O events
  73. win.SwapBuffers()
  74. win.PollEvents()
  75. }
  76. }