Horde3D

Next-Generation Graphics Engine
It is currently 25.11.2024, 02:37

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 12.01.2011, 03:22 
Offline

Joined: 08.11.2010, 10:46
Posts: 35
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)?


Top
 Profile  
Reply with quote  
PostPosted: 12.01.2011, 07:30 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
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.


Top
 Profile  
Reply with quote  
PostPosted: 14.01.2011, 12:17 
Offline

Joined: 08.11.2010, 10:46
Posts: 35
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.


Top
 Profile  
Reply with quote  
PostPosted: 28.02.2011, 06:29 
Offline

Joined: 08.11.2010, 10:46
Posts: 35
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() );


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group