root/plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZODE/src/HeightfieldResource.cpp @ 3463

Revision 3463, 4.9 KB (checked in by trichards, 6 months ago)

Adding HeightfieldResource? implementation and code for loading heightmap terrains to ZODE.

Line 
1//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2// Zen Game Engine Framework
3//
4// Copyright (C) 2001 - 2010 Tony Richards
5// Copyright (C) 2008 - 2010 Matthew Alan Gray
6//
7//  This software is provided 'as-is', without any express or implied
8//  warranty.  In no event will the authors be held liable for any damages
9//  arising from the use of this software.
10//
11//  Permission is granted to anyone to use this software for any purpose,
12//  including commercial applications, and to alter it and redistribute it
13//  freely, subject to the following restrictions:
14//
15//  1. The origin of this software must not be misrepresented; you must not
16//     claim that you wrote the original software. If you use this software
17//     in a product, an acknowledgment in the product documentation would be
18//     appreciated but is not required.
19//  2. Altered source versions must be plainly marked as such, and must not be
20//     misrepresented as being the original software.
21//  3. This notice may not be removed or altered from any source distribution.
22//
23//  Tony Richards trichards@indiezen.com
24//  Matthew Alan Gray mgray@indiezen.org
25//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
26#include "HeightfieldResource.hpp"
27
28#include <Zen/Core/Math/Math.hpp>
29
30#include <boost/lexical_cast.hpp>
31
32#include <stdio.h>
33#include <stdlib.h>
34
35//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
36namespace Zen {
37namespace ZODE {
38//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
39HeightfieldResource::HeightfieldResource(config_type& _config)
40:   m_config(_config)
41,   m_spaceId(NULL)
42,   m_heightfieldDataId(NULL)
43,   m_heightfieldGeometryId(NULL)
44{
45}
46
47//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
48HeightfieldResource::~HeightfieldResource()
49{
50    if( m_heightfieldGeometryId != NULL )
51    {
52        dGeomDestroy(m_heightfieldGeometryId);
53    }
54
55    if( m_heightfieldDataId != NULL )
56    {
57        dGeomHeightfieldDataDestroy(m_heightfieldDataId);
58    }
59}
60
61//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
62void
63HeightfieldResource::initialize()
64{
65    const std::string fileName = m_config["fileName"];
66    const Math::Real width = boost::lexical_cast<Math::Real,std::string>(m_config["width"]);
67    const Math::Real depth = boost::lexical_cast<Math::Real,std::string>(m_config["depth"]);
68    const int widthSamples = boost::lexical_cast<int,std::string>(m_config["widthSamples"]);
69    const int depthSamples = boost::lexical_cast<int,std::string>(m_config["depthSamples"]);
70    const Math::Real scale = boost::lexical_cast<Math::Real,std::string>(m_config["scale"]);
71    const Math::Real thickness = boost::lexical_cast<Math::Real,std::string>(m_config["thickness"]);
72    const bool wrap = (m_config["wrap"] == "true");
73
74    /// dHeightfieldDataID is actually a pointer to a struct.
75    m_heightfieldDataId = dGeomHeightfieldDataCreate();
76
77    /// TODO Need to open file and read height data into a buffer.
78    /// (Assuming unsigned short for now).
79    /// While we're reading the height data, we can get the
80    /// height bounds of the heightmap as well.
81    /// (These may need to be scaled versions of the raw min/max
82    /// based on the scale config param).
83    const short *pBuffer;
84    Math::Real minHeight;
85    Math::Real maxHeight;
86
87    /// Build heightfield data from buffer.
88    dGeomHeightfieldDataBuildShort(
89        m_heightfieldDataId,
90        pBuffer,
91        true,       /// This copies the height data local to the dHeightfieldData object.
92                    /// A 0 here would make the dHeightfieldData object access the data
93                    /// by reference, requiring that the data persist throughout the lifetime
94                    /// of the dHeightfieldData object.
95        width,
96        depth,
97        widthSamples,
98        depthSamples,
99        scale,
100        0.0f,       /// Assuming a zero offset for now.
101        thickness,
102        wrap
103    );
104
105    /// Setting the heightfield height bounds to those
106    /// found when reading the heightmap in from the file.
107    dGeomHeightfieldDataSetBounds(
108        m_heightfieldDataId,
109        minHeight,
110        maxHeight
111    );
112
113    /// Create the heightfield geometry object.
114    m_heightfieldGeometryId = dCreateHeightfield(
115        m_spaceId,          /// TODO Get a spaceId for the current zone.
116        m_heightfieldDataId,
117        true        /// Setting the geometry as placeable so that it
118                    /// can be transformed with dGeomSetPosition()
119                    /// and dGeomSetRotation().
120    );
121}
122
123//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
124}   // namespace ZODE
125}   // namespace Zen
126//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Note: See TracBrowser for help on using the browser.