Texture Mapping

<< Click to Display Table of Contents >>

Navigation:  XStream® HDVR® SDK > Advanced Functionality > 3D Polygon Meshes >

Texture Mapping

Previous pageReturn to chapter overviewNext page

C++ C++ Java Java .NET .NET

Summary

 

The XStream® HDVR® SDK supports the application of texture maps to IVertexCallListContext or hdrcVertexCallListContext mesh objects. Use of texture maps is optional. Mesh objects can also be colorized with vertex color data. A texture map object is contained in an ITextureMapContext or hdrcTextureMapContext object. The texture source for a texture mapping operation can be either an image file loaded from disk or the output from a render engine.

 

Mesh-TextureMap

 

Setting UV Coordinates

 

The first step in applying a texture map to an IVertexCallListContext or hdrcVertexCallListContext object is to set UV coordinates for each vertex in the mesh. UV coordinates map areas of an image buffer to vertices in the mesh object. For more information on UV mapping see http://en.wikipedia.org/wiki/UV_mapping.

 

To apply UV coordinates to a mesh in the C++ API, use the IVertexCallListContext::GetTextureCoords() method to obtain an array of VECTOR2F objects. Each entry in the array corresponds to a single vertex in the IVertexCallListContext vertex array. The VECTOR2F::x and VECTOR2F::y members represent the U and V coordinates respectively in a UV coordinate pair. The value for the UV coordinates must be between 1 and 0. Once the texture coordinates have been set, use the IVertexCallListContext::SetTextureCoordsModified() method to apply the coordinates to the IVertexCallListContext object. The IVertexCallListContext::SetTextureCoordsModified() method takes as input parameters the range of values in the array that have been changed. The range of values is inclusive of the starting value and exclusive of the ending value. For example, if the array to be modified has 10 elements and all have been modified, the starting value is 0 and the ending value is 10. Only elements 0 through 9 are valid.

 

// Get the number of vertices in this object

unsigned int vertexCount;

pVertexCallList->GetNumVertices(&vertexCount);

 

// Set UV coordinate values.

VECTOR2F *pUVcoords = NULL;

pVertexCallList->GetTextureCoords(&pUVcoords);

for(int i = 0; i < vertexCount; i++) {

    pUVcoords[i].x = 0;

    pUVcoords[i].y = 1;

}

pVertexCallList->SetTextureCoordsModified(true, 0, vertexCount);

 

To apply UV coordinates to a mesh in the Java/.NET APIs, use the hdrcVertexCallListContext.lockTextureCoords() method to obtain an array of float objects. Each pair of entries in the array corresponds to a single vertex in the hdrcVertexCallListContext vertex array. Each pair of float variables represent the U and V coordinates respectively in a UV coordinate pair. The value for the UV coordinates must be between 1 and 0. Once the texture coordinates have been set, use the hdrcVertexCallListContext.unlockTextureCoords() method to apply the coordinates to the hdrcVertexCallListContext object. The hdrcVertexCallListContext.lock/unlockTextureCoordsModified() methods takes as input parameters the range of values in the array that have been changed. The range of values is inclusive of the starting value and exclusive of the ending value. For example, if the array to be modified has 10 elements and all have been modified, the starting value is 0 and the ending value is 10. Only elements 0 through 9 are valid.

 

// Get the number of vertices in this object

int vertexCount; 

vertexCount = vertexCallList.getNumVertices();

 

// Set UV coordinate values.

float[] uvCoords = vertexCallList.lockTextureCoords(0, vertexCount);

for(int i = 0; i < numVerts; i++) {

    uvCoords[i].x = 0;

    uvCoords[i].y = 1;

}

pVertexCallList.unlockTextureCoords(true, 0, numVerts);

 

File Based Texture Mapping

 

Once the UV coordinates for an IVertexCallListContext object have been configured, a texture bitmap must be applied. The first step in applying a bitmap image is to create an ITextureMapContext object. This is done with the IServerContext::CreateTextureMap() method. After the ITextureMapContext object is created, the image file is loaded with the ITextureMapContext::LoadTextureMap() method. This method takes the file name of an image file to be loaded. This function supports a wide range of image formats. After the image has been loaded, it is applied to the IVertexCallListContext object with the IVertexCallListContext::SetTextureMap() method. Each IVertexCallListContext object can have only one texture source applied at a time. Use NULL as the source texture to clear the texture map.

 

// Create an ITextureMapContext object.

ITextureMapContext *pTextureMap = NULL;

pServerContext->CreateTextureMap(&pTextureMap);

 

// Load an image file into the ITextureMapContext object.

pTextureMap->LoadTextureMap("C:/data/image.jpg");

 

// Apply the texture to an IVertexCallListContext object.

// The IVertexCallListContext object must have UV coordinates assigned.

pVertexCallList->SetTextureMap(pTextureMap);

 

Once the UV coordinates for an hdrcVertexCallListContext object have been configured, a texture bitmap must be applied. The first step in applying a bitmap image is to create an hdrcTextureMapContext object. This is done with the hdrcServerContext,createTextureMap() method. After the hdrcTextureMapContext object is created, the image file is loaded with the hdrcTextureMapContext.loadTextureMap() method. This method takes the file name of an image file to be loaded. This function supports a wide range of image formats. After the image has been loaded, it is applied to the hdrcVertexCallListContext object with the hdrcVertexCallListContext.setTextureMap() method. Each hdrcVertexCallListContext object can have only one texture source applied at a time. Use null as the source texture to clear the texture map.

 

// Create an hdrcTextureMapContext object.

hdrcTextureMapContext textureMap = serverContext.createTextureMap();

 

// Load an image file into the hdrcTextureMapContext object.

textureMap.loadTextureMap("C:/data/image.jpg");

 

// Apply the texture to a hdcrVertexCallListContext object.

// The hdrcVertexCallListContext object must have UV coordinates assigned.

vertexCallList.setTextureMap(textureMap);

 

Engine Based Texture Mapping

 

An IRenderEngineContext object (in addition to rendering image files) can render an image that is used to generate a texture image source. When used as the texture source, the IRenderEngineContext can have a completely different transfer function, and other render parameter settings, than the engine that renders the image displayed on the user's screen.

 

The only restriction is that the texture source engine and the on-screen engine must be of the same type. For example, if the application is using a render engine of the type ENUM_RENDER_TYPE::RT_PARALLEL to render on-screen images, then only render engines of type RT_PARALLEL may be used as texture mapping sources with that engine. All adaptive render engine types may be used as texture sources: see the chapter on Rendering Modes for more information. Prior to using an engine as a texture source, the RENDER_PARAMS structure for the engine must be initialized and an initial image rendered, or the engine's image buffer will have nothing to use as a texture image.

 

IRenderQueue objects cannot be used as a texture source.

 

The ITextureMapContext object is created and applied as discussed in the section on File Based Texture Mapping.

 

// Create an IRenderEngineContext object.

IRenderEngineContext pRenderEngine = NULL;

pServerContext->CreateRenderEngine(&pRenderEngine, RECID_PAR);

 

// Configure the IRenderEngineContext object.

RENDER_PARAMS rp;

pServerContext->LoadPreset("C:/data/preset.xml", &rp);

pRenderEngine->SetRenderParams( &rp );

 

// Render an initial image

pRenderEngine->Render(pImgReq, pImgRes);

 

// Create an ITextureMapContext object.

ITextureMapContext pTextureMap = NULL;

pServerContext->CreateTextureMap(&pTextureMap);

 

// Set the IRenderEngineContext object as the texture source.

pVertexCallList->SetTextureMap(pRenderEngine);

 

// When necessary, the texture source engine should be updated as follows.

// This approach will result in much better performance than a synchronous render.

RENDER_PARAMS newRP;

userCode->ConfigureNewRenderParams(&newRP);

pRenderEngine->SetRenderToTextureParams(&newRP)

 

 

An hdrcRenderEngineContext object (in addition to rendering image files) can render an image that is used to generate a texture image source. When used as the texture source, the hdrcRenderEngineContext can have a completely different transfer function, and other render parameter settings, than the engine that renders the image displayed on the user's screen.

 

The only restriction is that the texture source engine and the on-screen engine must be of the same type. For example, if the application is using a render engine of the type RT_PARALLEL to render on-screen images, then only render engines of type RT_PARALLEL may be used as texture mapping sources with that engine. All adaptive render engine types may be used as texture sources: see the chapter on Rendering Modes for more information. Prior to using an engine as a texture source, the RENDER_PARAMS structure for the engine must be initialized and an initial image rendered, or the engine's image buffer will have nothing to use as a texture image.

 

hdrcRenderQueue objects cannot be used as a texture source.

 

The ITextureMapContext object is created and applied as discussed in the section on File Based Texture Mapping.

 

// Create a hdrcRenderEngineContext object.

hdrcRenderEngineContext pRenderEngine = serverContext.createRenderEngine(hdrcDefines.RENDER_ENGINE_ID_PAR);

 

// Configure the IRenderEngineContext object.

RENDER_PARAMS rp = new RENDER_PARAMS();

serverContext.loadPreset("C:/data/preset.xml", rp);

renderEngine.setRenderParams(rp);

 

// Render an initial image

renderEngine.render(imgReq, imgRes);

 

// Create an hdrcTextureMapContext object.

hdrcTextureMapContext textureMap = serverContext.createTextureMap();

 

// Set the hdrcRenderEngineContext object as the texture source.

vertexCallList.setTextureMapEngine(renderEngine);

 

// When necessary, the texture source engine should be updated as follows.

// This approach will result in much better performance than a synchronous render.

RENDER_PARAMS newRP = new RENDER_PARAMS();

userCode.ConfigureNewRenderParams(newRP);

renderEngine.setRenderToTextureParams(newRP)

 

Texture Lighting

 

Texture mapped objects are by default not affected by lighting. This means they will be flat shaded regardless of the object's orientation or ambient/diffuse light settings. Unlit texture mapped objects work best when engine based texture maps are applied as they are generally used as user interface or volume visualization components. When loading file based texture map objects, such as prostheses or surgical guides, the user will generally wish to have lighting applied to the object.

 

Texture mapped object lighting can be controlled with the IVertexCallListContext::SetTextureLighting() method. This method takes a single boolean parameter. Set this value to true if lighting is desired, set to false if no lighting is desired.

 

// Turn lighting on for texture mapped object.

pVertexCallList->SetTextureLighting(true);

 

Texture mapped object lighting can be controlled with the hdrcVertexCallListContext::setTextureLighting() method. This method takes a single boolean parameter. Set this value to true if lighting is desired, set to false if no lighting is desired.

 

// Turn lighting on for texture mapped object.

pVertexCallList->SetTextureLighting(true);

 

Texture Mapped OBJ Files

 

The C++ API supports the loading of textured OBJ files using the IPolygonUtilContext::LoadOBJ() method. The OBJ file format supports mesh data that includes UV coordinates. Using this mechanism, the application can load texture mapped OBJ files from any third party modeling application. The IVertexCallListContext object that is created from the OBJ file includes the UV coordinates present in the OBJ file. The texture map for the OBJ file must be applied separately using the IVertexCallListContext::SetTextureMap() method. Only a single texture map may be applied to an IVertexCallListContext object at a time. See Mesh File Import for more information on loading OBJ files.

 

the Java/.NET API supports the loading of textured OBJ files using the hdrcPolygonUtilContext,loadOBJ() method. The OBJ file format supports mesh data that includes UV coordinates. Using this mechanism, the application can load texture mapped OBJ files from any third party modeling application. The hdrcVertexCallListContext object that is created from the OBJ file includes the UV coordinates present in the OBJ file. The texture map for the OBJ file must be applied separately with the hdrcVertexCallListContext.setTextureMap() method. Only a single texture map may be applied to a hdrcVertexCallListContext object at a time. See Mesh File Import for more information on loading OBJ files.

 

 

Texture Mapping Restrictions

 

Only one texture map object, either from a file or a render engine, may be applied to an IVertexCallListContext or hdrcVertexCallListContext object at a time.

 

The texture source engine and the on-screen engine must be of the same type. For example, if the application is using a render engine of the type ENUM_RENDER_TYPE::RT_PARALLEL to render on-screen images, then only render engines of type RT_PARALLEL may be used as texture mapping sources with that engine. All adaptive render engine types may be used as texture sources.