X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FAnimation%2FSkeleton.cpp;h=19150fb4762da55d652afd5e9322da15bbfc4833;hb=1a6ef830cdd01a9b7a8bbcfde8a51b7c430671a3;hp=607be81689c4c3b787229d0c8829692af5248c18;hpb=1991e81a5c3bcc9cd0d6d49f56ebd8bcbd76e7b4;p=lugaru.git diff --git a/Source/Animation/Skeleton.cpp b/Source/Animation/Skeleton.cpp index 607be81..19150fb 100644 --- a/Source/Animation/Skeleton.cpp +++ b/Source/Animation/Skeleton.cpp @@ -18,61 +18,44 @@ You should have received a copy of the GNU General Public License along with Lugaru. If not, see . */ -/**> HEADER FILES <**/ -#include "Game.h" -#include "Animation/Skeleton.h" -#include "openal_wrapper.h" -#include "Animation/Animation.h" -#include "Utils/Folders.h" +#include "Animation/Skeleton.hpp" + +#include "Animation/Animation.hpp" +#include "Audio/openal_wrapper.hpp" +#include "Game.hpp" +#include "Utils/Folders.hpp" +#include "Tutorial.hpp" extern float multiplier; extern float gravity; extern Terrain terrain; -extern Objects objects; extern int environment; extern float camerashake; extern bool freeze; extern int detail; -extern int tutoriallevel; extern int whichjointstartarray[26]; extern int whichjointendarray[26]; -extern bool visibleloading; - -Skeleton::Skeleton() +Skeleton::Skeleton() : + selected(0), + id(0), + num_models(0), + clothes(false), + spinny(false), + skinsize(0), + checkdelay(0), + longdead(0), + broken(false), + free(0), + oldfree(0), + freetime(0), + freefall(false) { - selected = 0; - memset(forwardjoints, 0, sizeof(forwardjoints)); - - id = 0; - memset(lowforwardjoints, 0, sizeof(lowforwardjoints)); - memset(jointlabels, 0, sizeof(jointlabels)); - - num_models = 0; - - clothes = 0; - spinny = 0; - memset(skinText, 0, sizeof(skinText)); - skinsize = 0; - - checkdelay = 0; - - longdead = 0; - broken = 0; - - free = 0; - oldfree = 0; - freetime = 0; - freefall = 0; -} - -Skeleton::~Skeleton() -{ } /* EFFECT @@ -124,12 +107,11 @@ void Skeleton::FindForwards() */ float Skeleton::DoConstraints(XYZ *coords, float *scale) { - float friction = 1.5; const float elasticity = .3; XYZ bounceness; const int numrepeats = 3; float groundlevel = .15; - int i, j, k, m; + unsigned i; XYZ temp; XYZ terrainnormal; int whichhit; @@ -147,7 +129,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale); terrainlight = *coords; - objects.SphereCheckPossible(&terrainlight, 1); + Object::SphereCheckPossible(&terrainlight, 1); //Add velocity for (i = 0; i < joints.size(); i++) { @@ -177,7 +159,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) float tempmult = multiplier; //multiplier/=numrepeats; - for (j = 0; j < numrepeats; j++) { + for (int j = 0; j < numrepeats; j++) { float r = .05; // right leg constraints? if (!joint(rightknee).locked && !joint(righthip).locked) { @@ -232,7 +214,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) joints[i].locked = 0; if (joints[i].delay > 0) { bool freely = true; - for (j = 0; j < joints.size(); j++) { + for (unsigned j = 0; j < joints.size(); j++) { if (joints[j].locked) freely = false; } @@ -246,6 +228,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) muscles[i].DoConstraint(spinny); } + float friction; for (i = 0; i < joints.size(); i++) { //Length constraints //Ground constraint @@ -256,7 +239,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) { joints[i].locked = 1; joints[i].delay = 1; - if (tutoriallevel != 1 || id == 0) { + if (!Tutorial::active || id == 0) { emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.); } breaking = true; @@ -265,7 +248,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) { joints[i].locked = 1; joints[i].delay = 1; - if (tutoriallevel != 1 || id == 0) { + if (!Tutorial::active || id == 0) { emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.); } } @@ -284,12 +267,12 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) else joints[i].velocity = 0; - if (tutoriallevel != 1 || id == 0) + if (!Tutorial::active || id == 0) if (findLengthfast(&bounceness) > 8000 && breaking) { // FIXME: this crashes because k is not initialized! // to reproduce, type 'wolfie' in console and play a while // I'll just comment it out for now - //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360); + //Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360); Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2); breaking = false; camerashake += .6; @@ -337,111 +320,110 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) if (longdead > 100) broken = 1; } - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) - for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) { - k = terrain.patchobjects[whichpatchx][whichpatchz][m]; - if (k < objects.numobjects && k >= 0) - if (objects.possible[k]) { - friction = objects.friction[k]; - XYZ start = joints[i].realoldposition; - XYZ end = joints[i].position * (*scale) + *coords; - whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]); - if (whichhit != -1) { - if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) { - joints[i].locked = 1; - joints[i].delay = 1; - if (tutoriallevel != 1 || id == 0) { - emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.); - } - breaking = true; + for (unsigned int m = 0; m < terrain.patchobjects[whichpatchx][whichpatchz].size(); m++) { + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; + if (k < Object::objects.size()) { + if (Object::objects[k]->possible) { + friction = Object::objects[k]->friction; + XYZ start = joints[i].realoldposition; + XYZ end = joints[i].position * (*scale) + *coords; + whichhit = Object::objects[k]->model.LineCheckPossible(&start, &end, &temp, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) { + joints[i].locked = 1; + joints[i].delay = 1; + if (!Tutorial::active || id == 0) { + emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.); } + breaking = true; + } - if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) { - joints[i].locked = 1; - joints[i].delay = 1; - if (tutoriallevel != 1 || id == 0) { - emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.); - } + if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) { + joints[i].locked = 1; + joints[i].delay = 1; + if (!Tutorial::active || id == 0) { + emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.); } + } - terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1; - if (terrainnormal.y > .8) - freefall = 0; - bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal))); - if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) { - bounceness = 0; - joints[i].velocity = joints[i].oldvelocity; - } - if (tutoriallevel != 1 || id == 0) - if (findLengthfast(&bounceness) > 4000 && breaking) { - objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360); - Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2); - breaking = false; - camerashake += .6; - - emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords); - - addEnvSound(*coords, 64); - } - if (objects.type[k] == treetrunktype) { - objects.rotx[k] += joints[i].velocity.x * multiplier * .4; - objects.roty[k] += joints[i].velocity.z * multiplier * .4; - objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4; - objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4; - } - if (!joints[i].locked) - damage += findLengthfast(&bounceness) / 2500; - ReflectVector(&joints[i].velocity, &terrainnormal); - frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal)); - joints[i].velocity -= bounceness; - if (1 - friction * frictionness > 0) - joints[i].velocity *= 1 - friction * frictionness; - else - joints[i].velocity = 0; - if (findLengthfast(&bounceness) > 2500) { - Normalise(&bounceness); - bounceness = bounceness * 50; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + if (terrainnormal.y > .8) + freefall = 0; + bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal))); + if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) { + bounceness = 0; + joints[i].velocity = joints[i].oldvelocity; + } + if (!Tutorial::active || id == 0) + if (findLengthfast(&bounceness) > 4000 && breaking) { + Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360); + Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2); + breaking = false; + camerashake += .6; + + emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords); + + addEnvSound(*coords, 64); } - joints[i].velocity += bounceness * elasticity; - - - if (!joints[i].locked) - if (findLengthfast(&joints[i].velocity) < 1) { - joints[i].locked = 1; - } - if (findLengthfast(&bounceness) > 500) - Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2); - joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005; - if (longdead > 100) - broken = 1; + if (Object::objects[k]->type == treetrunktype) { + Object::objects[k]->rotx += joints[i].velocity.x * multiplier * .4; + Object::objects[k]->roty += joints[i].velocity.z * multiplier * .4; + Object::objects[k + 1]->rotx += joints[i].velocity.x * multiplier * .4; + Object::objects[k + 1]->roty += joints[i].velocity.z * multiplier * .4; + } + if (!joints[i].locked) + damage += findLengthfast(&bounceness) / 2500; + ReflectVector(&joints[i].velocity, &terrainnormal); + frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal)); + joints[i].velocity -= bounceness; + if (1 - friction * frictionness > 0) + joints[i].velocity *= 1 - friction * frictionness; + else + joints[i].velocity = 0; + if (findLengthfast(&bounceness) > 2500) { + Normalise(&bounceness); + bounceness = bounceness * 50; } + joints[i].velocity += bounceness * elasticity; + + + if (!joints[i].locked) + if (findLengthfast(&joints[i].velocity) < 1) { + joints[i].locked = 1; + } + if (findLengthfast(&bounceness) > 500) + Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2); + joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005; + if (longdead > 100) + broken = 1; } + } } + } joints[i].realoldposition = joints[i].position * (*scale) + *coords; } } multiplier = tempmult; - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) - for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) { - k = terrain.patchobjects[whichpatchx][whichpatchz][m]; - if (objects.possible[k]) { - for (i = 0; i < 26; i++) { - //Make this less stupid - XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords; - XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords; - whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]); - if (whichhit != -1) { - joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale); - for (j = 0; j < muscles.size(); j++) { - if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i])) - muscles[j].DoConstraint(spinny); - } + for (unsigned int m = 0; m < terrain.patchobjects[whichpatchx][whichpatchz].size(); m++) { + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; + if (Object::objects[k]->possible) { + for (i = 0; i < 26; i++) { + //Make this less stupid + XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords; + XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords; + whichhit = Object::objects[k]->model.LineCheckSlidePossible(&start, &end, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale); + for (unsigned j = 0; j < muscles.size(); j++) { + if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i])) + muscles[j].DoConstraint(spinny); } } } } + } for (i = 0; i < joints.size(); i++) { switch (joints[i].label) { @@ -490,8 +472,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) */ void Skeleton::DoGravity(float *scale) { - static int i; - for (i = 0; i < joints.size(); i++) { + for (unsigned i = 0; i < joints.size(); i++) { if ( ( ((joints[i].label != leftknee) && (joints[i].label != rightknee)) || @@ -501,8 +482,9 @@ void Skeleton::DoGravity(float *scale) ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) || (forward.y < .3) ) - ) + ) { joints[i].velocity.y += gravity * multiplier / (*scale); + } } } @@ -629,7 +611,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil GLfloat M[16]; FILE *tfile; float lSize; - int i, j, num_joints, num_muscles; + int j, num_joints, num_muscles; LOGFUNC; @@ -646,17 +628,17 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil model[5].loadnotex(model6filename); model[6].loadnotex(model7filename); - for (i = 0; i < num_models; i++) { + for (int i = 0; i < num_models; i++) { model[i].Rotate(180, 0, 0); model[i].Scale(.04, .04, .04); model[i].CalculateNormals(0); } - drawmodel.load(modelfilename, 0); + drawmodel.load(modelfilename); drawmodel.Rotate(180, 0, 0); drawmodel.Scale(.04, .04, .04); drawmodel.FlipTexCoords(); - if ((tutoriallevel == 1) && (id != 0)) { + if ((Tutorial::active) && (id != 0)) { drawmodel.UniformTexCoords(); drawmodel.ScaleTexCoords(0.1); } @@ -667,13 +649,13 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil modellow.Scale(.04, .04, .04); modellow.CalculateNormals(0); - drawmodellow.load(modellowfilename, 0); + drawmodellow.load(modellowfilename); drawmodellow.Rotate(180, 0, 0); drawmodellow.Scale(.04, .04, .04); drawmodellow.FlipTexCoords(); - if (tutoriallevel == 1 && id != 0) + if (Tutorial::active && id != 0) drawmodellow.UniformTexCoords(); - if (tutoriallevel == 1 && id != 0) + if (Tutorial::active && id != 0) drawmodellow.ScaleTexCoords(0.1); drawmodellow.CalculateNormals(0); @@ -683,7 +665,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil modelclothes.Scale(.041, .04, .041); modelclothes.CalculateNormals(0); - drawmodelclothes.load(modelclothesfilename, 0); + drawmodelclothes.load(modelclothesfilename); drawmodelclothes.Rotate(180, 0, 0); drawmodelclothes.Scale(.04, .04, .04); drawmodelclothes.FlipTexCoords(); @@ -703,7 +685,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil joints.resize(num_joints); // read info for each joint - for (i = 0; i < num_joints; i++) { + for (int i = 0; i < num_joints; i++) { joints[i].load(tfile, joints); } @@ -714,7 +696,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil muscles.resize(num_muscles); // for each muscle... - for (i = 0; i < num_muscles; i++) { + for (int i = 0; i < num_muscles; i++) { muscles[i].load(tfile, model[0].vertexNum, joints); } @@ -729,22 +711,23 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // ??? for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].vertices.size(); i++) { + for (unsigned i = 0; i < muscles[j].vertices.size(); i++) { for (int k = 0; k < num_models; k++) { - if (muscles[j].vertices[i] < model[k].vertexNum) + if (muscles[j].vertices[i] < model[k].vertexNum) { model[k].owner[muscles[j].vertices[i]] = j; + } } } } // calculate some stuff FindForwards(); - for (i = 0; i < num_muscles; i++) { + for (int i = 0; i < num_muscles; i++) { FindRotationMuscle(i, -1); } // this seems to use opengl purely for matrix calculations for (int k = 0; k < num_models; k++) { - for (i = 0; i < model[k].vertexNum; i++) { + for (int i = 0; i < model[k].vertexNum; i++) { model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -770,7 +753,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // skip joints section fseek(tfile, sizeof(num_joints), SEEK_CUR); - for (i = 0; i < num_joints; i++) { + for (int i = 0; i < num_joints; i++) { // skip joint info lSize = sizeof(XYZ) + sizeof(float) @@ -790,7 +773,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // skip num_muscles fseek(tfile, sizeof(num_muscles), SEEK_CUR); - for (i = 0; i < num_muscles; i++) { + for (int i = 0; i < num_muscles; i++) { // skip muscle info lSize = sizeof(float) + sizeof(float) @@ -811,14 +794,15 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil } for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].verticeslow.size(); i++) { - if (muscles[j].verticeslow[i] < modellow.vertexNum) + for (unsigned i = 0; i < muscles[j].verticeslow.size(); i++) { + if (muscles[j].verticeslow[i] < modellow.vertexNum) { modellow.owner[muscles[j].verticeslow[i]] = j; + } } } // use opengl for its matrix math - for (i = 0; i < modellow.vertexNum; i++) { + for (int i = 0; i < modellow.vertexNum; i++) { modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -844,7 +828,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // skip num_joints fseek(tfile, sizeof(num_joints), SEEK_CUR); - for (i = 0; i < num_joints; i++) { + for (int i = 0; i < num_joints; i++) { // skip joint info lSize = sizeof(XYZ) + sizeof(float) @@ -864,7 +848,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // skip num_muscles fseek(tfile, sizeof(num_muscles), SEEK_CUR); - for (i = 0; i < num_muscles; i++) { + for (int i = 0; i < num_muscles; i++) { // skip muscle info lSize = sizeof(float) + sizeof(float) @@ -887,14 +871,15 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // ??? lSize = sizeof(int); for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].verticesclothes.size(); i++) { - if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum) + for (unsigned i = 0; i < muscles[j].verticesclothes.size(); i++) { + if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum) { modelclothes.owner[muscles[j].verticesclothes[i]] = j; + } } } // use opengl for its matrix math - for (i = 0; i < modelclothes.vertexNum; i++) { + for (int i = 0; i < modelclothes.vertexNum; i++) { modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -914,7 +899,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil } fclose(tfile); - for (i = 0; i < num_joints; i++) { + for (int i = 0; i < num_joints; i++) { for (j = 0; j < num_joints; j++) { if (joints[i].label == j) jointlabels[j] = i;