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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "Headers/Object3D.h"

#include "Managers/Headers/SceneManager.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Geometry/Material/Headers/Material.h"

namespace Divide {

Object3D::Object3D(const ObjectType& type, U32 flag) : Object3D("", type, flag)

{
}

Object3D::Object3D(const stringImpl& name, const ObjectType& type, U32 flag) : SceneNode(name,TYPE_OBJECT3D),
                                                                                _update(false),
                                                                                _geometryType(type),
                                                                                _geometryFlagMask(flag),
                                                                                _geometryPartitionId(0)
{
    _buffer = bitCompare(_geometryFlagMask, OBJECT_FLAG_NO_VB) ? nullptr : GFX_DEVICE.newVB();
}

Object3D::~Object3D()
{
	if ( !bitCompare( _geometryFlagMask, OBJECT_FLAG_NO_VB ) ) {
        MemoryManager::DELETE( _buffer );
	}
}

void Object3D::setGeometryVB(VertexBuffer* const vb) {
	DIVIDE_ASSERT(_buffer == nullptr, 
                  "Object3D error: Please remove the previous vertex buffer of this Object3D before specifying a new one!");
	_buffer = vb;
}

VertexBuffer* const Object3D::getGeometryVB() const {
    return _buffer;
}

void Object3D::getDrawCommands(SceneGraphNode* const sgn, 
                               const RenderStage& currentRenderStage, 
                               SceneRenderState& sceneRenderState, 
                               vectorImpl<GenericDrawCommand>& drawCommandsOut) {
    RenderingComponent* const renderable = sgn->getComponent<RenderingComponent>();
    assert(renderable != nullptr);

    VertexBuffer* const vb = getGeometryVB();

    GenericDrawCommand drawCmd;
    drawCmd.renderWireframe(renderable->renderWireframe());
    drawCmd.stateHash(renderable->getDrawStateHash(currentRenderStage));
    drawCmd.shaderProgram(renderable->getDrawShader(currentRenderStage));
    drawCmd.sourceBuffer(vb);
    drawCmd.indexCount((U32)(vb->getIndexCount()));

    drawCommandsOut.push_back(drawCmd);
}

void Object3D::render(SceneGraphNode* const sgn, const SceneRenderState& sceneRenderState, const RenderStage& currentRenderStage){
    GFX_DEVICE.submitRenderCommand(sgn->getComponent<RenderingComponent>()->getDrawCommands());
}

bool Object3D::onDraw(SceneGraphNode* const sgn, const RenderStage& currentStage){
    return getState() == RES_LOADED;
}

void Object3D::computeNormals() {
    if (!getGeometryVB()) return;

    vec3<F32> v1 , v2, normal;
    //Code from http://devmaster.net/forums/topic/1065-calculating-normals-of-a-mesh/

    vectorImpl<vec3<F32> >* normal_buffer = nullptr;
    normal_buffer = MemoryManager_NEW vectorImpl<vec3<F32> >[getGeometryVB()->getPosition().size()];

    for( U32 i = 0; i < getGeometryVB()->getIndexCount(); i += 3 ) {
        // get the three vertices that make the faces
        const vec3<F32>& p1 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+0));
        const vec3<F32>& p2 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+1));
        const vec3<F32>& p3 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+2));

        v1.set(p2 - p1);
        v2.set(p3 - p1);
        normal.cross(v1, v2);
        normal.normalize();

        // Store the face's normal for each of the vertices that make up the face.
        normal_buffer[getGeometryVB()->getIndex(i+0)].push_back( normal );
        normal_buffer[getGeometryVB()->getIndex(i+1)].push_back( normal );
        normal_buffer[getGeometryVB()->getIndex(i+2)].push_back( normal );
    }

    getGeometryVB()->resizeNormalCount((U32)getGeometryVB()->getPosition().size());
    // Now loop through each vertex vector, and average out all the normals stored.
    vec3<F32> currentNormal;
    for(U32 i = 0; i < getGeometryVB()->getPosition().size(); ++i ){
        currentNormal.reset();
        for(U32 j = 0; j < normal_buffer[i].size(); ++j ){
            currentNormal += normal_buffer[i][j];
        }
        currentNormal /= normal_buffer[i].size();

        getGeometryVB()->modifyNormalValue(i, currentNormal);
    }
    MemoryManager::DELETE_ARRAY( normal_buffer );
}

void Object3D::computeTangents(){
    if (!getGeometryVB()) return;

    //Code from: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/#header-1
    // inputs
    const vectorImpl<vec2<F32> > & uvs      = getGeometryVB()->getTexcoord();
    //const vectorImpl<vec3<F32> > & normals  = _geometry->getNormal();

    vec3<F32> deltaPos1, deltaPos2;
    vec2<F32> deltaUV1, deltaUV2;
    vec3<F32> tangent, bitangent;

    for( U32 i = 0; i < getGeometryVB()->getIndexCount(); i += 3 ) {
        // get the three vertices that make the faces
        const vec3<F32>& v0 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+0));
        const vec3<F32>& v1 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+1));
        const vec3<F32>& v2 = getGeometryVB()->getPosition(getGeometryVB()->getIndex(i+2));

        // Shortcuts for UVs
        const vec2<F32> & uv0 = uvs[getGeometryVB()->getIndex(i+0)];
        const vec2<F32> & uv1 = uvs[getGeometryVB()->getIndex(i+1)];
        const vec2<F32> & uv2 = uvs[getGeometryVB()->getIndex(i+2)];

        // Edges of the triangle : position delta
        deltaPos1.set(v1-v0);
        deltaPos2.set(v2-v0);

        // UV delta
        deltaUV1.set(uv1-uv0);
        deltaUV2.set(uv2-uv0);

        F32 r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
        tangent.set((deltaPos1 * deltaUV2.y   - deltaPos2 * deltaUV1.y)*r);
        bitangent.set((deltaPos2 * deltaUV1.x   - deltaPos1 * deltaUV2.x)*r);

        // Set the same tangent for all three vertices of the triangle.
        // They will be merged later, in vbindexer.cpp
        getGeometryVB()->addTangent(tangent);
        getGeometryVB()->addTangent(tangent);
        getGeometryVB()->addTangent(tangent);
        // Same thing for binormals
        getGeometryVB()->addBiTangent(bitangent);
        getGeometryVB()->addBiTangent(bitangent);
        getGeometryVB()->addBiTangent(bitangent);
    }
}

//Create a list of triangles from the vertices + indices lists based on primitive type
bool Object3D::computeTriangleList(bool force){
    VertexBuffer* geometry = getGeometryVB();
	DIVIDE_ASSERT(geometry != nullptr, "Object3D error: Please specify a valid VertexBuffer before calculating the triangle list!");

    U32 partitionOffset = geometry->getPartitionOffset(_geometryPartitionId);
    U32 partitionCount  = geometry->getPartitionCount(_geometryPartitionId);
    PrimitiveType type  = (_geometryType == MESH || _geometryType == SUBMESH ? TRIANGLES : TRIANGLE_STRIP);
    //We can't have a VB without vertex positions
    DIVIDE_ASSERT(!geometry->getPosition().empty(), 
                  "Object3D error: computeTriangleList called with no position data available!");

    if(!_geometryTriangles.empty() && force) 
        _geometryTriangles.resize(0);

    if(geometry->getIndexCount() == 0) 
        return false;
    
    U32 indiceCount = partitionCount;
    bool largeIndices = geometry->usesLargeIndices();
    if(type == TRIANGLE_STRIP){
        U32 indiceStart = 2 + partitionOffset;
        U32 indiceEnd = indiceCount + partitionOffset;
        vec3<U32> curTriangle;
        _geometryTriangles.reserve(indiceCount * 0.5);
        if(largeIndices){
            const vectorImpl<U32>& indices = geometry->getIndicesL();
            for(U32 i = indiceStart; i < indiceEnd; i++){
                curTriangle.set(indices[i - 2], indices[i - 1], indices[i]);
                //Check for correct winding
                if (i % 2 != 0) std::swap(curTriangle.y, curTriangle.z);
                _geometryTriangles.push_back(curTriangle);
            }
        }else{
            const vectorImpl<U16>& indices = geometry->getIndicesS();
            for(U32 i = indiceStart; i < indiceEnd; i++){
                curTriangle.set(indices[i - 2], indices[i - 1], indices[i]);
                //Check for correct winding
                if (i % 2 != 0) std::swap(curTriangle.y, curTriangle.z);
                _geometryTriangles.push_back(curTriangle);
            }
        }
    }else if (type == TRIANGLES){
        indiceCount /= 3;
        _geometryTriangles.reserve(indiceCount);
        if(largeIndices){
            const vectorImpl<U32>& indices = geometry->getIndicesL();
            for(U32 i = 0; i < indiceCount; i++){
                    _geometryTriangles.push_back(vec3<U32>(indices[i*3 + 0],indices[i*3 + 1],indices[i*3 + 2]));
            }
        }else{
            const vectorImpl<U16>& indices = geometry->getIndicesS();
                for(U32 i = 0; i < indiceCount; i++){
                    _geometryTriangles.push_back(vec3<U32>(indices[i*3 + 0],indices[i*3 + 1],indices[i*3 + 2]));
            }
        }
    }

    //Check for degenerate triangles
    _geometryTriangles.erase(std::partition(_geometryTriangles.begin(), _geometryTriangles.end(),
                            [](const vec3<U32>& triangle) -> bool {
                            return (triangle.x != triangle.y && triangle.x != triangle.z && triangle.y != triangle.z);
                        }), _geometryTriangles.end());

    DIVIDE_ASSERT(!_geometryTriangles.empty(), "Object3D error: computeTriangleList() failed to generate any triangles!");
    return true;
}

};

Commits for Divide-Framework/trunk/Source Code/Geometry/Shapes/Object3D.cpp

Diff revisions: vs.
Revision Author Commited Message
334 Diff Diff IonutCava picture IonutCava Wed 10 Dec, 2014 17:21:22 +0000

[Ionut]
- Reorganized the Hardware project and renamed to "Platform"
- Reorganized the Server project

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)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 Diff Diff IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

324 Diff Diff IonutCava picture IonutCava Sun 14 Sep, 2014 14:01:39 +0000

[Ionut]
- New memory management system (no custom allocators in this commit)
— Memory tracer to detect memory leaks on shutdown (from: https://www.relisoft.com/book/tech/9new.html)
— SAFE_DELETE_XYZ are now inline template functions not macros
— ASSERTION system now supports logging and message boxes and continue on error configurable for each build type
— Fixed a lot of memory leaks detected by the new system (some still remain)

- Added SRGB support (part 1)
— Albedo textures are loaded as SRGB (make sure resources are SRGB)
— Shaders output gamma corrected colors
— Moved terrain textures back to 2 array system (albedo and normal maps, albedo maps using a SRGB sampler)

- Sky is now loaded via the ResourceCache system
- Added include safeguards in shader atoms

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

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)

307 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 19:45:12 +0000

[Ionut]
- More editor work:
— Per object skeleton rendering, bounding box rendering and shadow map toggling
— Full screen depth preview toggle
— Text entry based transforms

291 k1ngp1n picture k1ngp1n Sun 27 Jul, 2014 19:27:53 +0000

[Ionut]
- nVidia compatibility fixes
- PhysX compatibility fixes