Textures

LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer=NULL; //顶点缓存

LPDIRECT3DTEXTURE9 g_Texture=NULL;//纹理对象

bool InitializeObjects()
{
 //将含有纹理坐标的数据填充到该顶点结构数组中
 stD3DVertex objData[]=
 {
  {-0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255), 0, 1},
  {0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255), 1, 1},
  {0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255), 1, 0},

  {0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255), 1, 0},
  {-0.3f, 0.4f, 0, D3DCOLOR_XRGB(255,255,255), 0, 0},
  {-0.3f, -0.4f, 0, D3DCOLOR_XRGB(255,255,255), 0, 1}
 };
 //创建顶点缓存
 if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData),0,
  D3DFVF_VERTEX,D3DPOOL_DEFAULT,
  &g_VertexBuffer,NULL))) 
  return false;
 void *ptr;

 //锁定顶点缓存,以进行读写操作。ptr指向该顶点缓存内存区域
 if(FAILED(g_VertexBuffer->Lock(0,sizeof(objData),
  (void**)&ptr,0)))  return false;
 //将objData内存区域的数据复制到ptr所指向的内存区域中
    memcpy(ptr,objData,sizeof(objData));

 g_VertexBuffer->Unlock();

 //加载纹理图像至该设备对象
 if(D3DXCreateTextureFromFile(g_D3DDevice,"ugp.tga",
  &g_Texture)!=D3D_OK)
  return false;

 // 设置纹理采样模式(纹理过滤器)
 g_D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER,
  D3DTEXF_LINEAR); // 线性过滤
 g_D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER,
  D3DTEXF_LINEAR); // 线性过滤

 g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);          // 关闭光照
 g_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);  // 不剔除任何面

 


 // Set the projection matrix.
 D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
  WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);

 g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);

 // Set default rendering states.
 g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

 // Define camera information.
 D3DXVECTOR3 cameraPos(0.0f, 0.0f, -1.0f);
 D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f);
 D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);

 // Build view matrix.创建视图矩阵
 D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
  &lookAtPos, &upDir);

 return true;
}


void RenderScene()
{
 // Clear the back buffer.
 g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
  D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

 // Begin the scene.  Start rendering.启动绘制
 g_D3DDevice->BeginScene();

 // Apply the view (camera).
 g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

 // 设置纹理
 g_D3DDevice->SetTexture(0, g_Texture);
 // 设置所要使用的顶点缓存与数据流的链接
 g_D3DDevice->SetStreamSource(0, g_VertexBuffer,
  0, sizeof(stD3DVertex));
 // 设置灵活顶点格式
 g_D3DDevice->SetFVF(D3DFVF_VERTEX);
 // 绘制该场景
 g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

 // End the scene.  Stop rendering.
 g_D3DDevice->EndScene();

 // Display the scene.
 g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}

你可能感兴趣的:(text)