Horde3D Wiki:HOWTO

From Horde3D Wiki
Revision as of 21:13, 25 January 2010 by Marciano (talk | contribs)
Jump to: navigation, search

The HOWTO contains quick answers for smaller problems.



How to enable anti-aliasing (MSAA)

Anti-aliasing can only be enabled for render targets. The RenderTarget element used in Pipeline resources has an attribute maxSamples which defines the maximum number of samples used for MSAA. The actual number of samples is controlled by the engine option SampleCount. This makes it possible to control the anti-aliasing quality from the application. If SampleCount is set to zero, MSAA is disabled. Note that the hardware needs to support the EXT_framebuffer_multisample extension in order for the MSAA to work.


How to get the size of a loaded texture

The following code queries the dimensions of the base mipmap of a loaded texture:

C++ Code
int width = h3dGetResParamI( texResHandle, H3DTexRes::ImageElem, 0, H3DTexRes::ImgWidthI );
int height = h3dGetResParamI( texResHandle, H3DTexRes::ImageElem, 0, H3DTexRes::ImgHeightI );

If you want to get the dimensions of mipmap level n, change the elemIdx parameter which is 0 above to n.


How to modify vertex data

Vertex data is stored as streams in geometry resources. A pointer to the vertex data can be obtained by mapping a stream. The following code modifies the y coordinates of all vertex positions stored in the specified geometry resource.

C++ Code
int vertCount = h3dGetResParamI( myGeoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertexCountI );
float *pVertPosData = (float *)h3dMapResStream( myGeoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertPosStream, true, true );

for( int i = 0; i < vertCount; ++i )
{
    pVertPosData[i * 3 + 1] = pVertPosData[i * 3 + 1] * 2;
}

h3dUnmapResStream( myGeoRes );


How to exchange a texture of a mesh

Consider you want to exchange the diffuse texture (sampler named albedoMap) of a given mesh (with node handle myMesh) by a texture resource with resource handle myNewTex. You can do that with the following code:

C++ Code
H3DRes matRes = h3dGetNodeParamI( myMesh, H3DMesh::MatResI );
int idx = h3dFindResElem( matRes, H3DMatRes::SamplerElem, H3DMatRes::SampNameStr, "albedoMap" );
h3dSetResParamI( matRes, H3DMatRes::SamplerElem, elemIdx, SampTexResI, myNewTex );

First you need to retrieve the material used by the mesh. After that, you can find the desired sampler in the material. Finally, once you have the sampler index, you can access the sampler data and override its texture resource.

Note: The code above changes directly the material resource. If several meshes are using the same material, all of them will have the new texture. If that is not desired, you can create a private copy of the material by cloning it.


How to display text in the scene

Usually, text is displayed as part of a GUI or HUD on top of the scene. However, sometimes it can be useful to display text inside the scene at the location of a specific node. The following snippet displays the names of all models at the nodes' origins. For obtaining the right screen position, the node positions are projected and converted to overlay coordinates.

C++ Code
// Get camera view matrix
const float *camTrans;
h3dGetNodeTransMats( _cam, 0x0, &camTrans );
Matrix4f viewMat( Matrix4f( camTrans ).inverted() );

// Get camera projection matrix
float camProj[16];
h3dGetCameraProjMat( _cam, camProj );
Matrix4f projMat( camProj );

// Loop over all model nodes
int cnt = h3dFindNodes( H3DRootNode, "", H3DNodeTypes::Model );
for( int i = 0; i < cnt; ++i )
{
	// Get node name
	H3DNode node = h3dGetNodeFindResult( i );
	const char *name = h3dGetNodeParamStr( node, H3DNodeParams::NameStr );

	// Get node position
	const float *nodeTrans;
	h3dGetNodeTransMats( node, 0, &nodeTrans ); 
	Vec4f pos( nodeTrans[12], nodeTrans[13], nodeTrans[14], 1 );

	// Project
	pos = projMat * viewMat * pos;
	float x = pos.x / pos.w;
	float y = pos.y / pos.w;

	// Transform to overlay coordinates
	x = x * 0.5f + 0.5f;
	y = -y * 0.5f + 0.5f;

	// Show text (avoid back-projection)
	if( pos.w > 0 ) h3dutShowText( name, x, y, 0.02f, 1.0f, 1.0f, 1.0f, _fontMatRes, 0 );
}