unsigned int SpriteBatch_GL3::PushTexture(Texture *texture)
{
	// check if the texture is already in use
	// if so, return... we dont need to add it to our list of active txtures again
	for(unsigned int i = 0; i <= m_currentTextureID; i++)
	{
		if( m_activeTextures[i] == texture )
			return i;
	}

	// if we've used all the textures we can, than we need to flush to make room for another texture change
	if (m_currentTextureID >= m_maxTextures-1)
		FlushBatch();

	// add the texture to our active texture list
	m_activeTextures[m_currentTextureID] = texture;

	// tell the shader which texture to use.
	char texAttrib[32];
	sprintf_s(texAttrib, 32, "u_texture%i", m_currentTextureID);
	glUniform1i( glGetUniformLocation(m_shader, texAttrib), m_currentTextureID);

	glActiveTexture( GL_TEXTURE0 + m_currentTextureID );
	glBindTexture( GL_TEXTURE_2D, texture->GetTextureHandle() );
	glActiveTexture( GL_TEXTURE0 );

	// incronment current texture for the next PushTexture() to use.
	m_currentTextureID += 1;

	// return what the current texture was.
	return m_currentTextureID-1;
}
Example #2
0
	void SpriteBatch::Impl::End()
	{
		if (!m_insideBeginEnd)
			throw Exception("Begin must be called before End");

		PrepareForRendering();
		FlushBatch();
		CleanupRendering();

		m_insideBeginEnd = false;
	}
void SpriteBatch_GL3::End()
{
	if (m_processingRender == false)
		return;

	FlushBatch();

	glUseProgram(0);

	m_processingRender = false;

	unsigned int viewWidth	= m_pApplication->GetWindowWidth();
	unsigned int viewHeight = m_pApplication->GetWindowHeight();

	// resetup the projection and view
	//GetOrtho(m_projection, 0.0f, (float)viewWidth, (float)viewHeight, 0.0f, -1.0f, 100.0f);
	GetOrtho(m_projection, m_fCameraX, m_fCameraX + (float)viewWidth, m_fCameraY + (float)viewHeight, m_fCameraY, -1.0f, 100.0f);
}
void SpriteBatch_GL3::DrawSpriteTransformed3x3(Texture *texture, float *transformMat3x3, float width, float height, float xOrigin, float yOrigin)
{
	if( texture == NULL ) texture = m_nullTexture;

	if( ShouldFlush() ) FlushBatch();
	unsigned int textureID = PushTexture( texture );

	if( width  == 0.0f )	width  = (float)texture->GetWidth();
	if( height == 0.0f )	height = (float)texture->GetHeight();

	float tlX = (0.0f - xOrigin) * width;		float tlY = (0.0f - yOrigin) * height;
	float trX = (1.0f - xOrigin) * width;		float trY = (0.0f - yOrigin) * height;
	float brX = (1.0f - xOrigin) * width;		float brY = (1.0f - yOrigin) * height;
	float blX = (0.0f - xOrigin) * width;		float blY = (1.0f - yOrigin) * height;

	// transform the points by the matrix
	// 0	1	2
	// 3	4	5
	// 6	7	8
	//--------------------------------------------------------------------------
	if( m_transformColumnMajor )
	{
		float x, y;

		x = tlX; y=tlY;
		tlX = x*transformMat3x3[0] + y*transformMat3x3[1] + transformMat3x3[2];
		tlY = x*transformMat3x3[3] + y*transformMat3x3[4] + transformMat3x3[5];

		x = trX; y=trY;
		trX = x*transformMat3x3[0] + y*transformMat3x3[1] + transformMat3x3[2];
		trY = x*transformMat3x3[3] + y*transformMat3x3[4] + transformMat3x3[5];

		x = brX; y=brY;
		brX = x*transformMat3x3[0] + y*transformMat3x3[1] + transformMat3x3[2];
		brY = x*transformMat3x3[3] + y*transformMat3x3[4] + transformMat3x3[5];

		x = blX; y=blY;
		blX = x*transformMat3x3[0] + y*transformMat3x3[1] + transformMat3x3[2];
		blY = x*transformMat3x3[3] + y*transformMat3x3[4] + transformMat3x3[5];
	}
	else
	{
		float x, y;

		x = tlX; y=tlY;
		tlX = x*transformMat3x3[0] + y*transformMat3x3[3] + transformMat3x3[6];
		tlY = x*transformMat3x3[1] + y*transformMat3x3[4] + transformMat3x3[7];

		x = trX; y=trY;
		trX = x*transformMat3x3[0] + y*transformMat3x3[3] + transformMat3x3[6];
		trY = x*transformMat3x3[1] + y*transformMat3x3[4] + transformMat3x3[7];

		x = brX; y=brY;
		brX = x*transformMat3x3[0] + y*transformMat3x3[3] + transformMat3x3[6];
		brY = x*transformMat3x3[1] + y*transformMat3x3[4] + transformMat3x3[7];

		x = blX; y=blY;
		blX = x*transformMat3x3[0] + y*transformMat3x3[3] + transformMat3x3[6];
		blY = x*transformMat3x3[1] + y*transformMat3x3[4] + transformMat3x3[7];
	}
	//--------------------------------------------------------------------------

	int index = m_currentVert;

	float r = m_r / 255.0f;
	float g = m_g / 255.0f;
	float b = m_b / 255.0f;
	float a = m_a / 255.0f;

	m_vertices[m_currentVert].pos[0] = tlX; 
	m_vertices[m_currentVert].pos[1] = tlY; 
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX; 
	m_vertices[m_currentVert].texcoord[1] = m_uvY + m_uvH; 
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = trX; 
	m_vertices[m_currentVert].pos[1] = trY; 
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX + m_uvW;
	m_vertices[m_currentVert].texcoord[1] = m_uvY + m_uvH;
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = brX;
	m_vertices[m_currentVert].pos[1] = brY;
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX + m_uvW;
	m_vertices[m_currentVert].texcoord[1] = m_uvY;
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = blX;
	m_vertices[m_currentVert].pos[1] = blY;
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX;
	m_vertices[m_currentVert].texcoord[1] = m_uvY;
	m_currentVert++;
	//-------------------

	m_indices[m_currentIndex++] = (index + 0);
	m_indices[m_currentIndex++] = (index + 2);
	m_indices[m_currentIndex++] = (index + 3);
	
	m_indices[m_currentIndex++] = (index + 0);
	m_indices[m_currentIndex++] = (index + 1);
	m_indices[m_currentIndex++] = (index + 2);
}
void SpriteBatch_GL3::DrawSprite( Texture *texture, float xPos, float yPos, float width, float height, float rotation, float xOrigin, float yOrigin)
{
	if( texture == NULL ) texture = m_nullTexture;

	if( ShouldFlush() ) FlushBatch();
	unsigned int textureID = PushTexture( texture );

	if( width  == 0.0f )	width  = (float)texture->GetWidth();
	if( height == 0.0f )	height = (float)texture->GetHeight();

	float tlX = (0.0f - xOrigin) * width;		float tlY = (0.0f - yOrigin) * height;
	float trX = (1.0f - xOrigin) * width;		float trY = (0.0f - yOrigin) * height;
	float brX = (1.0f - xOrigin) * width;		float brY = (1.0f - yOrigin) * height;
	float blX = (0.0f - xOrigin) * width;		float blY = (1.0f - yOrigin) * height;

	if( rotation != 0.0f )
	{
		float si = sinf(rotation); float co  = cosf(rotation);
		RotateAround(tlX, tlY, tlX, tlY, si, co);
		RotateAround(trX, trY, trX, trY, si, co);
		RotateAround(brX, brY, brX, brY, si, co);
		RotateAround(blX, blY, blX, blY, si, co);
	}

	int index = m_currentVert;

	float r = m_r / 255.0f;
	float g = m_g / 255.0f;
	float b = m_b / 255.0f;
	float a = m_a / 255.0f;

	m_vertices[m_currentVert].pos[0] = xPos + tlX; 
	m_vertices[m_currentVert].pos[1] = yPos + tlY; 
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX; 
	m_vertices[m_currentVert].texcoord[1] = m_uvY + m_uvH; 
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = xPos + trX; 
	m_vertices[m_currentVert].pos[1] = yPos + trY; 
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX + m_uvW;
	m_vertices[m_currentVert].texcoord[1] = m_uvY + m_uvH;
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = xPos + brX;
	m_vertices[m_currentVert].pos[1] = yPos + brY;
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX + m_uvW;
	m_vertices[m_currentVert].texcoord[1] = m_uvY;
	m_currentVert++;
	
	m_vertices[m_currentVert].pos[0] = xPos + blX;
	m_vertices[m_currentVert].pos[1] = yPos + blY;
	m_vertices[m_currentVert].pos[2] = (float)textureID;
	m_vertices[m_currentVert].color[0] = r;
	m_vertices[m_currentVert].color[1] = g;
	m_vertices[m_currentVert].color[2] = b;
	m_vertices[m_currentVert].color[3] = a;
	m_vertices[m_currentVert].texcoord[0] = m_uvX;
	m_vertices[m_currentVert].texcoord[1] = m_uvY;
	m_currentVert++;
	//-------------------

	m_indices[m_currentIndex++] = (index + 0);
	m_indices[m_currentIndex++] = (index + 2);
	m_indices[m_currentIndex++] = (index + 3);
	
	m_indices[m_currentIndex++] = (index + 0);
	m_indices[m_currentIndex++] = (index + 1);
	m_indices[m_currentIndex++] = (index + 2);
}