

MeshExample = {}

function MeshExample.start()
	local h = 0.5

	-- 6 faces, 4 vertices each
	local verts = {
		-- front (+Z)
		vec3(-h, -h, h), vec3(h, -h, h), vec3(h, h, h), vec3(-h, h, h),
		-- back (-Z)
		vec3(h, -h, -h), vec3(-h, -h, -h), vec3(-h, h, -h), vec3(h, h, -h),
		-- right (+X)
		vec3(h, -h, h), vec3(h, -h, -h), vec3(h, h, -h), vec3(h, h, h),
		-- left (-X)
		vec3(-h, -h, -h), vec3(-h, -h, h), vec3(-h, h, h), vec3(-h, h, -h),
		-- top (+Y)
		vec3(-h, h, h), vec3(h, h, h), vec3(h, h, -h), vec3(-h, h, -h),
		-- bottom (-Y)
		vec3(-h, -h, -h), vec3(h, -h, -h), vec3(h, -h, h), vec3(-h, -h, h),
	}

	-- per-face base colors
	local faceColors = {
		Config.palette[5],
		Config.palette[6],
		Config.palette[9],
		Config.palette[11],
		Config.palette[17],
		Config.palette[18],
	}

	MeshExample.baseColors = {}
	for face = 1, 6 do
		local c = faceColors[face]
		for v = 1, 4 do
			MeshExample.baseColors[(face - 1) * 4 + v] = c
		end
	end

	MeshExample.mesh = allocMesh()
	setMeshVertices(MeshExample.mesh, VERT_POSITION, verts)

	computeNormals(MeshExample.mesh)

	setMeshVertices(MeshExample.mesh, VERT_COLORBASE, MeshExample.baseColors)

	-- each quad maps the full 0-1 UV range
	local uvs = {}
	for face = 1, 6 do
		local i = (face - 1) * 4
		uvs[i + 1] = vec2(0.0, 0.0)
		uvs[i + 2] = vec2(1.0, 0.0)
		uvs[i + 3] = vec2(1.0, 1.0)
		uvs[i + 4] = vec2(0.0, 1.0)
	end
	setMeshVertices(MeshExample.mesh, VERT_UV, uvs)
		
	local pageId = 1
	setMeshTexture(MeshExample.mesh, pageId, vec2(0.0, 0.0), vec2(0.5, 0.5))
	setMeshMicroShader(MeshExample.mesh, "useTexture", 1.0, MICROSHADER_UNIFORM)

	MeshExample.model = allocModel()
	setModelMesh(MeshExample.model, MeshExample.mesh)

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

function MeshExample.update()
	clearScreen(vec4(255.0, 255.0, 255.0, 255.0))

	local currentTime = getTime()
	local dt = currentTime - MeshExample.lastTime
	MeshExample.lastTime = currentTime

	MeshExample.angle = MeshExample.angle + 45.0 * dt
	MeshExample.colorTimer = MeshExample.colorTimer + dt

	-- dynamic vertex colors: per-face pulsing brightness
	local colors = {}
	for face = 1, 6 do
		local pulse = 0.7 + 0.3 * math.sin(MeshExample.colorTimer * 2.0 + face * 1.2)
		for v = 1, 4 do
			local idx = (face - 1) * 4 + v
			local base = MeshExample.baseColors[idx]
			colors[idx] = vec4(base.x * pulse, base.y * pulse, base.z * pulse, base.w)
		end
	end
	setMeshVertices(MeshExample.mesh, VERT_COLORBASE, colors)

	local modelMat = mat4()
	modelMat.rotation = eulerRot(0.0, MeshExample.angle, 0.0)
	setModelMatrix(MeshExample.model, modelMat)

	-- camera
	local cameraPos = vec3(0.0, 1.0, 3.0)
	local lookDir = vec3Normalize(cameraPos - vec3(0.0, 0.0, 0.0))
	local cameraMat = mat4LookAt(cameraPos, lookDir, vec3(0.0, 1.0, 0.0))
	setCamera(cameraMat, MeshExample.fov)

	-- lighting
	local lightDir = vec3Normalize(vec3(0.5, 1.0, 0.7))
	PolyUtil.meshLight(MeshExample.mesh, lightDir, vec4(255.0, 255.0, 255.0, 255.0), vec4(80.0, 80.0, 80.0, 255.0))

	PolyUtil.beginLighting()
	drawMesh(MeshExample.mesh)
	PolyUtil.endLighting()
end
