Example #1
0
func (s *System) Run(ctx *scene.Context) {
	s.init(ctx)
	defer s.finish()
	// TODO: take active camera node
	w := float32(s.Width)
	h := float32(s.Height)
	projM := mathgl.Ortho(0, w, h, 0, -30, 30)
	viewM := mathgl.Translate3D(0, 0, 0)
	worldM := mathgl.Translate3D(0, 0, 0)
	var rect struct {
		WVP     mathgl.Mat4f   `uniform:"WorldViewProjectionM"`
		Diffuse *gfx.Sampler2D `uniform:"Diffuse"`
		geomobj gfx.Geometry
		geom    gfx.GeometryLayout
	}
	rect.WVP = projM.Mul4(viewM).Mul4(worldM)
	s.cmds <- func() {
		quadbuffer := geometry.NewBuilder(s.shader.VertexFormat())
		quadbuffer.Clear()
		quadbuffer.P(0, 0, 0).UV(-1, -1).Cf(1, 1, 1, 1)
		quadbuffer.P(w/3, 0, 0).UV(-1, -1)
		quadbuffer.P(w/3, h/3, 0).UV(-1, -1)
		quadbuffer.P(0, h/3, 0).UV(-1, -1)
		quadbuffer.Indices(0, 1, 2, 2, 0, 3)
		rect.geomobj.Alloc(gfx.StaticDraw)
		err := rect.geomobj.CopyFrom(quadbuffer)
		if err != nil {
			panic(err)
		}
		s.shader.Use()
		rect.geom.Layout(&s.shader, &rect.geomobj)
		/*
			err = rect.geomobj.CopyFrom(quadbuffer)
			if err != nil {
				panic(err)
			}
		*/
		whiteImg := image.NewNRGBA(image.Rect(0, 0, 1, 1))
		whiteImg.Set(0, 0, color.White)
		white, err := gfx.Image(whiteImg)
		if err != nil {
			panic(err)
		}
		rect.Diffuse = white
	}

	for ctx.Step() {
		s.cmds <- func() {
			gl.Viewport(0, 0, s.Width, s.Height)
			gl.ClearColor(0, 0, 0, 1.0)
			gl.ClearDepth(1)
			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		}
		s.meshes.update(s.cmds, &s.shader)
		s.draw()
		s.sync()
	}
}
Example #2
0
func BenchmarkBuilderTinyVerts(b *testing.B) {
	bdr := geometry.NewBuilder(gfx.VertexPosition)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for q := 0; q < builderQuads; q++ {
			bdr.Position(0, 0, 0)
			bdr.Position(1, 0, 0)
			bdr.Position(1, 1, 0)
			bdr.Position(0, 1, 0)
			bdr.Indices(0, 1, 2, 2, 0, 3)
		}
	}
}
Example #3
0
func BenchmarkBuilderFatVerts(b *testing.B) {
	bdr := geometry.NewBuilder(gfx.VertexPosition | gfx.VertexColor |
		gfx.VertexTexcoord)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for q := 0; q < builderQuads; q++ {
			bdr.Position(0, 0, 0).Color(128, 0, 255, 255).Texcoord(0, 0)
			bdr.Position(1, 0, 0)
			bdr.Position(1, 1, 0)
			bdr.Position(0, 1, 0)
			bdr.Indices(0, 1, 2, 2, 0, 3)
		}
	}
}
Example #4
0
func (c *controlsys) Run(ctx *scene.Context) {
	var (
		ctls   controlColumn
		meshes graphics.MeshColumn
	)
	ctx.Stage(scene.DefaultStage - 1)
	ctx.Declare(&controlColumn{
		ctls:   map[scene.Ref]control{},
		meshes: map[scene.Ref]scene.Ref{},
		dirty:  map[scene.Ref]struct{}{},
	})
	ctx.Require(&ctls, &meshes)
	for ctx.Step() {
		dirty := make([]scene.Ref, len(ctls.dirty))
		for ref := range ctls.dirty {
			ctl := ctls.ctls[ref]
			meshref := ctls.meshes[ref]
			if meshref == 0 {
				meshref = ctx.Alloc(&meshes)
				gfx.DefaultVertexAttributes[gfx.VertexPosition] = "Position"
				gfx.DefaultVertexAttributes[gfx.VertexColor] = "Color"
				gfx.DefaultVertexAttributes[gfx.VertexTexcoord] = "UV"
				// diffuse = white texture
				// color = white, but by default i guess?
				meshes.Add(meshref, graphics.Mesh{
					G: graphics.Geometry{geometry.NewBuilder(gfx.DefaultVertexAttributes.Format())},
					M: graphics.M().Diffuse(0),
				})
				ctls.meshes[ref] = meshref
			}
			g := meshes.ModifyGeom(meshref)
			buildRect(g, ctl.Rect)
			dirty = append(dirty, ref)
		}
		for _, ref := range dirty {
			delete(ctls.dirty, ref)
		}
	}
}