Example #1
0
File: main.go Project: chsc/g3
func render(engine g3.Engine) {
	gdev := engine.GetGraphicsDevice()
	gdev.Clear()

	gdev.SetMatrix(g3.MatrixModelView, &modelView)
	gdev.SetShader(mapShader)

	lpos := modelView.Transform(lightPos)
	mapShader.SetVec3(locLight, &lpos)
	mapShader.SetTexture(locStone, 0)
	mapShader.SetTexture(locGrass, 1)

	geoMipMap.Render(gdev, frustum)
}
Example #2
0
File: main.go Project: chsc/g3
func initialize(engine g3.Engine) os.Error {
	gdev := engine.GetGraphicsDevice()

	// Load and create heigh map
	hmap, err := geo.NewHeightMapFromImageFile("../../../data/heightmaps/map1.png")
	if err != nil {
		return err
	}
	geoMipMap = geo.NewGeoMipMap(gdev, hmap, 3, 32, 32, 5, 0.01, 0.3)

	// Load and compile shader
	sources, err := g3.ReadStringsFromFiles("../../../data/shaders/map.vs.glsl", "../../../data/shaders/map.fs.glsl")
	if err != nil {
		return err
	}
	mapShader = gdev.NewShader(sources[0], sources[1])
	gdev.SetShader(mapShader)

	// Setup textures
	images, err := g3.ReadImagesFromFiles("../../../data/textures/stone.jpg", "../../../data/textures/grass.jpg")
	if err != nil {
		return err
	}
	texStone = gdev.NewTexture2D(images[0])
	texGrass = gdev.NewTexture2D(images[1])
	gdev.SetTexture2D(texStone, 0)
	gdev.SetTexture2D(texGrass, 1)

	// Setup texture sampler
	locStone = mapShader.GetUniformLocation("textureStone")
	locGrass = mapShader.GetUniformLocation("textureGrass")

	// Setup light position
	locLight = mapShader.GetUniformLocation("lightPos")

	//fmt.Println("locs:", locStone, locGrass, locLight)

	// Setup projection matrix
	projection = g3.MakePerspectiveMatrix(45.0, 640.0/480.0, 0.001, 100.0)
	gdev.SetMatrix(g3.MatrixProjection, &projection)

	return nil
}
Example #3
0
File: main.go Project: chsc/g3
func multiplexEvents(engine g3.Engine) {
	for {
		select {
		case me := <-engine.MouseEventChan():
			alpha += float32(-me.Dx) / 100
			beta += float32(-me.Dy) / 100
			beta = g3.Clamp(beta, -g3.Pi/2, g3.Pi/2)
			ud := g3.MakeXRotationMatrix(beta)
			lr := g3.MakeZRotationMatrix(alpha)
			m := lr.Multiply(&ud)
			dir = m.Transform(dirBase)
			up = m.Transform(upBase)
			left = m.Transform(leftBase)
		case ke := <-engine.KeyEventChan():
			//fmt.Println(ke)
			if ke.Type == g3.KeyPressed {
				switch ke.Key {
				case g3.KeyW:
					speed += 0.0001
				case g3.KeyS:
					speed -= 0.0001
				case g3.KeyA:
					pos.Accumulate(left.Scaled(0.01))
				case g3.KeyD:
					pos.Accumulate(left.Scaled(-0.01))
				case g3.KeyF1:
					if wireframe {
						engine.GetGraphicsDevice().SetFillMode(g3.FillSolid)
					} else {
						engine.GetGraphicsDevice().SetFillMode(g3.FillWireFrame)
					}
					wireframe = !wireframe
				case g3.KeyPageUp:
					pos.Z += 0.01
				case g3.KeyPageDown:
					pos.Z -= 0.01
				}
			}
		case fe := <-engine.FrameEventChan():
			update(engine, fe.DeltaTime)
			render(engine)
			engine.SwapBuffers()
		case se := <-engine.SystemEventChan():
			//fmt.Println(se)
			if se.Type == g3.SystemQuit {
				return
			}
		}
	}
}