[ Pobierz całość w formacie PDF ]
.Also, that a child frame is availablein the scene.Several preparatory operations are usually required before a texture can be applied to the mesh.These are as follows:1.Create a material and set its properties.2.Set the material on the mesh.3.Set the quality for the meshbuilder.4.Create a suitable wrap.5.Apply the wrap to the meshbuilder object.Much of the programming related to these preparatory operations was discussed previously in thischapter in the context of materials and wraps.In this case the coding can proceed as follows:// General data for routineHRESULT retval;LPDIRECT3DRMMATERIAL2 sphereMaterial = NULL;LPDIRECT3DRMMESHBUILDER3 sphereMeshBuilder = NULL;LPDIRECT3DRMWRAP aWrap = NULL;// Variables for wrap and texture processingD3DRMBOX bBox; // Bounding boxD3DVALUE minY, maxY; // y coordinates min and maxD3DVALUE bHeight; // Height.// Step 1:// Create a material and define its propertiesretval = lpD3DRM->CreateMaterial(D3DVAL(15.0),&sphereMaterial);if(FAILED(retval)){// Material creation error handler goes here}// Set ambient property color and intensityretval = sphereMaterial->SetAmbient(D3DVALUE(2.0), // redD3DVALUE(2.0), // greenD3DVALUE(2.0)); // blueif(FAILED(retval)){// Ambient property error handler goes here}// Step 2:// Set the material on the meshretval = sphereMeshBuilder->SetMaterial(sphereMaterial);if(FAILED(retval)){// Material setting error handler goes here}// Step 3:// Set quality for meshbuilder// Use highest qualityretval = sphereMeshBuilder->SetQuality(D3DRMRENDER_GOURAUD);if(FAILED(retval)){// Quality setting error handler goes here}// Step 4:// Create a cylindrical wrap for the spherical mesh// First obtain bounding box for sphereretval = sphereMeshBuilder->GetBox(&bBox);if(FAILED(retval)){// Bounding box error handler goes here}// ASSERT:// D3DVECTOR types bBox.min and bBox.max now hold the// bounding box dimensions// Store values in local variablesmaxY = bBox.max.y;minY = bBox.min.y;bHeight = maxY - minY;// Using the bounding box data, create a cylindrical wrapretval = lpD3DRM->CreateWrap(D3DRMWRAP_CYLINDR, // Wrap typeNULL, // NULL referenceD3DVAL(0.0), // x of OriginD3DVAL(0.0), // yD3DVAL(0.0), // zD3DVAL(0.0), // x of z-axisD3DVAL(1.0), // yD3DVAL(0.0), // zD3DVAL(0.0), // x of y-axisD3DVAL(0.0), // yD3DVAL(1.0), // zD3DVAL(0.0), // Texture originD3DDivide(minY, bHeight),D3DVAL(1.0), // Scale factorD3DDivide(D3DVAL(1.0), bHeight),&aWrap); // Address of wrapif (FAILED(retval)){// Wrap creation error handler goes here}// Step 5:// Apply wrap to meshbuilderretval = aWrap->Apply((LPDIRECT3DRMOBJECT) sphereMeshBuilder);if (FAILED(retval)){// Wrap application error handler goes here}At this point you are ready to proceed with loading and applying the texture.Loading the textureA texture is a 2D bitmap.The source of a texture can be any image that is in a compatible format(.bmp or.ppm) or that can be converted into these formats.Scanned pictures, texture librariesfrom drawing and modeling programs, and clip art provide easy access to many useful textures.Inaddition, applications often create their own art for textures, usually by means of drawing or bitmapediting.Whatever the origin, the retained mode texture file is a square bitmap whose pixel size is apower of 2.Because the.ppm format exists mostly for compatibility reasons, in the followingexamples we assume that texture files are in.bmp format.The LoadTexture() function of IDirect3DRM3 provides a way for loading a texture from a file.The call assumes that the texture file is in either.bmp of.ppm formats and that it has a color depthof 8, 24, or 32 bits-per-pixel.The function's general form is as follows:HRESULT LoadTexture(const char *lpFileName, // 1LPDIRECT3DRMTEXTURE3 *lplpD3DRMTexture // 2);The first parameter is a pointer to the filename of the texture file.The filename can be entereddirectly in the call by enclosing it in double quotation marks.If the call succeeds, the secondparameter is the address of a pointer that is initialized with an object of IDirect3DRMTexture3interface.This object provides access to all the functions listed in Table 22-3.The function returnsD3DRM_OK if it succeeds, or an error otherwise.The following code fragment shows loading a texture contained in a file named limestn3.bmp andapplying the texture to a mesh.// General data for routineLPDIRECT3DRMTEXTURE3 aTex = NULL;LPDIRECT3DRMFRAME3 childframe = NULL;.// Load texture and set texture to meshretval = lpD3DRM->LoadTexture("limestn3.bmp", &aTex);if (FAILED(retval)){// Load failure error handler goes here}// Change the number of shades from default (16) to 32retval = aTex->SetShades(32);if (FAILED(retval)){// Texture shades change error handler goes here}// Apply texture to meshbuilder objectretval = sphereMeshBuilder->SetTexture(aTex);if (FAILED(retval)){// Set texture error handler goes here}// Texture is made visible by adding the meshbuilder as a// a visual to the child frameretval = childframe->AddVisual((LPDIRECT3DRMVISUAL)sphereMeshBuilder);if (FAILED(retval)){// Adding visual error handler goes here}If all has gone well, the application can now proceed to render the scene in the normal manner.Textures from X FilesRetained mode provides an alternative approach to textures that does not require creatingmeshes, calculating wraps, and manipulating rendering properties.This option consists of thefollowing steps:1.The desired object and its texture are created in a modeling program.2.The textured object is saved as an X format file, or converted into X format using the Conv3dsutility discussed in the previous chapter.The texture file itself, and the texture file reference in theX file, may require editing.3.In the application, an object of type IDirect3DRMFrame3 is created and the Load() functionof IDirect3DRMFrame3 is used to load the textured object, from the X file, into one or moreframes.The material mode is set to D3DRMMATERIAL_FROMFRAME and the textured objects arerendered.In many cases this easy approach to textures is a viable option.Creating a textured objectThe mechanics of applying textures to objects vary with different modeling programs
[ Pobierz całość w formacie PDF ]