#include "SnowProcessor.h" void SnowProcessor::buildMesh() { float triangles[18][3] { {0.05, 0, 0}, { -0.049745191718798834, 0.005041418536478406, 0 }, { -0.049745191718798834, -0.005041418536478393, 0 }, { 0.02500000000000001, 0.04330127018922193, 0 }, { -0.02923859238309949, -0.04055989047636787, 0 }, { -0.02050659933569937, -0.04560130901284627, 0 }, { -0.02499999999999999, 0.04330127018922194, 0 }, { 0.02050659933569934, -0.04560130901284628, 0 }, { 0.029238592383099463, -0.04055989047636788, 0 }, { -0.05, 6.123233995736766e-18, 0 }, { 0.049745191718798834, -0.005041418536478412, 0 }, { 0.049745191718798834, 0.005041418536478387, 0 }, { -0.025000000000000022, -0.04330127018922192, 0 }, { 0.0292385923830995, 0.040559890476367856, 0 }, { 0.020506599335699385, 0.04560130901284626, 0 }, { 0.02500000000000001, -0.04330127018922193, 0 }, { -0.020506599335699358, 0.04560130901284628, 0 }, { -0.029238592383099477, 0.04055989047636788, 0 }, }; mesh.init(18); for (int i = 0; i < 18; i++) mesh.setVertexAttrib(i, MESH_POSITION, triangles[i]); float white[3] = { 5, 5, 5 }; mesh.setMeshAttrib(MESH_AMBIENT_AND_DIFFUSE, white); meshBuilt = true; } void SnowProcessor::bindEvents() { __super::bindEvents(); bindEvent(ItemDusted); } TreeNode* SnowProcessor::getEventInfoObject(const char* eventName) { auto eventFolder = eventfunctions(classobject(holder))->find("eventInfo"); if (eventFolder) { auto info = eventFolder->find(eventName); if (info) { return info; } } return __super::getEventInfoObject(eventName); } void SnowProcessor::bindVariables(void) { Processor::bindVariables(); bindStateVariable(lastFallTime); bindStateVariable(flakes); bindVariable(rate); bindVariable(flakeCount); } double SnowProcessor::onReset() { Processor::onReset(); while (flakes.size() > flakeCount) flakes.pop_back(); double xSize = 1; double ySize = 1; double zSize = 2; while (flakes.size() < flakeCount) { double flakeX = uniform(0, xSize); double flakeY = uniform(0, -ySize); double flakeZ = zSize; double flakeRX = uniform(0, 360); double flakeRY = uniform(0, 360); flakes.add(new Snowflake(flakeX, flakeY, flakeZ, flakeRX, flakeRY)); } for (Snowflake* flake : flakes) { flake->reset(); } lastFallTime = 0; buildMesh(); return 0; } double SnowProcessor::onDraw(treenode view) { fglDisable(GL_TEXTURE_2D); if (!meshBuilt) buildMesh(); double curTime = time(); double dt = curTime - lastFallTime; double distance = rate * dt; for (Snowflake* flake : flakes) { flake->fall(distance); double x = 0, y = 0, z = 0; double rx = 0, ry = 0; flake->getTranslation(x, y, z); flake->getRotation(rx, ry); fglPushMatrix(); fglTranslate(x, y, z); fglRotate(rx, 1, 0, 0); fglRotate(ry, 0, 1, 0); mesh.draw(GL_TRIANGLES); fglPopMatrix(); } fglEnable(GL_TEXTURE_2D); lastFallTime = curTime; return __super::onDraw(view); } double SnowProcessor::onReceive(treenode item, int port) { double result = __super::onReceive(item, port); FIRE_SDT_EVENT(onItemDusted, item); return result; }