Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №36 / BlitzStyle
.cpp#include "BlitzStyle.h"
using namespace std;
int sign (double val)
{
if (val>=0) return 1; else return -1;
}
Texture::Texture()
{
m_number=-1;
m_tex_filter=GL_NEAREST;
m_tex_wrap=GL_REPEAT;
m_env_mode=GL_REPLACE;
}
void Texture::setParams (GLenum tex_filter, GLenum tex_wrap, GLenum env_mode)
{
m_tex_filter=tex_filter;
m_tex_wrap=tex_wrap;
m_env_mode=env_mode;
}
Entity::Entity()
{
m_number=-1;
m_visible=true;
m_xscale=1; m_yscale=1; m_zscale=1;
m_parent=0;
m_texture=0;
m_quadObj=gluNewQuadric();
gluQuadricTexture (m_quadObj, GL_TRUE);
m_alpha=1.0;
m_amb[0]=1; m_amb[1]=1; m_amb[2]=1; m_amb[3]=m_alpha;
m_dif[0]=1; m_dif[1]=1; m_dif[2]=1; m_dif[3]=m_alpha;
m_spec[0]=1; m_spec[1]=1; m_spec[2]=1; m_spec[3]=m_alpha;
LoadIdentity();
}
Entity::~Entity()
{
;
}
void Entity::LoadIdentity ()
{
int i,j;
for (i=0; i<4; i++)
for (j=0; j<4; j++)
{if (i==j) m_matrix[j*4+i]=1; else m_matrix[j*4+i]=0;}
}
void Entity::CalcVector()
{
}
void Entity::setNumber (long int number)
{
list <Entity*>::iterator i;
m_number=number;
if (number==-1)
{
for (i=m_children.begin(); i!=m_children.end(); i++) (*i)->setParent(0);
m_children.clear();
}
}
void Entity::setParent (Entity* parent)
{
m_parent=parent;
}
void Entity::PositionEntity (double x, double y, double z)
{
Command c;
c.name='p';
c.param[0]=x; c.param[1]=y; c.param[2]=z;
m_command.push_back(c);
}
void Entity::MoveEntity (double dx, double dy, double dz)
{
Command c;
c.name='m';
c.param[0]=dx; c.param[1]=dy; c.param[2]=dz;
m_command.push_back(c);
}
void Entity::RotateEntityOX (double pitch)
{
Command c;
c.name='r';
c.param[0]=pitch; c.param[1]=1.0; c.param[2]=0.0; c.param[3]=0.0;
m_command.push_back(c);
}
void Entity::RotateEntityOY (double yaw)
{
Command c;
c.name='r';
c.param[0]=yaw; c.param[1]=0.0; c.param[2]=1.0; c.param[3]=0.0;
m_command.push_back(c);
}
void Entity::RotateEntityOZ (double roll)
{
Command c;
c.name='r';
c.param[0]=roll; c.param[1]=0.0; c.param[2]=0.0; c.param[3]=1.0;
m_command.push_back(c);
}
void Entity::TurnEntityOX (double pitch)
{
Command c;
c.name='t';
c.param[0]=pitch; c.param[1]=1.0; c.param[2]=0.0; c.param[3]=0.0;
m_command.push_back(c);
}
void Entity::TurnEntityOY (double yaw)
{
Command c;
c.name='t';
c.param[0]=yaw; c.param[1]=0.0; c.param[2]=1.0; c.param[3]=0.0;
m_command.push_back(c);
}
void Entity::TurnEntityOZ (double roll)
{
Command c;
c.name='t';
c.param[0]=roll; c.param[1]=0.0; c.param[2]=0.0; c.param[3]=1.0;
m_command.push_back(c);
}
void Entity::ScaleEntity (double xscale, double yscale, double zscale)
{
m_xscale=xscale; m_yscale=yscale; m_zscale=zscale;
}
void Entity::LoadMatrix (double* matrix)
{
Command c;
c.name='M';
for (int i=0; i<16; i++) c.param[i]=matrix[i];
m_command.push_back(c);
}
void Entity::EntityMaterial (GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb)
{
m_amb[0]=ambr; m_amb[1]=ambg; m_amb[2]=ambb; m_amb[3]=1.0;
m_dif[0]=difr; m_dif[1]=difg; m_dif[2]=difb; m_dif[3]=1.0;
m_spec[0]=specr; m_spec[1]=specg; m_spec[2]=specb; m_spec[3]=1.0;
}
void Entity::EntityShininess (GLfloat shine)
{
m_shininess=shine*128.0;
if (m_shininess > 128.0) m_shininess=128.0;
}
void Entity::Prepare()
{
int i;
double sn, cs;
glMaterialfv (GL_FRONT, GL_AMBIENT, m_amb);
glMaterialfv (GL_FRONT, GL_DIFFUSE, m_dif);
glMaterialfv (GL_FRONT, GL_SPECULAR, m_spec);
glMaterialf (GL_FRONT, GL_SHININESS, m_shininess);
if (m_texture)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_texture->getTexFilter());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_texture->getTexFilter());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_texture->getTexWrap());
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_texture->getTexWrap());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_texture->getEnvMode());
glBindTexture (GL_TEXTURE_2D, m_texture->getNumber()+1);
glEnable (GL_TEXTURE_2D);
}
else glDisable (GL_TEXTURE_2D);
glPushMatrix();
glLoadMatrixd (m_matrix);
for (i=0; i<m_command.size(); i++)
{
switch (m_command[i].name)
{
case 'p':
{
glGetDoublev(GL_MODELVIEW_MATRIX, m_matrix);
m_matrix[12]=m_command[i].param[0];
m_matrix[13]=m_command[i].param[1];
m_matrix[14]=m_command[i].param[2];
glLoadMatrixd (m_matrix);
} break;
case 'm':
glTranslated (m_command[i].param[0], m_command[i].param[1], m_command[i].param[2]); break;
case 'r':
{
glGetDoublev(GL_MODELVIEW_MATRIX, m_matrix);
sn=sin(m_command[i].param[0]*3.1416/180.0); cs=cos(m_command[i].param[0]*3.1416/180.0);
if (m_command[i].param[1]==1.0)
{
m_matrix[5]=cs;
m_matrix[6]=sn;
m_matrix[9]=-sn;
m_matrix[10]=cs;
}
else if (m_command[i].param[2]==1.0)
{
m_matrix[0]=cs;
m_matrix[2]=-sn;
m_matrix[8]=sn;
m_matrix[10]=cs;
}
else
{
m_matrix[0]=cs;
m_matrix[1]=sn;
m_matrix[4]=-sn;
m_matrix[5]=cs;
}
glLoadMatrixd (m_matrix);
} break;
case 'M':
{
for (int j=0; j<16; j++) m_matrix[j]=m_command[i].param[j];
glLoadMatrixd (m_matrix);
}
break;
case 't':
glRotated (m_command[i].param[0], m_command[i].param[1], m_command[i].param[2], m_command[i].param[3]); break;
default:;
};
}
glGetDoublev(GL_MODELVIEW_MATRIX, m_matrix);
glPopMatrix();
if (m_parent) glMultMatrixd (m_parent->getMatrix());
glMultMatrixd(m_matrix);
glScaled (m_xscale, m_yscale, m_zscale);
m_command.clear();
}
void Entity::Draw()
{
;
}
BlitzStyle::BlitzStyle()
{
m_last=-1;
m_last_tex=-1;
}
BlitzStyle::~BlitzStyle()
{
;
}
long int BlitzStyle::AddEntity(Entity* e)
{
m_last++;
e->setNumber(m_last);
m_entity.push_back(e);
m_hierarchy.push_back(e);
glNewList (m_last+1, GL_COMPILE);
e->Draw();
glEndList();
return m_last;
}
Entity* BlitzStyle::DeleteEntity (int id)
{
glDeleteLists (id+1, 1);
Entity* tmp=m_entity[id];
m_entity[id]->setNumber(-1);
m_hierarchy.remove (tmp);
m_entity[id]=0;
return tmp;
}
bool BlitzStyle::EntityExists (int id)
{
if (id>=0 && id<=m_last && m_entity[id]) return true;
else return false;
}
void BlitzStyle::EntityParent (int entity_id, int parent_id)
{
list <Entity*>::iterator i;
m_entity[entity_id]->setParent(m_entity[parent_id]);
m_entity[parent_id]->AddChild(m_entity[entity_id]);
if (parent_id > entity_id)
{
for (i=m_hierarchy.begin(); i!=m_hierarchy.end(); i++)
{
if (*i==m_entity[entity_id]) *i=m_entity[parent_id];
else if (*i==m_entity[parent_id]) {*i=m_entity[entity_id]; break;}
}
}
}
void BlitzStyle::FreeEntity (int id)
{
(m_entity[id]->getParent()) -> FreeChild(m_entity[id]);
m_entity[id]->setParent(0);
}
void BlitzStyle::PositionEntity (int id, double x, double y, double z)
{
m_entity[id]->PositionEntity (x, y, z);
}
void BlitzStyle::MoveEntity (int id, double dx, double dy, double dz)
{
m_entity[id]->MoveEntity (dx, dy, dz);
}
void BlitzStyle::RotateEntityOX (int id, double pitch)
{
m_entity[id]->RotateEntityOX (pitch);
}
void BlitzStyle::RotateEntityOY (int id, double yaw)
{
m_entity[id]->RotateEntityOY (yaw);
}
void BlitzStyle::RotateEntityOZ (int id, double roll)
{
m_entity[id]->RotateEntityOZ (roll);
}
void BlitzStyle::TurnEntityOX (int id, double pitch)
{
m_entity[id]->TurnEntityOX (pitch);
}
void BlitzStyle::TurnEntityOY (int id, double yaw)
{
m_entity[id]->TurnEntityOY (yaw);
}
void BlitzStyle::TurnEntityOZ (int id, double roll)
{
m_entity[id]->TurnEntityOZ (roll);
}
void BlitzStyle::ScaleEntity (int id, double xscale, double yscale, double zscale)
{
m_entity[id]->ScaleEntity (xscale, yscale, zscale);
}
void BlitzStyle::LoadMatrix (int id, double* matrix)
{
m_entity[id]->LoadMatrix (matrix);
}
void BlitzStyle::EntityMaterial (int id, GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb)
{
m_entity[id]->EntityMaterial (ambr, ambg, ambb, difr, difg, difb, specr, specg, specb);
}
void BlitzStyle::EntityShininess (int id, GLfloat shine)
{
m_entity[id]->EntityShininess (shine);
}
int BlitzStyle::LoadTexture (char* path)
{
m_last_tex++;
Texture* tex=new Texture();
tex->setNumber (m_last_tex);
m_texture.push_back (tex);
glBindTexture (GL_TEXTURE_2D, m_last_tex+1);
AUX_RGBImageRec* image=auxDIBImageLoad (path);
if (image)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->data);
}
return m_last_tex;
}
void BlitzStyle::TextureMode (int texture_id, GLenum tex_filter, GLenum tex_wrap, GLenum env_mode)
{
m_texture[texture_id]->setParams (tex_filter, tex_wrap, env_mode);
}
void BlitzStyle::AddTexture (int entity_id, int texture_id)
{
m_entity[entity_id]->setTexture (m_texture[texture_id]);
}
void BlitzStyle::UpdateWorld()
{
list <Entity*>::iterator i;
for (i=m_hierarchy.begin(); i!=m_hierarchy.end(); i++)
{
glPushMatrix();
(*i)->Prepare();
if ((*i)->isVisible()) glCallList ((*i)->getNumber()+1);
glPopMatrix();
}
}