Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "Headers/Frustum.h"
#include "Headers/Camera.h"

#include "Hardware/Video/Headers/GFXDevice.h"
#include "Core/Math/BoundingVolumes/Headers/BoundingBox.h"

namespace Divide {

Frustum::Frustum(Camera& parentCamera) : _pointsDirty(true),
                                         _parentCamera(parentCamera)
{
}

Frustum::FrustCollision Frustum::ContainsPoint(const vec3<F32>& point) const {
    for (const Plane<F32>& frustumPlane : _frustumPlanes) {
        if (frustumPlane.classifyPoint(point) != Plane<F32>::POSITIVE_SIDE) {
            return FRUSTUM_OUT;
        }
    }
    
    return FRUSTUM_IN;
}

Frustum::FrustCollision Frustum::ContainsSphere(const vec3<F32>& center, F32 radius) const {
    F32 distance = 0.0f;

    for (const Plane<F32>& frustumPlane : _frustumPlanes) {
        distance = frustumPlane.getDistance(center);

        if (distance < -radius) {     
            return FRUSTUM_OUT;
        }

        if (std::fabs(distance) < radius) {
            return FRUSTUM_INTERSECT;
        }
    }

    return FRUSTUM_IN;
}

Frustum::FrustCollision Frustum::ContainsBoundingBox(const BoundingBox& bbox) const {
    const vec3<F32> *boxCorners = bbox.getPoints();

    I32 iPtIn = 1, iTotalIn = 0, iInCount = 8;

    for (const Plane<F32>& frustumPlane : _frustumPlanes) {
        iInCount = 8;
        iPtIn = 1;

        for (U8 c = 0; c < 8; ++c) {
            if (frustumPlane.classifyPoint(boxCorners[c]) == Plane<F32>::NEGATIVE_SIDE) {
                iPtIn = 0;
                iInCount--;
            }
        }

        if (iInCount == 0) {
            return FRUSTUM_OUT;
        }

        iTotalIn += iPtIn;
    }

    return iTotalIn == 6 ? FRUSTUM_IN : FRUSTUM_INTERSECT;
}

void Frustum::Extract() {

    _viewProjectionMatrixCache.set(GFX_DEVICE.getMatrix(VIEW_PROJECTION_MATRIX));
    
	if (_viewProjectionMatrixCacheOld == _viewProjectionMatrixCache) {
        return;
	}

    _viewProjectionMatrixCacheOld.set(_viewProjectionMatrixCache);

    Plane<F32>& rightPlane  = _frustumPlanes[0];
    Plane<F32>& leftPlane   = _frustumPlanes[1];
    Plane<F32>& bottomPlane = _frustumPlanes[2];
    Plane<F32>& topPlane    = _frustumPlanes[3];
    Plane<F32>& farPlane    = _frustumPlanes[4];
    Plane<F32>& nearPlane   = _frustumPlanes[5];

    const F32* mat = &_viewProjectionMatrixCache.mat[0];

    rightPlane.set(mat[ 3] - mat[ 0], 
                   mat[ 7] - mat[ 4], 
                   mat[11] - mat[ 8], 
                   mat[15] - mat[12]); 
    rightPlane.normalize();

    leftPlane.set(mat[ 3] + mat[ 0],
                  mat[ 7] + mat[ 4],
                  mat[11] + mat[ 8], 
                  mat[15] + mat[12]);
    leftPlane.normalize();

    bottomPlane.set(mat[ 3] + mat[ 1], 
                    mat[ 7] + mat[ 5], 
                    mat[11] + mat[ 9], 
                    mat[15] + mat[13]); 
    bottomPlane.normalize();

    topPlane.set(mat[ 3] - mat[ 1], 
                 mat[ 7] - mat[ 5], 
                 mat[11] - mat[ 9], 
                 mat[15] - mat[13]);
    topPlane.normalize();

    farPlane.set(mat[ 3] - mat[ 2], 
                 mat[ 7] - mat[ 6], 
                 mat[11] - mat[10], 
                 mat[15] - mat[14]); 
    farPlane.normalize();

    nearPlane.set(mat[ 3] + mat[ 2], 
                  mat[ 7] + mat[ 6], 
                  mat[11] + mat[10], 
                  mat[15] + mat[14]); 
    nearPlane.normalize();


    _pointsDirty = true;
}

void Frustum::intersectionPoint(const Plane<F32> & a, 
                                const Plane<F32> & b, 
                                const Plane<F32> & c, 
                                vec3<F32>& outResult) {

    outResult.set((a.getDistance() * (cross(b.getNormal(), c.getNormal()))) + 
                  (b.getDistance() * (cross(c.getNormal(), a.getNormal()))) + 
                  (c.getDistance() * (cross(a.getNormal(), b.getNormal()))) / 
                  -dot(a.getNormal(), cross(b.getNormal(), c.getNormal())));
}

void Frustum::updatePoints() {
    if (!_pointsDirty) {
        return;
    }

    const Plane<F32>& rightPlane  = _frustumPlanes[0];
    const Plane<F32>& leftPlane   = _frustumPlanes[1];
    const Plane<F32>& bottomPlane = _frustumPlanes[2];
    const Plane<F32>& topPlane    = _frustumPlanes[3];
    const Plane<F32>& farPlane    = _frustumPlanes[4];
    const Plane<F32>& nearPlane   = _frustumPlanes[5];

    intersectionPoint(nearPlane, leftPlane,  topPlane,    _frustumPoints[0]);
    intersectionPoint(nearPlane, rightPlane, topPlane,    _frustumPoints[1]);
    intersectionPoint(nearPlane, rightPlane, bottomPlane, _frustumPoints[2]);
    intersectionPoint(nearPlane, leftPlane,  bottomPlane, _frustumPoints[3]);
    intersectionPoint(farPlane,  leftPlane,  topPlane,    _frustumPoints[4]);
    intersectionPoint(farPlane,  rightPlane, topPlane,    _frustumPoints[5]);
    intersectionPoint(farPlane,  rightPlane, bottomPlane, _frustumPoints[6]);
    intersectionPoint(farPlane,  leftPlane,  bottomPlane, _frustumPoints[7]);

    _pointsDirty = false;
}

// Get the frustum corners in WorldSpace. cornerWS must be a vector with at least 8 allocated slots
void Frustum::getCornersWorldSpace(vectorImpl<vec3<F32> >& cornersWS){
    assert(cornersWS.size() >= 8);

    updatePoints();

    for (U8 i = 0; i < 8; ++i) {
        cornersWS[i].set(_frustumPoints[i]);
    }

}

// Get the frustum corners in ViewSpace. cornerVS must be a vector with at least 8 allocated slots
void Frustum::getCornersViewSpace(vectorImpl<vec3<F32> >& cornersVS){
    assert(cornersVS.size() >= 8);

    updatePoints();

    const mat4<F32>& viewMatrix = _parentCamera.getViewMatrix();
    for (U8 i = 0; i < 8; ++i) {
        cornersVS[i].set(viewMatrix.transform(_frustumPoints[i]));
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Rendering/Camera/Frustum.cpp

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

323 Diff Diff IonutCava picture IonutCava Thu 11 Sep, 2014 20:58:50 +0000

[Ionut]
- Reworked GFXDevice API initialization system
- Added a separate map for boolean values in ParamHandler
- More cleanups in GFXDevice class
- Fixed an infinite loop issue in Task class

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

289 Diff Diff IonutCava picture IonutCava Fri 25 Jul, 2014 17:46:28 +0000

[Ionut]
- Added transform interpolation support to avoid object stuttering when frame rate varies (see Transform.cpp)
- Updated the quaternion class to be a wrapper around a regular vec4 to take advantage of it’s built in functions (length, dot, cross, etc)

264 k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]