Exemple #1
0
func (r *Receiver) Init(window *glfw.Window) {
	r.Window = window
	r.LoadConfiguration("gameconf.json")
	gtk.Bind(&r.Data)
	r.Shaders = gtk.NewShaderLibrary()
	r.Shaders.LoadProgram(ProgramScene, "scene.v.glsl", "scene.f.glsl")
	r.Shaders.BindProgramLocations(ProgramScene, &r.SceneLoc)
	gtk.PanicOnError()

	r.Data.Projection = glm.Ident4d()
	r.Data.Cameraview = glm.Ident4d()

	r.Data.Scene = gtk.EmptyModel("root")
	model, err := gtk.LoadSceneAsModel("drivetrain.dae")
	if err != nil {
		panic(err)
	}
	r.Data.Scene.AddChild(model)
	r.Data.Car, _ = model.FindModelWithName("Car")
	grid := gtk.EmptyModel("Grid")
	grid.AddGeometry(gtk.Grid(100))
	r.Data.Scene.AddChild(grid)

	r.Car = sim.NewCar()
	r.ResetKeyBindingDefaults()

	r.UIState = UIState{
		sim.Controls{},
		glm.Vec4d{0, 1, 0, 0},
		glm.Vec4d{1, 0, 0, 0},
		glm.QuatIdentd(),
		10,
	}
}
Exemple #2
0
func (r *Receiver) Init(window *glfw.Window) {
	r.Window = window
	r.LoadConfiguration("gameconf.json")
	r.Invalid = true
	gtk.Bind(&r.Data)
	// var err error
	// err = gtk.LoadTexture(r.Data.Tex0, "tex4.png")
	// panicOnErr(err)
	// err = gtk.LoadTexture(r.Data.Tex1, "tex3.png")
	// panicOnErr(err)

	r.Shaders = gtk.NewShaderLibrary()
	r.Shaders.LoadProgram(PROGRAM_SCENE, "scene.v.glsl", "scene.f.glsl")
	r.Shaders.LoadProgram(PROGRAM_FILL, "fill.v.glsl", "fill.f.glsl")
	r.Shaders.BindProgramLocations(PROGRAM_SCENE, &r.SceneLoc)
	r.Shaders.BindProgramLocations(PROGRAM_FILL, &r.FillLoc)
	gtk.PanicOnError()

	r.Data.Projection = glm.Ident4d()
	r.Data.Cameraview = glm.Ident4d()
	r.Data.Inception = glm.Ident4d()

	r.Data.Scene = gtk.EmptyModel("root")
	r.LoadScene("portal.dae")

	quadElements := gtk.MakeElements(portal.QuadElements)
	r.Data.Scene.AddGeometry(NewPlane("plane1", portal.Quad{
		glm.Vec4d{0, 0, 0, 1},
		glm.Vec4d{0, 1, 0, 0},
		glm.Vec4d{1, 0, 0, 0},
		glm.Vec4d{10, 10, 1, 0},
	},
		quadElements,
	))
	r.Data.Fill = NewPlane("plane1", portal.Quad{
		glm.Vec4d{0, 0, 0, 1},
		glm.Vec4d{0, 0, 1, 0},
		glm.Vec4d{1, 0, 0, 0},
		glm.Vec4d{1, 1, 1, 0},
	},
		quadElements,
	)

	// r.Portals = append(r.Portals, CreatePortals()...)
	r.Data.Portal = gtk.EmptyModel("portals")
	for i, p := range r.Portals {
		r.Data.Portal.AddGeometry(NewPlane(fmt.Sprintf("portal_%d", i), p.EventHorizon, quadElements))
	}
	r.Player = Player{
		glm.Vec4d{0, 1, 0, 1},
		glm.Vec4d{0, 0, 0, 0},
		glm.Vec4d{0, 1, 0, 0},
		glm.Vec4d{1, 0, 0, 0},
		glm.QuatIdentd(),
		glm.QuatIdentd(),
	}
}
Exemple #3
0
func (r *Receiver) LoadScene(filename string) {
	doc, err := collada.LoadDocument(filename)
	panicOnErr(err)
	index, err := gtk.NewIndex(doc)
	panicOnErr(err)
	r.SceneIndex = index

	model := gtk.EmptyModel("scene")
	switch doc.Asset.UpAxis {
	case collada.Xup:
	case collada.Yup:
	case collada.Zup:
		model.Transform = glm.HomogRotate3DXd(-90).Mul4(glm.HomogRotate3DZd(90))
	}

	portalPattern, _ := regexp.Compile("^Portal_(\\d+)_(\\d+)")

	geometryTemplates := make(map[collada.Id][]*gtk.Geometry)
	for id, mesh := range r.SceneIndex.Mesh {
		geoms := make([]*gtk.Geometry, 0)
		for _, pl := range mesh.Polylist {
			matches := portalPattern.FindStringSubmatch(mesh.VerticesId)
			if matches == nil {
				elements := make([]*gtk.DrawElements, 0)
				drawElements := gtk.NewDrawElements(pl.TriangleElements, gl.TRIANGLES)
				if drawElements != nil {
					elements = append(elements, drawElements)
				}
				geometry := gtk.NewGeometry(string(id), pl.VertexData, pl.NormalData, elements)
				geoms = append(geoms, geometry)
			} else {
				fmt.Println("ignoring Portal")
			}
		}
		if len(geoms) > 0 {
			geometryTemplates[id] = geoms
		}
	}

	portalLink := map[int]int{}
	portals := map[int]portal.Quad{}
	for _, node := range r.SceneIndex.VisualScene.Node {
		matches := portalPattern.FindStringSubmatch(node.Name)
		transform := r.SceneIndex.Transforms[node.Id]
		if matches == nil {
			geoms := make([]*gtk.Geometry, 0)
			for _, geoinstance := range node.InstanceGeometry {
				geoid, _ := geoinstance.Url.Id()
				geoms = append(geoms, geometryTemplates[geoid]...)
			}
			if len(geoms) > 0 {
				child := gtk.NewModel(node.Name, []*gtk.Model{}, geoms, transform)
				model.AddChild(child)
			}
		} else {
			index, err := strconv.Atoi(matches[1])
			if err != nil {
				panic(err)
			}
			exit, err := strconv.Atoi(matches[2])
			if err != nil {
				panic(err)
			}

			mt := model.Transform.Mul4(transform)
			center := mt.Mul4x1(glm.Vec4d{0, 0, 0, 1})
			normal := mt.Mul4x1(glm.Vec4d{0, 0, 1, 0}).Normalize()
			planev := mt.Mul4x1(glm.Vec4d{1, 0, 0, 0}).Normalize()
			scale := glm.Vec4d{}
			n := 0
			for i := 0; i < 3; i++ {
				sum := 0.0
				for j := 0; j < 3; j++ {
					sum += float64(mt[n] * mt[n])
					n++
				}
				n++
				scale[i] = math.Sqrt(sum)
			}

			quad := portal.Quad{
				center,
				normal,
				planev,
				scale,
			}
			portalLink[index] = exit
			portals[index] = quad
			// fmt.Println("portal node", node.Name, quad)
		}
	}
	r.Data.Scene.AddChild(model)

	for id, quad := range portals {
		exit, ok := portals[portalLink[id]]
		if ok {
			up := portal.Cross3D(exit.Normal, exit.PlaneV)
			// fmt.Println("pre ",exit)
			exit = exit.Apply(glm.HomogRotate3Dd(math.Pi, up))
			// fmt.Println("post",exit)
			_, inverse, pva, _ := portal.PortalTransform(quad, exit)
			portal := portal.Portal{quad, inverse, pva}
			r.Portals = append(r.Portals, portal)
		} else {
			fmt.Println("no exit for portal", id, portalLink[id])
		}
	}
}