Example #1
0
	void SpriteBatch::draw(RenderTarget& target)
	{
		if ((!m_atlas && !m_texture) || m_objects.empty())
			return;

		static bool shaderLoaded = false;
		if (!shaderLoaded)
		{
			batchShader.LoadShader("Shaders/batchvertexshader.vert", "Shaders/batchfragmentshader.frag");
			shaderLoaded = true;
		}

		m_spriteBuffer.clear(true, false);
		m_spriteBuffer.addVertices(m_vertexData);

		for (size_t i = 0; i < m_vertexData.size() / 4; ++i)
		{
			const pmath::Mat4& m = m_objects[i]->GetTransform();

			m_spriteBuffer.m_vertexData[0 + (i * 4)].position *= m;
			m_spriteBuffer.m_vertexData[1 + (i * 4)].position *= m;
			m_spriteBuffer.m_vertexData[2 + (i * 4)].position *= m;
			m_spriteBuffer.m_vertexData[3 + (i * 4)].position *= m;
		}

		m_spriteBuffer.setData();

		m_atlas ? m_atlas->Bind() : m_texture->Bind();

		batchShader.Use();

		batchShader.SetUniform("unifProjection", target.GetCamera().GetProjectionTransform());
		batchShader.SetUniform("unifSampler", 0);
		//batchShader.SetUniform("unifColor", 1, 1, 1, 1);


		uth::Graphics::BindBuffer(ARRAY_BUFFER, m_spriteBuffer.m_arrayBuffer);
		batchShader.setAttributeData("attrPosition", 3, FLOAT_TYPE, sizeof(Vertex), (void*)0);
		batchShader.setAttributeData("attrUV", 2, FLOAT_TYPE, sizeof(Vertex), (void*)(3 * sizeof(float)));
		batchShader.setAttributeData("attrColor", 4, FLOAT_TYPE, sizeof(Vertex), (void*)(5 * sizeof(float)));

		uth::Graphics::BindBuffer(ELEMENT_ARRAY_BUFFER, m_spriteBuffer.m_elementBuffer);
		uth::Graphics::DrawElements(TRIANGLES, m_spriteBuffer.getIndices().size(), UNSIGNED_SHORT_TYPE, (void*)0);

		target.SetShader(nullptr);
	}