// Liquid3D - Moteur 3D C++ NDS par Morukutsu
// Surcouche de l'ulibrary de Brunni
// Version alpha 1

#include "CSkydomeNode.h"

namespace L3D
{
	CSkydomeNode::CSkydomeNode()
	{
		mNodeType = NODETYPE_SKYDOME;
		mColor = 32767;
	}

	CSkydomeNode::~CSkydomeNode()
	{

	}

	//Fonction d'affichage
	void CSkydomeNode::Draw()
	{
		glDisable(GL_TEXTURE_2D);
		glPushMatrix();
			//Déplacement de l'ensemble des objets
			glTranslate3f32(mPosEns.x, mPosEns.y, mPosEns.z);
			MATRIX_SCALE = mScaleEns.x; MATRIX_SCALE = mScaleEns.y; MATRIX_SCALE = mScaleEns.z;
			//glRotatef32(1.0f, mRotEns.x, mRotEns.y, mRotEns.z);
			glRotateXi(mRotEns.x);
			glRotateYi(mRotEns.y);
			glRotateZi(mRotEns.z);
			
			glPushMatrix();
				//Déplacement de cet objet en particulier
				glTranslate3f32(mPosObj.x, mPosObj.y, mPosObj.z);
				MATRIX_SCALE = mScaleObj.x; MATRIX_SCALE = mScaleObj.y; MATRIX_SCALE = mScaleObj.z;
				glRotateXi(mRotObj.x);
				glRotateYi(mRotObj.y);
				glRotateZi(mRotObj.z);
				//glRotatef32(1.0f, mRotObj.x, mRotObj.y, mRotObj.z);
				
				//Affichage du cube
				ulDisableTexture();	
				//glBindTexture(GL_TEXTURE_2D, 0);
				//ulVertexColor(32767);
				glMaterialf(GL_AMBIENT, mAmbientColor);
				glMaterialf(GL_DIFFUSE, mColor);
				glMaterialf(GL_SPECULAR, mSpecularColor | BIT(15)); // notice the additional BIT(15)!
				glMaterialf(GL_EMISSION, mEmissiveColor);
				//glMaterialShinyness();

				glPolyFmt(POLY_ALPHA(mAlpha) | POLY_ID(0) | POLY_CULL_NONE) ; //TO-DO : Paramètrage du fmt
				
				ulVertexBegin(GL_TRIANGLE_STRIP);

				v3d pt;
				int dphi = 15;
				int dtheta = 25;
				float dtor = 3.14f/180.0f;
				for(int mPhi = 0; mPhi <= 90 - dphi; mPhi += dphi)
				{
					for(int mTheta = 0; mTheta <= 360 - dtheta; mTheta += dtheta)
					{
						pt.x = floatToFixed(5.0 * sinf(mPhi*dtor) * cosf(mTheta*dtor), 12);
						pt.y = floatToFixed(5.0 * sinf(mPhi*dtor) * sinf(mTheta*dtor), 12);
						pt.z = floatToFixed(5.0 * cosf(mPhi*dtor), 12);
						glVertex3v16(pt.x, pt.y, pt.z);
						pt.x = floatToFixed(5.0 * sinf((mPhi+dphi)*dtor) * cosf(mTheta*dtor), 12);
						pt.y = floatToFixed(5.0 * sinf((mPhi+dphi)*dtor) * sinf(mTheta*dtor), 12);
						pt.z = floatToFixed(5.0 * cosf((mPhi+dphi)*dtor), 12);
						glVertex3v16(pt.x, pt.y, pt.z);
						pt.x = floatToFixed(5.0 * sinf(mPhi*dtor) * cosf((mTheta+dtheta)*dtor), 12);
						pt.y = floatToFixed(5.0 * sinf(mPhi*dtor) * sinf((mTheta+dtheta)*dtor), 12);
						pt.z = floatToFixed(5.0 * cosf(mPhi*dtor), 12);
						glVertex3v16(pt.x, pt.y, pt.z);
						if(mPhi >= -90 && mPhi <= 90)
						{
							pt.x = floatToFixed(5.0 * sinf((mPhi+dphi)*dtor) * cosf((mTheta+dtheta)*dtor), 12);
							pt.y = floatToFixed(5.0 * sinf((mPhi+dphi)*dtor) * sinf((mTheta+dtheta)*dtor), 12);
							pt.z = floatToFixed(5.0 * cosf((mPhi+dphi)*dtor), 12);
							glVertex3v16(pt.x, pt.y, pt.z);
						}
					}
				}
				
				ulVertexEnd();

				glPolyFmt(POLY_ALPHA(31) ) ;
				ulVertexColor(32767);
			glPopMatrix(1);
			//Affichage des enfants
			glEnable(GL_TEXTURE_2D);
			for(unsigned int k = 0; k < mChildrenCount; k++)
				mChildren[k]->Draw();
		glPopMatrix(1);
		
	}

	void CSkydomeNode::Update()
	{

	}

	//Fonctions spécifiques à ce noeud
	void CSkydomeNode::Resize(int32 taille)
	{
		m_r = taille;
	}

	void CSkydomeNode::Tint(int color)
	{
		mColor = color;
	}
};