How to load material and texture manually?

mrkissinger

30-08-2007 23:01:14

I tried to load mesh/material/texture/skeleton manually by these codes:


mm = ogre.MaterialManager.getSingleton()
rm = ogre.ResourceGroupManager.getSingleton()
tm = ogre.TextureManager.getSingleton()
meshm=ogre.MeshManager.getSingleton()

tm.setDefaultNumMipmaps (5)
mm.setDefaultTextureFiltering(ogre.TFO_ANISOTROPIC)
mm.setDefaultAnisotropy(2)
rm.initialiseAllResourceGroups()


mesh=meshm.load('data/Mesh/robot.mesh',ogre.ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME)
logger.debug("Mesh ===> %s " % mesh.getName() )
mesh.setSkeletonName("data/Skeleton/robot.skeleton")
ds = rm.openResource("data/Material/robot.material", "General", True)
mm.parseScript(ds, "General")
# TODO: Material name must be changed
material=mm.getByName("Robot")
logger.debug("Material ===> %s, %s" % (material.getName(), material) )

texture=tm.load("data/Map/r2skin.jpg", "General")
logger.debug("Texture ===> %s" % texture)

mpass=material.getTechnique(1).getPass(0)
mpass.getTextureUnitState(texture.getName())
entity = sceneManager.createEntity('robot', mesh.getName())
entity.setMaterialName(material.getName())
#entity = sceneManager.createEntity('robot', 'data/Mesh/robot.mesh')
sceneManager.getRootSceneNode().attachObject(entity)
state=entity.getAnimationState('Walk')


The mesh and skeleton and even the animation were loaded.
The material affected as well.
But the texture is not loaded.

The material file is:

material Robot
{
// Hardware skinning techniique
technique
{
pass
{
vertex_program_ref Ogre/HardwareSkinningOneWeight
{
param_named_auto worldMatrix3x4Array world_matrix_array_3x4
param_named_auto viewProjectionMatrix viewproj_matrix
param_named_auto lightPos[0] light_position 0
param_named_auto lightPos[1] light_position 1
param_named_auto lightDiffuseColour[0] light_diffuse_colour 0
param_named_auto lightDiffuseColour[1] light_diffuse_colour 1
param_named_auto ambient ambient_light_colour

}
// alternate shadow caster program
shadow_caster_vertex_program_ref Ogre/HardwareSkinningOneWeightShadowCaster
{
param_named_auto worldMatrix3x4Array world_matrix_array_3x4
param_named_auto viewProjectionMatrix viewproj_matrix
param_named_auto ambient ambient_light_colour

}

texture_unit
{
// texture r2skin.jpg
}
}
}

// Software blending technique
technique
{
pass
{

texture_unit
{
// texture r2skin.jpg
}
}
}
}



I do NOT want to use resources.cfg, can anyone help?

mrkissinger

01-09-2007 20:18:27

Had anyone try to change texture_unit at runtime?

mrkissinger

01-09-2007 21:38:17

I got this result.


I really have no idea how to resolve.

HELP!!!

dermont

05-09-2007 10:18:12

http://www.ogre3d.org/docs/api/html/cla ... State.html
To change texture at runtime TextureUnitState::setTextureName.

mrkissinger

06-09-2007 08:52:12

My material file does NOT contain texture_unit, so I have to create the object at runtime.

I found the error was caused by sharing memory between skeleton and material when I created them at runtime.

I discussed this problem at there:
http://www.ogre3d.org/phpBB2addons/view ... c2bfdfa429

But seems no one reply.

dermont

09-09-2007 10:20:26

Well the material file posted above appears to have a texture_unit. I don't know what you mean by the error being caused by sharing memory between skeleton and material. I would check your Ogre.log for errors etc (i.e are the cg vertex programs being loaded etc)

It appears to work OK for me on Linux( Ogre1.4.4 and Ogre1.5 ) but I'm probably not understanding what your trying to do.

data

data
Robot.Material
robot_2.skeleton (copy of robot.skeleton)
robot_2.mesh (copy of robot.mesh)
XXXr2skin.jpg (copy of r2skin.jpg)
data/materials
Examples.program (as per media/materials/scripts)
data/programs
(as per media/materials/programs)
data/packs
OgreCore.Zip

resources.cfg

[Bootstrap]
Zip=./data/packs/OgreCore.zip

# Resource locations to be added to the default path
[General]
FileSystem=.
FileSystem=./data/materials
FileSystem=./data/programs


Material

material Robot
{

// Hardware skinning techniique
technique
{
pass
{
vertex_program_ref Ogre/HardwareSkinningOneWeight
{
param_named_auto worldMatrix3x4Array world_matrix_array_3x4
param_named_auto viewProjectionMatrix viewproj_matrix
param_named_auto lightPos[0] light_position 0
param_named_auto lightPos[1] light_position 1
param_named_auto lightDiffuseColour[0] light_diffuse_colour 0
param_named_auto lightDiffuseColour[1] light_diffuse_colour 1
param_named_auto ambient ambient_light_colour

}
// alternate shadow caster program
shadow_caster_vertex_program_ref Ogre/HardwareSkinningOneWeightShadowCaster
{
param_named_auto worldMatrix3x4Array world_matrix_array_3x4
param_named_auto viewProjectionMatrix viewproj_matrix
param_named_auto ambient ambient_light_colour

}

//texture_unit
//{
//texture r2skin.jpg
//}
}
}


// Software blending technique
technique
{
pass
{

//texture_unit
//{
//texture r2skin.jpg
//}
}
}

}


code

import ogre.renderer.OGRE as ogre
import SampleFramework as sf
import os


class SkeletalApplication(sf.Application):


def _createScene(self):
sceneManager = self.sceneManager
camera = self.camera

ogre.Animation.setDefaultInterpolationMode(ogre.Animation.IM_SPLINE)
sceneManager.AmbientLight = ogre.ColourValue(0.5, 0.5, 0.5)

self.animationStates = []
self.animationSpeeds = []

self.viewport.BackgroundColour = ogre.ColourValue.Blue

light = sceneManager.createLight('BlueLight')
light.setPosition (-200, -80, -100)
light.setDiffuseColour (ogre.ColourValue(0.5, 0.5, 1.0) )

light = sceneManager.createLight('GreenLight')
light.setPosition (0, 0, -100)
light.setDiffuseColour (0.5, 1.0, 0.5)

camera.setPosition (100, 50, 100)
camera.lookAt(-50, 50, 0)

materialManager = ogre.MaterialManager.getSingleton()
resourceManager = ogre.ResourceGroupManager.getSingleton()
meshManager = ogre.MeshManager.getSingleton()
skeletonManager = ogre.SkeletonManager.getSingleton()
textureManager = ogre.TextureManager.getSingleton()
media_path = "./data"

textureManager.setDefaultNumMipmaps(5)
materialManager.setDefaultTextureFiltering(ogre.TFO_ANISOTROPIC)
materialManager.setDefaultAnisotropy(2)

## load our mesh
mesh_name = os.path.join(media_path , "robot_2.mesh")
mesh = meshManager.load( mesh_name , "General")

## unload/remove current mesh skeleton (name is embeded in mesh)
skeletonManager.unload("robot.skeleton")
skeletonManager.remove("robot.skeleton")

## load skeleton
skel_name = os.path.join(media_path,"robot_2.skeleton")
skeleton = skeletonManager.load(skel_name, 'General')


## load material
material_name = os.path.join(media_path,"Robot.Material")
ds = resourceManager.openResource(material_name, "General", False)
materialManager.parseScript(ds, "General")
material = materialManager.load("Robot","General")
##material = meshManager.getByName("Robot")


## create texture unit
texture_name = os.path.join(media_path,"XXXr2skin.jpg")
material.getTechnique(0).getPass(0).createTextureUnitState( texture_name )
assert material.getTechnique(0).getPass(0).getNumTextureUnitStates()==1

mesh.setSkeletonName(skeleton.getName())
#mesh._notifySkeleton(skeleton)
entity = sceneManager.createEntity('robot' , mesh.getName() )
entity.getSubEntity(0).setMaterialName(material.getName())
sceneManager.getRootSceneNode().createChildSceneNode().attachObject(entity)


## This or similar may be needed
## entity._deinitialise()
## mesh.setSkeletonName(skeleton.getName())
## OR ##mesh._notifySkeleton(skeleton)
## entity._initialise()
## entity.getSubEntity(0).setMaterialName(material.getName())

## animation
self.animationStates.append(entity.getAnimationState('Walk'))
self.animationStates[0].Enabled = True
self.animationSpeeds.append(ogre.Math.RangeRandom(0.5, 1.5))

## debug text
techniquePass = entity.getSubEntity(0).material.getBestTechnique().getPass(0)
if techniquePass.hasVertexProgram():
if techniquePass.vertexProgram.skeletalAnimationIncluded:
sf.Application.debugText = 'Hardware skinning is enabled'
print 'Hardware skinning is enabled'
else:
sf.Application.debugText = 'Software skinning is enabled'

def _createFrameListener(self):
self.frameListener = SkeletalAnimationFrameListener( self.renderWindow,
self.camera,
self.animationStates,
self.animationSpeeds )
self.root.addFrameListener(self.frameListener)

class SkeletalAnimationFrameListener(sf.FrameListener):
def __init__( self, renderWindow, camera, animationStates, animationSpeeds):

sf.FrameListener.__init__(self, renderWindow, camera)
self.animationStates = animationStates
self.animationSpeeds = animationSpeeds

def frameStarted(self, frameEvent):

for index in range(len(self.animationStates)):
t = frameEvent.timeSinceLastFrame
s = self.animationSpeeds[index]
self.animationStates[index].addTime(t * s)
return sf.FrameListener.frameStarted(self, frameEvent)

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

mrkissinger

11-09-2007 03:21:27

I removed the texture_unit after the first post.

material Robot
{


// Software blending technique
technique
{
pass
{
}
}
}

It doesn't matter.

I think this problem is caused by the modified mesh file.
You can download the mesh file from:
http://mrkissinger.drivehq.com/robot.mesh.xml.bz2 (11K)

In this mesh file, the link to the skeleton file and the refer to the material file were both removed.
This causes my problem.

In the resources.cfg, there MUST only one filesystem (the current directory) could be provided.
Because there are tooooo many materials and animations (thousands of files for each type) could be attached to the mesh at the RUNTIME. I cannot place all these files in a single directory.
Instead, I MUST place them into a two-level tree, for example, 'a/b/abcd.material'. I cannot use resources.cfg to let OGRE exam the whole tree, for speed and memory reasons.

dermont

11-09-2007 12:15:18

Ok with the above mesh I see your problem. Don't know a solution other than adding a dummy skeletonlink to the xml file:

<skeletonlink name="None" />

You'll probably get a better reply on the Main Forum.

mrkissinger

12-09-2007 09:18:24

It gave a warning, but however it works now.


Mesh: Loading data/Mesh/robot.mesh.
Skeleton: Loading None
Unable to load skeleton None for Mesh data/Mesh/robot.mesh. This Mesh will not be animated. You can ignore this message if you are using an offline tool.