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) : Object()
83 model.loaddecal("Models/Box.solid");
87 model.loaddecal("Models/Cool.solid");
91 model.loaddecal("Models/Wall.solid");
95 model.loaddecal("Models/Tunnel.solid");
99 model.loaddecal("Models/Chimney.solid");
103 model.load("Models/Spike.solid");
107 model.loaddecal("Models/Weird.solid");
111 model.loaddecal("Models/Rock.solid");
119 model.load("Models/TreeTrunk.solid");
123 scale += fabs((float)(Random() % 100) / 900) * scale;
124 model.load("Models/Leaves.solid");
128 position.y = terrain.getHeight(position.x, position.z) - .3;
129 model.load("Models/Bush.solid");
132 model.loaddecal("Models/Platform.solid");
133 model.Rotate(90, 0, 0);
141 if (friction == 1.5 && fabs(pitch) > 5) {
145 if (type == boxtype || type == cooltype || type == spiketype || type == weirdtype || type == walltype || type == chimneytype || type == tunneltype || type == platformtype) {
146 model.ScaleTexCoords(scale * 1.5);
148 if (type == rocktype) {
149 model.ScaleTexCoords(scale * 3);
152 if (type == treetrunktype || type == treeleavestype || type == rocktype) {
155 model.Scale(.3 * scale, .3 * scale, .3 * scale);
156 model.Rotate(90, 1, 1);
157 model.Rotate(pitch, 0, 0);
158 if (type == rocktype) {
159 model.Rotate(yaw * 5, 0, 0);
161 model.CalculateNormals(1);
162 model.ScaleNormals(-1, -1, -1);
165 void Object::handleFire()
167 if (type == firetype) {
172 if ((type == bushtype) || (type == firetype)) {
173 flamedelay -= multiplier * 3;
174 } else if (type == treeleavestype) {
175 flamedelay -= multiplier * 4;
177 while ((flamedelay < 0) && onfire) {
179 if ((type == bushtype) || (type == firetype)) {
180 spawnpoint.x = ((float)(Random() % 100)) / 30 * scale;
181 spawnpoint.y = ((float)(Random() % 100) + 60) / 30 * scale;
183 spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
184 spawnpoint += position;
185 Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 5 * scale, 1);
186 } else if (type == treeleavestype) {
187 spawnpoint.x = ((float)(Random() % 100)) / 80 * scale;
188 spawnpoint.y = ((float)(Random() % 100) + 80) / 12 * scale;
190 spawnpoint = DoRotation(spawnpoint, 0, Random() % 360, 0);
191 spawnpoint += position;
192 Sprite::MakeSprite(flamesprite, spawnpoint, spawnpoint * 0, 1, 1, 1, (.6 + (float)abs(Random() % 100) / 200 - .25) * 6, 1);
198 void Object::doShadows(XYZ lightloc)
200 XYZ testpoint, testpoint2, terrainpoint, col;
202 if (type != treeleavestype && type != treetrunktype && type != bushtype && type != firetype) {
203 for (int j = 0; j < model.vertexNum; j++) {
204 terrainpoint = position + DoRotation(model.vertex[j] + model.normals[j] * .1, 0, yaw, 0);
206 patchx = terrainpoint.x / (terrain.size / subdivision * terrain.scale);
207 patchz = terrainpoint.z / (terrain.size / subdivision * terrain.scale);
208 if (patchx >= 0 && patchz >= 0 && patchx < subdivision && patchz < subdivision) {
209 if (terrain.patchobjectnum[patchx][patchz]) {
210 for (int k = 0; k < terrain.patchobjectnum[patchx][patchz]; k++) {
211 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);
223 col = model.normals[j] - DoRotation(lightloc * shadowed, 0, -yaw, 0);
225 for (unsigned int k = 0; k < model.Triangles.size(); k++) {
226 if (model.Triangles[k].vertex[0] == j) {
228 model.vArray[l + 2] = col.x;
229 model.vArray[l + 3] = col.y;
230 model.vArray[l + 4] = col.z;
232 if (model.Triangles[k].vertex[1] == j) {
234 model.vArray[l + 10] = col.x;
235 model.vArray[l + 11] = col.y;
236 model.vArray[l + 12] = col.z;
238 if (model.Triangles[k].vertex[2] == j) {
240 model.vArray[l + 18] = col.x;
241 model.vArray[l + 19] = col.y;
242 model.vArray[l + 20] = col.z;
251 void Object::handleRot(int divide)
253 messedwith -= multiplier;
254 if (rotxvel || rotx) {
255 if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx);
256 if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx);
257 if (rotx > 0) rotxvel -= multiplier * 4;
258 if (rotx < 0) rotxvel += multiplier * 4;
259 if (rotxvel > 0) rotxvel -= multiplier * 4;
260 if (rotxvel < 0) rotxvel += multiplier * 4;
261 if (fabs(rotx) < multiplier * 4)
263 if (fabs(rotxvel) < multiplier * 4)
266 rotx += rotxvel * multiplier * 4;
268 if (rotyvel || roty) {
269 if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty);
270 if (roty < 0) rotyvel += multiplier * 8 * fabs(roty);
271 if (roty > 0) rotyvel -= multiplier * 4;
272 if (roty < 0) rotyvel += multiplier * 4;
273 if (rotyvel > 0) rotyvel -= multiplier * 4;
274 if (rotyvel < 0) rotyvel += multiplier * 4;
275 if (fabs(roty) < multiplier * 4)
277 if (fabs(rotyvel) < multiplier * 4)
280 roty += rotyvel * multiplier * 4;
283 glRotatef(roty / divide, 1, 0, 0);
286 glRotatef(-rotx / divide, 0, 0, 1);
300 static float distance;
301 static XYZ moved, terrainlight;
303 if (type == firetype) {
306 moved = DoRotation(model.boundingspherecenter, 0, yaw, 0);
307 if (type == tunneltype || frustum.SphereInFrustum(position.x + moved.x, position.y + moved.y, position.z + moved.z, model.boundingsphereradius)) {
308 distance = distsq(&viewer, &position);
310 hidden = !(distsqflat(&viewer, &position) > playerdist + 3 || (type != bushtype && type != treeleavestype));
313 if (detail == 2 && distance > viewdistance * viewdistance / 4 && environment == desertenvironment)
314 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, blurness );
316 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
317 distance = (viewdistance * viewdistance - (distance - (viewdistance * viewdistance * fadestart)) * (1 / (1 - fadestart))) / viewdistance / viewdistance;
323 glMatrixMode(GL_MODELVIEW);
326 glEnable(GL_LIGHTING);
328 glDisable(GL_LIGHTING);
330 glTranslatef(position.x, position.y, position.z);
331 if (type == bushtype) {
334 if (type == treetrunktype || type == treeleavestype) {
335 if (type == treetrunktype || environment == desertenvironment) {
341 if (environment == snowyenvironment) {
342 if (type == treeleavestype) {
343 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
345 if (type == treetrunktype) {
346 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
348 if (type == bushtype) {
349 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
352 if (environment == grassyenvironment) {
353 if (type == treeleavestype) {
354 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .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 * .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 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
363 if (environment == desertenvironment) {
364 if (type == bushtype) {
365 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
368 glRotatef(yaw, 0, 1, 0);
369 glColor4f((1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, distance);
372 glAlphaFunc(GL_GREATER, 0.5);
375 glAlphaFunc(GL_GREATER, 0.1);
377 if (type != treetrunktype && type != treeleavestype && type != bushtype && type != rocktype) {
378 glEnable(GL_CULL_FACE);
379 glAlphaFunc(GL_GREATER, 0.0001);
380 model.drawdifftex(boxtextureptr);
381 model.drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
383 if (type == rocktype) {
384 glEnable(GL_CULL_FACE);
385 glAlphaFunc(GL_GREATER, 0.0001);
386 glColor4f((1 - shadowed) / 2 + light.ambient[0], (1 - shadowed) / 2 + light.ambient[1], (1 - shadowed) / 2 + light.ambient[2], distance);
387 model.drawdifftex(rocktextureptr);
388 model.drawdecals(terrain.shadowtexture, terrain.bloodtexture, terrain.bloodtexture2, terrain.breaktexture);
390 if (type == treeleavestype) {
391 glDisable(GL_CULL_FACE);
392 glDisable(GL_LIGHTING);
393 terrainlight = terrain.getLighting(position.x, position.z);
394 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
396 glAlphaFunc(GL_GREATER, 0.2);
398 model.drawdifftex(treetextureptr);
400 if (type == bushtype) {
401 glDisable(GL_CULL_FACE);
402 glDisable(GL_LIGHTING);
403 terrainlight = terrain.getLighting(position.x, position.z);
404 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
406 glAlphaFunc(GL_GREATER, 0.2);
408 model.drawdifftex(bushtextureptr);
410 if (type == treetrunktype) {
411 glEnable(GL_CULL_FACE);
412 terrainlight = terrain.getLighting(position.x, position.z);
413 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance);
414 model.drawdifftex(treetextureptr);
423 void Object::drawSecondPass()
425 static float distance;
426 static XYZ moved, terrainlight;
428 if (type != treeleavestype && type != bushtype) {
431 moved = DoRotation(model.boundingspherecenter, 0, yaw, 0);
432 if (frustum.SphereInFrustum(position.x + moved.x, position.y + moved.y, position.z + moved.z, model.boundingsphereradius)) {
433 hidden = distsqflat(&viewer, &position) <= playerdist + 3;
436 glMatrixMode(GL_MODELVIEW);
438 glEnable(GL_LIGHTING);
440 glTranslatef(position.x, position.y, position.z);
441 if (type == bushtype) {
444 if (type == treetrunktype || type == treeleavestype) {
447 if (environment == snowyenvironment) {
448 if (type == treeleavestype) {
449 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
451 if (type == treetrunktype) {
452 glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
454 if (type == bushtype) {
455 glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
458 if (environment == grassyenvironment) {
459 if (type == treeleavestype) {
460 glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .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 * .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 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0);
469 glRotatef(yaw, 0, 1, 0);
470 glColor4f(1, 1, 1, distance);
471 if (type == treeleavestype) {
472 glDisable(GL_CULL_FACE);
473 glDisable(GL_LIGHTING);
474 terrainlight = terrain.getLighting(position.x, position.z);
477 glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, .3);
478 glAlphaFunc(GL_GREATER, 0);
479 glDisable(GL_ALPHA_TEST);
480 model.drawdifftex(treetextureptr);
482 if (type == bushtype) {
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(bushtextureptr);
498 void Object::ComputeCenter()
501 for (unsigned i = 0; i < objects.size(); i++) {
502 center += objects[i]->position;
504 center /= objects.size();
507 void Object::ComputeRadius()
509 float maxdistance = 0;
511 for (unsigned int i = 0; i < objects.size(); i++) {
512 tempdist = distsq(¢er, &objects[i]->position);
513 if (tempdist > maxdistance) {
514 maxdistance = tempdist;
517 radius = fast_sqrt(maxdistance);
520 void Object::LoadObjectsFromFile(FILE* tfile, bool skip)
525 float yaw, pitch, scale, lastscale;
526 funpackf(tfile, "Bi", &numobjects);
530 for (int i = 0; i < numobjects; i++) {
531 funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &type, &yaw, &pitch, &position.x, &position.y, &position.z, &scale);
533 if (type == treeleavestype) {
536 objects.emplace_back(new Object(object_type(type), position, yaw, pitch, scale));
542 void Object::addToTerrain(unsigned id)
544 if ((type != treeleavestype) && (type != bushtype) && (type != firetype)) {
545 terrain.AddObject(position + DoRotation(model.boundingspherecenter, 0, yaw, 0), model.boundingsphereradius, id);
549 if ((type == treetrunktype) && (position.y < (terrain.getHeight(position.x, position.z) + 1))) {
550 terrain.MakeDecal(shadowdecalpermanent, position, 2, .4, 0);
553 if ((type == bushtype) && (position.y < (terrain.getHeight(position.x, position.z) + 1))) {
554 terrain.MakeDecal(shadowdecalpermanent, position, 1, .4, 0);
559 void Object::AddObjectsToTerrain()
561 for (unsigned i = 0; i < objects.size(); i++) {
562 objects[i]->addToTerrain(i);
566 void Object::SphereCheckPossible(XYZ *p1, float radius)
568 int whichpatchx = p1->x / (terrain.size / subdivision * terrain.scale);
569 int whichpatchz = p1->z / (terrain.size / subdivision * terrain.scale);
571 if (whichpatchx >= 0 && whichpatchz >= 0 && whichpatchx < subdivision && whichpatchz < subdivision) {
572 if (terrain.patchobjectnum[whichpatchx][whichpatchz] > 0 && terrain.patchobjectnum[whichpatchx][whichpatchz] < 500) {
573 for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) {
574 int i = terrain.patchobjects[whichpatchx][whichpatchz][j];
575 objects[i]->possible = false;
576 if (objects[i]->model.SphereCheckPossible(p1, radius, &objects[i]->position, &objects[i]->yaw) != -1) {
577 objects[i]->possible = true;
586 for (unsigned i = 0; i < objects.size(); i++) {
590 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
591 for (unsigned i = 0; i < objects.size(); i++) {
592 objects[i]->drawSecondPass();
594 if (environment == desertenvironment) {
595 glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0 );
597 glEnable(GL_ALPHA_TEST);
598 SetUpLight(&light, 0);
601 void Object::DeleteObject(int which)
603 objects.erase(objects.begin() + which);
606 void Object::MakeObject(int atype, XYZ where, float ayaw, float apitch, float ascale)
608 if ((atype != treeleavestype && atype != bushtype) || foliage == 1) {
609 unsigned nextid = objects.size();
610 objects.emplace_back(new Object(object_type(atype), where, ayaw, apitch, ascale));
611 objects.back()->addToTerrain(nextid);
615 void Object::DoStuff()
617 for (unsigned i = 0; i < objects.size(); i++) {
618 objects[i]->handleFire();
622 void Object::DoShadows()
625 lightloc = light.location;
629 Normalise(&lightloc);
631 for (unsigned i = 0; i < objects.size(); i++) {
632 objects[i]->doShadows(lightloc);
636 int Object::checkcollide(XYZ startpoint, XYZ endpoint)
638 float minx, minz, maxx, maxz, miny, maxy;
640 minx = min(startpoint.x, endpoint.x) - 1;
641 miny = min(startpoint.y, endpoint.y) - 1;
642 minz = min(startpoint.z, endpoint.z) - 1;
643 maxx = max(startpoint.x, endpoint.x) + 1;
644 maxy = max(startpoint.y, endpoint.y) + 1;
645 maxz = max(startpoint.z, endpoint.z) + 1;
647 for (unsigned int i = 0; i < objects.size(); i++) {
648 if (checkcollide(startpoint, endpoint, i, minx, miny, minz, maxx, maxy, maxz) != -1) {
656 int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what)
658 float minx, minz, maxx, maxz, miny, maxy;
660 minx = min(startpoint.x, endpoint.x) - 1;
661 miny = min(startpoint.y, endpoint.y) - 1;
662 minz = min(startpoint.z, endpoint.z) - 1;
663 maxx = max(startpoint.x, endpoint.x) + 1;
664 maxy = max(startpoint.y, endpoint.y) + 1;
665 maxz = max(startpoint.z, endpoint.z) + 1;
667 return checkcollide(startpoint, endpoint, what, minx, miny, minz, maxx, maxy, maxz);
670 int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what, float minx, float miny, float minz, float maxx, float maxy, float maxz)
672 XYZ colpoint, colviewer, coltarget;
675 if (terrain.lineTerrain(startpoint, endpoint, &colpoint) != -1) {
679 if ( objects[what]->position.x > minx - objects[what]->model.boundingsphereradius &&
680 objects[what]->position.x < maxx + objects[what]->model.boundingsphereradius &&
681 objects[what]->position.y > miny - objects[what]->model.boundingsphereradius &&
682 objects[what]->position.y < maxy + objects[what]->model.boundingsphereradius &&
683 objects[what]->position.z > minz - objects[what]->model.boundingsphereradius &&
684 objects[what]->position.z < maxz + objects[what]->model.boundingsphereradius) {
685 if ( objects[what]->type != treeleavestype &&
686 objects[what]->type != bushtype &&
687 objects[what]->type != firetype) {
688 colviewer = startpoint;
689 coltarget = endpoint;
690 if (objects[what]->model.LineCheck(&colviewer, &coltarget, &colpoint, &objects[what]->position, &objects[what]->yaw) != -1) {
700 //~ Object::~Objects()
702 //~ boxtextureptr.destroy();
703 //~ treetextureptr.destroy();
704 //~ bushtextureptr.destroy();
705 //~ rocktextureptr.destroy();