Exemple #1
0
func main() {
	app := example.Open()
	defer app.Close()
	bgfx.Init()
	defer bgfx.Shutdown()
	bgfx.Reset(app.Width, app.Height, bgfx.ResetVSync)
	bgfx.SetDebug(bgfx.DebugText)
	bgfx.SetViewClear(
		0,
		bgfx.ClearColor|bgfx.ClearDepth,
		0x303030ff,
		1.0,
		0,
	)
	for app.Continue() {
		bgfx.SetViewRect(0, 0, 0, app.Width, app.Height)
		bgfx.Submit(0)
		bgfx.DebugTextClear()
		bgfx.DebugTextPrintf(0, 1, 0x4f, app.Title)
		bgfx.DebugTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text.")
		bgfx.Frame()
	}
}
Exemple #2
0
func main() {
	app := example.Open()
	defer app.Close()
	bgfx.Init()
	defer bgfx.Shutdown()

	bgfx.Reset(app.Width, app.Height, bgfx.ResetVSync)
	bgfx.SetDebug(bgfx.DebugText)
	bgfx.SetViewClear(
		0,
		bgfx.ClearColor|bgfx.ClearDepth,
		0x303030ff,
		1.0,
		0,
	)
	bgfx.SetViewRect(0, 0, 0, app.Width, app.Height)
	bgfx.Submit(0)

	var vd bgfx.VertexDecl
	vd.Begin()
	vd.Add(bgfx.AttribPosition, 3, bgfx.AttribTypeFloat, false, false)
	vd.Add(bgfx.AttribColor0, 4, bgfx.AttribTypeUint8, true, false)
	vd.Add(bgfx.AttribTexcoord0, 2, bgfx.AttribTypeFloat, true, false)
	vd.End()

	uTime := bgfx.CreateUniform("u_time", bgfx.Uniform1f, 1)
	uMtx := bgfx.CreateUniform("u_mtx", bgfx.Uniform4x4fv, 1)
	uLightDir := bgfx.CreateUniform("u_lightDir", bgfx.Uniform3fv, 1)
	defer bgfx.DestroyUniform(uTime)
	defer bgfx.DestroyUniform(uMtx)
	defer bgfx.DestroyUniform(uLightDir)

	prog := assets.LoadProgram("vs_raymarching", "fs_raymarching")
	defer bgfx.DestroyProgram(prog)

	for app.Continue() {
		bgfx.DebugTextClear()
		bgfx.DebugTextPrintf(0, 1, 0x4f, app.Title)
		bgfx.DebugTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.")
		bgfx.DebugTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", app.DeltaTime*1000.0)

		var (
			eye = [3]float32{0, 0, -15.0}
			at  = [3]float32{0, 0, 0}
			up  = [3]float32{1, 0, 0}
		)
		view := mat4.LookAtLH(eye, at, up)
		proj := mat4.PerspectiveLH(
			cgm.ToRadians(60),
			float32(app.Width)/float32(app.Height),
			0.1, 100.0,
		)
		bgfx.SetViewRect(0, 0, 0, app.Width, app.Height)
		bgfx.SetViewTransform(0, [16]float32(view), [16]float32(proj))
		bgfx.Submit(0)

		ortho := mat4.OrthoLH(0, float32(app.Width), float32(app.Height), 0, 0, 100)
		bgfx.SetViewRect(1, 0, 0, app.Width, app.Height)
		bgfx.SetViewTransform(1, mat4.Identity(), ortho)

		viewProj := mat4.Mul(proj, view)
		mtx := mat4.RotateXYZ(
			cgm.Radians(app.Time),
			cgm.Radians(app.Time)*0.37,
			0,
		)
		invMtx := mat4.Inv(mtx)
		lightDirModel := [3]float32{-0.4, -0.5, -1.0}
		lightDirModelN := vec3.Normal(lightDirModel)
		lightDir := mat4.Mul4(invMtx,
			[4]float32{
				lightDirModelN[0],
				lightDirModelN[1],
				lightDirModelN[2],
				0,
			})
		invMvp := mat4.Inv(mat4.Mul(viewProj, mtx))

		bgfx.SetUniform(uTime, &app.Time, 1)
		bgfx.SetUniform(uLightDir, &lightDir, 1)
		bgfx.SetUniform(uMtx, &invMvp, 1)

		renderScreenSpaceQuad(1, prog, vd, 0, 0, float32(app.Width), float32(app.Height))

		bgfx.Frame()
	}
}
Exemple #3
0
func main() {
	app := example.Open()
	defer app.Close()
	bgfx.Init()
	defer bgfx.Shutdown()

	bgfx.Reset(app.Width, app.Height, bgfx.ResetVSync)
	bgfx.SetDebug(bgfx.DebugText)
	bgfx.SetViewClear(
		0,
		bgfx.ClearColor|bgfx.ClearDepth,
		0x303030ff,
		1.0,
		0,
	)

	instancingSupported := bgfx.Caps().Supported&bgfx.CapsInstancing != 0

	var vd bgfx.VertexDecl
	vd.Begin()
	vd.Add(bgfx.AttribPosition, 3, bgfx.AttribTypeFloat, false, false)
	vd.Add(bgfx.AttribNormal, 4, bgfx.AttribTypeUint8, true, true)
	vd.Add(bgfx.AttribTangent, 4, bgfx.AttribTypeUint8, true, true)
	vd.Add(bgfx.AttribTexcoord0, 2, bgfx.AttribTypeInt16, true, true)
	vd.End()
	example.CalculateTangents(vertices, len(vertices), vd, indices)

	vb := bgfx.CreateVertexBuffer(vertices, vd)
	defer bgfx.DestroyVertexBuffer(vb)
	ib := bgfx.CreateIndexBuffer(indices)
	defer bgfx.DestroyIndexBuffer(ib)

	const numLights = 4
	uTexColor := bgfx.CreateUniform("u_texColor", bgfx.Uniform1iv, 1)
	uTexNormal := bgfx.CreateUniform("u_texNormal", bgfx.Uniform1iv, 1)
	uLightPosRadius := bgfx.CreateUniform("u_lightPosRadius", bgfx.Uniform4fv, numLights)
	uLightRgbInnerR := bgfx.CreateUniform("u_lightRgbInnerR", bgfx.Uniform4fv, numLights)

	vsbump := "vs_bump"
	if instancingSupported {
		vsbump = "vs_bump_instanced"
	}
	prog := assets.LoadProgram(vsbump, "fs_bump")
	defer bgfx.DestroyProgram(prog)

	textureColor := assets.LoadTexture("fieldstone-rgba.dds", 0)
	textureNormal := assets.LoadTexture("fieldstone-n.dds", 0)

	for app.Continue() {
		var (
			eye = [3]float32{0, 0, -7.0}
			at  = [3]float32{0, 0, 0}
			up  = [3]float32{1, 0, 0}
		)
		view := mat4.LookAtLH(eye, at, up)
		proj := mat4.PerspectiveLH(
			cgm.ToRadians(60),
			float32(app.Width)/float32(app.Height),
			0.1, 100.0,
		)
		bgfx.SetViewTransform(0, view, proj)
		bgfx.SetViewRect(0, 0, 0, app.Width, app.Height)
		bgfx.DebugTextClear()
		bgfx.DebugTextPrintf(0, 1, 0x4f, app.Title)
		bgfx.DebugTextPrintf(0, 2, 0x6f, "Description: Loading textures.")
		bgfx.DebugTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", app.DeltaTime*1000.0)
		bgfx.Submit(0)

		const halfPi = math.Pi / 2
		var lightPosRadius [4][4]float32
		for i := 0; i < numLights; i++ {
			fi := float32(i)
			lightPosRadius[i][0] = float32(math.Sin(float64(app.Time*(0.1+fi*0.17)+fi*halfPi*1.37)) * 3.0)
			lightPosRadius[i][1] = float32(math.Cos(float64(app.Time*(0.2+fi*0.29)+fi*halfPi*1.49)) * 3.0)
			lightPosRadius[i][2] = -2.5
			lightPosRadius[i][3] = 3.0
		}
		bgfx.SetUniform(uLightPosRadius, &lightPosRadius, numLights)

		lightRgbInnerR := [4][4]float32{
			{1.0, 0.7, 0.2, 0.8},
			{0.7, 0.2, 1.0, 0.8},
			{0.2, 1.0, 0.7, 0.8},
			{1.0, 0.4, 0.2, 0.8},
		}
		bgfx.SetUniform(uLightRgbInnerR, &lightRgbInnerR, numLights)

		if instancingSupported {
			for y := 0; y < 3; y++ {
				const numInstances = 3
				const instanceStride = 64
				idb := bgfx.AllocInstanceDataBuffer(numInstances, instanceStride)
				for x := 0; x < 3; x++ {
					mtx := mat4.RotateXYZ(
						cgm.Radians(app.Time)*0.023+cgm.Radians(x)*0.21,
						cgm.Radians(app.Time)*0.03+cgm.Radians(y)*0.37,
						0,
					)
					mtx[12] = -3 + float32(x)*3
					mtx[13] = -3 + float32(y)*3
					mtx[14] = 0
					binary.Write(&idb, binary.LittleEndian, mtx)
				}
				bgfx.SetInstanceDataBuffer(idb)
				bgfx.SetProgram(prog)
				bgfx.SetVertexBuffer(vb)
				bgfx.SetIndexBuffer(ib)
				bgfx.SetTexture(0, uTexColor, textureColor)
				bgfx.SetTexture(1, uTexNormal, textureNormal)
				bgfx.SetState(bgfx.StateDefault & (^bgfx.StateCullMask))
				bgfx.Submit(0)
			}
		} else {
			for y := 0; y < 3; y++ {
				for x := 0; x < 3; x++ {
					mtx := mat4.RotateXYZ(
						cgm.Radians(app.Time)*0.023+cgm.Radians(x)*0.21,
						cgm.Radians(app.Time)*0.03+cgm.Radians(y)*0.37,
						0,
					)
					mtx[12] = -3 + float32(x)*3
					mtx[13] = -3 + float32(y)*3
					mtx[14] = 0

					bgfx.SetTransform([16]float32(mtx))
					bgfx.SetProgram(prog)
					bgfx.SetVertexBuffer(vb)
					bgfx.SetIndexBuffer(ib)
					bgfx.SetTexture(0, uTexColor, textureColor)
					bgfx.SetTexture(1, uTexNormal, textureNormal)
					bgfx.SetState(bgfx.StateDefault & (^bgfx.StateCullMask))
					bgfx.Submit(0)
				}
			}
		}
		bgfx.Frame()
	}
}
Exemple #4
0
func main() {
	app := example.Open()
	defer app.Close()
	bgfx.Init()
	defer bgfx.Shutdown()

	bgfx.Reset(app.Width, app.Height, bgfx.ResetVSync)
	bgfx.SetDebug(bgfx.DebugText)
	bgfx.SetViewClear(
		0,
		bgfx.ClearColor|bgfx.ClearDepth,
		0x303030ff,
		1.0,
		0,
	)

	var vd bgfx.VertexDecl
	vd.Begin()
	vd.Add(bgfx.AttribPosition, 3, bgfx.AttribTypeFloat, false, false)
	vd.Add(bgfx.AttribColor0, 4, bgfx.AttribTypeUint8, true, false)
	vd.End()
	vb := bgfx.CreateVertexBuffer(vertices, vd)
	defer bgfx.DestroyVertexBuffer(vb)
	ib := bgfx.CreateIndexBuffer(indices)
	defer bgfx.DestroyIndexBuffer(ib)
	prog := assets.LoadProgram("vs_instancing", "fs_instancing")
	defer bgfx.DestroyProgram(prog)

	caps := bgfx.Caps()
	for app.Continue() {
		var (
			eye = [3]float32{0, 0, -35.0}
			at  = [3]float32{0, 0, 0}
			up  = [3]float32{1, 0, 0}
		)
		view := mat4.LookAtLH(eye, at, up)
		proj := mat4.PerspectiveLH(
			cgm.ToRadians(60),
			float32(app.Width)/float32(app.Height),
			0.1, 100,
		)
		bgfx.SetViewTransform(0, view, proj)
		bgfx.SetViewRect(0, 0, 0, app.Width, app.Height)
		bgfx.DebugTextClear()
		bgfx.DebugTextPrintf(0, 1, 0x4f, app.Title)
		bgfx.DebugTextPrintf(0, 2, 0x6f, "Description: Geometry instancing.")
		bgfx.DebugTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", app.DeltaTime*1000.0)
		bgfx.Submit(0)

		if caps.Supported&bgfx.CapsInstancing == 0 {
			color := uint8(0x01)
			if uint32(app.Time*2)&1 != 0 {
				color = 0x1f
			}
			bgfx.DebugTextPrintf(0, 5, color, " Instancing is not supported by GPU. ")
			bgfx.Frame()
			continue
		}

		const stride = 80
		idb := bgfx.AllocInstanceDataBuffer(11*11, stride)
		// Submit 11x11 cubes
		time64 := float64(app.Time)
		for y := 0; y < 11; y++ {
			for x := 0; x < 11; x++ {
				mtx := mat4.RotateXYZ(
					cgm.Radians(app.Time)+cgm.Radians(x)*0.21,
					cgm.Radians(app.Time)+cgm.Radians(y)*0.37,
					0,
				)
				mtx[12] = -15 + float32(x)*3
				mtx[13] = -15 + float32(y)*3
				mtx[14] = 0
				color := [4]float32{
					float32(math.Sin(time64+float64(x)/11.0)*0.5 + 0.5),
					float32(math.Cos(time64+float64(y)/11.0)*0.5 + 0.5),
					float32(math.Sin(time64*3.0)*0.5 + 0.5),
					1.0,
				}
				binary.Write(&idb, binary.LittleEndian, mtx)
				binary.Write(&idb, binary.LittleEndian, color)
			}
		}

		bgfx.SetProgram(prog)
		bgfx.SetVertexBuffer(vb)
		bgfx.SetIndexBuffer(ib)
		bgfx.SetInstanceDataBuffer(idb)
		bgfx.SetState(bgfx.StateDefault)
		bgfx.Submit(0)

		bgfx.Frame()
	}
}