![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
TransparencyThe Direct3DRMTexture interface lets you specify a color within a texture that is to be treated as transparent by Direct3D. Portions of a texture that are transparent allow meshes and textures that would normally be hidden by the texture to be visible. By default, a textures transparency is disabled. The Direct3DRMTexture SetDecalTransparency() function can be used to enable and disable transparency. When transparency is enabled, any black pixels within the texture are transparent. The transparent color can be specified with the SetDecalTransparencyColor() function. The OrbStar DemoThe OrbStar demo uses transparency to animate a star within a sphere. The star mesh and the sphere mesh are both placed at the origin. The sphere is scaled to be larger than the star, so the star wouldnt be visible if the texture used on the sphere wasnt partially transparent. The OrbStar demo is shown in Figure 5.8.
The texture that is applied to the sphere in the demo is shown in Figure 5.9.
The OrbStar demo demonstrates the following techniques:
The OrbStarWin ClassThe OrbStarWin class provides most of the OrbStar demos functionality. The class definition looks like this: class OrbStarWin : public RMWin { public: BOOL CreateScene(); static void MoveSphere(LPDIRECT3DRMFRAME frame, void* arg, D3DVALUE delta); static void MoveStar(LPDIRECT3DRMFRAME frame, void* arg, D3DVALUE delta); protected: //{{AFX_MSG(OrbStarWin) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; The class declares three functions: CreateScene(), MoveSphere(), and MoveStar(). The CreateScene() function constructs the demos scene. The MoveSphere() and MoveStar() functions are callback functions that we will use to animate the two meshes in the scene. The OrbStarWin::CreateScene() FunctionThe CreateScene() function for the OrbStar demo creates the star and sphere meshes, the texture that is to be applied to the sphere, two light sources, and a viewport. The CreateScene() function is shown as Listing 5.4. Listing 5.4 The OrbStarWin::CreateScene() function. BOOL OrbStarWin::CreateScene() { //-------- STAR MESH -------- D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_STARMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER starbuilder; d3drm->CreateMeshBuilder( &starbuilder ); starbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); starbuilder->SetColorRGB( D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.0) ); ScaleMesh( starbuilder, D3DVALUE(20) ); //--------- STAR FRAME -------- LPDIRECT3DRMFRAME starframe; d3drm->CreateFrame( scene, &starframe ); starframe->SetRotation( scene, D3DVALUE(1.0), D3DVALUE(0.0),D3DVALUE(0.0), D3DVALUE(0.1) ); starframe->AddVisual( starbuilder ); starframe->AddMoveCallback( MoveStar, NULL ); starframe->Release(); starframe=0; starbuilder->Release(); starbuilder=0; //--------- SPHERE MESH -------- resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER spherebuilder; d3drm->CreateMeshBuilder( &spherebuilder ); spherebuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); spherebuilder->SetPerspective( TRUE ); ScaleMesh( spherebuilder, D3DVALUE(25) ); //---------- SPHERE TEXTURE ------ LPDIRECT3DRMTEXTURE texture; HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE( IDR_TRANSTEXTURE), "TEXTURE" ); d3drm->LoadTextureFromResource( texture_id, &texture ); texture->SetDecalTransparency( TRUE ); spherebuilder->SetTexture( texture ); texture->Release(); texture=0; //---------- SPHERE WRAP -------- D3DRMBOX box; spherebuilder->GetBox( &box ); D3DVALUE width=box.max.x-box.min.x; D3DVALUE height=box.max.y-box.min.y; LPDIRECT3DRMWRAP wrap; d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL, D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0), // origin D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0), // z axis D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0), // y axis D3DVALUE(0.5), D3DVALUE(0.5), // origin D3DDivide(1,width), D3DDivide(1,height), // scale &wrap ); wrap->Apply( spherebuilder ); wrap->Release(); wrap=0; //-------- SPHERE FRAME ---------- LPDIRECT3DRMFRAME sphereframe; d3drm->CreateFrame( scene, &sphereframe ); sphereframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(0), D3DVALUE(.1) ); sphereframe->AddVisual( spherebuilder ); sphereframe->AddMoveCallback( MoveSphere, NULL ); sphereframe->Release(); sphereframe=0; spherebuilder->Release(); spherebuilder=0; //----------- LIGHT -------- LPDIRECT3DRMLIGHT light1, light2; d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT, D3DVALUE(0.8),D3DVALUE(0.8), D3DVALUE(0.8), &light1 ); d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL, D3DVALUE(0.9), D3DVALUE(0.9), D3DVALUE(0.9), &light2 ); LPDIRECT3DRMFRAME lightframe; d3drm->CreateFrame( scene, &lightframe ); lightframe->SetOrientation( scene, D3DVALUE(-1), D3DVALUE(-1), D3DVALUE(1), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) ); lightframe->AddLight( light1 ); lightframe->AddLight( light2 ); lightframe->Release(); lightframe=0; light1->Release(); lightframe=0; light2->Release(); lightframe=0; //----------- VIEWPORT -------- d3drm->CreateFrame( scene, &camera ); camera->SetPosition( scene, D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(-50) ); d3drm->CreateViewport( device, camera, 0, 0, device->GetWidth(), device->GetHeight(), &viewport ); return TRUE; }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |