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

#include "CCubeNode.h"

namespace L3D
{
	CCubeNode::CCubeNode(int32 taille, int color)
	{
		mNodeType = NODETYPE_CUBE;
		m_r = taille;
		mColor = color;
	}

	CCubeNode::~CCubeNode()
	{

	}

	//Fonction d'affichage
	void CCubeNode::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_BACK | POLY_FORMAT_LIGHT0) ; //TO-DO : Paramètrage du fmt
				
				ulVertexBegin(GL_QUADS);
				glNormal(NORMAL_PACK(0, 0, 511));
				glVertex3v16(-m_r, -m_r,  m_r);
				glVertex3v16( m_r, -m_r,  m_r);
				glVertex3v16( m_r,  m_r,  m_r);
				glVertex3v16(-m_r,  m_r,  m_r);
				// Back Face
				glNormal(NORMAL_PACK(0, 0, -512));
				glVertex3v16(-m_r, -m_r, -m_r);
				glVertex3v16(-m_r,  m_r, -m_r);
				glVertex3v16( m_r,  m_r, -m_r);
				glVertex3v16( m_r, -m_r, -m_r);
				// Top Face
				glNormal(NORMAL_PACK(0, 511, 0));
				glVertex3v16(-m_r,  m_r, -m_r);
				glVertex3v16(-m_r,  m_r,  m_r);
				glVertex3v16( m_r,  m_r,  m_r);
				glVertex3v16( m_r,  m_r, -m_r);
				// Bottom Face
				glNormal(NORMAL_PACK(0, -512, 0));
				glVertex3v16(-m_r, -m_r, -m_r);
				glVertex3v16( m_r, -m_r, -m_r);
				glVertex3v16( m_r, -m_r,  m_r);
				glVertex3v16(-m_r, -m_r,  m_r);
				// Right Face
				glNormal(NORMAL_PACK(511, 0, 0));
				glVertex3v16( m_r, -m_r, -m_r);
				glVertex3v16( m_r,  m_r, -m_r);
				glVertex3v16( m_r,  m_r,  m_r);
				glVertex3v16( m_r, -m_r,  m_r);
				// Left Face
				glNormal(NORMAL_PACK(-512, 0, 0));	
				glVertex3v16(-m_r, -m_r, -m_r);
				glVertex3v16(-m_r, -m_r,  m_r);
				glVertex3v16(-m_r,  m_r,  m_r);
				glVertex3v16(-m_r,  m_r, -m_r);
				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 CCubeNode::Update()
	{

	}

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

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