Google

Trouble building ogreterrain.

A discussion on Python-Ogre, the library which allows you to drive OGRE directly from Python

Moderators: OGRE Team, Python-Ogre Moderators

Trouble building ogreterrain.

Postby andrewmac » Sun Feb 20, 2011 5:19 pm

Hey everyone,

I'm using the trunk version ( 1.7.2 ) built on Ubuntu 10.10 and everything is going great except a hit an issue with the building of the module ogreterrain. Heres my log.out:

Code: Select all
02-20 16:01 PythonOgre.BuildModule INFO     Building Source code for ogreterrain
02-20 16:01 PythonOgre.BuildModule DEBUG    Spawning 'python generate_code.py' in '/home/andy/dev/python-ogre/code_generators/ogreterrain'
02-20 16:02 PythonOgre.BuildModule WARNING  Task Failed
02-20 16:02 PythonOgre.BuildModule DEBUG   
02-20 16:02 PythonOgre.BuildModule DEBUG    ../common_utils/__init__.py:7: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5
INFO Creating xml file "/home/andy/dev/python-ogre/code_generators/cache/ogreterrain_1.7.2_cache.xml" from source file "/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain.h" ...
pygccxml.cxx_parser: INFO     Creating xml file "/home/andy/dev/python-ogre/code_generators/cache/ogreterrain_1.7.2_cache.xml" from source file "/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain.h" ...
INFO gccxml cmd: /home/andy/dev/root/usr/bin/gccxml  -I"/home/andy/dev/python-ogre" -I"/home/andy/dev/root/usr/include/boost" -I"/home/andy/dev/root/usr/include/OGRE" -I"/home/andy/dev/ogre/Components/Terrain/include" -I"/home/andy/dev/ogre/Components/Paging/include"  -D"OGRE_NONCLIENT_BUILD" -D"OGRE_GCC_VISIBILITY" -D"__PYTHONOGRE_BUILD_CODE" -D"HAVE_OGRE_BUILDSETTINGS_H" -D"OGRE_TERRAIN_EXPORTS" -D"VERSION_1.7.2"  "/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain.h" -fxml="/home/andy/dev/python-ogre/code_generators/cache/ogreterrain_1.7.2_cache.xml"
pygccxml.cxx_parser: INFO     gccxml cmd: /home/andy/dev/root/usr/bin/gccxml  -I"/home/andy/dev/python-ogre" -I"/home/andy/dev/root/usr/include/boost" -I"/home/andy/dev/root/usr/include/OGRE" -I"/home/andy/dev/ogre/Components/Terrain/include" -I"/home/andy/dev/ogre/Components/Paging/include"  -D"OGRE_NONCLIENT_BUILD" -D"OGRE_GCC_VISIBILITY" -D"__PYTHONOGRE_BUILD_CODE" -D"HAVE_OGRE_BUILDSETTINGS_H" -D"OGRE_TERRAIN_EXPORTS" -D"VERSION_1.7.2"  "/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain.h" -fxml="/home/andy/dev/python-ogre/code_generators/cache/ogreterrain_1.7.2_cache.xml"
Traceback (most recent call last):
  File "generate_code.py", line 443, in <module>
    generate_code()
  File "generate_code.py", line 334, in generate_code
    , cflags=environment.ogreterrain.cflags
  File "/home/andy/dev/root/usr/lib/python2.6/site-packages/pyplusplus/module_builder/boost_python_builder.py", line 95, in __init__
    , indexing_suite_version)
  File "/home/andy/dev/root/usr/lib/python2.6/site-packages/pyplusplus/module_builder/boost_python_builder.py", line 138, in __parse_declarations
    decls = reader.read_files( files, compilation_mode )
  File "/home/andy/dev/root/usr/lib/python2.6/site-packages/pygccxml/parser/project_reader.py", line 217, in read_files
    return self.__parse_file_by_file(files)
  File "/home/andy/dev/root/usr/lib/python2.6/site-packages/pygccxml/parser/project_reader.py", line 254, in __parse_file_by_file
    reader.create_xml_file( header, prj_file.cached_source_file )
  File "/home/andy/dev/root/usr/lib/python2.6/site-packages/pygccxml/parser/source_reader.py", line 179, in create_xml_file
    raise error
pygccxml.parser.source_reader.gccxml_runtime_error_t: Error occured while running GCC-XML: In file included from /home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain.h:14:
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:36: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:36: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:36: error: '_Tmap_traits' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:36: error: wrong number of template arguments (2, should be 3)
/home/andy/dev/root/usr/include/OGRE/OgreIteratorWrapper.h:46: error: provided for 'template<class T, class IteratorType, class ValType> class Ogre::IteratorWrapper'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:36: error: expected unqualified-id before '>' token
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:38: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:38: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:38: error: '_Tmap_traits' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:38: error: wrong number of template arguments (2, should be 3)
/home/andy/dev/root/usr/include/OGRE/OgreIteratorWrapper.h:46: error: provided for 'template<class T, class IteratorType, class ValType> class Ogre::IteratorWrapper'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:38: error: expected unqualified-id before '>' token
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:40: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:40: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:40: error: '_Tmap_traits' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:40: error: template argument 2 is invalid
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:40: error: expected unqualified-id before '>' token
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:42: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:42: error: '_Tree' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:42: error: '_Tmap_traits' is not a member of 'std'
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:42: error: template argument 2 is invalid
/home/andy/dev/python-ogre/code_generators/ogreterrain/python_ogreterrain_aliases.h:42: error: expected unqualified-id before '>' token


Any hints, suggestions or assistance in anyway is greatly appreciated. Possibly worth noting: the only modules I've compiled are ogre, ois, ogrepaging and caelum.

Thanks,
Andrew.
andrewmac
Gnoblar
 
Posts: 7
Kudos: 1
Joined: 20 Feb 2011

Re: Trouble building ogreterrain.

Postby dermont » Sun Feb 20, 2011 5:39 pm

http://sourceforge.net/tracker/download ... id=3032565

You will probably be better applying the above patch manually, it's just a simple if/else for Linux and about 6 months old.
HTH.
dermont
Bugbear
 
Posts: 895
Kudos: 37
Joined: 27 Sep 2005

Re: Trouble building ogreterrain.

Postby andrewmac » Sun Feb 20, 2011 6:38 pm

You sir, are amazing. That completely worked.
andrewmac
Gnoblar
 
Posts: 7
Kudos: 1
Joined: 20 Feb 2011

Re: Trouble building ogreterrain.

Postby andrewmac » Sun Feb 20, 2011 8:35 pm

So I'm translating the ogre tutorial ( http://www.ogre3d.org/tikiwiki/Basic+Tutorial+3 ) on how to use the terrain system to put on the wiki and I'm running into two issues:

Using TerrainGlobalOptions - SOLVED
C++ Code:
Code: Select all
Ogre::TerrainGlobalOptions* mTerrainGlobals;
mTerrainGlobals = OGRE_NEW Ogre::TerrainGlobalOptions();


My Translation:
Code: Select all
globalOptions = ogreterrain.TerrainGlobalOptions()


The error:
Code: Select all
python: /home/andy/dev/ogre/OgreMain/include/OgreSingleton.h:80: Ogre::Singleton<T>::Singleton() [with T = Ogre::TerrainGlobalOptions]: Assertion `!ms_Singleton' failed.


The solution:
Code: Select all
globalOptions = ogreterrain.TerrainGlobalOptions.getSingleton()


Resizing LayerList
C++ Code:
Code: Select all
Ogre::Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings();
...
defaultimp.layerList.resize(3);


My Translation:
Code: Select all
defaultimp = self.terrainGroup.getDefaultImportSettings()
...
defaultimp.layerList.resize(3)


The error:
Code: Select all
AttributeError: 'stdVectorTerrainLayerInstance' object has no attribute 'resize'


Most of the other code translated okay with a few minor changes but these two have me stumped.
edit: solved the first one.
andrewmac
Gnoblar
 
Posts: 7
Kudos: 1
Joined: 20 Feb 2011

Re: Trouble building ogreterrain.

Postby dermont » Mon Feb 21, 2011 5:56 am

You can do a print dir(defaultimp.layerList), the stdVector*s are implemented in a more pythonic way(?). Your code would be something like (untested).

Code: Select all
l = ogreterrain.LayerInstance()
l.worldSize = 100
l.textureNames.append("dirt_grayrocky_diffusespecular.dds")
l.textureNames.append("dirt_grayrocky_normalheight.dds")
defaultimp.layerList.append(l)


Do upload your translation of the tutorial once you are finished.

Edit:
Also there is python-ogre/demos/samples/Sample_Terrain.py. I didn't even know this existed.
http://python-ogre.svn.sourceforge.net/ ... s/samples/

Code: Select all
        # Configure default import settings for if we use imported image
        defaultimp = self.mTerrainGroup.getDefaultImportSettings()
        defaultimp.terrainSize = TERRAIN_SIZE
        defaultimp.worldSize = TERRAIN_WORLD_SIZE
        defaultimp.inputScale = 600
        defaultimp.minBatchSize = 33
        defaultimp.maxBatchSize = 65

        # textures
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )

        defaultimp.layerList[0].worldSize = 100
        defaultimp.layerList[0].textureNames.append("dirt_grayrocky_diffusespecular.dds")
        defaultimp.layerList[0].textureNames.append("dirt_grayrocky_normalheight.dds")
        defaultimp.layerList[1].worldSize = 30
        defaultimp.layerList[1].textureNames.append("grass_green-01_diffusespecular.dds")
        defaultimp.layerList[1].textureNames.append("grass_green-01_normalheight.dds")

        defaultimp.layerList[2].worldSize = 200
        defaultimp.layerList[2].textureNames.append("growth_weirdfungus-03_diffusespecular.dds")
        defaultimp.layerList[2].textureNames.append("growth_weirdfungus-03_normalheight.dds")
dermont
Bugbear
 
Posts: 895
Kudos: 37
Joined: 27 Sep 2005

Re: Trouble building ogreterrain.

Postby dermont » Mon Feb 21, 2011 10:39 am

I tried the Sample_Terrain.py and couldn't quite get the defaultimp.layerList[0] property assignments to work. The following works for me on Linux:

Code: Select all
        # textures
        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 100
        layer.textureNames.append("dirt_grayrocky_diffusespecular.dds")
        layer.textureNames.append("dirt_grayrocky_normalheight.dds")
        defaultimp.layerList.append(layer)

        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 30
        layer.textureNames.append("grass_green-01_diffusespecular.dds")
        layer.textureNames.append("grass_green-01_normalheight.dds")
        defaultimp.layerList.append(layer)

        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 200
        layer.textureNames.append("growth_weirdfungus-03_diffusespecular.dds")
        layer.textureNames.append("growth_weirdfungus-03_normalheight.dds")
        defaultimp.layerList.append(layer)


Which means as a workaround, to change an item you would have to:
Code: Select all
        layer = defaultimp.layerList[0]
        do something to layer
        defaultimp.layerList[0] = layer


I'll take another look at the patch, if Andy already hasn't made the updates; though there are other examples of the above problem in the ogre module.

Also you should delete your media/testTerrain_00000000.dat file (if it exiasts) after you update your source; otherwise you will be left pulling your hair out trying to figure why your changes are not working.

For this message the author dermont has received kudos
dermont
Bugbear
 
Posts: 895
Kudos: 37
Joined: 27 Sep 2005

Re: Trouble building ogreterrain.

Postby andrewmac » Mon Feb 21, 2011 8:09 pm

Thanks, that worked. Both issues solved now. I've managed to convert a good portion of that tutorial now and it compiles and runs fine. However I'm getting an odd output on the landscape. It seems to show stripes for some reason. The laptop im developing on right now has an onboard intel graphics card, so I'm wondering if perhaps this is just it's way of saying "go setup a proper dev pc"

I've included the code and a screenshot. If anyone has any ideas what could cause this kind of strange output please let me know. Or if you have the time to run the code it would be good to know if it runs properly on a decent graphics card.

Screenshot:
http://i.imgur.com/gzCQV.png

Demo_Terrain3.py: (save it to python-ogre/demos/ogre/)
Code: Select all
# This code is in the Public Domain
# -----------------------------------------------------------------------------
# This source file is part of Python-Ogre
# For the latest info, see http://python-ogre.org/
#
# It is likely based on original code from OGRE and/or PyOgre
# For the latest info, see http://www.ogre3d.org/
#
# You may use this sample code for anything you like, it is not covered by the
# LGPL.
# -----------------------------------------------------------------------------
import sys
sys.path.insert(0,'..')
import PythonOgreConfig

import ogre.renderer.OGRE as ogre
import ogre.renderer.ogreterrain as ogreterrain
import SampleFramework as sf
import ctypes as ctypes

def Clamp ( val, low, high ):
    if val < low: return low
    if val > high: return high
    return val

class TerrainApplication(sf.Application):

    def _chooseSceneManager(self):
        # self.sceneManager = self.root.createSceneManager("TerrainSceneManager")
        self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC)

    def _createScene(self):
        sceneManager = self.sceneManager
       
        # setting up the camera.
        camera = self.camera
        camera.setPosition(1683, 50, 2116)
        camera.lookAt(1963, 50, 1660)
        camera.setNearClipDistance(0.1)
        camera.setFarClipDistance(50000)
   
        if (self.root.getRenderSystem().getCapabilities().hasCapability(ogre.RSC_INFINITE_FAR_PLANE)):
            camera.setFarClipDistance(0)
 
        self.materialManager = ogre.MaterialManager.getSingleton()
        self.materialManager.setDefaultTextureFiltering(ogre.TFO_ANISOTROPIC)
        self.materialManager.setDefaultAnisotropy(7)
 
        lightdir = ogre.Vector3(0.55, -0.3, 0.75)
        lightdir.normalise()
       
        light = self.sceneManager.createLight("tstLight")
        light.setType(ogre.Light.LT_DIRECTIONAL)
        light.setDirection(lightdir)
        light.setDiffuseColour(ogre.ColourValue(1.0, 1.0, 1.0))
        light.setSpecularColour(ogre.ColourValue(0.4, 0.4, 0.4))
 
        sceneManager.AmbientLight = 0.2, 0.2, 0.2
       
        self.terrainGlobals = ogreterrain.TerrainGlobalOptions()

        self.terrainGroup = ogreterrain.TerrainGroup(self.sceneManager, ogreterrain.Terrain.ALIGN_X_Z, 513, 12000)
        self.terrainGroup.setFilenameConvention("BasicTutorial3Terrain", "dat")
        self.terrainGroup.setOrigin(ogre.Vector3(0, 0, 0))
       
        self.configureTerrainDefaults(light)
       
        self.defineTerrain(0, 0)
       
        self.terrainGroup.loadAllTerrains(True)

        if (self.terrainsImported):
            it = self.terrainGroup.getTerrainIterator()
            for t in self.terrainGroup.getTerrainIterator():
                self.initBlendMaps(t.instance)

        self.terrainGroup.freeTemporaryResources()
       
    def configureTerrainDefaults(self, light):
        self.terrainGlobals.setMaxPixelError(8)
        self.terrainGlobals.setCompositeMapDistance(3000)
        self.terrainGlobals.setLightMapDirection(light.getDerivedDirection())
        self.terrainGlobals.setCompositeMapAmbient(self.sceneManager.getAmbientLight())
        self.terrainGlobals.setCompositeMapDiffuse(light.getDiffuseColour())
   
   #// Configure default import settings for if we use imported image
        defaultimp = self.terrainGroup.getDefaultImportSettings()
        defaultimp.terrainSize = 513
        defaultimp.worldSize = 12000
        defaultimp.inputScale = 600
        defaultimp.minBatchSize = 33
        defaultimp.maxBatchSize = 65

        # textures
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )
        defaultimp.layerList.append(ogreterrain.Terrain.LayerInstance() )

        defaultimp.layerList[0].worldSize = 100
        defaultimp.layerList[0].textureNames.append("dirt_grayrocky_diffusespecular.dds")
        defaultimp.layerList[0].textureNames.append("dirt_grayrocky_normalheight.dds")
        defaultimp.layerList[1].worldSize = 30
        defaultimp.layerList[1].textureNames.append("grass_green-01_diffusespecular.dds")
        defaultimp.layerList[1].textureNames.append("grass_green-01_normalheight.dds")
        defaultimp.layerList[2].worldSize = 200
        defaultimp.layerList[2].textureNames.append("growth_weirdfungus-03_diffusespecular.dds")
        defaultimp.layerList[2].textureNames.append("growth_weirdfungus-03_normalheight.dds")
       
    def defineTerrain(self, x, y):
       filename = self.terrainGroup.generateFilename(x, y)
       RGM = ogre.ResourceGroupManager.getSingleton()
       if ( RGM.resourceExists(self.terrainGroup.getResourceGroup(), filename) ):
          self.terrainGroup.defineTerrain(x, y)
       else :
          img = self.getTerrainImage((x % 2) != 0, (y%2) != 0)
                self.terrainGroup.defineTerrain(x, y, img)
                self.terrainsImported = True
               
    def getTerrainImage(self, flipX, flipY):
       img = ogre.Image()
        img.load("terrain.png", ogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME)
        if flipX:
            img.flipAroundY()
        if flipY:
            img.flipAroundX()
        return img
           
    def initBlendMaps(self, terrain):
        blendMap0 = terrain.getLayerBlendMap(1)
        blendMap1 = terrain.getLayerBlendMap(2)
        minHeight0 = 70
        fadeDist0 = 40.0
        minHeight1 = 70
        fadeDist1 = 15.0

        pBlend1 = blendMap1.getBlendPointer()    # returns the address of the buffer
        size = terrain.getLayerBlendMapSize() * terrain.getLayerBlendMapSize()
        blend_data=(ctypes.c_float * size).from_address(pBlend1)
        index = 0
        for y in range(terrain.getLayerBlendMapSize()):
            for x in range( terrain.getLayerBlendMapSize() ):
                # using ctypes
                tx=ctypes.c_float(0.0)
                ty=ctypes.c_float(0.0)
               
                blendMap0.convertImageToTerrainSpace(x, y, ctypes.addressof(tx), ctypes.addressof(ty))
                height = terrain.getHeightAtTerrainPosition(tx.value, ty.value)
                val = (height - minHeight0) / fadeDist0
                val = Clamp(val, 0, 1)

                val = (height - minHeight1) / fadeDist1
                val = Clamp(val, 0, 1)
                blend_data [index] = val
                index += 1
               
        blendMap0.dirty()
        blendMap1.dirty()
        blendMap0.update()
        blendMap1.update()       

if __name__ == '__main__':
    try:
        application = TerrainApplication()
        application.go()
    except ogre.OgreException, e:
        print e
andrewmac
Gnoblar
 
Posts: 7
Kudos: 1
Joined: 20 Feb 2011

Re: Trouble building ogreterrain.

Postby dermont » Tue Feb 22, 2011 4:51 am

It may be your graphics card, try:

1) Printing out the name of the defaultimp.layerList[2].textureNames, you will probably find they are empty, if so you can update your code as I suggested previously.

Code: Select all
        # textures
        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 100
        layer.textureNames.append("dirt_grayrocky_diffusespecular.dds")
        layer.textureNames.append("dirt_grayrocky_normalheight.dds")
        defaultimp.layerList.append(layer)

        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 30
        layer.textureNames.append("grass_green-01_diffusespecular.dds")
        layer.textureNames.append("grass_green-01_normalheight.dds")
        defaultimp.layerList.append(layer)

        layer = ogreterrain.Terrain.LayerInstance()
        layer.worldSize = 200
        layer.textureNames.append("growth_weirdfungus-03_diffusespecular.dds")
        layer.textureNames.append("growth_weirdfungus-03_normalheight.dds")
        defaultimp.layerList.append(layer)



2) Checking Ogre.log to see if the resources are being loaded and are defined in your resources.cfg (see the Samples dir) or even easier run from Samples directory.

Your demo has a few indentation problems and you probably need to do some clean-up on shutdown, other than that it runs OK here.
dermont
Bugbear
 
Posts: 895
Kudos: 37
Joined: 27 Sep 2005

Re: Trouble building ogreterrain.

Postby andrewmac » Tue Feb 22, 2011 8:00 pm

Wow, I really don't know how I missed that. I guess I figured once the code compiled with my original version that it would work, but after replacing it with your code it produced an error which made me realize it couldn't find the textures. All issues are resolved and it's looking good.

Here's a screenshot: http://i.imgur.com/HtfSW.jpg

These two lines need to be added to resources.cfg inside python-ogre/demos/ogre/:
Code: Select all
FileSystem=../media/materials/textures/nvidia
FileSystem=../media/PCZAppMedia


Here's a fixed version of Demo_Terrain3.py with indents corrected:
Code: Select all
import sys
sys.path.insert(0,'..')
import PythonOgreConfig

import ogre.renderer.OGRE as ogre
import ogre.renderer.ogreterrain as ogreterrain
import SampleFramework as sf
import ctypes as ctypes

def Clamp ( val, low, high ):
       if val < low: return low
       if val > high: return high
       return val

class TerrainApplication(sf.Application):

   def _chooseSceneManager(self):
      # self.sceneManager = self.root.createSceneManager("TerrainSceneManager")
      self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC)

   def _createScene(self):
      sceneManager = self.sceneManager
       
      # setting up the camera.
      camera = self.camera
      camera.setPosition(1683, 100, 2116)
      camera.lookAt(1963, 50, 1660)
      camera.setNearClipDistance(0.1)
      camera.setFarClipDistance(50000)
   
      if (self.root.getRenderSystem().getCapabilities().hasCapability(ogre.RSC_INFINITE_FAR_PLANE)):
         camera.setFarClipDistance(0)
 
      self.materialManager = ogre.MaterialManager.getSingleton()
      self.materialManager.setDefaultTextureFiltering(ogre.TFO_ANISOTROPIC)
      self.materialManager.setDefaultAnisotropy(7)
 
      lightdir = ogre.Vector3(0.55, -0.3, 0.75)
      lightdir.normalise()
       
      light = self.sceneManager.createLight("tstLight")
      light.setType(ogre.Light.LT_DIRECTIONAL)
      light.setDirection(lightdir)
      light.setDiffuseColour(ogre.ColourValue(1.0, 1.0, 1.0))
      light.setSpecularColour(ogre.ColourValue(0.4, 0.4, 0.4))
 
      sceneManager.AmbientLight = 0.2, 0.2, 0.2
           
      self.terrainGlobals = ogreterrain.TerrainGlobalOptions()
      self.terrainGroup = ogreterrain.TerrainGroup(self.sceneManager, ogreterrain.Terrain.ALIGN_X_Z, 513, 12000)
      self.terrainGroup.setFilenameConvention("BasicTutorial3Terrain", "dat")
      self.terrainGroup.setOrigin(ogre.Vector3(0, 0, 0))
       
      self.configureTerrainDefaults(light)
       
      self.defineTerrain(0, 0)
       
      self.terrainGroup.loadAllTerrains(True)

      if (self.terrainsImported):
         it = self.terrainGroup.getTerrainIterator()
         for t in self.terrainGroup.getTerrainIterator():
            self.initBlendMaps(t.instance)

      self.terrainGroup.freeTemporaryResources()
       
   def configureTerrainDefaults(self, light):
      self.terrainGlobals.setMaxPixelError(8)
      self.terrainGlobals.setCompositeMapDistance(3000)
      self.terrainGlobals.setLightMapDirection(light.getDerivedDirection())
      self.terrainGlobals.setCompositeMapAmbient(self.sceneManager.getAmbientLight())
      self.terrainGlobals.setCompositeMapDiffuse(light.getDiffuseColour())
   
      # Configure default import settings for if we use imported image
      defaultimp = self.terrainGroup.getDefaultImportSettings()
      defaultimp.terrainSize = 513
      defaultimp.worldSize = 12000
      defaultimp.inputScale = 600
      defaultimp.minBatchSize = 33
      defaultimp.maxBatchSize = 65

      # textures
      layer0 = ogreterrain.Terrain.LayerInstance()
      layer0.worldSize = 100
      layer0.textureNames.append("dirt_grayrocky_diffusespecular.dds")
      layer0.textureNames.append("dirt_grayrocky_normalheight.dds")
      defaultimp.layerList.append(layer0)

      layer1 = ogreterrain.Terrain.LayerInstance()
      layer1.worldSize = 30
      layer1.textureNames.append("grass_green-01_diffusespecular.dds")
      layer1.textureNames.append("grass_green-01_normalheight.dds")
      defaultimp.layerList.append(layer1)

      layer2 = ogreterrain.Terrain.LayerInstance()
      layer2.worldSize = 200
      layer2.textureNames.append("growth_weirdfungus-03_diffusespecular.dds")
      layer2.textureNames.append("growth_weirdfungus-03_normalheight.dds")
      defaultimp.layerList.append(layer2)
       
   def defineTerrain(self, x, y):
      filename = self.terrainGroup.generateFilename(x, y)
      RGM = ogre.ResourceGroupManager.getSingleton()
      if ( RGM.resourceExists(self.terrainGroup.getResourceGroup(), filename) ):
         self.terrainGroup.defineTerrain(x, y)
      else :
         img = self.getTerrainImage((x % 2) != 0, (y%2) != 0)
         self.terrainGroup.defineTerrain(x, y, img)
         self.terrainsImported = True
               
   def getTerrainImage(self, flipX, flipY):
      img = ogre.Image()
      img.load("terrain.png", ogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME)
      if flipX:
         img.flipAroundY()
      if flipY:
         img.flipAroundX()
      return img
           
   def initBlendMaps(self, terrain):
      blendMap0 = terrain.getLayerBlendMap(1)
      blendMap1 = terrain.getLayerBlendMap(2)
      minHeight0 = 70
      fadeDist0 = 40.0
      minHeight1 = 70
      fadeDist1 = 15.0

      pBlend1 = blendMap1.getBlendPointer()    # returns the address of the buffer
      size = terrain.getLayerBlendMapSize() * terrain.getLayerBlendMapSize()
      blend_data=(ctypes.c_float * size).from_address(pBlend1)
      index = 0
      for y in range(terrain.getLayerBlendMapSize()):
         for x in range( terrain.getLayerBlendMapSize() ):
            # using ctypes
            tx = ctypes.c_float(0.0)
            ty = ctypes.c_float(0.0)
               
            blendMap0.convertImageToTerrainSpace(x, y, ctypes.addressof(tx), ctypes.addressof(ty))
            height = terrain.getHeightAtTerrainPosition(tx.value, ty.value)
            val = (height - minHeight0) / fadeDist0
            val = Clamp(val, 0, 1)

            val = (height - minHeight1) / fadeDist1
            val = Clamp(val, 0, 1)
            blend_data [index] = val
            index += 1
               
      blendMap0.dirty()
      blendMap1.dirty()
      blendMap0.update()
      blendMap1.update()       

if __name__ == '__main__':
   try:
      application = TerrainApplication()
      application.go()
   except ogre.OgreException, e:
      print e


Now that it's working I'll throw together a step by step to put on the wiki. Thanks again for all the assistance dermont.

For this message the author andrewmac has received kudos
andrewmac
Gnoblar
 
Posts: 7
Kudos: 1
Joined: 20 Feb 2011

Re: Trouble building ogreterrain.

Postby Mohican » Sun May 01, 2011 1:27 pm

Thank you very much for this Code Snippet.
It helped me significantly with getting the new Terrain system working!
:D
User avatar
Mohican
Halfling
 
Posts: 66
Kudos: 6
Joined: 23 Apr 2011
Location: Japan


Return to Python-Ogre

Who is online

Users browsing this forum: No registered users and 1 guest

cron