예제 #1
0
파일: system.go 프로젝트: james4k/exp
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()
	}
}
예제 #2
0
파일: main.go 프로젝트: james4k/gfx
func createTexture(r io.Reader) (gfx.Sampler, error) {
	img, err := png.Decode(r)
	if err != nil {
		return nil, err
	}

	rgbaImg, ok := img.(*image.NRGBA)
	if !ok {
		return nil, errors.New("texture must be an NRGBA image")
	}
	return gfx.Image(rgbaImg)
}