kick
/Users/morten/Programmering/cpp/kick/src/kick/math/frustum.cpp
00001 //
00002 //  frustum.cpp
00003 //  KickCPP
00004 //
00005 //  Created by Morten Nobel-Jørgensen on 21/12/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #include "kick/math/frustum.h"
00010 #include <glm/gtc/type_ptr.hpp>
00011 
00012 using namespace glm;
00013 
00014 namespace kick {
00015     FrustumIntersection Frustum::intersectAabb(Bounds3 aabb){
00016         vec3 center = aabb.center();
00017         vec3 halfVector = aabb.diagonal() * 0.5f;
00018         FrustumIntersection result = FrustumIntersection::Inside;
00019         
00020         for (int i = 0; i < 6; i++) {
00021             vec3 planeNormal = vec3(planes[i]);
00022             float e = dot(halfVector, abs(planeNormal));
00023             float s = dot(center, planeNormal) + planes[i].w;
00024             
00025             if (s - e > 0) {
00026                 // inside
00027             } else if (s + e < 0) { // outside
00028                 return FrustumIntersection::Outside;
00029             } else  {  // intersecting
00030                 result = FrustumIntersection::Intersecting;
00031             }
00032         }
00033         return result;
00034     }
00035 
00036     void Frustum::extractPlanes(glm::mat4 viewProjectionMatrix, bool normalizePlaneNormals){
00037         float* _viewProjectionMatrix = value_ptr(viewProjectionMatrix);
00038         auto _11 = _viewProjectionMatrix[0], _21 = _viewProjectionMatrix[1], _31 = _viewProjectionMatrix[2], _41 = _viewProjectionMatrix[3],
00039         _12 = _viewProjectionMatrix[4], _22 = _viewProjectionMatrix[5], _32 = _viewProjectionMatrix[6], _42 = _viewProjectionMatrix[7],
00040         _13 = _viewProjectionMatrix[8], _23 = _viewProjectionMatrix[9], _33 = _viewProjectionMatrix[10], _43 = _viewProjectionMatrix[11],
00041         _14 = _viewProjectionMatrix[12], _24 = _viewProjectionMatrix[13], _34 = _viewProjectionMatrix[14], _44 = _viewProjectionMatrix[15];
00042         
00043         // Left clipping plane
00044         planes[0] = vec4{
00045             _41 + _11,
00046             _42 + _12,
00047             _43 + _13,
00048             _44 + _14};
00049         
00050         // Right clipping plane
00051         planes[1] = vec4{
00052             _41 - _11,
00053             _42 - _12,
00054             _43 - _13,
00055             _44 - _14
00056         };
00057         // Top clipping plane
00058         planes[2] = vec4{
00059             _41 - _21,
00060             _42 - _22,
00061             _43 - _23,
00062             _44 - _24};
00063         // Bottom clipping plane
00064         planes[3] = vec4{
00065             _41 + _21,
00066             _42 + _22,
00067             _43 + _23,
00068             _44 + _24
00069         };
00070         // Near clipping plane
00071         planes[4] = vec4{
00072             _41 + _31,
00073             _42 + _32,
00074             _43 + _33,
00075             _44 + _34
00076         };
00077         // Far clipping plane
00078         planes[5] = vec4{
00079             _41 - _31,
00080             _42 - _32,
00081             _43 - _33,
00082             _44 - _34
00083         };
00084         if (normalizePlaneNormals) {
00085             for (int i=0;i<6;i++){
00086                 planes[i] = normalize(planes[i]);
00087             }
00088         }
00089     }
00090 }
 All Classes Functions Variables