| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205 |
- #include "FactoryClient.h"
- #include <File.h>
- #include <FileSystem.h>
- #include <Image.h>
- #include <M3File.h>
- #include <Texture.h>
- #include "Globals.h"
- #include "Load.h"
- using namespace Network;
- using namespace Framework;
- void createDefaultCube(Screen* zScreen)
- {
- Model3DData* data = zScreen->zGraphicsApi()->createModel("cube");
- data->setAmbientFactor(0.f);
- data->setDiffusFactor(1.f);
- data->setSpecularFactor(0.f);
- float size = 1;
- float left, right, top, bottom;
- // Calculate the screen coordinates of the left side of the bitmap.
- right = (float)((-size / 2.0));
- // Calculate the screen coordinates of the right side of the bitmap.
- left = right + (float)size;
- // Calculate the screen coordinates of the top of the bitmap.
- top = (float)(size / 2.0);
- // Calculate the screen coordinates of the bottom of the bitmap.
- bottom = top - (float)size;
- float front = -size / 2;
- float back = front + size;
- Vertex3D* vertecies = new Vertex3D[24];
- for (int i = 0; i < 24; i++)
- vertecies[i].knochenId = 0;
- // front side
- vertecies[0].pos = Vec3<float>(left, front, top);
- vertecies[0].tPos = Vec2<float>(0.f, 0.f);
- vertecies[1].pos = Vec3<float>(right, front, top);
- vertecies[1].tPos = Vec2<float>(1.f, 0.f);
- vertecies[2].pos = Vec3<float>(left, front, bottom);
- vertecies[2].tPos = Vec2<float>(0.f, 1.f);
- vertecies[3].pos = Vec3<float>(right, front, bottom);
- vertecies[3].tPos = Vec2<float>(1.f, 1.f);
- // back side
- vertecies[4].pos = Vec3<float>(right, back, top);
- vertecies[4].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[5].pos = Vec3<float>(left, back, top);
- vertecies[5].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[6].pos = Vec3<float>(right, back, bottom);
- vertecies[6].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[7].pos = Vec3<float>(left, back, bottom);
- vertecies[7].tPos = Vec2<float>(1.0f, 1.0f);
- // left side
- vertecies[8].pos = Vec3<float>(left, back, top);
- vertecies[8].tPos = Vec2<float>(0.f, 0.f);
- vertecies[9].pos = Vec3<float>(left, front, top);
- vertecies[9].tPos = Vec2<float>(1.f, 0.f);
- vertecies[10].pos = Vec3<float>(left, back, bottom);
- vertecies[10].tPos = Vec2<float>(0.f, 1.f);
- vertecies[11].pos = Vec3<float>(left, front, bottom);
- vertecies[11].tPos = Vec2<float>(1.f, 1.f);
- // right side
- vertecies[12].pos = Vec3<float>(right, front, top);
- vertecies[12].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[13].pos = Vec3<float>(right, back, top);
- vertecies[13].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[14].pos = Vec3<float>(right, front, bottom);
- vertecies[14].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[15].pos = Vec3<float>(right, back, bottom);
- vertecies[15].tPos = Vec2<float>(1.0f, 1.0f);
- // top side
- vertecies[16].pos = Vec3<float>(left, back, top);
- vertecies[16].tPos = Vec2<float>(0.f, 0.f);
- vertecies[17].pos = Vec3<float>(right, back, top);
- vertecies[17].tPos = Vec2<float>(1.f, 0.f);
- vertecies[18].pos = Vec3<float>(left, front, top);
- vertecies[18].tPos = Vec2<float>(0.f, 1.f);
- vertecies[19].pos = Vec3<float>(right, front, top);
- vertecies[19].tPos = Vec2<float>(1.f, 1.f);
- // botom side
- vertecies[20].pos = Vec3<float>(left, front, bottom);
- vertecies[20].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[21].pos = Vec3<float>(right, front, bottom);
- vertecies[21].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[22].pos = Vec3<float>(left, back, bottom);
- vertecies[22].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[23].pos = Vec3<float>(right, back, bottom);
- vertecies[23].tPos = Vec2<float>(1.0f, 1.0f);
- data->setVertecies(vertecies, 24);
- // the order of the polygons has to be NORTH (front), EAST (left), SOUTH
- // (back), WEST (right), TOP, BOTTOM according to the Area definition
- // front side
- Polygon3D* p
- = new Polygon3D(); // looking from (0,0,0) to (1,0,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0;
- p->indexList[1] = 1;
- p->indexList[2] = 2;
- p->indexList[3] = 1;
- p->indexList[4] = 3;
- p->indexList[5] = 2;
- data->addPolygon(p);
- // left side
- p = new Polygon3D(); // looking from (0,0,0) to (0,1,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 8;
- p->indexList[1] = 1 + 8;
- p->indexList[2] = 2 + 8;
- p->indexList[3] = 1 + 8;
- p->indexList[4] = 3 + 8;
- p->indexList[5] = 2 + 8;
- data->addPolygon(p);
- // back side
- p = new Polygon3D(); // looking from (0,0,0) to (-1,0,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 4;
- p->indexList[1] = 1 + 4;
- p->indexList[2] = 2 + 4;
- p->indexList[3] = 1 + 4;
- p->indexList[4] = 3 + 4;
- p->indexList[5] = 2 + 4;
- data->addPolygon(p);
- // right side
- p = new Polygon3D(); // looking from (0,0,0) to (0,-1,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 12;
- p->indexList[1] = 1 + 12;
- p->indexList[2] = 2 + 12;
- p->indexList[3] = 1 + 12;
- p->indexList[4] = 3 + 12;
- p->indexList[5] = 2 + 12;
- data->addPolygon(p);
- // top side
- p = new Polygon3D(); // looking from (0,0,0) to (0,0,-1) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 16;
- p->indexList[1] = 1 + 16;
- p->indexList[2] = 2 + 16;
- p->indexList[3] = 1 + 16;
- p->indexList[4] = 3 + 16;
- p->indexList[5] = 2 + 16;
- data->addPolygon(p);
- // botom side
- p = new Polygon3D(); // looking from (0,0,0) to (0,0,1) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 20;
- p->indexList[1] = 1 + 20;
- p->indexList[2] = 2 + 20;
- p->indexList[3] = 1 + 20;
- p->indexList[4] = 3 + 20;
- p->indexList[5] = 2 + 20;
- data->addPolygon(p);
- data->calculateNormals();
- data->release();
- }
- void createGrass(Screen* zScreen)
- {
- Model3DData* data = zScreen->zGraphicsApi()->createModel("grass");
- data->setAmbientFactor(0.f);
- data->setDiffusFactor(1.f);
- data->setSpecularFactor(0.f);
- float size = 1;
- float left, right, top, bottom;
- // Calculate the screen coordinates of the left side of the bitmap.
- right = (float)((-size / 2.0));
- // Calculate the screen coordinates of the right side of the bitmap.
- left = right + (float)size;
- // Calculate the screen coordinates of the top of the bitmap.
- top = (float)(size / 2.0);
- // Calculate the screen coordinates of the bottom of the bitmap.
- bottom = top - (float)size;
- float front = -size / 2;
- float back = front + size;
- Vertex3D* vertecies = new Vertex3D[32];
- for (int i = 0; i < 32; i++)
- vertecies[i].knochenId = 0;
- // first y plane
- vertecies[0].pos = Vec3<float>(left, front + 0.2f, top);
- vertecies[0].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[1].pos = Vec3<float>(right, front + 0.2f, top);
- vertecies[1].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[2].pos = Vec3<float>(left, front + 0.2f, bottom);
- vertecies[2].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[3].pos = Vec3<float>(right, front + 0.2f, bottom);
- vertecies[3].tPos = Vec2<float>(0.99f, 0.99f);
- // second y plane
- vertecies[4].pos = Vec3<float>(left, front + 0.4f, top);
- vertecies[4].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[5].pos = Vec3<float>(right, front + 0.4f, top);
- vertecies[5].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[6].pos = Vec3<float>(left, front + 0.4f, bottom);
- vertecies[6].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[7].pos = Vec3<float>(right, front + 0.4f, bottom);
- vertecies[7].tPos = Vec2<float>(0.99f, 0.99f);
- // third y plane
- vertecies[8].pos = Vec3<float>(left, front + 0.6f, top);
- vertecies[8].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[9].pos = Vec3<float>(right, front + 0.6f, top);
- vertecies[9].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[10].pos = Vec3<float>(left, front + 0.6f, bottom);
- vertecies[10].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[11].pos = Vec3<float>(right, front + 0.6f, bottom);
- vertecies[11].tPos = Vec2<float>(0.99f, 0.99f);
- // forth y plane
- vertecies[12].pos = Vec3<float>(left, front + 0.8f, top);
- vertecies[12].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[13].pos = Vec3<float>(right, front + 0.8f, top);
- vertecies[13].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[14].pos = Vec3<float>(left, front + 0.8f, bottom);
- vertecies[14].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[15].pos = Vec3<float>(right, front + 0.8f, bottom);
- vertecies[15].tPos = Vec2<float>(0.99f, 0.99f);
- // first x plane
- vertecies[16].pos = Vec3<float>(right + 0.2f, front, top);
- vertecies[16].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[17].pos = Vec3<float>(right + 0.2f, back, top);
- vertecies[17].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[18].pos = Vec3<float>(right + 0.2f, front, bottom);
- vertecies[18].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[19].pos = Vec3<float>(right + 0.2f, back, bottom);
- vertecies[19].tPos = Vec2<float>(0.99f, 0.99f);
- // second x plane
- vertecies[20].pos = Vec3<float>(right + 0.4f, front, top);
- vertecies[20].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[21].pos = Vec3<float>(right + 0.4f, back, top);
- vertecies[21].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[22].pos = Vec3<float>(right + 0.4f, front, bottom);
- vertecies[22].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[23].pos = Vec3<float>(right + 0.4f, back, bottom);
- vertecies[23].tPos = Vec2<float>(0.99f, 0.99f);
- // third x plane
- vertecies[24].pos = Vec3<float>(right + 0.6f, front, top);
- vertecies[24].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[25].pos = Vec3<float>(right + 0.6f, back, top);
- vertecies[25].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[26].pos = Vec3<float>(right + 0.6f, front, bottom);
- vertecies[26].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[27].pos = Vec3<float>(right + 0.6f, back, bottom);
- vertecies[27].tPos = Vec2<float>(0.99f, 0.99f);
- // forth x plane
- vertecies[28].pos = Vec3<float>(right + 0.8f, front, top);
- vertecies[28].tPos = Vec2<float>(0.01f, 0.01f);
- vertecies[29].pos = Vec3<float>(right + 0.8f, back, top);
- vertecies[29].tPos = Vec2<float>(0.99f, 0.01f);
- vertecies[30].pos = Vec3<float>(right + 0.8f, front, bottom);
- vertecies[30].tPos = Vec2<float>(0.01f, 0.99f);
- vertecies[31].pos = Vec3<float>(right + 0.8f, back, bottom);
- vertecies[31].tPos = Vec2<float>(0.99f, 0.99f);
- for (int i = 0; i < 16; i++)
- {
- vertecies[i].normal = Vec3<float>(0, 1, 0);
- }
- for (int i = 16; i < 32; i++)
- {
- vertecies[i].normal = Vec3<float>(1, 0, 0);
- }
- data->setVertecies(vertecies, 32);
- Polygon3D* p = new Polygon3D();
- p->indexAnz = 6 * 8;
- p->indexList = new int[p->indexAnz];
- for (int i = 0; i < 8; i++)
- {
- p->indexList[i * 6 + 0] = 0 + i * 4;
- p->indexList[i * 6 + 1] = 1 + i * 4;
- p->indexList[i * 6 + 2] = 2 + i * 4;
- p->indexList[i * 6 + 3] = 1 + i * 4;
- p->indexList[i * 6 + 4] = 3 + i * 4;
- p->indexList[i * 6 + 5] = 2 + i * 4;
- }
- data->addPolygon(p);
- // data->calculateNormals();
- data->release();
- }
- void createFluidCube(Screen* zScreen)
- {
- Model3DData* data = zScreen->zGraphicsApi()->createModel("fluid");
- data->setAmbientFactor(0.f);
- data->setDiffusFactor(1.f);
- data->setSpecularFactor(0.f);
- float size = 1;
- float left, right, top, bottom;
- // Calculate the screen coordinates of the left side of the bitmap.
- right = (float)((-size / 2.0));
- // Calculate the screen coordinates of the right side of the bitmap.
- left = right + (float)size;
- // Calculate the screen coordinates of the top of the bitmap.
- top = (float)(size / 2.0);
- // Calculate the screen coordinates of the bottom of the bitmap.
- bottom = top - (float)size;
- float front = -size / 2;
- float back = front + size;
- Vertex3D* vertecies = new Vertex3D[24];
- for (int i = 0; i < 24; i++)
- vertecies[i].knochenId = 0;
- // front side
- vertecies[0].pos = Vec3<float>(left, front, top);
- vertecies[0].tPos = Vec2<float>(0.f, 0.f);
- vertecies[1].pos = Vec3<float>(right, front, top);
- vertecies[1].tPos = Vec2<float>(1.f, 0.f);
- vertecies[2].pos = Vec3<float>(left, front, bottom);
- vertecies[2].tPos = Vec2<float>(0.f, 1.f);
- vertecies[3].pos = Vec3<float>(right, front, bottom);
- vertecies[3].tPos = Vec2<float>(1.f, 1.f);
- // back side
- vertecies[4].pos = Vec3<float>(right, back, top);
- vertecies[4].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[5].pos = Vec3<float>(left, back, top);
- vertecies[5].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[6].pos = Vec3<float>(right, back, bottom);
- vertecies[6].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[7].pos = Vec3<float>(left, back, bottom);
- vertecies[7].tPos = Vec2<float>(1.0f, 1.0f);
- // left side
- vertecies[8].pos = Vec3<float>(left, back, top);
- vertecies[8].tPos = Vec2<float>(0.f, 0.f);
- vertecies[9].pos = Vec3<float>(left, front, top);
- vertecies[9].tPos = Vec2<float>(1.f, 0.f);
- vertecies[10].pos = Vec3<float>(left, back, bottom);
- vertecies[10].tPos = Vec2<float>(0.f, 1.f);
- vertecies[11].pos = Vec3<float>(left, front, bottom);
- vertecies[11].tPos = Vec2<float>(1.f, 1.f);
- // right side
- vertecies[12].pos = Vec3<float>(right, front, top);
- vertecies[12].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[13].pos = Vec3<float>(right, back, top);
- vertecies[13].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[14].pos = Vec3<float>(right, front, bottom);
- vertecies[14].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[15].pos = Vec3<float>(right, back, bottom);
- vertecies[15].tPos = Vec2<float>(1.0f, 1.0f);
- // top side
- vertecies[16].pos = Vec3<float>(left, back, top);
- vertecies[16].tPos = Vec2<float>(0.f, 0.f);
- vertecies[17].pos = Vec3<float>(right, back, top);
- vertecies[17].tPos = Vec2<float>(1.f, 0.f);
- vertecies[18].pos = Vec3<float>(left, front, top);
- vertecies[18].tPos = Vec2<float>(0.f, 1.f);
- vertecies[19].pos = Vec3<float>(right, front, top);
- vertecies[19].tPos = Vec2<float>(1.f, 1.f);
- // botom side
- vertecies[20].pos = Vec3<float>(left, front, bottom);
- vertecies[20].tPos = Vec2<float>(0.0f, 0.0f);
- vertecies[21].pos = Vec3<float>(right, front, bottom);
- vertecies[21].tPos = Vec2<float>(1.0f, 0.0f);
- vertecies[22].pos = Vec3<float>(left, back, bottom);
- vertecies[22].tPos = Vec2<float>(0.0f, 1.0f);
- vertecies[23].pos = Vec3<float>(right, back, bottom);
- vertecies[23].tPos = Vec2<float>(1.0f, 1.0f);
- data->setVertecies(vertecies, 24);
- // the order of the polygons has to be NORTH (front), EAST (left), SOUTH
- // (back), WEST (right), TOP, BOTTOM according to the Area definition front
- // side
- Polygon3D* p
- = new Polygon3D(); // looking from (0,0,0) to (1,0,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0;
- p->indexList[1] = 1;
- p->indexList[2] = 2;
- p->indexList[3] = 1;
- p->indexList[4] = 3;
- p->indexList[5] = 2;
- data->addPolygon(p);
- // left side
- p = new Polygon3D(); // looking from (0,0,0) to (0,1,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 8;
- p->indexList[1] = 1 + 8;
- p->indexList[2] = 2 + 8;
- p->indexList[3] = 1 + 8;
- p->indexList[4] = 3 + 8;
- p->indexList[5] = 2 + 8;
- data->addPolygon(p);
- // back side
- p = new Polygon3D(); // looking from (0,0,0) to (-1,0,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 4;
- p->indexList[1] = 1 + 4;
- p->indexList[2] = 2 + 4;
- p->indexList[3] = 1 + 4;
- p->indexList[4] = 3 + 4;
- p->indexList[5] = 2 + 4;
- data->addPolygon(p);
- // right side
- p = new Polygon3D(); // looking from (0,0,0) to (0,-1,0) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 12;
- p->indexList[1] = 1 + 12;
- p->indexList[2] = 2 + 12;
- p->indexList[3] = 1 + 12;
- p->indexList[4] = 3 + 12;
- p->indexList[5] = 2 + 12;
- data->addPolygon(p);
- // top side
- p = new Polygon3D(); // looking from (0,0,0) to (0,0,-1) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 16;
- p->indexList[1] = 1 + 16;
- p->indexList[2] = 2 + 16;
- p->indexList[3] = 1 + 16;
- p->indexList[4] = 3 + 16;
- p->indexList[5] = 2 + 16;
- data->addPolygon(p);
- // botom side
- p = new Polygon3D(); // looking from (0,0,0) to (0,0,1) to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 20;
- p->indexList[1] = 1 + 20;
- p->indexList[2] = 2 + 20;
- p->indexList[3] = 1 + 20;
- p->indexList[4] = 3 + 20;
- p->indexList[5] = 2 + 20;
- data->addPolygon(p);
- data->calculateNormals();
- data->release();
- }
- void createModels(Screen* zScreen)
- {
- createDefaultCube(zScreen);
- createGrass(zScreen);
- createFluidCube(zScreen);
- }
- struct FileInfo
- {
- Text path;
- __int64 size;
- int year;
- int month;
- int day;
- int seconds;
- };
- FactoryClient::FactoryClient()
- {
- client = 0;
- background = 0;
- foreground = 0;
- backgroundReader = 0;
- foregroundReader = 0;
- bgReaderUsage = 0;
- fgReaderUsage = 0;
- }
- FactoryClient::~FactoryClient()
- {
- if (client) disconnect();
- }
- void FactoryClient::loadServerInfo()
- {
- LoadMenu* loadMenu = (LoadMenu*)(Menu*)menuRegister->get("load");
- Text resourcePath = "data/resources/";
- resourcePath.append() << client->getServerIp() << "/"
- << client->getServerPort() << "/";
- Text tmp = resourcePath + "metainfo.dat";
- File metaInfo(tmp);
- Trie<FileInfo*> metaInfos;
- Array<FileInfo*> metaInfoList;
- Array<FileInfo*> downloadOrder;
- if (metaInfo.exists())
- {
- loadMenu->beginNextStage(
- "reading resource metadata", (int)metaInfo.getSize());
- metaInfo.open(File::Style::read);
- while (!metaInfo.isEnd())
- {
- short len = 0;
- metaInfo.read((char*)&len, 2);
- char* path = new char[len + 1];
- metaInfo.read(path, len);
- path[len] = 0;
- FileInfo* fi = new FileInfo();
- fi->path = Text(path);
- fi->size = 0;
- metaInfo.read((char*)&fi->year, 4);
- metaInfo.read((char*)&fi->month, 4);
- metaInfo.read((char*)&fi->day, 4);
- metaInfo.read((char*)&fi->seconds, 4);
- metaInfos.set(path, len, fi);
- delete[] path;
- metaInfoList.add(fi);
- loadMenu->stageProgress(18 + len);
- }
- metaInfo.close();
- }
- short len = 0;
- foreground->getMessage((char*)&len, 2);
- while (len)
- {
- char* path = new char[len + 1];
- foreground->getMessage(path, len);
- path[len] = 0;
- FileInfo* fi = metaInfos.get(path, len);
- int year;
- int month;
- int day;
- int seconds;
- foreground->getMessage((char*)&year, 4);
- foreground->getMessage((char*)&month, 4);
- foreground->getMessage((char*)&day, 4);
- foreground->getMessage((char*)&seconds, 4);
- if (!fi || fi->year != year || fi->month != month || fi->day != day
- || fi->seconds != seconds)
- {
- if (!fi)
- {
- fi = new FileInfo();
- fi->path = Text(path);
- metaInfos.set(path, len, fi);
- metaInfoList.add(fi);
- }
- fi->year = year;
- fi->month = month;
- fi->day = day;
- fi->seconds = seconds;
- foreground->send("\1", 1);
- foreground->getMessage((char*)&fi->size, 8);
- downloadOrder.add(fi);
- }
- else
- {
- foreground->send("\0", 1);
- }
- delete[] path;
- foreground->getMessage((char*)&len, 2);
- }
- loadMenu->allProgress(1);
- char* buffer = new char[4096];
- loadMenu->beginNextStage(
- "download updated resouces", downloadOrder.getEntryCount());
- for (FileInfo* info : downloadOrder)
- {
- loadMenu->beginNextStep(info->path, (int)info->size);
- tmp = resourcePath + info->path;
- File file(tmp);
- if (!file.exists())
- {
- file.create();
- }
- file.open(File::Style::write);
- __int64 remaining = info->size;
- while (remaining > 0)
- {
- int toRead = remaining > 4096 ? 4096 : (int)remaining;
- foreground->getMessage(buffer, toRead);
- file.write(buffer, toRead);
- remaining -= toRead;
- loadMenu->stepProgress(toRead);
- }
- file.close();
- loadMenu->stageProgress(1);
- }
- delete[] buffer;
- loadMenu->allProgress(1);
- loadMenu->beginNextStage(
- "writing resource metadata", metaInfoList.getEntryCount());
- if (!metaInfo.exists())
- {
- metaInfo.create();
- }
- metaInfo.open(File::Style::write);
- for (FileInfo* info : metaInfoList)
- {
- short len = (short)info->path.getLength();
- metaInfo.write((char*)&len, 2);
- metaInfo.write(info->path.getText(), len);
- metaInfo.write((char*)&info->year, 4);
- metaInfo.write((char*)&info->month, 4);
- metaInfo.write((char*)&info->day, 4);
- metaInfo.write((char*)&info->seconds, 4);
- loadMenu->stageProgress(1);
- }
- metaInfo.close();
- loadMenu->allProgress(1);
- loadMenu->beginNextStage("loading resources", metaInfoList.getEntryCount());
- for (FileInfo* info : metaInfoList)
- {
- if (info->path.endsWith(".ltdb"))
- {
- LTDBFile dat;
- dat.setFile(new Text(resourcePath + info->path));
- dat.readData(0);
- loadMenu->beginNextStep(info->path, dat.getImageCount());
- for (Text* name : *dat.zImageListe())
- {
- Image* b = dat.load(0, new Text(*name));
- uiFactory.initParam.bildschirm->zGraphicsApi()
- ->createOrGetTexture(info->path + "/" + *name, b)
- ->release();
- loadMenu->stepProgress(1);
- }
- }
- else if (info->path.endsWith(".m3"))
- {
- M3File dat(resourcePath + info->path);
- dat.readData();
- loadMenu->beginNextStep(info->path, dat.getModelCount());
- for (int i = 0; i < dat.getModelCount(); i++)
- {
- Model3DData* d = dat.loadModel(dat.zModelName(i)->getText(),
- uiFactory.initParam.bildschirm->zGraphicsApi(),
- info->path + "/" + *dat.zModelName(i));
- d->release();
- loadMenu->stepProgress(1);
- }
- }
- loadMenu->stageProgress(1);
- }
- loadMenu->allProgress(1);
- createModels(uiFactory.initParam.bildschirm);
- loadMenu->allProgress(1);
- std::cout << "downloading server type information\n";
- // receive type information
- for (int i = 0; i < blockTypeCount; i++)
- blockTypes[i]->release();
- delete[] blockTypes;
- for (int i = 0; i < itemTypeCount; i++)
- itemTypes[i]->release();
- delete[] itemTypes;
- for (int i = 0; i < entityTypeCount; i++)
- entityTypes[i]->release();
- delete[] entityTypes;
- foregroundReader->read((char*)&blockTypeCount, 4);
- blockTypes = new BlockType*[blockTypeCount];
- for (int i = 0; i < blockTypeCount; i++)
- {
- int id;
- foregroundReader->read((char*)&id, 4);
- bool needsInstance;
- foregroundReader->read((char*)&needsInstance, 1);
- bool needsSubscription;
- foregroundReader->read((char*)&needsSubscription, 1);
- bool fluid;
- foregroundReader->read((char*)&fluid, 1);
- char maxFlowDistance = 0;
- if (fluid) foregroundReader->read((char*)&maxFlowDistance, 1);
- Direction flowDirection;
- foregroundReader->read((char*)&flowDirection, 1);
- int maxHp;
- foregroundReader->read((char*)&maxHp, 4);
- blockTypes[i] = new BlockType(id,
- needsInstance,
- ModelInfo(foregroundReader),
- maxHp,
- needsSubscription,
- fluid,
- maxFlowDistance,
- flowDirection);
- }
- loadMenu->allProgress(1);
- foregroundReader->read((char*)&itemTypeCount, 4);
- itemTypes = new ItemType*[itemTypeCount];
- for (int i = 0; i < itemTypeCount; i++)
- {
- int id;
- foregroundReader->read((char*)&id, 4);
- char len;
- foregroundReader->read((char*)&len, 1);
- char* name = new char[len + 1];
- foregroundReader->read(name, len);
- name[len] = 0;
- short tlen;
- foregroundReader->read((char*)&tlen, 2);
- char* tooltipUIML = new char[tlen + 1];
- foregroundReader->read(tooltipUIML, tlen);
- tooltipUIML[tlen] = 0;
- itemTypes[i] = new ItemType(
- id, ModelInfo(foregroundReader), Text(name), Text(tooltipUIML));
- delete[] name;
- delete[] tooltipUIML;
- }
- loadMenu->allProgress(1);
- foregroundReader->read((char*)&entityTypeCount, 4);
- entityTypes = new EntityType*[entityTypeCount];
- for (int i = 0; i < entityTypeCount; i++)
- {
- int id;
- foregroundReader->read((char*)&id, 4);
- entityTypes[i] = new EntityType(id, ModelInfo(foregroundReader));
- }
- loadMenu->allProgress(1);
- loadMenu->beginNextStage("rendering item icons", itemTypeCount);
- Cam3D* kam = new Cam3D();
- World3D* w = new World3D();
- w->addDiffuseLight(DiffuseLight{
- Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f)});
- kam->setWorld(w);
- kam->setScreenPosition(0, 0);
- kam->setScreenSize(50, 50);
- kam->setPosition(Vec3<float>(0, 0, 0));
- kam->setRotation(
- {(float)PI / 2.f, 0.f, std::atan2(0.f, -1.f) + (float)PI / 2});
- Image* b = new Image();
- b->newImage(50, 50, 0);
- for (int i = 0; i < itemTypeCount; i++)
- {
- Model3D* mdl = new Model3D();
- Model3DData* data = itemTypes[i]->getItemModel();
- if (data)
- {
- Vec3<float> min = data->getMinPos();
- Vec3<float> max = data->getMaxPos();
- float maxX = MAX(
- MAX(MAX(abs(min.x), abs(max.x)), MAX(abs(min.y), abs(max.y))),
- MAX(abs(min.z), abs(max.z)));
- kam->setPosition(Vec3<float>(maxX * 3.5f, 0.f, 0.f));
- }
- mdl->setModelData(data);
- mdl->setModelTextur(itemTypes[i]->getItemTextur());
- mdl->setPosition(Vec3<float>(0.f, 0.f, 0.f));
- mdl->setRotation(0.25f, 0.25f, 0.55f);
- mdl->setAmbientFactor(0.8f);
- mdl->setDiffusFactor(0.1f);
- mdl->setSpecularFactor(0.1f);
- mdl->setSize(itemTypes[i]->getSize());
- w->addDrawable(mdl);
- w->tick(0);
- window->zScreen()->lock();
- DX11Texture* t = (DX11Texture*)window->zScreen()
- ->zGraphicsApi()
- ->createOrGetTexture(Text("rendered/items/")
- + itemTypes[i]->getId(),
- dynamic_cast<Image*>(b->getThis()));
- window->zScreen()->zGraphicsApi()->renderKamera(kam, t);
- Image* result = new Image();
- t->copyToImage(result);
- itemTypes[i]->setBild(result);
- t->release();
- window->zScreen()->unlock();
- w->removeDrawable(mdl);
- loadMenu->stageProgress(1);
- }
- b->release();
- kam->release();
- loadMenu->allProgress(1);
- }
- bool FactoryClient::connect(Text ip, unsigned short sslPort)
- {
- if (client) disconnect();
- client = new SSLClient();
- if (!client->connect(sslPort, ip)) return false;
- char c;
- while (client->hasMessage(1))
- client->getMessage(&c, 1);
- this->ip = ip;
- return 1;
- }
- int FactoryClient::ping()
- {
- Timer zm;
- zm.measureStart();
- if (!client->send("\3", 1)) return -1;
- char c;
- client->getMessage(&c, 1);
- zm.measureEnd();
- return (int)(zm.getSekunden() * 1000);
- }
- int FactoryClient::status(Framework::Text name, Framework::Text secret)
- {
- if (!client->send("\4", 1)) return 404;
- char c;
- client->getMessage(&c, 1);
- if (c == 1)
- {
- char len = (char)name.getLength();
- client->send(&len, 1);
- client->send(name, len);
- short sLen = (short)secret.getLength();
- client->send((char*)&sLen, 2);
- client->send(secret, sLen);
- char res;
- client->getMessage(&res, 1);
- if (res == 1) return 200;
- if (res == 0) return 403;
- }
- return 404;
- }
- int FactoryClient::join(
- Framework::Text name, Framework::Text& secret, unsigned short port)
- {
- client->send("\1", 1);
- char len = (char)name.getLength();
- client->send(&len, 1);
- client->send(name, len);
- short sLen = (short)secret.getLength();
- client->send((char*)&sLen, 2);
- client->send(secret, sLen);
- char res;
- client->getMessage(&res, 1);
- if (res == 1 || res == 2)
- {
- if (res == 2)
- {
- client->getMessage((char*)&sLen, 2);
- char* buffer = new char[sLen + 1];
- client->getMessage(buffer, sLen);
- buffer[sLen] = 0;
- secret = buffer;
- delete[] buffer;
- }
- short keyLen;
- client->getMessage((char*)&keyLen, 2);
- char* key = new char[keyLen];
- client->getMessage(key, keyLen);
- foreground = new Client();
- if (!foreground->connect(port, ip))
- {
- delete[] key;
- return false;
- }
- if (!foreground->send((char*)&keyLen, 2))
- {
- delete[] key;
- return false;
- }
- if (!foreground->send(key, keyLen))
- {
- delete[] key;
- return false;
- }
- background = new Client();
- if (!background->connect(port, ip))
- {
- delete[] key;
- foreground->release();
- foreground = 0;
- background->release();
- background = 0;
- return false;
- }
- if (!background->send((char*)&keyLen, 2))
- {
- delete[] key;
- foreground->release();
- foreground = 0;
- background->release();
- background = 0;
- return false;
- }
- if (!background->send(key, keyLen))
- {
- delete[] key;
- foreground->release();
- foreground = 0;
- background->release();
- background = 0;
- return false;
- }
- delete[] key;
- bool bg = 0;
- if (!foreground->send((char*)&bg, 1))
- {
- delete[] key;
- return 201;
- }
- foregroundReader = new NetworkReader(foreground);
- bg = 1;
- if (!background->send((char*)&bg, 1)) return 201;
- backgroundReader = new NetworkReader(background);
- char res;
- foregroundReader->read(&res, 1);
- if (res != 1) return 403;
- backgroundReader->read(&res, 1);
- if (res != 1) return 403;
- client->disconnect();
- loadServerInfo();
- return 200;
- }
- if (res == 0) return 403;
- return 500;
- }
- void FactoryClient::disconnect()
- {
- if (client)
- {
- NetworkReader* fgReader = foregroundReader;
- NetworkReader* bgReader = backgroundReader;
- backgroundReader = 0;
- foregroundReader = 0;
- if (foreground) foreground->disconnect();
- if (background) background->disconnect();
- while (fgReaderUsage > 0 || bgReaderUsage > 0)
- Sleep(100);
- delete fgReader;
- delete bgReader;
- client->release();
- client = 0;
- if (foreground) foreground->release();
- foreground = 0;
- if (background) background->release();
- background = 0;
- }
- }
- NetworkReader* FactoryClient::getNextForegroundMessage()
- {
- fgReaderUsage++;
- if (!foreground) return 0;
- if (!foreground->isConnected()) return 0;
- if (!foreground->hasMessage(0)) return 0;
- return foregroundReader;
- }
- NetworkReader* FactoryClient::getNextBackgroundMessage()
- {
- bgReaderUsage++;
- if (!background) return 0;
- if (!background->isConnected()) return 0;
- if (!background->hasMessage(0)) return 0;
- return backgroundReader;
- }
- void FactoryClient::endMessageReading(bool bg)
- {
- if (bg)
- bgReaderUsage--;
- else
- fgReaderUsage--;
- }
- void FactoryClient::sendPlayerAction(const char* data, unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- length += 1;
- foreground->send((char*)&length, 2);
- char msgId = 2;
- foreground->send(&msgId, 1);
- foreground->send((char*)data, length - 1);
- cs.unlock();
- }
- void FactoryClient::sendPlayerFaceDirection(Vec3<float> dir)
- {
- if (!foreground) return;
- cs.lock();
- short length = 14;
- foreground->send((char*)&length, 2);
- char msgId = 2; // player message
- foreground->send(&msgId, 1);
- foreground->send(&msgId, 1); // set face direction
- foreground->send((char*)&dir.x, 4);
- foreground->send((char*)&dir.y, 4);
- foreground->send((char*)&dir.z, 4);
- cs.unlock();
- }
- void FactoryClient::sendPlayerMovement(int flags)
- {
- if (!foreground) return;
- cs.lock();
- short length = 6;
- foreground->send((char*)&length, 2);
- char msgId = 2; // player message
- foreground->send(&msgId, 1);
- msgId = 10; // set movement
- foreground->send(&msgId, 1);
- foreground->send((char*)&flags, 4);
- cs.unlock();
- }
- void FactoryClient::entityAPIRequest(
- int entityId, const char* message, unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- length += 5;
- foreground->send((char*)&length, 2);
- char msgId = 3;
- foreground->send(&msgId, 1);
- foreground->send((char*)&entityId, 4);
- foreground->send(message, length - 5);
- cs.unlock();
- }
- void FactoryClient::blockAPIRequest(
- Vec3<int> pos, const char* message, unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- length += 14;
- foreground->send((char*)&length, 2);
- char msgId = 1;
- foreground->send(&msgId, 1);
- foreground->send(&msgId, 1);
- foreground->send((char*)&pos.x, 4);
- foreground->send((char*)&pos.y, 4);
- foreground->send((char*)&pos.z, 4);
- foreground->send(message, length - 14);
- cs.unlock();
- }
- void FactoryClient::componentAPIRequest(
- int* address, int addrLen, const char* message, unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- short sum = length + addrLen * 4 + (addrLen > 2 ? 3 : 2);
- foreground->send((char*)&sum, 2);
- if (addrLen > 2)
- {
- char msgId = 7; // dimension request
- foreground->send(&msgId, 1);
- foreground->send((char*)&address[0], 4); // dimension id
- msgId = 1; // block request
- foreground->send(&msgId, 1);
- foreground->send((char*)&address[1], 4); // position x
- foreground->send((char*)&address[2], 4); // position y
- foreground->send((char*)&address[3], 4); // position z
- msgId = 2; // component request
- foreground->send(&msgId, 1);
- foreground->send((char*)&address[4], 4); // component index
- }
- else
- {
- char msgId = 3; // entity request
- foreground->send(&msgId, 1);
- foreground->send((char*)&address[0], 4); // entity id
- msgId = 2; // component request
- foreground->send(&msgId, 1);
- foreground->send((char*)&address[1], 4); // component index
- }
- foreground->send(message, length);
- cs.unlock();
- }
- void FactoryClient::blockAPIRequest(
- int dimensionId, Vec3<int> pos, const char* message, unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- length += 18;
- foreground->send((char*)&length, 2);
- char msgId = 7;
- foreground->send(&msgId, 1);
- foreground->send((char*)&dimensionId, 4);
- msgId = 1;
- foreground->send(&msgId, 1);
- foreground->send((char*)&pos.x, 4);
- foreground->send((char*)&pos.y, 4);
- foreground->send((char*)&pos.z, 4);
- foreground->send(message, length - 18);
- cs.unlock();
- }
- void FactoryClient::chunkAPIRequest(
- Point center, const char* message, unsigned short length)
- {
- if (!foreground) return;
- length += 10;
- cs.lock();
- foreground->send((char*)&length, 2);
- char type = 1;
- foreground->send(&type, 1);
- type = 0;
- foreground->send(&type, 1);
- foreground->send((char*)¢er.x, 4);
- foreground->send((char*)¢er.y, 4);
- foreground->send(message, length - 10);
- cs.unlock();
- }
- void FactoryClient::dimensionAPIRequest(
- const char* message, unsigned short length)
- {
- if (!foreground) return;
- length += 1;
- cs.lock();
- foreground->send((char*)&length, 2);
- char type = 1;
- foreground->send(&type, 1);
- foreground->send(message, length - 1);
- cs.unlock();
- }
- void FactoryClient::inventoryAPIRequest(
- Framework::Either<int, Framework::VecN<int, 4>> target,
- const char* message,
- unsigned short length)
- {
- if (!foreground) return;
- cs.lock();
- length += target.isA() ? 6 : 18;
- foreground->send((char*)&length, 2);
- char msgId = 4;
- foreground->send(&msgId, 1);
- bool isEntity = target.isA();
- foreground->send((char*)&isEntity, 1);
- if (target.isA())
- {
- int id = target.getA();
- foreground->send((char*)&id, 4);
- }
- else
- {
- for (int i = 0; i < 4; i++)
- {
- int v = target.getB()[i];
- foreground->send((char*)&v, 4);
- }
- }
- foreground->send(message, length - (target.isA() ? 6 : 18));
- cs.unlock();
- }
- void FactoryClient::uiRequest(
- Framework::Text dialogId, const char* message, unsigned short length)
- {
- if (!foreground) return;
- short nameLen = (short)dialogId.getLength();
- length += nameLen + 3;
- foreground->send((char*)&length, 2);
- cs.lock();
- char msgId = 8;
- foreground->send(&msgId, 1);
- foreground->send((char*)&nameLen, 2);
- foreground->send(dialogId, nameLen);
- foreground->send(message, length - nameLen - 3);
- cs.unlock();
- }
- void FactoryClient::craftingUIMLRequest(int itemTypeId)
- {
- if (!foreground) return;
- cs.lock();
- short length = 5;
- foreground->send((char*)&length, 2);
- char msgId = 5;
- foreground->send(&msgId, 1);
- foreground->send((char*)&itemTypeId, 4);
- cs.unlock();
- }
- void FactoryClient::sendChatMessage(Framework::Text message)
- {
- if (!background) return;
- cs.lock();
- short length = message.getLength() + 4;
- background->send((char*)&length, 2);
- char msgId = 6;
- background->send(&msgId, 1);
- msgId = 0;
- background->send(&msgId, 1);
- length = message.getLength();
- background->send((char*)&length, 2);
- background->send(message.getText(), message.getLength());
- cs.unlock();
- }
- void FactoryClient::chatAPIRequest(const char* data, unsigned short length)
- {
- if (!background) return;
- cs.lock();
- short totalLength = length + 1;
- background->send((char*)&totalLength, 2);
- char msgId = 6;
- background->send(&msgId, 1);
- background->send(data, length);
- cs.unlock();
- }
- bool FactoryClient::isConnected()
- {
- return foreground && background && foreground->isConnected()
- && background->isConnected();
- }
- void FactoryClient::leaveGame()
- {
- cs.lock();
- disconnect();
- cs.unlock();
- }
|