Esempio n. 1
0
func (n nativeObject) rebuild(o *gfx.Object, c *gfx.Camera) nativeObject {
	// The "Model" matrix is the Object's transformation matrix, we feed it
	// directly in.
	n.model = gfx.ConvertMat4(o.Transform.Mat4())

	// The "View" matrix is the coordinate system conversion, multiplied
	// against the camera object's transformation matrix
	view := zUpRightToYUpRight
	if c != nil {
		// Apply inverse of camera object transformation.
		camInverse, _ := c.Object.Transform.Mat4().Inverse()
		view = camInverse.Mul(view)
	}
	n.view = gfx.ConvertMat4(view)

	// The "Projection" matrix is the camera's projection matrix.
	projection := math.Mat4Identity
	if c != nil {
		projection = c.Projection.Mat4()
	}
	n.projection = gfx.ConvertMat4(projection)

	// The "MVP" matrix is Model * View * Projection matrix.
	mvp := o.Transform.Mat4()
	mvp = mvp.Mul(view)
	mvp = mvp.Mul(projection)
	n.mvp = gfx.ConvertMat4(mvp)
	return n
}
Esempio n. 2
0
// gfxLoop is responsible for drawing things to the window. This loop must be
// independent of the Chippy main loop.
func gfxLoop(w *chippy.Window, r gfx.Renderer) {
	w.SetSize(640, 640)
	w.SetPositionCenter(chippy.DefaultScreen())
	glr := r.(*gl2.Renderer)
	glr.UpdateBounds(image.Rect(0, 0, 640, 640))

	// Create a perspective viewing frustum matrix.
	width, height := 640, 640
	aspectRatio := float64(width) / float64(height)
	viewMat := gfx.ConvertMat4(math.Mat4Perspective(75.0, aspectRatio, 0.001, 1000.0))

	// Create a camera.
	camera := &gfx.Camera{
		Object:  new(gfx.Object),
		Frustum: viewMat,
	}
	_ = camera

	// Create a wireframe shader.
	shader := &gfx.Shader{
		Name:     "wireframe shader",
		GLSLVert: wireVert,
		GLSLFrag: wireFrag,
		Inputs:   make(map[string]interface{}),
	}

	// Wait for the shader to load (not strictly required).
	onLoad := make(chan *gfx.Shader, 1)
	r.LoadShader(shader, onLoad)
	<-onLoad
	if shader.Loaded {
		fmt.Println("Shader loaded")
	} else {
		fmt.Println(string(shader.Error))
	}

	// Create a triangle object.
	triangle := &gfx.Object{
		Shader: shader,
		State:  gfx.DefaultState,
		Meshes: []*gfx.Mesh{
			&gfx.Mesh{
				Vertices: []gfx.Vec3{
					// Top
					{0, .9, 0},
					{-.9, -.9, 0},
					{.9, -.9, 0},
				},
				Colors: []gfx.Color{
					// Top
					{1, 0, 0, 0},
					{0, 1, 0, 0},
					{0, 0, 1, 0},
				},
			},
		},
	}
	triangle.State.FaceCulling = gfx.NoFaceCulling

	octree := gfx.NewOctree()

	update := make(chan *gfx.Object)
	go func() {
		events := w.Events()
		for {
			e := <-events
			kev, ok := e.(keyboard.TypedEvent)
			if ok {
				if kev.Rune == 'f' || kev.Rune == 'b' {
					for i := 0; i < 100; i++ {
						s := mySpatial{
							aabb: randomAABB(0.1, 0.25),
						}
						if kev.Rune == 'b' {
							s = mySpatial{
								aabb: randomAABB(0.1, 0.5),
							}
						}
						//s.aabb.Min.Z = -.1
						//s.aabb.Max.Z = .1
						//octree = gfx.NewOctree()
						octree.Add(s)
						//fmt.Println(s.AABB().Center(), s.AABB())
					}

					// Create new mesh and ask the renderer to load it.
					newMesh := octreeMesh(octree)
					onLoad := make(chan *gfx.Mesh, 1)
					r.LoadMesh(newMesh, onLoad)
					<-onLoad

					// Take ownership of the triangle.
					<-update

					// Swap the mesh.
					triangle.Meshes[0] = newMesh

					// Give back ownership.
					update <- triangle
				}

				if kev.Rune == 'q' {
					// Take ownership of the triangle.
					<-update

					// Update rotation.
					v, ok := triangle.Shader.Inputs["rx"]
					rx := float32(0.0)
					if ok {
						rx = v.(float32)
					}
					rx += 0.1
					triangle.Shader.Inputs["rx"] = rx

					// Give back ownership.
					update <- triangle
				}

				if kev.Rune == 's' || kev.Rune == 'S' {
					fmt.Println("Writing screenshot to file...")
					// Download the image from the graphics hardware and save
					// it to disk.
					complete := make(chan image.Image, 1)
					r.Download(image.Rect(0, 0, 0, 0), complete)
					img := <-complete // Wait for download to complete.

					// Save to png.
					f, err := os.Create("screenshot.png")
					if err != nil {
						log.Fatal(err)
					}
					err = png.Encode(f, img)
					if err != nil {
						log.Fatal(err)
					}
					fmt.Println("Wrote texture to screenshot.png")
				}
			}

		}
	}()

	triangleDrawn := make(chan *gfx.Object, 1)
	for {
		// Clear the entire area (empty rectangle means "the whole area").
		r.Clear(image.Rect(0, 0, 0, 0), gfx.Color{1, 1, 1, 1})
		r.ClearDepth(image.Rect(0, 0, 0, 0), 1.0)

		// See if someone else needs ownership of the triangle before we draw.
		select {
		case update <- triangle:
			// Wait for them to give ownership back.
			<-update
		default:
		}

		// Draw the triangle to the screen.
		r.Draw(image.Rect(0, 0, 0, 0), triangle, triangleDrawn)

		// Render the whole frame.
		r.Render()

		select {
		case <-triangleDrawn:
			// Allow updates to the triangle if needed.
			select {
			case update <- triangle:
				<-update
			default:
			}
		}
	}
}