OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Ogre Compatible HeightMap
View page
Source of version: 3
(current)
I've noticed a few posts around about people using Ogre on their headless server platforms with a null renderer because they need heightmap support that is compatible with ogre. I was guilty of this as well, but I finally did some research and created a heightmap system that works the same as Ogre's. Well, not exactly the same, but it supports the feature that I needed, namely __getHeightAt(x,z)__. This is not necessarily a drop in replacement, but it should get anyone interested in such a thing a real head start. __Note:__ The current Ogre version also has the method ''Ogre::TerrainSceneManager::[http://www.ogre3d.org/docs/api/html/classOgre_1_1TerrainSceneManager.html#Ogre_1_1TerrainSceneManagera6|getHeightAt(x,y)]'' According to my tests the following code works "identically" to the Ogre 1.4.5. {CODE(wrap="1", colors="c++")}#include <iostream> #include <vector> #include <cmath> #include <fstream> #include <iomanip> class HeightMap { public: HeightMap(int width, float widthScale, float heightScale, const std::string& rawFilename, bool is16bit = true) : m_Width(width), m_WidthScale(widthScale), m_HeightScale(heightScale) { std::ifstream ifs; // open the file (should add some error checking here) ifs.open(rawFilename.c_str(), std::ios::binary); // create the new vertex array m_Vertices = new float[m_Width * m_Width]; unsigned short tmp = 0; float invScale = 1.0f / 65535.0f; // loop through and populate the vertices for(int y = 0; y < m_Width; ++y) { for(int x = 0; x < m_Width; ++x) { // get first byte tmp = ifs.get(); // if its 16 bit, get the second if(is16bit) tmp += ifs.get() << 8; // add the current value to the correct position m_Vertices[y*m_Width + x] = tmp * invScale; } } ifs.close(); }//end HeightMap() ~HeightMap() { delete[] m_Vertices; }//end ~HeightMap /** * Print the height map to standard output, useful for debugging */ void printHeightMap() { // now print out the heightmap for(int y = 0; y < m_Width; ++y) { for(int x = 0; x < m_Width; ++x) { std::cout << std::setw(2) << static_cast<int>(m_Vertices[y*m_Width + x] * heightScale)<< ","; } std::cout << std::endl; } }//end printHeightMap /** * This function is merely a modified form of the function * of the same name in TerrainRenderable. */ float getHeightAt(float x, float z) { float start_x = 0; float start_y = indexHeight(0,0); float start_z = 0; float end_x = m_Width*m_WidthScale/4; float end_y = indexHeight(m_Width,m_Width); float end_z = m_Width*m_WidthScale/4; float x_pct = ( x - start_x ) / ( end_x - start_x ); float z_pct = ( z - start_z ) / ( end_z - start_z ); float x_pt = x_pct * ( float ) ( m_Width); float z_pt = z_pct * ( float ) ( m_Width); int x_index = ( int ) x_pt; int z_index = ( int ) z_pt; // If we got to the far right / bottom edge, move one back if (x_index == m_Width) { --x_index; x_pct = 1.0f; } else { // get remainder x_pct = x_pt - x_index; } if (z_index == m_Width) { --z_index; z_pct = 1.0f; } else { z_pct = z_pt - z_index; } //bilinear interpolate to find the height. float t1 = indexHeight( x_index, z_index); float t2 = indexHeight( x_index + 1, z_index); float b1 = indexHeight( x_index, z_index + 1); float b2 = indexHeight( x_index + 1, z_index + 1); float midpoint = (b1 + t2) / 2.0; if (x_pct + z_pct <= 1) { b2 = midpoint + (midpoint - t1); } else { t1 = midpoint + (midpoint - b2); } float t = ( t1 * ( 1 - x_pct ) ) + ( t2 * ( x_pct ) ); float b = ( b1 * ( 1 - x_pct ) ) + ( b2 * ( x_pct ) ); float h = ( t * ( 1 - z_pct ) ) + ( b * ( z_pct ) ); return h * m_HeightScale; }//end getHeightAt private: /** * Simple function for pulling out a value from the array */ inline float indexHeight(int x, int y) { return m_Vertices[m_Width * y + x]; }//end indexHeight private: float* m_Vertices; int m_Width; float m_WidthScale; float m_HeightScale; };{CODE}
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
87 online users