Skip to content

Commit

Permalink
merge with glmext
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzzzrm committed Apr 29, 2014
2 parents 649315d + 8aa03d1 commit 0d0df47
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 70 deletions.
4 changes: 3 additions & 1 deletion src/ai/basictasks/fighterfighttask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ai/boardcomputer.h"
#include "utils/geometryhelper.h"
#include "utils/glmext/safenormalize.h"
#include "utils/randfloat.h"
#include "voxel/voxelclusterbounds.h"
#include "worldobject/ship.h"
Expand Down Expand Up @@ -134,7 +135,8 @@ void FighterFightTask::setState(State newState) {
float FighterFightTask::angleToTarget() {
WorldObject* worldObject = boardComputer()->worldObject();
glm::vec3 shipDirection = glm::vec3(0, 0, -1);
glm::vec3 targetDirection = glm::inverse(worldObject->transform().orientation()) * glm::normalize(m_primaryTarget->transform().position() - worldObject->transform().position());
glm::vec3 targetDirection = glm::inverse(worldObject->transform().orientation()) * safeNormalize(m_primaryTarget->transform().position() - worldObject->transform().position());
float angle = GeometryHelper::angleBetween(shipDirection, targetDirection);
return glm::degrees(angle);
}

14 changes: 8 additions & 6 deletions src/ai/boardcomputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#include "collision/collisionfilter.h"

#include "utils/glmext/safenormalize.h"
#include "utils/randvec3.h"
#include "utils/geometryhelper.h"

#include "physics/physics.h"

#include "worldobject/ship.h"
Expand Down Expand Up @@ -47,13 +49,13 @@ void BoardComputer::moveTo(const glm::vec3& position, bool decelerate) {
// the projection is already past the target, but we don't want to deaccelerate
// instead, project the target from our current position to a sphere around our position
float projectionDistance = glm::length(projectedPosition - currentPosition);
glm::vec3 fakePosition = currentPosition + glm::normalize(position - currentPosition) * 1.5f * projectionDistance;
glm::vec3 fakePosition = currentPosition + safeNormalize(position - currentPosition) * 1.5f * projectionDistance;

delta = fakePosition - projectedPosition;
}
}

glm::vec3 direction = glm::inverse(m_worldObject->transform().orientation()) * glm::normalize(delta);
glm::vec3 direction = glm::inverse(m_worldObject->transform().orientation()) * safeNormalize(delta);
m_engineState.setDirectional(direction);
}

Expand All @@ -71,7 +73,7 @@ void BoardComputer::rotateTo(const glm::vec3& position, const glm::vec3& up) {
if (position == m_worldObject->transform().position()) {
return;
}
glm::vec3 targetDirection = glm::inverse(m_worldObject->transform().orientation()) * glm::normalize(position - m_worldObject->transform().position());
glm::vec3 targetDirection = glm::inverse(m_worldObject->transform().orientation()) * safeNormalize(position - m_worldObject->transform().position());

// The rotation that needs to be performed, in the local coordinate-sys
glm::quat rotation = GeometryHelper::quatFromTo(projectedDirection, targetDirection);
Expand All @@ -96,12 +98,12 @@ void BoardComputer::rotateTo(const glm::vec3& position, const glm::vec3& up) {

glm::vec3 BoardComputer::rotateUpTo(const glm::vec3& up) {
glm::vec3 upDirection = glm::vec3(0, 1, 0);
glm::vec3 newUpDirection = glm::inverse(m_worldObject->transform().orientation()) * glm::normalize(up);
glm::vec3 newUpDirection = glm::inverse(m_worldObject->transform().orientation()) * safeNormalize(up, glm::vec3(0, 1, 0));
glm::quat upRotation = GeometryHelper::quatFromTo(upDirection, newUpDirection);

if (glm::abs(glm::angle(upRotation)) > s_minActAngle) {
glm::vec3 euler = glm::eulerAngles(upRotation);
return (glm::normalize(euler) * 0.5f);
return (safeNormalize(euler) * 0.5f);
}

return glm::vec3(0.0f);
Expand All @@ -115,7 +117,7 @@ glm::vec3 BoardComputer::rotateUpAuto(const glm::quat& rotation) {
glm::quat upRotation = GeometryHelper::quatFromTo(upDirection, newUpDirection);
glm::vec3 euler = glm::eulerAngles(upRotation);

return (glm::normalize(euler) * 0.5f);
return (safeNormalize(euler) * 0.5f);
}

return glm::vec3(0.0f);
Expand Down
3 changes: 2 additions & 1 deletion src/ai/squad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "worldobject/ship.h"
#include "ai/aigrouptask.h"
#include "utils/glmext/safenormalize.h"
#include "physics/physics.h"
#include "voxel/voxelclusterbounds.h"

Expand Down Expand Up @@ -120,5 +121,5 @@ glm::vec3 Squad::calculateFormationPosition(Ship* member, int position) {
}
distance += member->bounds().sphere().radius() + 10;
glm::vec3 direction = (position % 2) ? glm::vec3(1, 0, 1) : glm::vec3(-1, 0, 1);
return m_leader->transform().position() + m_leader->physics().speed().directional() + m_leader->transform().orientation() * (distance * glm::normalize(direction));
return m_leader->transform().position() + m_leader->physics().speed().directional() + m_leader->transform().orientation() * (distance * safeNormalize(direction));
}
5 changes: 3 additions & 2 deletions src/equipment/hardpoint.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "hardpoint.h"

#include "utils/glmext/safenormalize.h"
#include "utils/geometryhelper.h"

#include "voxel/specialvoxels/hardpointvoxel.h"
Expand Down Expand Up @@ -37,8 +38,8 @@ const glm::vec3& Hardpoint::direction() const {
}

void Hardpoint::setDirection(const glm::vec3& direction) {
assert(glm::length(direction) > 0);
m_direction = glm::normalize(direction);
assert(normalizeable(direction));
m_direction = safeNormalize(direction);
}

float Hardpoint::fieldOfAim() const {
Expand Down
7 changes: 4 additions & 3 deletions src/equipment/weapons/gun.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gun.h"

#include "utils/geometryhelper.h"
#include "utils/glmext/safenormalize.h"

#include "bullet.h"

Expand Down Expand Up @@ -67,15 +68,15 @@ void Gun::setupBullet(Bullet* bullet, const glm::vec3& point) {
WorldObject* firingWorldObject = m_hardpoint->components()->worldObject();

glm::quat worldObjectOrientation = firingWorldObject->transform().orientation();
glm::vec3 bulletDirection = glm::normalize(point - m_hardpoint->voxel()->position());
glm::vec3 bulletDirection = safeNormalize(point - m_hardpoint->voxel()->position(), glm::vec3(0, 0, -1));
glm::vec3 hardpointDirection = worldObjectOrientation * glm::vec3(0, 0, -1);
glm::vec3 bulletUp = glm::cross(bulletDirection, hardpointDirection);

//bulletTransform.setOrientation(Math::quatFromDir(bulletDirection));
bulletTransform.setOrientation(m_hardpoint->components()->worldObject()->transform().orientation());

if (bulletUp != glm::vec3(0)) {
glm::vec3 rotationAxis = glm::normalize(bulletUp);
if (normalizeable(bulletUp)) {
glm::vec3 rotationAxis = safeNormalize(bulletUp);
float angle = GeometryHelper::angleBetween(bulletDirection, hardpointDirection);
glm::quat bulletOrientation = glm::angleAxis(-angle, rotationAxis);
bulletTransform.rotateWorld(bulletOrientation); //then rotate towards target
Expand Down
5 changes: 3 additions & 2 deletions src/equipment/weapons/splitrocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "resource/worldelementbuilder.h"

#include "utils/geometryhelper.h"
#include "utils/glmext/safenormalize.h"
#include "utils/randfloatpool.h"

#include "voxeleffect/voxelexplosiongenerator.h"
Expand Down Expand Up @@ -156,8 +157,8 @@ void SplitRocket::setChildSpeed(WorldObject* child, const glm::quat& launchOrien

speed.setDirectional(launchOrientation * glm::vec3(0, 0, -1) * glm::length(speed.directional()));

if (glm::length(speed.directional()) > 0.0f) {
glm::vec3 boostDirection = glm::normalize(speed.directional());
if (normalizeable(speed.directional())) {
glm::vec3 boostDirection = safeNormalize(speed.directional());
glm::vec3 boost = boostDirection * RandFloatPool::randomize(m_childrenSpeedBoost, m_childrenSpeedBoostRandomization);
speed.setDirectional(speed.directional() + boost);
}
Expand Down
4 changes: 3 additions & 1 deletion src/gamestate/gameplay/input/gameplayfreecaminput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "input/inputmapping.h"

#include "utils/glmext/safenormalize.h"


GamePlayFreecamInput::GamePlayFreecamInput(GamePlay& gamePlay) :
GamePlayInput(gamePlay),
Expand Down Expand Up @@ -86,7 +88,7 @@ void GamePlayFreecamInput::applyUpdates() {
// collect them and apply them here

if (glm::length(m_moveUpdate) > 1.0f) {
m_moveUpdate = glm::normalize(m_moveUpdate);
m_moveUpdate = safeNormalize(m_moveUpdate);
}

m_position += m_orientation * (m_moveUpdate * prop_moveFactor.get());
Expand Down
15 changes: 9 additions & 6 deletions src/gamestate/gameplay/input/gameplaynormalinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
#include "input/inputmapping.h"
#include "input/inputconfigurator.h"

#include "ui/hud/hud.h"
#include "ui/targetselector.h"
#include "ui/hud/crosshair.h"

#include "utils/glmext/safenormalize.h"


#include "world/world.h"
#include "worldobject/worldobject.h"
#include "worldobject/worldobjectcomponents.h"
#include "worldobject/ship.h"

#include "player.h"

#include "ui/hud/hud.h"
#include "ui/targetselector.h"
#include "ui/hud/crosshair.h"



/*
Expand Down Expand Up @@ -184,13 +187,13 @@ void GamePlayNormalInput::applyUpdates() {
m_rocketUpdate = false;

if (glm::length(m_moveUpdate) > 1.0f) {
m_moveUpdate = glm::normalize(m_moveUpdate);
m_moveUpdate = safeNormalize(m_moveUpdate);
}
World::instance()->player().move(m_moveUpdate);
m_moveUpdate = glm::vec3(0);

if (glm::length(m_rotateUpdate) > 1.0f) {
m_rotateUpdate = glm::normalize(m_rotateUpdate);
m_rotateUpdate = safeNormalize(m_rotateUpdate);
}
World::instance()->player().rotate(m_rotateUpdate);
m_rotateUpdate = glm::vec3(0);
Expand Down
4 changes: 3 additions & 1 deletion src/physics/impulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <cassert>

#include "utils/glmext/safenormalize.h"


Impulse::Impulse(WorldObject* worldObject, Voxel* voxel, const glm::vec3& speed, float mass, const glm::vec3& normal):
m_worldObject(worldObject),
m_voxel(voxel),
m_speed(speed),
m_mass(mass),
m_normal(glm::normalize(normal))
m_normal(safeNormalize(normal))
{
assert(m_voxel);
}
Expand Down
5 changes: 4 additions & 1 deletion src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "ui/hud/aimhelperhudget.h"
#include "ui/hud/crosshair.h"

#include "utils/glmext/safenormalize.h"

#include "worldobject/worldobjectinfo.h"

#include "utils/aimer.h"
Expand Down Expand Up @@ -68,6 +70,7 @@ CameraHead& Player::cameraHead() {
HUD& Player::hud() {
return *m_hud;
}
#include "utils/glmext/safenormalize.h"

void Player::fire() {
if (ship()) {
Expand All @@ -76,7 +79,7 @@ void Player::fire() {
if(m_hud->aimHelper().hovered()) {
targetPoint = m_hud->aimHelper().targetPoint();
} else {
glm::vec3 shootDirection(glm::normalize(m_hud->crossHair().worldPosition() - cameraHead().position()));
glm::vec3 shootDirection(safeNormalize(m_hud->crossHair().worldPosition() - cameraHead().position()));
Ray ray(m_hud->crossHair().worldPosition(), shootDirection);
targetPoint = m_aimer->aim(ray);
}
Expand Down
14 changes: 8 additions & 6 deletions src/ui/hud/hudelements.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "hudelements.h"

#include "utils/glmext/safenormalize.h"

#include "voxel/voxelrenderer.h"

#include "hud.h"
Expand All @@ -11,14 +13,14 @@

HUDElements::HUDElements(HUD& hud):
m_hud(hud),
m_targetName(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(0, -1.1f, -2)), 0.025f, "")),
m_speedLabel(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(1.5f, -1.1f, -2)), 0.020f, "")),
m_shieldLabel(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(-1.5f, -1.1f, -2)), 0.020f, "")),
m_missionTitle(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(0.0f, 1.0f, -2)), 0.020f, "")),
m_targetName(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(0, -1.1f, -2)), 0.025f, "")),
m_speedLabel(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(1.5f, -1.1f, -2)), 0.020f, "")),
m_shieldLabel(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(-1.5f, -1.1f, -2)), 0.020f, "")),
m_missionTitle(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(0.0f, 1.0f, -2)), 0.020f, "")),
m_missionTitleHider(new HudgetHideAnimation(*m_missionTitle)),
m_missionCaption(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(0.0f, 0.8f, -2)), 0.010f, "")),
m_missionCaption(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(0.0f, 0.8f, -2)), 0.010f, "")),
m_missionCaptionHider(new HudgetHideAnimation(*m_missionCaption)),
m_missionMessage(new TextFieldHudget(&m_hud, glm::normalize(glm::vec3(-0.9f, -0.9f, -2)), 0.010f, "")),
m_missionMessage(new TextFieldHudget(&m_hud, safeNormalize(glm::vec3(-0.9f, -0.9f, -2)), 0.010f, "")),
m_missionMessageHider(new HudgetHideAnimation(*m_missionMessage))
{
addHudget(m_targetName);
Expand Down
5 changes: 3 additions & 2 deletions src/ui/hud/hudget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "hudget.h"

#include "utils/geometryhelper.h"
#include "utils/glmext/safenormalize.h"

#include "hud.h"

Expand Down Expand Up @@ -84,11 +85,11 @@ void Hudget::setRelativeDistance(float relativeDistance) {
}

void Hudget::pointToWorldPoint(const glm::vec3& worldPoint) {
m_direction = glm::normalize(glm::inverse(m_hud->orientation()) * (worldPoint - m_hud->position()));
m_direction = safeNormalize(glm::inverse(m_hud->orientation()) * (worldPoint - m_hud->position()));
}

void Hudget::pointToLocalPoint(const glm::vec3& localPoint) {
m_direction = glm::normalize(localPoint);
m_direction = safeNormalize(localPoint);
}

glm::vec3 Hudget::localDirection() const {
Expand Down
7 changes: 5 additions & 2 deletions src/ui/hud/objecthudget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

#include "worldobject/worldobjectinfo.h"

#include "utils/glmext/safenormalize.h"

#include "world/world.h"

#include "worldobject/worldobjectinfo.h"
#include "worldobject/worldobject.h"
#include "worldobject/ship.h"

Expand Down Expand Up @@ -148,9 +151,9 @@ glm::vec3 ObjectHudget::closestPointInsideFov() {
glm::vec3 pointInsideFov;

if (angleX < angleY) {
pointInsideFov = glm::normalize(intersectionX);
pointInsideFov = safeNormalize(intersectionX);
} else {
pointInsideFov = glm::normalize(intersectionY);
pointInsideFov = safeNormalize(intersectionY);
}
pointInsideFov.x = glm::abs(pointInsideFov.x);
pointInsideFov.y = glm::abs(pointInsideFov.y);
Expand Down
11 changes: 7 additions & 4 deletions src/utils/geometryhelper.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "geometryhelper.h"

#include "randvec3.h"
#include "geometry/ray.h"

#include "utils/glmext/safenormalize.h"

#include "randvec3.h"


bool GeometryHelper::intersectRectangle(const Ray* ray, const glm::vec3& p, const glm::vec3& q, const glm::vec3& r, const glm::vec3& s) {
glm::vec3 intersectionDummy;
Expand All @@ -27,7 +30,7 @@ glm::vec3 GeometryHelper::plane(const glm::vec3& p, const glm::vec3& q, const gl
}

float GeometryHelper::angleBetween(const glm::vec3& u, const glm::vec3& v) {
float angle = glm::acos(glm::clamp(glm::dot(glm::normalize(u), glm::normalize(v)), -1.0f, 1.0f));
float angle = glm::acos(glm::clamp(glm::dot(safeNormalize(u), safeNormalize(v)), -1.0f, 1.0f));
assert(std::isfinite(angle));
return angle;
}
Expand All @@ -44,7 +47,7 @@ glm::quat GeometryHelper::quatFromTo(const glm::vec3& u, const glm::vec3& v) {
if (w == glm::vec3(0)) {
w = RandVec3::randUnitVec();
}
return glm::angleAxis(angle, glm::normalize(w));
return glm::angleAxis(angle, safeNormalize(w));
}

glm::quat GeometryHelper::quatFromViewDirection(const glm::vec3& direction) {
Expand All @@ -57,5 +60,5 @@ glm::quat GeometryHelper::quatFromViewDirection(const glm::vec3& direction) {
w = RandVec3::randUnitVec();
}

return glm::angleAxis(angle, glm::normalize(w));
return glm::angleAxis(angle, safeNormalize(w));
}
5 changes: 0 additions & 5 deletions src/utils/geometryhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ class GeometryHelper {

// Returns the quaternion from (0,0,-1) to dir
static glm::quat quatFromViewDirection(const glm::vec3& dir);

// Normalize, and return value again if glm::length(value) is 0
template<typename T>
static T safeNormalize(const T& value);
};

#include "geometryhelper.inl"
16 changes: 0 additions & 16 deletions src/utils/geometryhelper.inl

This file was deleted.

Loading

0 comments on commit 0d0df47

Please sign in to comment.