1 bool HGE_Impl::_init_lost() 2 { 3 CRenderTargetList *target=pTargets; 4 5 // Store render target 6 //屏幕表面 7 pScreenSurf=0; 8 //屏幕深度 9 pScreenDepth=0; 10 11 pD3DDevice->GetRenderTarget(&pScreenSurf); //取渲染目标表面,这个函数在DX9有些变化 12 pD3DDevice->GetDepthStencilSurface(&pScreenDepth); //取屏幕深度 13 //当存在渲染目标时 14 while(target) 15 { 16 //如果目标纹理存在,则重新创建纹理 17 if(target->pTex) 18 D3DXCreateTexture(pD3DDevice, target->width, target->height, 1, D3DUSAGE_RENDERTARGET, 19 d3dpp->BackBufferFormat, D3DPOOL_DEFAULT, &target->pTex); 20 //如果深度存在,则重新创建深度表面 21 if(target->pDepth) 22 pD3DDevice->CreateDepthStencilSurface(target->width, target->height, 23 D3DFMT_D16, D3DMULTISAMPLE_NONE, &target->pDepth); 24 target=target->next; 25 } 26 27 // Create Vertex buffer 28 //创建顶点缓存 29 if( FAILED (pD3DDevice->CreateVertexBuffer(VERTEX_BUFFER_SIZE*sizeof(hgeVertex), 30 D3DUSAGE_WRITEONLY, 31 D3DFVF_HGEVERTEX, 32 D3DPOOL_DEFAULT, &pVB ))) 33 { 34 _PostError("Can't create D3D vertex buffer"); 35 return false; 36 } 37 //设置顶点格式,在Dx9中,将DX8的这个函数SetVertexShader分成了SetVertexShader和SetFVF 38 //在DX9中,我们只需要使用SetFVF就可以设置顶点格式了 39 pD3DDevice->SetVertexShader( D3DFVF_HGEVERTEX ); 40 //把一个顶点缓存绑定到一个设备数据流 41 pD3DDevice->SetStreamSource( 0, pVB, sizeof(hgeVertex) ); 42 43 // Create and setup Index buffer 44 //创建索引缓存 45 if( FAILED( pD3DDevice->CreateIndexBuffer(VERTEX_BUFFER_SIZE*6/4*sizeof(WORD), 46 D3DUSAGE_WRITEONLY, 47 D3DFMT_INDEX16, 48 D3DPOOL_DEFAULT, &pIB ) ) ) 49 { 50 _PostError("Can't create D3D index buffer"); 51 return false; 52 } 53 //锁定顶点缓存 54 WORD *pIndices, n=0; 55 if( FAILED( pIB->Lock( 0, 0, (BYTE**)&pIndices, 0 ) ) ) 56 { 57 _PostError("Can't lock D3D index buffer"); 58 return false; 59 } 60 //建立索引 这里主要是针对hgeQuad的方式。DX中没有四边形图元,所以这里是由两个三角形拼起来的。 61 //绘制三角形的时候,默认是逆时针方向绘三个顶点 62 for(int i=0; i<VERTEX_BUFFER_SIZE/4; i++) { 63 /* 64 第一个三角形 65 pIndices[0] = n; 66 pIndices[1] = n+1; 67 pIndices[2] = n+2; 68 */ 69 *pIndices++=n; 70 *pIndices++=n+1; 71 *pIndices++=n+2; 72 /* 73 第二个三角形 74 pIndices[0] = n+2; 75 pIndices[1] = n+3; 76 pIndices[2] = n; 77 */ 78 *pIndices++=n+2; 79 *pIndices++=n+3; 80 *pIndices++=n; 81 n+=4; 82 } 83 pIB->Unlock(); 84 pD3DDevice->SetIndices(pIB,0); 85 86 // Set common render states 87 //设置通用渲染状态 88 89 //pD3DDevice->SetRenderState( D3DRS_LASTPIXEL, FALSE ); 90 //D3DRS_CULLMODE 这个参数用来指定三角形背面的剔除方式。 91 //D3DCULL_NONE,不要剔除背面; 92 //D3DCULL_CW,按照顺时针的方向的顶点剔除背面; 93 //D3DCULL_CCW,按照逆时针方向剔除背面。 94 pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); 95 //关闭光照 96 pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); 97 //打开alpha通道 98 pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); 99 //下面设置alpha混合系数 100 pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); //设置源混合方式 D3DBLEND_SRCALPHA:Blend factor is (As, As, As, As). 101 pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); //设置目的混合方式 D3DBLEND_INVSRCALPHA:Blend factor is ( 1 - As, 1 - As, 1 - As, 1 - As). 102 103 //调用alpha测试,用于提升性能 104 pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE ); 105 //测试参考值,0x01相当于对于全透明的,将不渲染 106 pD3DDevice->SetRenderState( D3DRS_ALPHAREF, 0x01 ); 107 //要求渲染>=参考值的像素 108 pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL ); 109 110 //设置状态 111 //纹理融合操作 112 pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); //设置颜色通道混合控制为:多参数 113 pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); // 114 pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); 115 116 pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); ////设置alpha通道混合控制为:多参数 117 pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); 118 pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); 119 //D3DTSS_MIPFILTER过滤采用近点采样 120 pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); 121 122 if(bTextureFilter) //如果使用纹理过滤 123 { 124 pD3DDevice->SetTextureStageState(0,D3DTSS_MAGFILTER,D3DTEXF_LINEAR); 125 pD3DDevice->SetTextureStageState(0,D3DTSS_MINFILTER,D3DTEXF_LINEAR); 126 } 127 else 128 { 129 pD3DDevice->SetTextureStageState(0,D3DTSS_MAGFILTER,D3DTEXF_POINT); 130 pD3DDevice->SetTextureStageState(0,D3DTSS_MINFILTER,D3DTEXF_POINT); 131 } 132 133 nPrim=0; 134 CurPrimType=HGEPRIM_QUADS; //当前主要操作类型 135 CurBlendMode = BLEND_DEFAULT; //当前混合方式 136 CurTexture = NULL; //当前纹理 137 138 pD3DDevice->SetTransform(D3DTS_VIEW, &matView); //设置视图,该值为默认值,没有变化 139 pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj); //设置投影,已设为正交投影,具体参考我前面的散笔:HGE
的坐标转换函数注释说明_SetProjectionMatrix
140 141 return true; 142 }
|