Смекни!
smekni.com

Автоматизированное редактирование частиц в компьютерной графике (стр. 13 из 13)

ptrColor->r = r; ptrColor->g = g; ptrColor->b = b; ptrColor->a = a; ptrColor++;

ptrColor->r = r; ptrColor->g = g; ptrColor->b = b; ptrColor->a = a; ptrColor++;

ptrColor->r = r; ptrColor->g = g; ptrColor->b = b; ptrColor->a = a; ptrColor++;

ptrColor->r = r; ptrColor->g = g; ptrColor->b = b; ptrColor->a = a; ptrColor++;

if(is_rotate)

{

double* m = viewMatrixCache;

glGetDoublev(GL_MODELVIEW_MATRIX, m);

double x2 = x + w;

double y2 = y + h;

double* tp = (double*)ptrVert;

ptrVert->x = m[0]*x + m[4]*y + m[12];ptrVert->y = m[1]*x + m[5]*y + m[13];ptrVert++;

ptrVert->x = m[0]*x2 + m[4]*y + m[12];ptrVert->y = m[1]*x2 + m[5]*y + m[13];ptrVert++;

ptrVert->x = m[0]*x2 + m[4]*y2 + m[12];ptrVert->y = m[1]*x2 + m[5]*y2 + m[13]; ptrVert++;

ptrVert->x = tp[0]; ptrVert->y = tp[1]; ptrVert++;

ptrVert->x = tp[4]; ptrVert->y = tp[5]; ptrVert++;

ptrVert->x = m[0]*x + m[4]*y2 + m[12];ptrVert->y = m[1]*x + m[5]*y2 + m[13];ptrVert++;

glPopMatrix();

}

else

{

ptrVert->x = x;ptrVert->y = y;ptrVert++;

ptrVert->x = x+w;ptrVert->y = y;ptrVert++;

ptrVert->x = x+w;ptrVert->y = y+h;ptrVert++;

ptrVert->x = x;ptrVert->y = y;ptrVert++;

ptrVert->x = x+w;ptrVert->y = y+h;ptrVert++;

ptrVert->x = x;ptrVert->y = y+h;ptrVert++;

}

count++;

}

}

glPushMatrix();

glTranslated(currentX, currentY, 0.0);

glEnable(GL_TEXTURE_2D);

GLint binded;

glGetIntegerv(GL_TEXTURE_BINDING_2D,&binded);

glBindTexture(GL_TEXTURE_2D, particleTexPtr->getName());

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2, GL_DOUBLE, 0, verCoord);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glTexCoordPointer(2, GL_DOUBLE, 0, texCoord);

glEnableClientState(GL_COLOR_ARRAY);

glColorPointer(4, GL_DOUBLE, 0, color);

glDrawArrays(GL_TRIANGLES, 0, count*6);

glDisableClientState(GL_COLOR_ARRAY);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisableClientState(GL_VERTEX_ARRAY);

glBindTexture(GL_TEXTURE_2D,binded);

glDisable(GL_TEXTURE_2D);

glPopMatrix();

}

bool ParticleSystem::save(TiXmlElement* root_ptr) const

{

TiXmlElement element("PS");

element.SetAttribute(TEXTURE_ATTR, data.particleTexFileName);

element.SetDoubleAttribute(LIFE_TIME_ATTR, data.pLifeTime);

element.SetDoubleAttribute(APPEAR_DELAY_ATTR, data.pAppearDelay);

element.SetDoubleAttribute(PARTICLE_SPEED_X_ATTR, data.pMoveSpeedX);

element.SetDoubleAttribute(PARTICLE_SPEED_Y_ATTR, data.pMoveSpeedY);

element.SetDoubleAttribute(GRAVITY_X_ATTR, data.pGravityX);

element.SetDoubleAttribute(GRAVITY_Y_ATTR, data.pGravityY);

element.SetAttribute(MAX_COUNT_ATTR, data.pMaxCount);

element.SetAttribute(START_COUNT_ATTR, data.pCountOnStart);

element.SetDoubleAttribute(APPEAR_BOX_W_ATTR, data.pAppearBoxSize.width);

element.SetDoubleAttribute(APPEAR_BOX_H_ATTR, data.pAppearBoxSize.height);

element.SetDoubleAttribute(RED_BEGIN_ATTR, data.pRedBegin);

element.SetDoubleAttribute(RED_END_ATTR, data.pRedEnd);

element.SetDoubleAttribute(GREEN_BEGIN_ATTR, data.pGreenBegin);

element.SetDoubleAttribute(GREEN_END_ATTR, data.pGreenEnd);

element.SetDoubleAttribute(BLUE_BEGIN_ATTR, data.pBlueBegin);

element.SetDoubleAttribute(BLUE_END_ATTR, data.pBlueEnd);

element.SetDoubleAttribute(ALPHA_BEGIN_ATTR, data.pAlphaBegin);

element.SetDoubleAttribute(ALPHA_END_ATTR, data.pAlphaEnd);

element.SetDoubleAttribute(SCALE_BEGIN_W_ATTR, data.pKFScaleBeginW);

element.SetDoubleAttribute(SCALE_BEGIN_H_ATTR, data.pKFScaleBeginH);

element.SetDoubleAttribute(SCALE_END_W_ATTR, data.pKFScaleEndW);

element.SetDoubleAttribute(SCALE_END_H_ATTR, data.pKFScaleEndH);

element.SetDoubleAttribute(DISPERSION_X_ATTR, data.pKFDispersionX);

element.SetDoubleAttribute(DISPERSION_Y_ATTR, data.pKFDispersionY);

element.SetDoubleAttribute(PS_X_ATTR, data.initialX);

element.SetDoubleAttribute(PS_Y_ATTR, data.initialY);

element.SetDoubleAttribute(PS_SPEED_X_ATTR, data.speedChangeX);

element.SetDoubleAttribute(PS_SPEED_Y_ATTR, data.speedChangeY);

element.SetDoubleAttribute(START_DELAY_ATTR, data.startDelay);

element.SetDoubleAttribute(STOP_DELAY_ATTR, data.stopDelay);

element.SetDoubleAttribute(ROTATE_ATTR, data.pAngleRotate);

TiXmlElement* ptr_element = (TiXmlElement*)root_ptr->InsertEndChild(element);

return((bool)(ptr_element != NULL));

}

bool ParticleSystem::load(TiXmlElement*& element_ptr)

{

TiXmlElement *next_element_ptr = (TiXmlElement*)element_ptr->FirstChild("PS");

if(!next_element_ptr)

{

next_element_ptr = (TiXmlElement*)element_ptr->NextSibling("PS");

if(!next_element_ptr)

return false;

}

//read version 1.1 parameters

if(next_element_ptr->QueryIntAttribute(MAX_COUNT_ATTR, &data.pMaxCount) != TIXML_SUCCESS)

data.pMaxCount = EXPL_MAX;

//read all version parameters (obligatory)

if(next_element_ptr->QueryIntAttribute(START_COUNT_ATTR, &data.pCountOnStart) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(LIFE_TIME_ATTR, &data.pLifeTime) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(APPEAR_DELAY_ATTR, &data.pAppearDelay) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PARTICLE_SPEED_X_ATTR, &data.pMoveSpeedX) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PARTICLE_SPEED_Y_ATTR, &data.pMoveSpeedY) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(GRAVITY_X_ATTR, &data.pGravityX) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(GRAVITY_Y_ATTR, &data.pGravityY) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(APPEAR_BOX_W_ATTR, &data.pAppearBoxSize.width) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(APPEAR_BOX_H_ATTR, &data.pAppearBoxSize.height) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(RED_BEGIN_ATTR, &data.pRedBegin) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(RED_END_ATTR, &data.pRedEnd) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(GREEN_BEGIN_ATTR, &data.pGreenBegin) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(GREEN_END_ATTR, &data.pGreenEnd) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(BLUE_BEGIN_ATTR, &data.pBlueBegin) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(BLUE_END_ATTR, &data.pBlueEnd) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(ALPHA_BEGIN_ATTR, &data.pAlphaBegin) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(ALPHA_END_ATTR, &data.pAlphaEnd) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(SCALE_BEGIN_W_ATTR, &data.pKFScaleBeginW) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(SCALE_BEGIN_H_ATTR, &data.pKFScaleBeginH) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(SCALE_END_W_ATTR, &data.pKFScaleEndW) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(SCALE_END_H_ATTR, &data.pKFScaleEndH) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(DISPERSION_X_ATTR, &data.pKFDispersionX) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(DISPERSION_Y_ATTR, &data.pKFDispersionY) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PS_X_ATTR, &data.initialX) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PS_Y_ATTR, &data.initialY) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PS_SPEED_X_ATTR, &data.speedChangeX) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(PS_SPEED_Y_ATTR, &data.speedChangeY) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(START_DELAY_ATTR, &data.startDelay) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(STOP_DELAY_ATTR, &data.stopDelay) != TIXML_SUCCESS ||

next_element_ptr->QueryDoubleAttribute(ROTATE_ATTR, &data.pAngleRotate) != TIXML_SUCCESS)

return false;

currentX = data.initialX;

currentY = data.initialY;

setParticleTex(next_element_ptr->Attribute(TEXTURE_ATTR));

element_ptr = next_element_ptr;

return true;

}

void ParticleSystem::callback(double sec_interval)

{

if(isFinishedb)

return;

//check for startInTime

startInTime -= sec_interval;

//if (((int)startInTime*maj_kof) > 0)

if(startInTime > 0.0)

return;

else

startInTime = 0.0;

//change DX DY

currentX += data.speedChangeX* sec_interval;

currentY += data.speedChangeY* sec_interval;

//check for the next particle

timeForNextParticle -= sec_interval;

double addParticleTime = 1.0;

if (timeForNextParticle <= 0.0)

{

addParticleTime = timeForNextParticle;

timeForNextParticle = data.pAppearDelay;

}

bool need_to_finish = true;

if(isRunb)

{

need_to_finish = false;

stopInTime -= sec_interval;

//if (((int)stopInTime*maj_kof) <= 0)

if(stopInTime <= 0.0)

stop();

}

//calculate entire number of particles alive

int curr_numof_particles = 0;

for(int i=0; i<EXPL_MAX; i++)

if(pTM[i] > 0.0)

++curr_numof_particles;

//process particles

for (int i=0; i<EXPL_MAX; i++)

{

pTM[i] -= sec_interval;

if (pTM[i] <= 0.0)

{

if (addParticleTime <= 0.0 && curr_numof_particles < data.pMaxCount && isRunb)

{

//add new particle

addParticleTime += data.pAppearDelay;

++curr_numof_particles;

initParticleAtIndex(i);

}

}

else

{

need_to_finish = false;

pX[i] += sec_interval * pDx[i];

pY[i] += sec_interval * pDy[i];

pDx[i] += sec_interval * data.pGravityX;

pDy[i] += sec_interval * data.pGravityY;

}

}

if(need_to_finish)

finish();

}

void ParticleSystem::setBoundColor(wxColour color)

{

boundColor = color;

}

wxColour ParticleSystem::getBoundColor()

{

return boundColor;

}