Horde3D Wiki:HOWTO

From Horde3D Wiki
Jump to: navigation, search

The HOWTO contains quick answers for smaller problems.


Contents


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
  1. int width = h3dGetResParamI( texResHandle, H3DTexRes::ImageElem, 0, H3DTexRes::ImgWidthI );
    
  2. 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 procedurally generate a texture

To generate a texture and bind it to a shader, you need to perform three steps:

First, create the texture handle, map data stream and write to it. Note that you need to perform this step before loading resources from disk (using H3dutLoadResourcesFromDisk())! Otherwise, the texture creation will fail.

C++ Code
  1. H3DRes texRes = h3dCreateTexture( "myTexRes", 64, 64, H3DFormats::TEX_RGBA16F, H3DResFlags::NoTexMipmaps );
    
  2. float *data = static_cast<float*>( h3dMapResStream( texRes, H3DTexRes::ImageElem, 0, H3DTexRes::ImgPixelStream, false, true ) );
    
  3. for( int row=0; row<64; ++row )
    
  4. {
    
  5.     float *rowPtr = data + row*64*4;
    
  6.     for( int col=0; col<64; ++col )
    
  7.     {
    
  8.         float *sample = rowPtr + col*4;
    
  9.         sample[0] = ...; // R
    
  10.         sample[1] = ...; // G
    
  11.         sample[2] = ...; // B
    
  12.         sample[3] = ...; // A
    
  13.     }
    
  14. }
    
  15. h3dUnmapResStream( texRes );
    

Second, to bind the texture to a shader, you need to include it in the material XML file like this:

<Material>
    <Shader source="shaders/myshader.shader" />
    <Sampler name="myTexture" map="myTexRes" />
</Material>

Finally, to reference the sampler in the shader, you need to declare it like this:

Shader Code
  1. [[FX]]
    
  2. sampler myTexture;
    
  3.  
    
  4. // ... declare other samplers, contexts etc.
    
  5.  
    
  6. [[FS_LIGHTING]]
    
  7.  
    
  8. varying vec2 texCoords; // ... assumed given by vertex shader ...
    
  9. uniform sampler2D myTexture;
    
  10.  
    
  11. void main( void )
    
  12. {
    
  13.   gl_FragColor = texture2D( myTexture, texCoords );
    
  14. }
    

Information taken from this thread on the forums.


How to procedurally generate a geometry resource

To generate a geometry resource from given data call h3dutCreateGeometryRes:

the following example will create a simple 2D plane.

C++ Code
  1. float posData[] = { 
    
  2.   0,  0, 0, 
    
  3.  10,  0, 0, 
    
  4.   0, 10, 0,
    
  5.  10, 10, 0
    
  6. };
    
  7.  
    
  8. unsigned int indexData[] = { 0, 1, 2, 2, 1, 3 };
    
  9. short normalData[] = {
    
  10.  0, 0, 1,
    
  11.  0, 0, 1,
    
  12.  0, 0, 1,
    
  13.  0, 0, 1
    
  14. };
    
  15.  
    
  16. float uvData[] = {
    
  17.  0, 0,
    
  18.  1, 0,
    
  19.  0, 1, 
    
  20.  1, 1
    
  21. };
    
  22.  
    
  23. H3DRes geoRes = h3dutCreateGeometryRes( "geoRes", 4, 6, posData, indexData, normalData, 0, 0, uvData, 0 );
    
  24. H3DNode model = h3dAddModelNode( H3DRootNode, "DynGeoModelNode", geoRes );
    
  25. h3dAddMeshNode( model, "DynGeoMesh", matRes, 0, 6, 0, 3 );
    


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
  1. int vertCount = h3dGetResParamI( myGeoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertexCountI );
    
  2. float *pVertPosData = (float *)h3dMapResStream( myGeoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertPosStream, true, true );
    
  3.  
    
  4. for( int i = 0; i < vertCount; ++i )
    
  5. {
    
  6.     pVertPosData[i * 3 + 1] = pVertPosData[i * 3 + 1] * 2;
    
  7. }
    
  8.  
    
  9. 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
  1. H3DRes matRes = h3dGetNodeParamI( myMesh, H3DMesh::MatResI );
    
  2. int idx = h3dFindResElem( matRes, H3DMatRes::SamplerElem, H3DMatRes::SampNameStr, "albedoMap" );
    
  3. h3dSetResParamI( matRes, H3DMatRes::SamplerElem, idx, H3DMatRes::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
  1. // Get camera view matrix
    
  2. const float *camTrans;
    
  3. h3dGetNodeTransMats( _cam, 0x0, &camTrans );
    
  4. Matrix4f viewMat( Matrix4f( camTrans ).inverted() );
    
  5.  
    
  6. // Get camera projection matrix
    
  7. float camProj[16];
    
  8. h3dGetCameraProjMat( _cam, camProj );
    
  9. Matrix4f projMat( camProj );
    
  10.  
    
  11. // Loop over all model nodes
    
  12. int cnt = h3dFindNodes( H3DRootNode, "", H3DNodeTypes::Model );
    
  13. for( int i = 0; i < cnt; ++i )
    
  14. {
    
  15.         // Get node name
    
  16.         H3DNode node = h3dGetNodeFindResult( i );
    
  17.         const char *name = h3dGetNodeParamStr( node, H3DNodeParams::NameStr );
    
  18.  
    
  19.         // Get node position
    
  20.         const float *nodeTrans;
    
  21.         h3dGetNodeTransMats( node, 0, &nodeTrans ); 
    
  22.         Vec4f pos( nodeTrans[12], nodeTrans[13], nodeTrans[14], 1 );
    
  23.  
    
  24.         // Project
    
  25.         pos = projMat * viewMat * pos;
    
  26.         float x = pos.x / pos.w;
    
  27.         float y = pos.y / pos.w;
    
  28.  
    
  29.         // Transform to overlay coordinates
    
  30.     const float aspectRatio = h3dGetViewportParams( 0x0, 0x0, 0x0, 0x0 );
    
  31.         x = (x * 0.5f + 0.5f)*aspectRatio;
    
  32.         y = -y * 0.5f + 0.5f;
    
  33.  
    
  34.         // Show text (avoid back-projection)
    
  35.         if( pos.w > 0 ) h3dutShowText( name, x, y, 0.02f, 1.0f, 1.0f, 1.0f, _fontMatRes);
    
  36. }
    


How to tell the camera to look at a point

Only pseudo code here, how the math behind this works can be found by searching for gluLookAt.

Pseudo Code
  1. forward = targetPosition - cameraPosition
    
  2. forward = normalize(forward)
    
  3.  
    
  4. up = Vector3(0, 1, 0) # vector pointing "up", can be almost any vector
    
  5. up = normalize(up)
    
  6.  
    
  7. right = cross(forward, up)
    
  8. right = normalize(right)
    
  9.  
    
  10. up = cross(right, forward)
    
  11.  
    
  12. # rotation matrix, column major format
    
  13. m1 = [right.x, up.x, -forward.x, 0,
    
  14.     right.y, up.y, -forward.y, 0,
    
  15.     right.z, up.z, -forward.z, 0,
    
  16.     0, 0, 0, 1]
    
  17.  
    
  18. # translation matrix, column major format
    
  19. m2 = [1, 0, 0, 0,
    
  20.     0, 1, 0, 0,
    
  21.     0, 0, 1, 0,
    
  22.     -cameraPosition.x, -cameraPosition.y, -cameraPosition.z, 1]
    
  23.  
    
  24. # combined transformation matrix ("view" matrix)
    
  25. m3 = m1 * m2
    
  26.  
    
  27. # since the camera node is just another node in Horde3D we have to invert the matrix m3
    
  28. m3 = invert(m3)
    
  29.  
    
  30. h3dSetNodeTransMat(cameraNode, m3)
    

This example will fail when either forward is a zero vector or forward is parallel to the choosen up vector.


How to orbit / rotate the camera around a point

Pseudo Code
  1. # setup: create an anchor node, so that the camera can always rotate around a relative (0, 0, 0) point
    
  2. anchorNode = h3dAddGroupNode(h3dRootNode, 'camera-anchor')
    
  3. cameraNode = h3dAddCameraNode(anchorNode, 'camera-orbiter')
    
  4.  
    
  5. # basically calculate point on the surface of a sphere
    
  6. # by using distance and two angles
    
  7.  
    
  8. phi = phi % 360.0
    
  9. theta = theta % 360.0
    
  10.  
    
  11. if phi < 0: # will break for phi=0 and theta=0 otherwise
    
  12.     phi = 0.0001
    
  13.  
    
  14. r = distance
    
  15. p = radians(phi)
    
  16. t = radians(theta)
    
  17.  
    
  18. x = r * cos(t) * sin(p)
    
  19. y = r * cos(p)
    
  20. z = r * sin(t) * sin(p)
    
  21.  
    
  22. # calculate vectors for camera look at
    
  23. forward = Vector3(0, 0, 0,) - Vector3(x, y, z)
    
  24. if phi >= 180.0:
    
  25.     up = Vector3(0, 1, 0)
    
  26. else:
    
  27.     up = Vector3(0, -1, 0)
    
  28.  
    
  29. # with the forward and up vector use the code from the look at example to point the camera at the point (0, 0, 0).
    

When following another scene node use that node instead of h3dRootNode as a parent in the h3dAddGroupNode call.

Personal tools