

RenderToSpriteExample = {}

function RenderToSpriteExample.start()
	local pageId = 1
	local models = loadModel("prof.gltf", pageId)
	RenderToSpriteExample.root = models[1]
	RenderToSpriteExample.meshes = modelFilter(models, MODEL_FILTER_MESH)

	for i = 1, #RenderToSpriteExample.meshes do
		setMeshTexture(RenderToSpriteExample.meshes[i], pageId)
		setMeshMicroShader(RenderToSpriteExample.meshes[i], "useTexture", 1.0, MICROSHADER_UNIFORM)
	end

	RenderToSpriteExample.renderSpriteFrame = allocSprite()
	RenderToSpriteExample.renderSpriteFrameData = {}
	RenderToSpriteExample.renderSpriteFrameData.color = vec4(0.0, 0.0, 0.0, 0.0)
	RenderToSpriteExample.renderSpriteFrameData.border = 1.0
	RenderToSpriteExample.renderSpriteFrameData.borderColor = vec4(255.0, 255.0, 255.0, 255.0)

	RenderToSpriteExample.renderSprite = allocSprite()
	RenderToSpriteExample.renderSpriteData = {}
	RenderToSpriteExample.renderSpriteData.position = vec3(220.0, 0.0, 0.0)
	RenderToSpriteExample.renderSpriteData.size = vec2(100.0, 100.0)
	RenderToSpriteExample.renderSpriteData.color = vec4(30.0, 30.0, 100.0, 255.0)
	setSprite(RenderToSpriteExample.renderSprite, RenderToSpriteExample.renderSpriteData)

	RenderToSpriteExample.angle = 0.0
	RenderToSpriteExample.fov = degToRad(60.0)
	RenderToSpriteExample.lastTime = getTime()
end

function drawScene()
	local cameraPos = vec3(0.0, 5.0, 10.0)
	local lookDir = vec3Normalize(cameraPos - vec3(0.0, 3.0, 0.0))
	local cameraMat = mat4LookAt(cameraPos, lookDir, vec3(0.0, 1.0, 0.0))
	setCamera(cameraMat, RenderToSpriteExample.fov)

	local lightDir = vec3Normalize(vec3(0.5, 1.0, 0.7))
	PolyUtil.beginLighting()
	for i = 1, #RenderToSpriteExample.meshes do
		PolyUtil.meshLight(RenderToSpriteExample.meshes[i], lightDir, vec4(255.0, 255.0, 255.0, 255.0), vec4(100.0, 100.0, 100.0, 255.0))
		drawMesh(RenderToSpriteExample.meshes[i])
	end
	PolyUtil.endLighting()
end

function RenderToSpriteExample.update()
	local currentTime = getTime()
	local dt = currentTime - RenderToSpriteExample.lastTime
	RenderToSpriteExample.lastTime = currentTime

	RenderToSpriteExample.angle = RenderToSpriteExample.angle + 45.0 * dt
	local modelMat = mat4()
	modelMat.rotation = eulerRot(0.0, RenderToSpriteExample.angle, 0.0)
	setModelMatrix(RenderToSpriteExample.root, modelMat)

	-- render regular scene
	drawScene()

	RenderToSpriteExample.renderSpriteFrameData.position = vec3(110.0, 50.0, 10.0)
	RenderToSpriteExample.renderSpriteFrameData.size = vec2(100.0, 100.0)
	setSprite(RenderToSpriteExample.renderSpriteFrame, RenderToSpriteExample.renderSpriteFrameData)
	drawSprite2D(RenderToSpriteExample.renderSpriteFrame)

	-- render scene to a sprite specifying the exact view region in pixels
	local rendSpriteViewPos = vec2(
		RenderToSpriteExample.renderSpriteFrameData.position.x, 
		RenderToSpriteExample.renderSpriteFrameData.position.y)

	local rendSpriteViewSize = RenderToSpriteExample.renderSpriteFrameData.size

	startRenderToSprite(RenderToSpriteExample.renderSprite, rendSpriteViewPos, rendSpriteViewSize)

	drawScene()
	drawSprite2D(RenderToSpriteExample.renderSprite)
end
