void FadeScreenF(float alpha)
{

	fadeVertices[0].color[A] = alpha * 255;
	fadeVertices[1].color[A] = alpha * 255;
	fadeVertices[2].color[A] = alpha * 255;
	fadeVertices[3].color[A] = alpha * 255;
	
	
	glDisable(GL_TEXTURE_2D);
	glEnableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	
	glVertexPointer(  2, GL_SHORT,  sizeof(xf_textureless_sprite_t), fadeVertices[0].pos);
	glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_textureless_sprite_t), fadeVertices[0].color);
	glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,fadeIndices);
	STATS_AddTriangles(6/2);
	
	
	glDisableClientState(GL_COLOR_ARRAY);
	glEnableClientState (GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_TEXTURE_2D);
	 
	
	
}
void RenderEntitiesMD5(void* md5Void)
{
	md5_object_t* md5Object;
	md5_mesh_t* currentMesh;
	int i;
	
	md5Object = (md5_object_t*)md5Void;
	
	
	/* Draw each mesh of the model */	
	for (i = 0; i < 1; i++)
    {
		currentMesh = &md5Object->md5Model.meshes[i];
		
		if (!renderer.isRenderingShadow)
		{
			//RenderNormalsF( currentMesh);
			glVertexAttribPointer(currentShader->vars[SHADER_ATT_NORMAL], 3, GL_SHORT, GL_TRUE,  sizeof(vertex_t), currentMesh->vertexArray->normal);
			glVertexAttribPointer(currentShader->vars[SHADER_ATT_TANGENT], 3, GL_SHORT, GL_TRUE,  sizeof(vertex_t), currentMesh->vertexArray->tangent);	
			glVertexAttribPointer(currentShader->vars[SHADER_ATT_UV], 2, GL_SHORT, GL_TRUE,  sizeof(vertex_t), currentMesh->vertexArray->text);			
			STATS_AddTriangles(currentMesh->num_tris);
		}
		glVertexAttribPointer(currentShader->vars[SHADER_ATT_VERTEX], 3, GL_FLOAT, GL_FALSE,  sizeof(vertex_t), currentMesh->vertexArray->pos);
		glDrawElements (GL_TRIANGLES, currentMesh->num_tris * 3, GL_UNSIGNED_SHORT, currentMesh->vertexIndices);
		
    }
}
void RenderColorlessSpritesF(xf_colorless_sprite_t* vertices, ushort numIndices, ushort* indices)
{
	glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), vertices->pos);
	glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), vertices->text);
	glDrawElements (GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT,indices);
	STATS_AddTriangles(numIndices/2);
}
void RenderPlayersBulletsF(void)
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	
	SetTextureF(bulletConfig.bulletTexture.textureId);
	
	//Player bullets
	glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), pBulletVertices->pos);
	glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), pBulletVertices->text);			
	glDrawElements (GL_TRIANGLES, numPBulletsIndices, GL_UNSIGNED_SHORT,bulletIndices);
	STATS_AddTriangles(numPBulletsIndices/3);
	
	//Also render enemy bullets
//	glColor4f(1, 1, 1, 1);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	
	glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), partLib.ss_vertices[0].pos);
	glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), partLib.ss_vertices[0].text);			
	glDrawElements (GL_TRIANGLES, partLib.num_indices, GL_UNSIGNED_SHORT,partLib.indices);
	STATS_AddTriangles(partLib.num_indices/3);
}
void RenderString(svertex_t* vertices,ushort* indices, uint numIndices)
{	
	
	
	glVertexAttribPointer(currentShader->vars[SHADER_ATT_VERTEX], 2, GL_SHORT, GL_FALSE,  sizeof(svertex_t), vertices->pos);
	glVertexAttribPointer(currentShader->vars[SHADER_ATT_UV], 2, GL_FLOAT, GL_FALSE,  sizeof(svertex_t), vertices->text);			
	//TODO CHANGE glDrawElements
	glDrawElements (GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT,indices);
	
	//glDrawArrays(GL_TRIANGLES, 0, numVertices);
	STATS_AddTriangles(numIndices/3);
	
}
void RenderEntitiesOBJ(void* objVoid)
{
	
	obj_t* obj;
	
	obj = (obj_t*)objVoid;
	
	if (!renderer.isRenderingShadow)
	{

		//RenderNormalsF( currentMesh);	
		glVertexAttribPointer(currentShader->vars[SHADER_ATT_TANGENT], 3, GL_SHORT, GL_TRUE,  sizeof(obj_vertex_t), obj->vertices[0].tangent);		
		glVertexAttribPointer(currentShader->vars[SHADER_ATT_NORMAL], 3, GL_SHORT, GL_TRUE,  sizeof(obj_vertex_t), obj->vertices[0].normal);
		glVertexAttribPointer(currentShader->vars[SHADER_ATT_UV], 2, GL_FLOAT, GL_FALSE,  sizeof(obj_vertex_t), obj->vertices[0].textCoo);	
		STATS_AddTriangles(obj->num_indices/3);
	}
	
	glVertexAttribPointer(currentShader->vars[SHADER_ATT_VERTEX], 3, GL_FLOAT, GL_FALSE,  sizeof(obj_vertex_t), obj->vertices[0].position);
	glDrawElements (GL_TRIANGLES, obj->num_indices, GL_UNSIGNED_SHORT, obj->indices);
	
	
}
void DrawControlsF(void)
{
	if (engine.controlMode == CONTROL_MODE_SWIP)
		return;
	
	glEnableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisable(GL_TEXTURE_2D);
	
	
	//glBindBuffer(GL_ARRAY_BUFFER, controlVBOId);

	
	glVertexPointer (2, GL_SHORT, sizeof(xf_textureless_sprite_t),controlVertices[0].pos);// (char *)NULL + 0);	
	glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_textureless_sprite_t),controlVertices[0].color);// (char *)NULL + 4);
	glDrawElements (GL_TRIANGLE_STRIP, controlNumIndices, GL_UNSIGNED_SHORT,controlIndices);
	STATS_AddTriangles(controlNumIndices/2);
		
	glDisableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	
	
	
}
void RenderFXSpritesF(void)
{
	int i,j;
	
	/*
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
	SetTextureF(smokeTexture.textureId);
	if (numSmokeIndices != 0)
	{
		glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), smokeVertices->pos);
		glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), smokeVertices->text);
		//glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), smokeVertices->color);
		glDrawElements (GL_TRIANGLES, numSmokeIndices, GL_UNSIGNED_SHORT,smokeIndices);
		STATS_AddTriangles(numSmokeIndices/3);
	}
	
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	 */
	
	
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	
	SetTextureF(smokeTexture.textureId);
	if (numSmokeIndices != 0)
	{
		glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), smokeVertices->pos);
		glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), smokeVertices->text);
		//glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), smokeVertices->color);
		glDrawElements (GL_TRIANGLES, numSmokeIndices, GL_UNSIGNED_SHORT,smokeIndices);
		STATS_AddTriangles(numSmokeIndices/3);
	}
	
	
	
	SetTextureF(ghostTexture.textureId);
	for(i=0 ; i <numPlayers ; i++)
	{
		/*
		if (i==0)
			glColor4f(0.8f, 0.8f, 1.0f, 0.9f);
		else {
			glColor4f(1.0f, 0.4f, 0.4f, 0.9f);
		}
*/
		
		
		for (j=0; j< GHOSTS_NUM; j++) 
		{
			if (players[i].ghosts[j].timeCounter >= GHOST_TTL_MS)
				continue;
			
			
			//vertices = &players[i].ghosts[j].wayPoints[players[i].ghosts[j].startVertexArray];
			glVertexPointer(  2, GL_SHORT,  sizeof(xf_colorless_sprite_t), players[i].ghosts[j].wayPoints[players[i].ghosts[j].startVertexArray].pos);
			glTexCoordPointer(2, GL_SHORT,  sizeof(xf_colorless_sprite_t), players[i].ghosts[j].wayPoints[players[i].ghosts[j].startVertexArray].text);
			//glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), vertices->color);
			glDrawArrays(GL_TRIANGLE_STRIP, 0, players[i].ghosts[j].lengthVertexArray);
			STATS_AddTriangles((players[i].ghosts[j].lengthVertexArray/2));
		}
	}

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	

	glEnableClientState(GL_COLOR_ARRAY);
	
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	//Render all particules
	if (numParticulesIndices != 0)
	{
		
		SetTextureF(bulletConfig.bulletTexture.textureId);
		glVertexPointer(  2, GL_SHORT,  sizeof(xf_sprite_t), particuleVertices->pos);
		glTexCoordPointer(2, GL_SHORT,  sizeof(xf_sprite_t), particuleVertices->text);
		glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), particuleVertices->color);
		glDrawElements (GL_TRIANGLES, numParticulesIndices, GL_UNSIGNED_SHORT,particuleIndices);
		STATS_AddTriangles(numParticulesIndices/3);
	}
	
	
	
	
	
	
	//Render all explosions 
	if (numExplosionIndices != 0)
	{
		SetTextureF(explosionTexture.textureId);
		glVertexPointer(  2, GL_SHORT,  sizeof(xf_sprite_t), explosionVertices->pos);
		glTexCoordPointer(2, GL_SHORT,  sizeof(xf_sprite_t), explosionVertices->text);
		//printf("REMOVE COLOR INDICES EXPLOSIONS RenderFXSpritesF !!!! \n");
		glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), explosionVertices->color);
		glDrawElements (GL_TRIANGLES, numExplosionIndices, GL_UNSIGNED_SHORT,explosionIndices);
		STATS_AddTriangles(numExplosionIndices/3);
	}
	
	
	//Render enemy FXs
	SetTextureF(bulletConfig.bulletTexture.textureId);
	glVertexPointer(  2, GL_SHORT,  sizeof(xf_sprite_t), enFxLib.ss_vertices[0].pos);
	glTexCoordPointer(2, GL_SHORT,  sizeof(xf_sprite_t), enFxLib.ss_vertices[0].text);
	glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(xf_sprite_t), enFxLib.ss_vertices[0].color);
	glDrawElements (GL_TRIANGLES, enFxLib.num_indices, GL_UNSIGNED_SHORT,enFxLib.indices);
	STATS_AddTriangles(enFxLib.num_indices/3);
	//printf("enFxLib.num_indices=%d\n",enFxLib.num_indices);
	
	
	
#ifdef RENDER_COLL_BOXEX	
	RenderCollisionBoxes();
#endif
	
	//glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	
	
	
	glDisableClientState(GL_COLOR_ARRAY);

	
	
}
//int traceRenderEntity = 0;
static void RenderEntityF(entity_t* entity)
{

//	printf("RenderEntityF Player1=%p\n",players[0].entity.material);
//	printf("RenderEntityF Player2=%p\n",players[1].entity.material);		
	
	glPushMatrix();
	
	//if (traceRenderEntity)
	{
		//entity->matrix[13] = 110;
	//	printf("[RenderEntityF] entity id=%d\n",entity->uid);
	//	printf("[RenderEntityF] entity pos=[%.2f,%.2f,%.2f,%.2f]\n",entity->matrix[12],entity->matrix[13],entity->matrix[14],entity->matrix[15]);
		//matrix_print(entity->matrix);
		
	}
	
	glMultMatrixf(entity->matrix);
	
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, entity->material->shininess);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, entity->material->specularColor);
	SetTextureF(entity->material->textures[TEXTURE_DIFFUSE].textureId);
	
	//Disabling blending for now
	/*
	if (entity->material->hasAlpha )
	{
		if (!renderer.isBlending)
		{
			renderer.isBlending = 1;
			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			STATS_AddBlendingSwitch();
		}
	}
	else
	{
		if (renderer.isBlending)
		{
			renderer.isBlending = 0;
			glDisable(GL_BLEND);
			STATS_AddBlendingSwitch();
		}
	}
	*/
	
		
	if (entity->model->memLocation == MD5_MEMLOC_VRAM)
	{

		glBindBuffer(GL_ARRAY_BUFFER, entity->model->vboId);
		
		glVertexPointer (3, GL_FLOAT, sizeof(vertex_t), (char *)NULL + VERTEX_T_DELTA_TO_POS);	
		glNormalPointer(GL_SHORT, sizeof(vertex_t), (char *)NULL + VERTEX_T_DELTA_TO_NORMAL);
		glTexCoordPointer (2, GL_SHORT, sizeof(vertex_t), (char *)NULL + VERTEX_T_DELTA_TO_TEXT);	
	}
	else 
	{
		glTexCoordPointer (2, GL_SHORT, sizeof(vertex_t), entity->model->vertexArray->text);	
		glVertexPointer (3, GL_FLOAT, sizeof(vertex_t), entity->model->vertexArray->pos);
		glNormalPointer(GL_SHORT, sizeof(vertex_t), entity->model->vertexArray->normal);
	}

	if (entity->usage == ENT_PARTIAL_DRAW)
	{
		
		glDrawElements (GL_TRIANGLES, entity->numIndices, GL_UNSIGNED_SHORT, entity->indices);	
		//glDrawArrays(GL_TRIANGLES,0,3);
		//glDrawElements (GL_TRIANGLES, 0, GL_UNSIGNED_SHORT, entity->indices);	

		
		STATS_AddTriangles(entity->numIndices/3);
	}
	else
	{
		glDrawElements (GL_TRIANGLES, entity->model->numIndices, GL_UNSIGNED_SHORT, entity->model->indices);	
		//glDrawArrays(GL_TRIANGLES,0,3);
		//glDrawElements (GL_TRIANGLES, 0, GL_UNSIGNED_SHORT, entity->model->indices);	

		STATS_AddTriangles(entity->model->numIndices/3);
	}

	
	
	
	
	
	
	
	
	//RenderNormalsF(entity->model);
	
	glPopMatrix();
}