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: TerrainMeshDecal
View page
Source of version: 3
(current)
!!Terrain Selection using a Mesh Decal When reading ((Intermediate Tutorial 5)), I wanted to use a decal to indicate where the user clicks on the terrain. With help from the forums, here's some simple code to achieve that goal with a mesh decal. The idea is simple - create a small square mesh that sits just above the terrain where the user clicked, and texture it. First, creating a manual object for the mesh. "Crosshairs" is the name of the material I'm using. Since this attaches the mesh to the scene, it should only be called once. {CODE(wrap="1", colors="c++")} // Initial creation of the mesh decal void createMeshDecal() { mMeshDecal = new Ogre::ManualObject("MeshDecal"); mSceneMgr->getRootSceneNode()->attachObject(mMeshDecal); int x_size = 4; // number of polygons int z_size = 4; mMeshDecal->begin("Crosshairs", Ogre::RenderOperation::OT_TRIANGLE_LIST); for (int i=0; i<=x_size; i++) { for (int j=0; j<=z_size; j++) { mMeshDecal->position(Ogre::Vector3(i, 0, j)); mMeshDecal->textureCoord((float)i / (float)x_size, (float)j / (float)z_size); } } for (int i=0; i<x_size; i++) { for (int j=0; j<z_size; j++) { mMeshDecal->quad( i * (x_size+1) + j, i * (x_size+1) + j + 1, (i + 1) * (x_size+1) + j + 1, (i + 1) * (x_size+1) + j); } } mMeshDecal->end(); } {CODE} Next, the code I call when the user clicks on the terrain. "rad" is the "radius" of the decal - I had vague thoughts of making the decal larger if it's further in the distance. {CODE(wrap="1", colors="c++")} // Update mesh decal when terrain selection happens void setMeshDecal(Ogre::Real x, Ogre::Real z, Ogre::Real rad) { Ogre::Real x1 = x - rad; Ogre::Real z1 = z - rad; int x_size = 4; // number of polygons int z_size = 4; Ogre::Real x_step = rad * 2/ x_size; Ogre::Real z_step = rad * 2/ z_size; mMeshDecal->beginUpdate(0); // redefine vertices for (int i=0; i<=x_size; i++) { for (int j=0; j<=z_size; j++) { mMeshDecal->position(Ogre::Vector3(x1, getTerrainHeight(x1, z1) + 1, z1)); mMeshDecal->textureCoord((float)i / (float)x_size, (float)j / (float)z_size); z1 += z_step; } x1 += x_step; z1 = z - rad; } // redefine quads for (int i=0; i<x_size; i++) { for (int j=0; j<z_size; j++) { mMeshDecal->quad( i * (x_size+1) + j, i * (x_size+1) + j + 1, (i + 1) * (x_size+1) + j + 1, (i + 1) * (x_size+1) + j); } } mMeshDecal->end(); } {CODE} Finally, figuring out the intersection with the ground: {CODE(wrap="1", colors="c++")} Ogre::Real getTerrainHeight(Ogre::Real x, Ogre::Real z) { Ogre::Ray* verticalRay = new Ogre::Ray( Ogre::Vector3(x, 5000, z), Ogre::Vector3::NEGATIVE_UNIT_Y ); mRaySceneQuery->setRay( *verticalRay ); // Execute query Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute(); Ogre::RaySceneQueryResult::iterator itr = result.begin( ); if ( itr != result.end() && itr->worldFragment ) { Ogre::Vector3 intersection = itr->worldFragment->singleIntersection; return intersection.y; } else { return 0; } } {CODE} And last, the material I'm using. Crosshairs.png is the image file from ((Intermediate Tutorial 5)). {CODE(wrap="1", colors="c++")} material Crosshairs { technique { pass { scene_blend alpha_blend depth_write off texture_unit { texture Crosshairs.png scale 1 1 } } } } {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
161 online users