2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 #include "Objects/Object.hpp"
24 extern float viewdistance;
25 extern float fadestart;
26 extern int environment;
27 extern float texscale;
29 extern float multiplier;
31 extern FRUSTUM frustum;
32 extern Terrain terrain;
35 extern float blurness;
37 extern float playerdist;
38 extern bool skyboxtexture;
40 std::vector<std::unique_ptr<Object>> Object::objects;
42 float Object::radius = 0;
43 Texture Object::boxtextureptr;
44 Texture Object::treetextureptr;
45 Texture Object::bushtextureptr;
46 Texture Object::rocktextureptr;
73 Object::Object(object_type _type, XYZ _position, float _yaw, float _pitch, float _scale)
84 model.loaddecal("Models/Box.solid");
88 model.loaddecal("Models/Cool.solid");
92 model.loaddecal("Models/Wall.solid");
96 model.loaddecal("Models/Tunnel.solid");
100 model.loaddecal("Models/Chimney.solid");
104 model.load("Models/Spike.solid");
108 model.loaddecal("Models/Weird.solid");
112 model.loaddecal("Models/Rock.solid");
120 model.load("Models/TreeTrunk.solid");
124 scale += fabs((float)(Random() % 100) / 900) * scale;
125 model.load("Models/Leaves.solid");
129 position.y = terrain.getHeight(position.x, position.z) - .3;
130 model.load("Models/Bush.solid");
133 model.loaddecal("Models/Platform.solid");
134 model.Rotate(90, 0, 0);
142 if (friction == 1.5 && fabs(pitch) > 5) {
146 if (type == boxtype || type == cooltype || type == spiketype || type == weirdtype || type == walltype || type == chimneytype || type == tunneltype || type == platformtype) {
147 model.ScaleTexCoords(scale * 1.5);
149 if (type == rocktype) {
150 model.ScaleTexCoords(scale * 3);
153 if (type == treetrunktype || type == treeleavestype || type == rocktype) {
156 model.Scale(.3 * scale, .3 * scale, .3 * scale);
157 model.Rotate(90, 1, 1);
158 model.Rotate(pitch, 0, 0);
159 if (type == rocktype) {
160 model.Rotate(yaw * 5, 0, 0);
162 model.CalculateNormals(1);
163 model.ScaleNormals(-1, -1, -1);
166 void Object::handleFire()
168 if (type == firetype) {
173 if ((type == bushtype) || (type == firetype)) {
174 flamedelay -= multiplier * 3;
175 } else if (type == treeleavestype) {
176 flamedelay -= multiplier * 4;
178 while ((flamedelay < 0) && onfire) {
180 if ((type == bushtype) || (type == firetype)) {
181 spawnpoint.x = ((float)(Random() % 100)) / 30 * scale;
182 spawnpoint.y = ((float)(Random() % 100) + 60) / 30 * scale;
184 spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
185 spawnpoint += position;
186 Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 5 * scale, 1);
187 } else if (type == treeleavestype) {
188 spawnpoint.x = ((float)(Random() % 100)) / 80 * scale;
189 spawnpoint.y = ((float)(Random() % 100) + 80) / 12 * scale;
191 spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
192 spawnpoint += position;
193 Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 6, 1);
199 void Object::doShadows(XYZ lightloc)
201 XYZ testpoint, testpoint2, terrainpoint, col;
203 if (type != treeleavestype && type != treetrunktype && type != bushtype && type != firetype) {
204 for (int j = 0; j < model.vertexNum; j++) {
205 terrainpoint = position + DoRotation(model.vertex[j] + model.normals[j] * .1, 0, yaw, 0);
207 patchx = terrainpoint.x / (terrain.size / subdivision * terrain.scale);
208 patchz = terrainpoint.z / (terrain.size / subdivision * terrain.scale);
209 if (patchx >= 0 && patchz >= 0 && patchx < subdivision && patchz < subdivision) {
210 for (unsigned int k = 0; k < terrain.patchobjects[patchx][patchz].size(); k++) {
211 unsigned int l = terrain.patchobjects[patchx][patchz][k];
212 if (objects[l]->type != treetrunktype) {
213 testpoint = terrainpoint;
214 testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed);
215 if (objects[l]->model.LineCheck(&testpoint, &testpoint2, &col, &objects[l]->position, &objects[l]->yaw) != -1) {
216 shadowed = 1 - (findDistance(&terrainpoint, &col) / 50);
222 col = model.normals[j] - DoRotation(lightloc * shadowed, 0, -yaw, 0);
224 for (unsigned int k = 0; k < model.Triangles.size(); k++) {
225 if (model.Triangles[k].vertex[0] == j) {
227 model.vArray[l + 2] = col.x;
228 model.vArray[l + 3] = col.y;
229 model.vArray[l + 4] = col.z;
231 if (model.Triangles[k].vertex[1] == j) {
233 model.vArray[l + 10] = col.x;
234 model.vArray[l + 11] = col.y;
235 model.vArray[l + 12] = col.z;
237 if (model.Triangles[k].vertex[2] == j) {
239 model.vArray[l + 18] = col.x;
240 model.vArray[l + 19] = col.y;
241 model.vArray[l + 20] = col.z;
250 void Object::handleRot(int divide)
252 messedwith -= multiplier;
253 if (rotxvel || rotx) {
255 rotxvel -= multiplier * 8 * fabs(rotx);
257 rotxvel += multiplier * 8 * fabs(rotx);
259 rotxvel -= multiplier * 4;
261 rotxvel += multiplier * 4;
263 rotxvel -= multiplier * 4;
265 rotxvel += multiplier * 4;
266 if (fabs(rotx) < multiplier * 4)
268 if (fabs(rotxvel) < multiplier * 4)
271 rotx += rotxvel * multiplier * 4;
273 if (rotyvel || roty) {
275 rotyvel -= multiplier * 8 * fabs(roty);
277 rotyvel += multiplier * 8 * fabs(roty);
279 rotyvel -= multiplier * 4;
281 rotyvel += multiplier * 4;
283 rotyvel -= multiplier * 4;
285 rotyvel += multiplier * 4;
286 if (fabs(roty) < multiplier * 4)
288 if (fabs(rotyvel) < multiplier * 4)
291 roty += rotyvel * multiplier * 4;
294 glRotatef(roty / divide, 1, 0, 0);
297 glRotatef(-rotx / divide, 0, 0, 1);
311 static float distance;
312 static XYZ moved, terrainlight;
314 if (type == firetype) {
317 moved = DoRotation(model.boundingspherecenter, 0, yaw, 0);
318 if (type == tunneltype || frustum.SphereInFrustum(position.x + moved.x, position.y + moved.y, position.z + moved.z, model.boundingsphereradius)) {
319 distance = distsq(&viewer, &position);
321 hidden = !(distsqflat(&viewer, &position) > playerdist + 3 || (type != bushtype && type != treeleavestype));
324 if (detail == 2 && distance > viewdistance * viewdistance / 4 && environment == desertenvironment)
325 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness);
327 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0);
328 distance = (viewdistance * viewdistance - (distance - (viewdistance * viewdistance * fadestart)) * (1 / (1 - fadestart))) / viewdistance / viewdistance;
334 glMatrixMode(GL_MODELVIEW);
337 glEnable(GL_LIGHTING);
339 glDisable(GL_LIGHTING);
341 glTranslatef(position.x, position.y, position.z);
342 if (type == bushtype) {
345 if (type == treetrunktype || type == treeleavestype) {
346 if (type == treetrunktype || environment == desertenvironment) {
352 if (environment == snowyenvironment) {
353 if (type == treeleavestype) {
354 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
356 if (type == treetrunktype) {
357 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
359 if (type == bushtype) {
360 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
363 if (environment == grassyenvironment) {
364 if (type == treeleavestype) {
365 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
367 if (type == treetrunktype) {
368 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
370 if (type == bushtype) {
371 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
374 if (environment == desertenvironment) {
375 if (type == bushtype) {
376 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
379 glRotatef(yaw, 0, 1, 0);
380 glColor4f((1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, distance);
383 glAlphaFunc(GL_GREATER, 0.5);
386 glAlphaFunc(GL_GREATER, 0.1);
388 if (type != treetrunktype && type != treeleavestype && type != bushtype && type != rocktype) {
389 glEnable(GL_CULL_FACE);
390 glAlphaFunc(GL_GREATER, 0.0001);
391 model.drawdifftex(boxtextureptr);
392 model.drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
394 if (type == rocktype) {
395 glEnable(GL_CULL_FACE);
396 glAlphaFunc(GL_GREATER, 0.0001);
397 glColor4f((1 - shadowed) / 2 + light.ambient[0], (1 - shadowed) / 2 + light.ambient[1], (1 - shadowed) / 2 + light.ambient[2], distance);
398 model.drawdifftex(rocktextureptr);
399 model.drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
401 if (type == treeleavestype) {
402 glDisable(GL_CULL_FACE);
403 glDisable(GL_LIGHTING);
404 terrainlight = terrain.getLighting(position.x, position.z);
405 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
407 glAlphaFunc(GL_GREATER, 0.2);
409 model.drawdifftex(treetextureptr);
411 if (type == bushtype) {
412 glDisable(GL_CULL_FACE);
413 glDisable(GL_LIGHTING);
414 terrainlight = terrain.getLighting(position.x, position.z);
415 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
417 glAlphaFunc(GL_GREATER, 0.2);
419 model.drawdifftex(bushtextureptr);
421 if (type == treetrunktype) {
422 glEnable(GL_CULL_FACE);
423 terrainlight = terrain.getLighting(position.x, position.z);
424 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
425 model.drawdifftex(treetextureptr);
434 void Object::drawSecondPass()
436 static float distance;
437 static XYZ moved, terrainlight;
439 if (type != treeleavestype && type != bushtype) {
442 moved = DoRotation(model.boundingspherecenter, 0, yaw, 0);
443 if (frustum.SphereInFrustum(position.x + moved.x, position.y + moved.y, position.z + moved.z, model.boundingsphereradius)) {
444 hidden = distsqflat(&viewer, &position) <= playerdist + 3;
447 glMatrixMode(GL_MODELVIEW);
449 glEnable(GL_LIGHTING);
451 glTranslatef(position.x, position.y, position.z);
452 if (type == bushtype) {
455 if (type == treetrunktype || type == treeleavestype) {
458 if (environment == snowyenvironment) {
459 if (type == treeleavestype) {
460 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
462 if (type == treetrunktype) {
463 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
465 if (type == bushtype) {
466 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
469 if (environment == grassyenvironment) {
470 if (type == treeleavestype) {
471 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
473 if (type == treetrunktype) {
474 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
476 if (type == bushtype) {
477 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
480 glRotatef(yaw, 0, 1, 0);
481 glColor4f(1, 1, 1, distance);
482 if (type == treeleavestype) {
483 glDisable(GL_CULL_FACE);
484 glDisable(GL_LIGHTING);
485 terrainlight = terrain.getLighting(position.x, position.z);
488 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, .3);
489 glAlphaFunc(GL_GREATER, 0);
490 glDisable(GL_ALPHA_TEST);
491 model.drawdifftex(treetextureptr);
493 if (type == bushtype) {
494 glDisable(GL_CULL_FACE);
495 glDisable(GL_LIGHTING);
496 terrainlight = terrain.getLighting(position.x, position.z);
499 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, .3);
500 glAlphaFunc(GL_GREATER, 0);
501 glDisable(GL_ALPHA_TEST);
502 model.drawdifftex(bushtextureptr);
509 void Object::ComputeCenter()
512 for (unsigned i = 0; i < objects.size(); i++) {
513 center += objects[i]->position;
515 center /= objects.size();
518 void Object::ComputeRadius()
520 float maxdistance = 0;
522 for (unsigned int i = 0; i < objects.size(); i++) {
523 tempdist = distsq(¢er, &objects[i]->position);
524 if (tempdist > maxdistance) {
525 maxdistance = tempdist;
528 radius = fast_sqrt(maxdistance);
531 void Object::LoadObjectsFromFile(FILE* tfile, bool skip)
536 float yaw, pitch, scale, lastscale;
537 funpackf(tfile, "Bi", &numobjects);
541 for (int i = 0; i < numobjects; i++) {
542 funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &type, &yaw, &pitch, &position.x, &position.y, &position.z, &scale);
544 if (type == treeleavestype) {
547 objects.emplace_back(new Object(object_type(type), position, yaw, pitch, scale));
553 void Object::addToTerrain(unsigned id)
555 if ((type != treeleavestype) && (type != bushtype) && (type != firetype)) {
556 terrain.AddObject(position + DoRotation(model.boundingspherecenter, 0, yaw, 0), model.boundingsphereradius, id);
560 if ((type == treetrunktype) && (position.y < (terrain.getHeight(position.x, position.z) + 1))) {
561 terrain.MakeDecal(shadowdecalpermanent, position, 2, .4, 0);
564 if ((type == bushtype) && (position.y < (terrain.getHeight(position.x, position.z) + 1))) {
565 terrain.MakeDecal(shadowdecalpermanent, position, 1, .4, 0);
570 void Object::AddObjectsToTerrain()
572 for (unsigned i = 0; i < objects.size(); i++) {
573 objects[i]->addToTerrain(i);
577 void Object::SphereCheckPossible(XYZ* p1, float radius)
579 int whichpatchx = p1->x / (terrain.size / subdivision * terrain.scale);
580 int whichpatchz = p1->z / (terrain.size / subdivision * terrain.scale);
582 if (whichpatchx >= 0 && whichpatchz >= 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
583 if (terrain.patchobjects[whichpatchx][whichpatchz].size() < 500) {
584 for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) {
585 unsigned int i = terrain.patchobjects[whichpatchx][whichpatchz][j];
586 objects[i]->possible = false;
587 if (objects[i]->model.SphereCheckPossible(p1, radius, &objects[i]->position, &objects[i]->yaw) != -1) {
588 objects[i]->possible = true;
597 for (unsigned i = 0; i < objects.size(); i++) {
601 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0);
602 for (unsigned i = 0; i < objects.size(); i++) {
603 objects[i]->drawSecondPass();
605 if (environment == desertenvironment) {
606 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0);
608 glEnable(GL_ALPHA_TEST);
609 SetUpLight(&light, 0);
612 void Object::DeleteObject(int which)
614 objects.erase(objects.begin() + which);
617 void Object::MakeObject(int atype, XYZ where, float ayaw, float apitch, float ascale)
619 if ((atype != treeleavestype && atype != bushtype) || foliage == 1) {
620 unsigned nextid = objects.size();
621 objects.emplace_back(new Object(object_type(atype), where, ayaw, apitch, ascale));
622 objects.back()->addToTerrain(nextid);
626 void Object::DoStuff()
628 for (unsigned i = 0; i < objects.size(); i++) {
629 objects[i]->handleFire();
633 void Object::DoShadows()
636 lightloc = light.location;
640 Normalise(&lightloc);
642 for (unsigned i = 0; i < objects.size(); i++) {
643 objects[i]->doShadows(lightloc);
647 int Object::checkcollide(XYZ startpoint, XYZ endpoint)
649 float minx, minz, maxx, maxz, miny, maxy;
651 minx = min(startpoint.x, endpoint.x) - 1;
652 miny = min(startpoint.y, endpoint.y) - 1;
653 minz = min(startpoint.z, endpoint.z) - 1;
654 maxx = max(startpoint.x, endpoint.x) + 1;
655 maxy = max(startpoint.y, endpoint.y) + 1;
656 maxz = max(startpoint.z, endpoint.z) + 1;
658 for (unsigned int i = 0; i < objects.size(); i++) {
659 if (checkcollide(startpoint, endpoint, i, minx, miny, minz, maxx, maxy, maxz) != -1) {
667 int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what)
669 float minx, minz, maxx, maxz, miny, maxy;
671 minx = min(startpoint.x, endpoint.x) - 1;
672 miny = min(startpoint.y, endpoint.y) - 1;
673 minz = min(startpoint.z, endpoint.z) - 1;
674 maxx = max(startpoint.x, endpoint.x) + 1;
675 maxy = max(startpoint.y, endpoint.y) + 1;
676 maxz = max(startpoint.z, endpoint.z) + 1;
678 return checkcollide(startpoint, endpoint, what, minx, miny, minz, maxx, maxy, maxz);
681 int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what, float minx, float miny, float minz, float maxx, float maxy, float maxz)
683 XYZ colpoint, colviewer, coltarget;
686 if (terrain.lineTerrain(startpoint, endpoint, &colpoint) != -1) {
690 if (objects[what]->position.x > minx - objects[what]->model.boundingsphereradius &&
691 objects[what]->position.x < maxx + objects[what]->model.boundingsphereradius &&
692 objects[what]->position.y > miny - objects[what]->model.boundingsphereradius &&
693 objects[what]->position.y < maxy + objects[what]->model.boundingsphereradius &&
694 objects[what]->position.z > minz - objects[what]->model.boundingsphereradius &&
695 objects[what]->position.z < maxz + objects[what]->model.boundingsphereradius) {
696 if (objects[what]->type != treeleavestype &&
697 objects[what]->type != bushtype &&
698 objects[what]->type != firetype) {
699 colviewer = startpoint;
700 coltarget = endpoint;
701 if (objects[what]->model.LineCheck(&colviewer, &coltarget, &colpoint, &objects[what]->position, &objects[what]->yaw) != -1) {
711 //~ Object::~Objects()
713 //~ boxtextureptr.destroy();
714 //~ treetextureptr.destroy();
715 //~ bushtextureptr.destroy();
716 //~ rocktextureptr.destroy();