X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FObjects%2FObject.cpp;h=796e6ef16f222009ba01f3d53d667099463938b8;hb=eae73ddd7a006ee6de9ffb01f3ec5f8e5fdc6d85;hp=3e1e100eaffb5b7d30942ec6dfa18d58a1f2eb8f;hpb=c45da89d9227cf85a1e52cd53e4f7df5343e7f00;p=lugaru.git diff --git a/Source/Objects/Object.cpp b/Source/Objects/Object.cpp index 3e1e100..796e6ef 100644 --- a/Source/Objects/Object.cpp +++ b/Source/Objects/Object.cpp @@ -49,7 +49,7 @@ Texture Object::rocktextureptr; Object::Object() : position(), - type(0), + type(boxtype), yaw(0), pitch(0), rotx(0), @@ -70,7 +70,7 @@ Object::Object() : { } -Object::Object(int _type, XYZ _position, float _yaw, float _pitch, float _scale) : Object() +Object::Object(object_type _type, XYZ _position, float _yaw, float _pitch, float _scale) : Object() { scale = _scale; type = _type; @@ -80,35 +80,35 @@ Object::Object(int _type, XYZ _position, float _yaw, float _pitch, float _scale) switch(type) { case boxtype: - model.loaddecal("Models/Box.solid", 0); + model.loaddecal("Models/Box.solid"); friction = 1.5; break; case cooltype: - model.loaddecal("Models/Cool.solid", 0); + model.loaddecal("Models/Cool.solid"); friction = 1.5; break; case walltype: - model.loaddecal("Models/Wall.solid", 0); + model.loaddecal("Models/Wall.solid"); friction = 1.5; break; case tunneltype: - model.loaddecal("Models/Tunnel.solid", 0); + model.loaddecal("Models/Tunnel.solid"); friction = 1.5; break; case chimneytype: - model.loaddecal("Models/Chimney.solid", 0); + model.loaddecal("Models/Chimney.solid"); friction = 1.5; break; case spiketype: - model.load("Models/Spike.solid", 0); + model.load("Models/Spike.solid"); friction = .4; break; case weirdtype: - model.loaddecal("Models/Weird.solid", 0); + model.loaddecal("Models/Weird.solid"); friction = 1.5; break; case rocktype: - model.loaddecal("Models/Rock.solid", 0); + model.loaddecal("Models/Rock.solid"); if (scale > .5) { friction = 1.5; } else { @@ -116,20 +116,20 @@ Object::Object(int _type, XYZ _position, float _yaw, float _pitch, float _scale) } break; case treetrunktype: - model.load("Models/TreeTrunk.solid", 0); + model.load("Models/TreeTrunk.solid"); friction = .4; break; case treeleavestype: scale += fabs((float)(Random() % 100) / 900) * scale; - model.load("Models/Leaves.solid", 0); + model.load("Models/Leaves.solid"); friction = 0; break; case bushtype: position.y = terrain.getHeight(position.x, position.z) - .3; - model.load("Models/Bush.solid", 0); + model.load("Models/Bush.solid"); break; case platformtype: - model.loaddecal("Models/Platform.solid", 0); + model.loaddecal("Models/Platform.solid"); model.Rotate(90, 0, 0); friction = 1.5; break; @@ -222,7 +222,7 @@ void Object::doShadows(XYZ lightloc) if (shadowed > 0) { col = model.normals[j] - DoRotation(lightloc * shadowed, 0, -yaw, 0); Normalise(&col); - for (int k = 0; k < model.TriangleNum; k++) { + for (unsigned int k = 0; k < model.Triangles.size(); k++) { if (model.Triangles[k].vertex[0] == j) { int l = k * 24; model.vArray[l + 2] = col.x; @@ -248,6 +248,53 @@ void Object::doShadows(XYZ lightloc) shadowed = 0; } +void Object::handleRot(int divide) +{ + messedwith -= multiplier; + if (rotxvel || rotx) { + if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); + if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); + if (rotx > 0) rotxvel -= multiplier * 4; + if (rotx < 0) rotxvel += multiplier * 4; + if (rotxvel > 0) rotxvel -= multiplier * 4; + if (rotxvel < 0) rotxvel += multiplier * 4; + if (fabs(rotx) < multiplier * 4) + rotx = 0; + if (fabs(rotxvel) < multiplier * 4) + rotxvel = 0; + + rotx += rotxvel * multiplier * 4; + } + if (rotyvel || roty) { + if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); + if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); + if (roty > 0) rotyvel -= multiplier * 4; + if (roty < 0) rotyvel += multiplier * 4; + if (rotyvel > 0) rotyvel -= multiplier * 4; + if (rotyvel < 0) rotyvel += multiplier * 4; + if (fabs(roty) < multiplier * 4) + roty = 0; + if (fabs(rotyvel) < multiplier * 4) + rotyvel = 0; + + roty += rotyvel * multiplier * 4; + } + if (roty) { + glRotatef(roty / divide, 1, 0, 0); + } + if (rotx) { + glRotatef(-rotx / divide, 0, 0, 1); + } + if (rotx > 10) + rotx = 10; + if (rotx < -10) + rotx = -10; + if (roty > 10) + roty = 10; + if (roty < -10) + roty = -10; +} + void Object::draw() { static float distance; @@ -282,178 +329,48 @@ void Object::draw() glDepthMask(1); glTranslatef(position.x, position.y, position.z); if (type == bushtype) { - messedwith -= multiplier; - if (rotxvel || rotx) { - if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); - if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); - if (rotx > 0) rotxvel -= multiplier * 4; - if (rotx < 0) rotxvel += multiplier * 4; - if (rotxvel > 0) rotxvel -= multiplier * 4; - if (rotxvel < 0) rotxvel += multiplier * 4; - if (fabs(rotx) < multiplier * 4) - rotx = 0; - if (fabs(rotxvel) < multiplier * 4) - rotxvel = 0; - - rotx += rotxvel * multiplier * 4; - } - if (rotyvel || roty) { - if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); - if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); - if (roty > 0) rotyvel -= multiplier * 4; - if (roty < 0) rotyvel += multiplier * 4; - if (rotyvel > 0) rotyvel -= multiplier * 4; - if (rotyvel < 0) rotyvel += multiplier * 4; - if (fabs(roty) < multiplier * 4) - roty = 0; - if (fabs(rotyvel) < multiplier * 4) - rotyvel = 0; - - roty += rotyvel * multiplier * 4; - } - if (roty) { - glRotatef(roty, 1, 0, 0); - } - if (rotx) { - glRotatef(-rotx, 0, 0, 1); - } - if (rotx > 10) - rotx = 10; - if (rotx < -10) - rotx = -10; - if (roty > 10) - roty = 10; - if (roty < -10) - roty = -10; + handleRot(1); } if (type == treetrunktype || type == treeleavestype) { if (type == treetrunktype || environment == desertenvironment) { - messedwith -= multiplier; - if (rotxvel || rotx) { - if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); - if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); - if (rotx > 0) rotxvel -= multiplier * 4; - if (rotx < 0) rotxvel += multiplier * 4; - if (rotxvel > 0) rotxvel -= multiplier * 4; - if (rotxvel < 0) rotxvel += multiplier * 4; - if (fabs(rotx) < multiplier * 4) - rotx = 0; - if (fabs(rotxvel) < multiplier * 4) - rotxvel = 0; - - rotx += rotxvel * multiplier * 4; - } - if (rotyvel || roty) { - if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); - if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); - if (roty > 0) rotyvel -= multiplier * 4; - if (roty < 0) rotyvel += multiplier * 4; - if (rotyvel > 0) rotyvel -= multiplier * 4; - if (rotyvel < 0) rotyvel += multiplier * 4; - if (fabs(roty) < multiplier * 4) - roty = 0; - if (fabs(rotyvel) < multiplier * 4) - rotyvel = 0; - - roty += rotyvel * multiplier * 4; - } - if (roty) { - glRotatef(roty / 6, 1, 0, 0); - } - if (rotx) { - glRotatef(-rotx / 6, 0, 0, 1); - } - if (rotx > 10) - rotx = 10; - if (rotx < -10) - rotx = -10; - if (roty > 10) - roty = 10; - if (roty < -10) - roty = -10; + handleRot(6); } else { - messedwith -= multiplier; - if (rotxvel || rotx) { - if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); - if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); - if (rotx > 0) rotxvel -= multiplier * 4; - if (rotx < 0) rotxvel += multiplier * 4; - if (rotxvel > 0) rotxvel -= multiplier * 4; - if (rotxvel < 0) rotxvel += multiplier * 4; - if (fabs(rotx) < multiplier * 4) - rotx = 0; - if (fabs(rotxvel) < multiplier * 4) - rotxvel = 0; - - rotx += rotxvel * multiplier * 4; - } - if (rotyvel || roty) { - if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); - if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); - if (roty > 0) rotyvel -= multiplier * 4; - if (roty < 0) rotyvel += multiplier * 4; - if (rotyvel > 0) rotyvel -= multiplier * 4; - if (rotyvel < 0) rotyvel += multiplier * 4; - if (fabs(roty) < multiplier * 4) - roty = 0; - if (fabs(rotyvel) < multiplier * 4) - rotyvel = 0; - - roty += rotyvel * multiplier * 4; - } - if (roty) { - glRotatef(roty / 4, 1, 0, 0); - } - if (rotx) { - glRotatef(-rotx / 4, 0, 0, 1); - } - if (rotx > 10) - rotx = 10; - if (rotx < -10) - rotx = -10; - if (roty > 10) - roty = 10; - if (roty < -10) - roty = -10; + handleRot(4); } - } - if (/*detail==2&&*/environment == snowyenvironment) { + if (environment == snowyenvironment) { if (type == treeleavestype) { glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == treetrunktype) { - glRotatef((sin(windvar + position.x * .3) + .5)*.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); + glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == bushtype) { glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } } - if (/*detail==2&&*/environment == grassyenvironment) { + if (environment == grassyenvironment) { if (type == treeleavestype) { glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == treetrunktype) { - glRotatef((sin(windvar + position.x * .3) + .5)*.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); + glRotatef((sin(windvar + position.x * .3) + .5) * .5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == bushtype) { glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } } - if (/*detail==2&&*/environment == desertenvironment) { + if (environment == desertenvironment) { if (type == bushtype) { glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } } glRotatef(yaw, 0, 1, 0); - if (distance > 1) - distance = 1; glColor4f((1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, (1 - shadowed) / 2 + .5, distance); if (distance >= 1) { glDisable(GL_BLEND); glAlphaFunc(GL_GREATER, 0.5); - } - if (distance < 1) { + } else { glEnable(GL_BLEND); glAlphaFunc(GL_GREATER, 0.1); } @@ -474,16 +391,9 @@ void Object::draw() glDisable(GL_CULL_FACE); glDisable(GL_LIGHTING); terrainlight = terrain.getLighting(position.x, position.z); - if (!hidden) { - glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance); - if (distance < 1) - glAlphaFunc(GL_GREATER, 0.2); - } - if (hidden) { - glDepthMask(0); - glEnable(GL_BLEND); - glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance / 3); - glAlphaFunc(GL_GREATER, 0); + glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance); + if (distance < 1) { + glAlphaFunc(GL_GREATER, 0.2); } model.drawdifftex(treetextureptr); } @@ -491,16 +401,9 @@ void Object::draw() glDisable(GL_CULL_FACE); glDisable(GL_LIGHTING); terrainlight = terrain.getLighting(position.x, position.z); - if (!hidden) { - glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance); - if (distance < 1) - glAlphaFunc(GL_GREATER, 0.2); - } - if (hidden) { - glDepthMask(0); - glEnable(GL_BLEND); - glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance / 3); - glAlphaFunc(GL_GREATER, 0); + glColor4f(terrainlight.x, terrainlight.y, terrainlight.z, distance); + if (distance < 1) { + glAlphaFunc(GL_GREATER, 0.2); } model.drawdifftex(bushtextureptr); } @@ -536,101 +439,17 @@ void Object::drawSecondPass() glDepthMask(1); glTranslatef(position.x, position.y, position.z); if (type == bushtype) { - messedwith -= multiplier; - if (rotxvel || rotx) { - if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); - if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); - if (rotx > 0) rotxvel -= multiplier * 4; - if (rotx < 0) rotxvel += multiplier * 4; - if (rotxvel > 0) rotxvel -= multiplier * 4; - if (rotxvel < 0) rotxvel += multiplier * 4; - if (fabs(rotx) < multiplier * 4) - rotx = 0; - if (fabs(rotxvel) < multiplier * 4) - rotxvel = 0; - - rotx += rotxvel * multiplier * 4; - } - if (rotyvel || roty) { - if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); - if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); - if (roty > 0) rotyvel -= multiplier * 4; - if (roty < 0) rotyvel += multiplier * 4; - if (rotyvel > 0) rotyvel -= multiplier * 4; - if (rotyvel < 0) rotyvel += multiplier * 4; - if (fabs(roty) < multiplier * 4) - roty = 0; - if (fabs(rotyvel) < multiplier * 4) - rotyvel = 0; - - roty += rotyvel * multiplier * 4; - } - if (roty) { - glRotatef(roty, 1, 0, 0); - } - if (rotx) { - glRotatef(-rotx, 0, 0, 1); - } - if (rotx > 10) - rotx = 10; - if (rotx < -10) - rotx = -10; - if (roty > 10) - roty = 10; - if (roty < -10) - roty = -10; + handleRot(1); } if (type == treetrunktype || type == treeleavestype) { - messedwith -= multiplier; - if (rotxvel || rotx) { - if (rotx > 0) rotxvel -= multiplier * 8 * fabs(rotx); - if (rotx < 0) rotxvel += multiplier * 8 * fabs(rotx); - if (rotx > 0) rotxvel -= multiplier * 4; - if (rotx < 0) rotxvel += multiplier * 4; - if (rotxvel > 0) rotxvel -= multiplier * 4; - if (rotxvel < 0) rotxvel += multiplier * 4; - if (fabs(rotx) < multiplier * 4) - rotx = 0; - if (fabs(rotxvel) < multiplier * 4) - rotxvel = 0; - - rotx += rotxvel * multiplier * 4; - } - if (rotyvel || roty) { - if (roty > 0) rotyvel -= multiplier * 8 * fabs(roty); - if (roty < 0) rotyvel += multiplier * 8 * fabs(roty); - if (roty > 0) rotyvel -= multiplier * 4; - if (roty < 0) rotyvel += multiplier * 4; - if (rotyvel > 0) rotyvel -= multiplier * 4; - if (rotyvel < 0) rotyvel += multiplier * 4; - if (fabs(roty) < multiplier * 4) - roty = 0; - if (fabs(rotyvel) < multiplier * 4) - rotyvel = 0; - - roty += rotyvel * multiplier * 4; - } - if (roty) { - glRotatef(roty / 2, 1, 0, 0); - } - if (rotx) { - glRotatef(-rotx / 2, 0, 0, 1); - } - if (rotx > 10) - rotx = 10; - if (rotx < -10) - rotx = -10; - if (roty > 10) - roty = 10; - if (roty < -10) - roty = -10; + handleRot(2); } if (environment == snowyenvironment) { if (type == treeleavestype) { glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == treetrunktype) { - glRotatef((sin(windvar + position.x * .3) + .5)*.5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); + glRotatef((sin(windvar + position.x * .3) + .5) * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == bushtype) { glRotatef((sin(windvar + position.x * .3) + .5) * 4 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); @@ -641,7 +460,7 @@ void Object::drawSecondPass() glRotatef((sin(windvar + position.x * .3) + .5) * 1.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == treetrunktype) { - glRotatef((sin(windvar + position.x * .3) + .5)*.5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); + glRotatef((sin(windvar + position.x * .3) + .5) * .5 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); } if (type == bushtype) { glRotatef((sin(windvar + position.x * .3) + .5) * 4 * .5 * (sin(windvar * 2 + position.x * .3) + 1) / 2, 1, 0, 0); @@ -689,7 +508,7 @@ void Object::ComputeRadius() { float maxdistance = 0; float tempdist; - for (int i = 0; i < objects.size(); i++) { + for (unsigned int i = 0; i < objects.size(); i++) { tempdist = distsq(¢er, &objects[i]->position); if (tempdist > maxdistance) { maxdistance = tempdist; @@ -714,7 +533,7 @@ void Object::LoadObjectsFromFile(FILE* tfile, bool skip) if (type == treeleavestype) { scale = lastscale; } - objects.emplace_back(new Object(type, position, yaw, pitch, scale)); + objects.emplace_back(new Object(object_type(type), position, yaw, pitch, scale)); lastscale = scale; } } @@ -788,7 +607,7 @@ void Object::MakeObject(int atype, XYZ where, float ayaw, float apitch, float as { if ((atype != treeleavestype && atype != bushtype) || foliage == 1) { unsigned nextid = objects.size(); - objects.emplace_back(new Object(atype, where, ayaw, apitch, ascale)); + objects.emplace_back(new Object(object_type(atype), where, ayaw, apitch, ascale)); objects.back()->addToTerrain(nextid); } } @@ -814,6 +633,70 @@ void Object::DoShadows() } } +int Object::checkcollide(XYZ startpoint, XYZ endpoint) +{ + float minx, minz, maxx, maxz, miny, maxy; + + minx = min(startpoint.x, endpoint.x) - 1; + miny = min(startpoint.y, endpoint.y) - 1; + minz = min(startpoint.z, endpoint.z) - 1; + maxx = max(startpoint.x, endpoint.x) + 1; + maxy = max(startpoint.y, endpoint.y) + 1; + maxz = max(startpoint.z, endpoint.z) + 1; + + for (unsigned int i = 0; i < objects.size(); i++) { + if (checkcollide(startpoint, endpoint, i, minx, miny, minz, maxx, maxy, maxz) != -1) { + return (int) i; + } + } + + return -1; +} + +int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what) +{ + float minx, minz, maxx, maxz, miny, maxy; + + minx = min(startpoint.x, endpoint.x) - 1; + miny = min(startpoint.y, endpoint.y) - 1; + minz = min(startpoint.z, endpoint.z) - 1; + maxx = max(startpoint.x, endpoint.x) + 1; + maxy = max(startpoint.y, endpoint.y) + 1; + maxz = max(startpoint.z, endpoint.z) + 1; + + return checkcollide(startpoint, endpoint, what, minx, miny, minz, maxx, maxy, maxz); +} + +int Object::checkcollide(XYZ startpoint, XYZ endpoint, int what, float minx, float miny, float minz, float maxx, float maxy, float maxz) +{ + XYZ colpoint, colviewer, coltarget; + + if (what == 1000) { + if (terrain.lineTerrain(startpoint, endpoint, &colpoint) != -1) { + return what; + } + } else { + if ( objects[what]->position.x > minx - objects[what]->model.boundingsphereradius && + objects[what]->position.x < maxx + objects[what]->model.boundingsphereradius && + objects[what]->position.y > miny - objects[what]->model.boundingsphereradius && + objects[what]->position.y < maxy + objects[what]->model.boundingsphereradius && + objects[what]->position.z > minz - objects[what]->model.boundingsphereradius && + objects[what]->position.z < maxz + objects[what]->model.boundingsphereradius) { + if ( objects[what]->type != treeleavestype && + objects[what]->type != bushtype && + objects[what]->type != firetype) { + colviewer = startpoint; + coltarget = endpoint; + if (objects[what]->model.LineCheck(&colviewer, &coltarget, &colpoint, &objects[what]->position, &objects[what]->yaw) != -1) { + return what; + } + } + } + } + + return -1; +} + //~ Object::~Objects() //~ { //~ boxtextureptr.destroy();