Horde3D
http://horde3d.org/forums/

Loading resources e.g. Material
http://horde3d.org/forums/viewtopic.php?f=2&t=1354
Page 1 of 1

Author:  vikingcode [ 12.01.2011, 03:22 ]
Post subject:  Loading resources e.g. Material

I am trying to understand how to load resources from memory.
I want to dynamically create geometry, load a texture, create a material and attach the texture, then create a mesh node and attach the material.
But how may I load the material? What kind of data is a material resource waiting for?
Code:
h3dLoadResource( materialResource, 0x0, 0 );


Here is my code
Code:
// Create a geometry resource
H3DRes geometryResource = h3dutCreateGeometryRes( name.c_str(), num_vertices, num_indices, &positionData[0], &indexData[0], &normalData[0], 0x0, 0x0, &textureData[0], 0x0 );
if ( !geometryResource )
   return 0x0;

// Add a new texture resource, with parameter source as the name/filename
H3DRes textureResource = h3dAddResource( H3DResTypes::Texture, source.c_str(), H3DResFlags::NoTexMipmaps );
if ( !textureResource )
{
   h3dRemoveResource( geometryResource );
   return 0x0;
}
// Load the file from disk, ensure we have at least one ImageElem resource element
if ( !h3dutLoadResourcesFromDisk( _contentDir.c_str() ) || h3dGetResElemCount( textureResource, H3DTexRes::ImageElem ) < 1 )
{
   h3dRemoveResource( textureResource );
   h3dRemoveResource( geometryResource );
   return 0x0;
}

// Create a model scene node and attach the geometry
H3DNode modelNode = h3dAddModelNode( H3DRootNode, (name + "Model").c_str(), geometryResource );
if ( !modelNode )
{
   h3dRemoveResource( textureResource );
   h3dRemoveResource( geometryResource );
   return 0x0;
}
h3dSetNodeParamI( modelNode, H3DModel::GeoResI, geometryResource ); // attach geometry

// Create a material resource and attach the texture
H3DRes materialResource = h3dAddResource( H3DResTypes::Material, (name + "Material").c_str(), 0 );
if ( !materialResource )
{
   h3dRemoveNode( modelNode );
   h3dRemoveResource( textureResource );
   h3dRemoveResource( geometryResource );
   return 0x0;
}
h3dSetResParamI( materialResource, H3DMatRes::SamplerElem, 0, H3DMatRes::SampTexResI, textureResource ); // attach texture
h3dSetResParamStr( materialResource, H3DMatRes::SamplerElem, 0, H3DMatRes::SampNameStr, (name + "Sampler").c_str() );   

// ????? Load the material resource ?????
bool resourceLoaded = h3dLoadResource( materialResource, 0x0, 0 );

// Create a mesh node and attach the material resource
H3DNode meshNode = h3dAddMeshNode( modelNode, (name + "Mesh").c_str(), materialResource, 0, num_indices, 0, num_vertices-1 );
if ( !meshNode )
{
   h3dRemoveResource( materialResource );
   h3dRemoveNode( modelNode );
   h3dRemoveResource( textureResource );
   h3dRemoveResource( geometryResource );
   return 0x0;
}

So how should I create the data for a material? What data is it looking for?

What I really want to know is, how can I do it dynamically in code without using xml files or any files at all (except the image file)?

Author:  DarkAngel [ 12.01.2011, 07:30 ]
Post subject:  Re: Loading resources e.g. Material

The resource data required for a material is an XML string, conforming to the material file format in the manual. You can build this string in memory (and ensure it's NULL-terminated), then get the size (including the NULL character), and pass the string-pointer and size into h3dLoadResource.

Author:  vikingcode [ 14.01.2011, 12:17 ]
Post subject:  Re: Loading resources e.g. Material

Thanks DarkAngel for pointing me in the right direction.
I will get back to you later with the results of how I went, and my sample project.

Author:  vikingcode [ 28.02.2011, 06:29 ]
Post subject:  Re: Loading resources e.g. Material

Hi,

This is an example of how I setup the resource data for a material that I want to load.
It's very easy.

Code:
// Create the material resource
H3DRes materialRes = h3dAddResource( H3DResTypes::Material, materialName.c_str() , 0 ); // we need the material name
if ( !materialRes  )
   return false;

// Setup the XML string to represent the material data
std::string materialData = std::string( "<Material>\n" );
materialData.append( "<Shader source=\"shaders/myShader.shader\"/>\n" );     // shader source
if ( texture )
{
   std::string textureName = h3dGetResName( texture );                        // we need the texture resource, if it exists
   materialData.append( "<ShaderFlag name=\"_F01_Textured\"/>\n" );   // shader flags
   materialData.append( "<Sampler name=\"albedoMap\" map=\"" );      // sampler name and attributes
   materialData.append( textureName.c_str() );               // name of the texture resource to set as the <map> value
   materialData.append( "\" allowCompression=\"true\" mipmaps=\"false\" />\n" );
}
materialData.append( "</Material>\n" );

// Load the material resource from the material data string
return h3dLoadResource( materialRes, materialData.c_str(), materialData.length() );

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/