#pragma once #include "WorldManager.h" void InitWorld(VWorld * world) { world->cameraPos.x = 0.0; world->cameraPos.y = 0.0; world->cameraPos.z = 0.0; world->view.x = 0.0; world->view.y = 0.0; world->view.z = -1.0; world->up.x = 0.0; world->up.y = 1.0; world->up.z = 0.0; world->Weapons = NULL; InitObject(&world->CrossHair); world->CrossHair.Position.x = 0.0; world->CrossHair.Position.y = 0.0; world->CrossHair.Position.z = -1.7; /* world->CrossHair.Rotation.x = 0.0; world->CrossHair.Rotation.y = 0.0; world->CrossHair.Rotation.z = 0.0; InitModel(&world->CrossHair.Model); */ BuildCrossHairModel(&world->CrossHair.Model); world->Objects = (VObject *) NewPtr(sizeof(VObject)); if (world->Objects != NULL) { Vector targetPos; InitObject(world->Objects); BuildTargetModel(&world->Objects->Model); targetPos.x = 2.0; targetPos.y = 1.0; targetPos.z = -7.0; MoveObjectToPoint(world->Objects, &targetPos); CalculateRadius(world->Objects); } } void DrawCrossHair(VWorld * world) { DrawObject(&world->CrossHair); } void CleanUpWorld(VWorld * world) { VWeapon * theWeapon; CleanUpObject(&world->CrossHair); while (world->Weapons != NULL) { theWeapon = world->Weapons; world->Weapons = theWeapon->next; CleanUpWeapon(theWeapon); DisposePtr((Ptr) theWeapon); } } void OffsetCamera(VWorld * world, Vector * offset, float interval) { world->cameraPos.x += offset->x / interval; world->cameraPos.y += offset->y / interval; world->cameraPos.z += offset->z / interval; world->view.x += offset->x / interval; world->view.y += offset->y / interval; world->view.z += offset->z / interval; } void RotateCamera(VWorld * world, Vector * rotate, float interval) { Quaternion Rotation, QuatView, Result; Vector NewRot, Forwards; NewRot = *rotate; NormalizeVector(&NewRot); RotateQuaternion(&NewRot, VectorMagnitude(rotate) / interval, &Rotation); ForwardsVector(world, 1, &Forwards); Vector2Quaternion(&Forwards, &QuatView); MultiplyQuaternion(&Rotation, &QuatView, &Result); ConjugateQuaternion(&Rotation); MultiplyQuaternion(&Result, &Rotation, &QuatView); world->view.x = world->cameraPos.x + QuatView.v.x; world->view.y = world->cameraPos.y + QuatView.v.y; world->view.z = world->cameraPos.z + QuatView.v.z; ConjugateQuaternion(&Rotation); Vector2Quaternion(&world->up, &QuatView); MultiplyQuaternion(&Rotation, &QuatView, &Result); ConjugateQuaternion(&Rotation); MultiplyQuaternion(&Result, &Rotation, &QuatView); world->up.x = QuatView.v.x; world->up.y = QuatView.v.y; world->up.z = QuatView.v.z; NormalizeVector(&world->up); } void ForwardsVector(VWorld * w, float Distance, Vector * Forwards) { Forwards->x = (w->view.x - w->cameraPos.x) * Distance; Forwards->y = (w->view.y - w->cameraPos.y) * Distance; Forwards->z = (w->view.z - w->cameraPos.z) * Distance; } void ApplyCamera(VWorld * world) { gluLookAt (world->cameraPos.x, world->cameraPos.y, world->cameraPos.z, world->view.x, world->view.y, world->view.z, world->up.x, world->up.y, world->up.z); } void DrawWorld(VWorld * world, Vector * rotate, Vector * movement, float interval) { VWeapon * theWeapon; VObject * theObject; /* Fill with semi transparent background */ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDepthMask(GL_FALSE); glColor4f(0.0, 0.0, 0.0, (0.024 / (interval > 1.0 ? 1.0 : interval))); glBegin(GL_QUADS); glVertex3f(-10.0, 10.0, -2.0); glVertex3f(10.0, 10.0, -2.0); glVertex3f(10.0, -10.0, -2.0); glVertex3f(-10.0, -10.0, -2.0); glEnd(); glDepthMask(GL_TRUE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); OffsetCamera(world, movement, interval); RotateCamera(world, rotate, interval); ApplyCamera(world); for (theWeapon = world->Weapons; theWeapon != NULL; theWeapon = theWeapon->next) { DrawWeapon(theWeapon); } for (theObject = world->Objects; theObject != NULL; theObject = theObject->next) { DrawObject(theObject); } } void RunWorld(VWorld * world, float interval) { VWeapon * theWeapon, * previousWeapon = NULL; VObject * theObject, * previousObject = NULL; for (theWeapon = world->Weapons; theWeapon != NULL; theWeapon = theWeapon->next) { if (theWeapon->taggedForRemoval) { if (previousWeapon == NULL) world->Weapons = theWeapon->next; else previousWeapon->next = theWeapon->next; CleanUpWeapon(theWeapon); DisposePtr((Ptr) theWeapon); theWeapon = previousWeapon; if (theWeapon == NULL) break; continue; } RunWeapon(theWeapon, interval, world->Objects); previousWeapon = theWeapon; } for (theObject = world->Objects; theObject != NULL; theObject = theObject->next) { if (theObject->taggedForRemoval) { if (previousObject == NULL) world->Objects = theObject->next; else previousObject->next = theObject->next; CleanUpObject(theObject); DisposePtr((Ptr) theObject); theObject = previousObject; if (theObject == NULL) break; continue; } /* Run object, if necessary */ previousObject = theObject; } }