
#include "stdafx.h"
#include "3DObject.h"

#include <gl\gl.h>			
#include <gl\glu.h>	

C_3DOBJECT_0::C_3DOBJECT_0()
{
	_3d=0;
	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_blend=0;
	_alpha=0;
	_natazeni = 1;
}

C_3DOBJECT_0::C_3DOBJECT_0(TAshlar ashlar)	
{
	_3d=0;
	_pos = ashlar;
	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_blend=0;
	_alpha=0;
	_natazeni = 1;
}

C_3DOBJECT_0::C_3DOBJECT_0(uint texture_id, TAshlar ashlar)
{
	_3d=0;
	_pos = ashlar;
	_fColor = RGBf_WHITE;
	_texture_id = texture_id;
	_begin_tag = GL_QUADS;
	_blend=0;
	_alpha=0;
	_natazeni = 1;
}

C_3DOBJECT_0::C_3DOBJECT_0(const float x, const float y, const float z, const float width, const float height, const float size_z)
{
	_3d=0;
	_pos.x = x;
	_pos.y = y;
	_pos.z = z;
	_pos.width = width;
	_pos.height = height;
	_pos.size_z = size_z;

	_fColor = RGBf_WHITE;
	_texture_id = 0;
	_begin_tag = GL_QUADS;
	_blend=0;
	_alpha=0;
	_natazeni = 1;
}

C_3DOBJECT_0::C_3DOBJECT_0(uint texture_id, const float x, const float y, const float z, const float width, const float height, const float size_z)
{
	_pos.x = x;
	_pos.y = y;
	_pos.z = z;
	_pos.width = width;
	_pos.height = height;
	_pos.size_z = size_z;
	
	_fColor = RGBf_WHITE;
	_texture_id = texture_id;
	_begin_tag = GL_QUADS;
	_blend=0;
	_alpha=0;
	_natazeni = 1;
}

void C_3DOBJECT_0::Render()
{
	//glPushMatrix();
	float x = _pos.x;
	float y = _pos.y;
	float z = _pos.z;
	float w = _pos.width;
	float h = _pos.height;
	float s = _pos.size_z;

	glColor4f(1,1,1,_fColor.b);

	(_texture_id == 0) ? glDisable(GL_TEXTURE_2D) :	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _texture_id);

	bool bLight = (bool)glIsEnabled(GL_LIGHTING);
	glDisable(GL_BLEND);
	if(_blend) 
	{
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE);  
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		//glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
		glBlendFunc( GL_SRC_ALPHA,  GL_DST_ALPHA);
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
		//glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
		glEnable(GL_BLEND);
		if(bLight) glDisable(GL_LIGHTING);
	}
	glDisable(GL_ALPHA_TEST);
	if(_alpha) glEnable( GL_ALPHA_TEST );

	_mapCoord = 1;
	int natazeni = _natazeni;
	glBegin(_begin_tag);
		
	// front side
		glNormal3f(0,0,1);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(natazeni,0);		glVertex3f(x+w, y, z);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x+w, y+h, z);
		glTexCoord2i(0,natazeni);		glVertex3f(x, y+h, z);
		if(_3d)
		{
		// back side
		glNormal3f(0,0,-1);
		glTexCoord2i(0,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(natazeni,0);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,natazeni);		glVertex3f(x, y+h, z+s);
		// left side
		glNormal3f(-1,0,0);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(natazeni,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x, y+h, z+s);
		glTexCoord2i(0,natazeni);		glVertex3f(x, y+h, z);
		// right side
		glNormal3f(1,0,0);
		glTexCoord2i(0,0);		glVertex3f(x+w, y, z);
		glTexCoord2i(natazeni,0);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,natazeni);		glVertex3f(x+w, y+h, z);
		// top side
		glNormal3f(0,-1,0);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(natazeni,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(0,natazeni);		glVertex3f(x+w, y, z);
		// bottom side
		glNormal3f(0,1,0);
		glTexCoord2i(0,0);		glVertex3f(x, y+h, z);
		glTexCoord2i(natazeni,0);		glVertex3f(x, y+h, z+s);
		glTexCoord2i(natazeni,natazeni);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,natazeni);		glVertex3f(x+w, y+h, z);
		}

/*
	glBegin(_begin_tag);

		// front side
		glNormal3f(0,0,1);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(1,0);		glVertex3f(x+w, y, z);
		glTexCoord2i(1,1);		glVertex3f(x+w, y+h, z);
		glTexCoord2i(0,1);		glVertex3f(x, y+h, z);
		if(_3d)
		{
		// back side
		glNormal3f(0,0,-1);
		glTexCoord2i(0,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(1,0);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(1,1);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,1);		glVertex3f(x, y+h, z+s);
		// left side
		glNormal3f(-1,0,0);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(1,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(1,1);		glVertex3f(x, y+h, z+s);
		glTexCoord2i(0,1);		glVertex3f(x, y+h, z);
		// right side
		glNormal3f(1,0,0);
		glTexCoord2i(0,0);		glVertex3f(x+w, y, z);
		glTexCoord2i(1,0);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(1,1);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,1);		glVertex3f(x+w, y+h, z);
		// top side
		glNormal3f(0,-1,0);
		glTexCoord2i(0,0);		glVertex3f(x, y, z);
		glTexCoord2i(1,0);		glVertex3f(x, y, z+s);
		glTexCoord2i(1,1);		glVertex3f(x+w, y, z+s);
		glTexCoord2i(0,1);		glVertex3f(x+w, y, z);
		// bottom side
		glNormal3f(0,1,0);
		glTexCoord2i(0,0);		glVertex3f(x, y+h, z);
		glTexCoord2i(1,0);		glVertex3f(x, y+h, z+s);
		glTexCoord2i(1,1);		glVertex3f(x+w, y+h, z+s);
		glTexCoord2i(0,1);		glVertex3f(x+w, y+h, z);
	}
*/
	glEnd();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	if(bLight) glEnable(GL_LIGHTING);
}
