Axis::Axis(void) : Primitive()
	
{
	setDrawingPrimitive(curitiba::render::IRenderer::LINES);
	std::vector<VertexData::Attr> *vertices = new std::vector<vec4>(6);
	std::vector<VertexData::Attr> *normals = new std::vector<vec4>(6);

	// FRONT
	vertices->at (0).set	(-1.0f,  0.0f,  0.0f);
	vertices->at (1).set	( 1.0f,  0.0f,  0.0f);
	vertices->at (2).set	( 0.0f, -1.0f,  0.0f);
	vertices->at (3).set	( 0.0f,  1.0f,  0.0f);
	vertices->at (4).set	( 0.0f,  0.0f, -1.0f);
	vertices->at (5).set	( 0.0f,  0.0f,  1.0f);

	VertexData &vertexData = getVertexData();

	vertexData.setDataFor (VertexData::getAttribIndex("position"), vertices);
	vertexData.setDataFor (VertexData::getAttribIndex("normal"), normals);

	MaterialGroup *aMaterialGroup = new MaterialGroup();
	std::vector<unsigned int> *indices = new std::vector<unsigned int>(2);
	indices->at (0) = 0;		
	indices->at (1) = 1;
	aMaterialGroup->setIndexList (indices);
	aMaterialGroup->setParent (this);
	aMaterialGroup->setMaterialName("__Emission Red");
	addMaterialGroup (aMaterialGroup);

	aMaterialGroup = new MaterialGroup();
	indices = new std::vector<unsigned int>(2);
	indices->at (0) = 2;		
	indices->at (1) = 3;
	aMaterialGroup->setIndexList (indices);
	aMaterialGroup->setParent (this);
	aMaterialGroup->setMaterialName("__Emission Green");
	addMaterialGroup (aMaterialGroup);

	aMaterialGroup = new MaterialGroup();
	indices = new std::vector<unsigned int>(2);
	indices->at (0) = 4;		
	indices->at (1) = 5;
	aMaterialGroup->setIndexList (indices);
	aMaterialGroup->setParent (this);
	aMaterialGroup->setMaterialName("__Emission Blue");
	addMaterialGroup (aMaterialGroup);
}
Quad::Quad(void) :
    SceneObject()
{
    Mesh *renderable = (Mesh *)RESOURCEMANAGER->createRenderable("Mesh");//new Mesh;
    std::vector<VertexData::Attr> *vertices = new std::vector<vec4>(4);
    std::vector<VertexData::Attr> *textureCoords = new std::vector<vec4>(4);
    std::vector<VertexData::Attr> *normals = new std::vector<vec4>(4);

    for (int i = 0; i < 4; ++i)
        normals->at(i).set(0.0f, 0.0f, 0.0f);

    vertices->at (0).set (-1.0f, -1.0f, -1.0f);
    vertices->at (1).set (1.0f, -1.0f, -1.0f);
    vertices->at (2).set (1.0f, 1.0f, -1.0f);
    vertices->at (3).set (-1.0f, 1.0f, -1.0f);

    textureCoords->at (0).set (0.0f, 0.0f, 0.0f);
    textureCoords->at (1).set (1.0f, 0.0f, 0.0f);
    textureCoords->at (2).set (1.0f, 1.0f, 0.0f);
    textureCoords->at (3).set (0.0f, 1.0f, 0.0f);

    VertexData &vertexData = renderable->getVertexData();

    vertexData.setDataFor (VertexData::getAttribIndex("position"), vertices);
    vertexData.setDataFor (VertexData::getAttribIndex("texCoord0"), textureCoords);
    vertexData.setDataFor (VertexData::getAttribIndex("normal"), normals);

    MaterialGroup *aMaterialGroup = new MaterialGroup();


    std::vector<unsigned int> *indices = new std::vector<unsigned int>(6);

    m_Transform = new SimpleTransform;

    indices->at (0) = 0;
    indices->at (1) = 1;
    indices->at (2) = 3;
    indices->at (3) = 1;
    indices->at (4) = 2;
    indices->at (5) = 3;

//	aMaterialGroup->setMaterialId (0);
    aMaterialGroup->setIndexList (indices);
    aMaterialGroup->setParent (renderable);
    aMaterialGroup->setMaterialName("__Quad");

    renderable->addMaterialGroup (aMaterialGroup);
    setRenderable (renderable);
}
void
Font::createSentenceRenderable(IRenderable &renderable, std::string sentence)
{
	assert(mMaterialName != "");

	int aux = sentence.length();
	int size = 0;

	for (int count = 0; count < aux; count++) {
	
		// if char exists in the font definition
		if (mChars.count(sentence[count])) 
			size++;
	}

	assert(size);

	// need to clear previous mesh 
	//Mesh *renderable = (Mesh *)RESOURCEMANAGER->createRenderable("Mesh", sentence, "Sentence");
	//renderable->setDrawingPrimitive(curitiba::render::IRenderer::TRIANGLES);

	std::vector<VertexData::Attr> *vertices = new std::vector<VertexData::Attr>(size*6);
	std::vector<VertexData::Attr> *texCoords = new std::vector<VertexData::Attr>(size*6);
	std::vector<VertexData::Attr> *normals = new std::vector<VertexData::Attr>(size*6);



	int i = 0;
	float hDisp = 0.0f, vDisp = 0.0f;

	for (int count = 0; count < aux; count++) {
	
		// get char at position count
		char c = sentence[count];
		if (c == ' ') {
			if (mFixedSize)
				hDisp += mChars[c].C + mChars[c].A; 
			else
				hDisp += mChars[c].C;	
		}
		// if char exists in the font definition
		else if (mChars.count(c)) {
			vertices->at(6*i  ).set(hDisp, vDisp + mHeight, 0.0f, 1.0f);
			vertices->at(6*i+1).set(hDisp + mChars[c].width, vDisp, 0.0f, 1.0f);
			vertices->at(6*i+2).set(hDisp, vDisp, 0.0f, 1.0f);			
			vertices->at(6*i+3).set(hDisp + mChars[c].width, vDisp, 0.0f, 1.0f);			
			vertices->at(6*i+4).set(hDisp, vDisp + mHeight,0.0f, 1.0f);			
			vertices->at(6*i+5).set(hDisp + mChars[c].width, vDisp + mHeight, 0.0f, 1.0f);		

			normals->at(6*i    ).set(0,0,1);
			normals->at(6*i+1  ).set(0,0,1);
			normals->at(6*i+2  ).set(0,0,1);
			normals->at(6*i+3  ).set(0,0,1);
			normals->at(6*i+4  ).set(0,0,1);
			normals->at(6*i+5  ).set(0,0,1);

			texCoords->at(6*i  ).set(mChars[c].x1, 1-mChars[c].y2, 0.0f, 1.0f);
			texCoords->at(6*i+1).set(mChars[c].x2, 1-mChars[c].y1, 0.0f, 1.0f);
			texCoords->at(6*i+2).set(mChars[c].x1, 1-mChars[c].y1, 0.0f, 1.0f);
			texCoords->at(6*i+3).set(mChars[c].x2, 1-mChars[c].y1, 0.0f, 1.0f);
			texCoords->at(6*i+4).set(mChars[c].x1, 1-mChars[c].y2, 0.0f, 1.0f);
			texCoords->at(6*i+5).set(mChars[c].x2, 1-mChars[c].y2, 0.0f, 1.0f);

			if (mFixedSize)
				hDisp += mChars[c].C + mChars[c].A; 
			else
				hDisp += mChars[c].C;
			i++;
		}
		// newline
		else if (c == '\n') {
			vDisp += mHeight;
			hDisp = 0.0f;
		}
	}

	VertexData &vertexData = renderable.getVertexData();
	vertexData.setDataFor (VertexData::getAttribIndex("position"), vertices);
	vertexData.setDataFor (VertexData::getAttribIndex("normal"), normals);
	vertexData.setDataFor (VertexData::getAttribIndex("texCoord0"), texCoords);

	std::vector<unsigned int> *indices = new std::vector<unsigned int>(size*6);
	for (int j = 0; j < size*6 ; j++)
		indices->push_back(j);

	MaterialGroup* auxMG;

	std::vector<IMaterialGroup *> aMatG = renderable.getMaterialGroups();
	if (aMatG.size()) {
		auxMG = (MaterialGroup *)aMatG[0];
		auxMG->setIndexList (indices);
	}
	else {
		auxMG = new MaterialGroup();
		auxMG->setMaterialName(mMaterialName);
		auxMG->setParent(&renderable);
		auxMG->setIndexList (indices);
		renderable.addMaterialGroup(auxMG);
	}
}
Camera::Camera (const std::string &name) :
	SceneObject(),

	m_Float4Props(COUNT_FLOAT4PROPERTY),
	m_FloatProps(COUNT_FLOATPROPERTY),
	m_Mat4Props(COUNT_MAT4PROPERTY),

	m_IsDynamic (false),
	m_pViewport (0),
	m_LookAt(false),
	m_LookAtPoint(0.0f, 0.0f, 0.0f),
	m_Spherical((float)M_PI,0.0f),
	m_PositionOffset (0.0f),
	m_IsOrtho (false)
{

	//mFloatAttribs.add(AttribFloat(FOV, 60.0f));
	//mFloatAttribs.add(AttribFloat(NEARP, 1.0f));
	//mFloatAttribs.add(AttribFloat(FARP, 10000.0f));
	//mFloatAttribs.add(AttribFloat(LEFT, -1.0f));
	//mFloatAttribs.add(AttribFloat(RIGHT, 1.0f));
	//mFloatAttribs.add(AttribFloat(TOP, 1.0f));
	//mFloatAttribs.add(AttribFloat(BOTTOM, -1.0f));

	//mVec4Attribs.add(AttribVec4(POSITION, 0.0f, 0.0f, 0.0f, 1.0f));
	//mVec4Attribs.add(AttribVec4(VIEW_VEC, 0.0f, 0.0f, -1.0f, 0.0f));
	//mVec4Attribs.add(AttribVec4(NORMALIZED_VIEW_VEC, 0.0f, 0.0f, -1.0f, 0.0f, true));
	//mVec4Attribs.add(AttribVec4(NORMALIZED_RIGHT_VEC, 1.0f, 0.0f, 0.0f, 0.0f, true));
	//mVec4Attribs.add(AttribVec4(UP_VEC, 0.0f, 1.0f, 0.0f, 0.0f));
	//mVec4Attribs.add(AttribVec4(NORMALIZED_UP_VEC, 0.0f, 1.0f, 0.0f, 0.0f, true));
	//mVec4Attribs.add(AttribVec4(LOOK_AT_POINT, 0.0f, 0.0f, -1.0, 1.0));

	m_Id = 0;
	m_Name = name;
	m_pViewport = CURITIBA->getDefaultViewport();

	m_Float4Props[POSITION].set(0.0f, 0.0f, 0.0f, 1.0f);
	m_Float4Props[VIEW_VEC].set(0.0f, 0.0f, -1.0f, 0.0f);
	m_Float4Props[NORMALIZED_VIEW_VEC].set(0.0f, 0.0f, -1.0f, 0.0f);

	m_Float4Props[NORMALIZED_RIGHT_VEC].set(1.0f, 0.0f, 0.0f, 0.0f);

	m_Float4Props[UP_VEC].set(0.0f, 1.0f, 0.0f, 0.0f);
	m_Float4Props[NORMALIZED_UP_VEC].set(0.0f, 1.0f, 0.0f, 0.0f);

	m_Float4Props[LOOK_AT_POINT].set(0.0f, 0.0f, -1.0f, 1.0f);

	m_FloatProps[FOV] = 60.0f;
	m_FloatProps[TOP] = 1.0f;
	m_FloatProps[BOTTOM] = -1.0f;
	m_FloatProps[LEFT] = -1.0f;
	m_FloatProps[RIGHT] = 1.0f;
	m_FloatProps[NEARP] = 1.0f;
	m_FloatProps[FARP] = 10000.0f;

	buildViewMatrix();
	buildViewMatrixInverse();

	m_StaticCondition = false;

	m_BoundingVolume = new BoundingBox;
	m_Transform = new SimpleTransform;
	setVectorsFromSpherical();

	// Adding a Mesh with the frustum lines
	Mesh *renderable =  (Mesh *)RESOURCEMANAGER->createRenderable("Mesh", m_Name, "Camera");
	renderable->setDrawingPrimitive(curitiba::render::IRenderer::LINES);
	std::vector<VertexData::Attr> *vertices = new std::vector<VertexData::Attr>(8);
	VertexData &vertexData = renderable->getVertexData();
	vertexData.setDataFor (VertexData::getAttribIndex("position"), vertices);

	MaterialGroup *aMaterialGroup = new MaterialGroup;
	
	std::vector<unsigned int> *indices = new std::vector<unsigned int>(16);
	indices->at (0) = Camera::TOP_LEFT_NEAR;		indices->at (1) = Camera::TOP_LEFT_FAR;
	indices->at (2) = Camera::TOP_RIGHT_NEAR;		indices->at (3) = Camera::TOP_RIGHT_FAR;
	indices->at (4) = Camera::BOTTOM_RIGHT_NEAR;	indices->at (5) = Camera::BOTTOM_RIGHT_FAR;
	indices->at (6) = Camera::BOTTOM_LEFT_NEAR;		indices->at (7) = Camera::BOTTOM_LEFT_FAR;

	indices->at (8) = Camera::TOP_LEFT_FAR;			indices->at (9) = Camera::TOP_RIGHT_FAR;
	indices->at (10) = Camera::TOP_RIGHT_FAR;		indices->at (11) = Camera::BOTTOM_RIGHT_FAR;
	indices->at (12) = Camera::BOTTOM_RIGHT_FAR;	indices->at (13) = Camera::BOTTOM_LEFT_FAR;
	indices->at (14) = Camera::BOTTOM_LEFT_FAR;		indices->at (15) = Camera::TOP_LEFT_FAR;

	aMaterialGroup->setIndexList (indices);
	aMaterialGroup->setParent (renderable);
	aMaterialGroup->setMaterialName("__Emission White");

	renderable->addMaterialGroup (aMaterialGroup);
	m_Transform = & m_Mat4Props[VIEW_INVERSE_MATRIX];
	setRenderable (renderable);

	aMaterialGroup = new MaterialGroup;
	indices = new std::vector<unsigned int>(8);
	indices->at (0) = Camera::TOP_LEFT_NEAR;		indices->at (1) = Camera::TOP_RIGHT_NEAR;
	indices->at (2) = Camera::TOP_RIGHT_NEAR;		indices->at (3) = Camera::BOTTOM_RIGHT_NEAR;
	indices->at (4) = Camera::BOTTOM_RIGHT_NEAR;	indices->at (5) = Camera::BOTTOM_LEFT_NEAR;
	indices->at (6) = Camera::BOTTOM_LEFT_NEAR;		indices->at (7) = Camera::TOP_LEFT_NEAR;

	aMaterialGroup->setIndexList (indices);
	aMaterialGroup->setParent (renderable);
	aMaterialGroup->setMaterialName("__Emission Red");

	renderable->addMaterialGroup (aMaterialGroup);
	setRenderable (renderable);

	IScene *s = RENDERMANAGER->createScene(name, "SceneAux");
	s->add(this);

	EVENTMANAGER->addListener("VIEWPORT_CHANGED", this);
}