コード例 #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 drawScene() {
	rotx += 0.5
	roty += 0.5

	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	rotmat := mathgl.HomogRotate3DX(rotx).Mul4(mathgl.HomogRotate3DY(roty))
	cube.worldM = mathgl.Translate3D(0, 0, 2).Mul4(rotmat)
	cube.WorldViewProjM = [16]float32(cube.projM.Mul4(cube.viewM).Mul4(cube.worldM))

	cube.shader.Use()
	cube.shader.SetUniforms(cube)
	cube.shader.SetGeometry(cube.geom)
	cube.shader.Draw()
}
コード例 #3
0
ファイル: main.go プロジェクト: james4k/gfx
func initScene() (err error) {
	gl.Enable(gl.DEPTH_TEST)

	gl.Viewport(0, 0, Width, Height)
	gl.ClearColor(0.5, 0.5, 0.5, 0.0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)

	cube.projM = mathgl.Ortho(-3, 3, -3, 3, -10.0, 10.0)
	cube.viewM = mathgl.Ident4f()
	cube.worldM = mathgl.Translate3D(0, 0, 2)
	cube.WorldViewProjM = [16]float32(cube.projM.Mul4(cube.viewM).Mul4(cube.worldM))

	goph, err := os.Open("gopher.png")
	if err != nil {
		panic(err)
	}
	defer goph.Close()

	// TODO: create cube geometry. could build out the vertex buffer manually, but this is a good place to do a mesh builder type thing.
	//cube.geom = ... mesh builder?

	gfx.DefaultVertexAttributes[gfx.VertexPosition] = "Position"
	gfx.DefaultVertexAttributes[gfx.VertexColor] = "Color"
	gfx.DefaultVertexAttributes[gfx.VertexTexcoord] = "UV"
	gfx.DefaultVertexAttributes[gfx.VertexNormal] = "Normal"
	vfmt := gfx.DefaultVertexAttributes.Format()
	builder := gfx.BuildGeometry(vfmt)
	builder.Position(-1, -1, 1).Texcoord(0, 0).Color(1, 1, 1).Normal(0, 0, 1)
	builder.Position(1, -1, 1).Texcoord(1, 0)
	builder.Position(1, 1, 1).Texcoord(1, 1)
	builder.Position(-1, 1, 1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	builder.Position(-1, -1, -1).Texcoord(0, 0).Normal(0, 0, -1)
	builder.Position(-1, 1, -1).Texcoord(1, 0)
	builder.Position(1, 1, -1).Texcoord(1, 1)
	builder.Position(1, -1, -1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	builder.Position(-1, 1, -1).Texcoord(0, 0).Normal(0, 1, 0)
	builder.Position(-1, 1, 1).Texcoord(1, 0)
	builder.Position(1, 1, 1).Texcoord(1, 1)
	builder.Position(1, 1, -1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	builder.Position(-1, -1, -1).Texcoord(0, 0).Normal(0, -1, 0)
	builder.Position(1, -1, -1).Texcoord(1, 0)
	builder.Position(1, -1, 1).Texcoord(1, 1)
	builder.Position(-1, -1, 1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	builder.Position(1, -1, -1).Texcoord(0, 0).Normal(1, 0, 0)
	builder.Position(1, 1, -1).Texcoord(1, 0)
	builder.Position(1, 1, 1).Texcoord(1, 1)
	builder.Position(1, -1, 1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	builder.Position(-1, -1, -1).Texcoord(0, 0).Normal(-1, 0, 0)
	builder.Position(-1, -1, 1).Texcoord(1, 0)
	builder.Position(-1, 1, 1).Texcoord(1, 1)
	builder.Position(-1, 1, -1).Texcoord(0, 1)
	builder.Indices(0, 1, 2, 2, 0, 3)
	cube.geom, err = gfx.NewGeometry(builder, gfx.StaticDraw)
	if err != nil {
		panic(err)
	}

	shader := gfx.BuildShader(gfx.DefaultVertexAttributes, vs, fs)
	cube.shader = shader
	cube.Diffuse, err = createTexture(goph)
	if err != nil {
		panic(err)
	}
	return
}