Direct3D C++纹理映射
大家好,谁能帮我在 Direct3D C++ 中进行纹理映射。我已经创建了一个基本游戏并想要对环境进行纹理处理。我已经看过许多在线教程,但到目前为止还没有运气
我正在为我的绘图代码创建一个自定义顶点:
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The position for the vertex
DWORD color; // The vertex color
};
这就是我绘制正方形的方式:
CUSTOMVERTEX g_Vertices[] =
{
{-1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f, 1.0f,-1.0f,0xFF0000FF},
{ 1.0f, 1.0f,-1.0f,0xFF0000FF}, { 1.0f, 1.0f,-1.0f,0xFF0000FF},
{ 1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f,-1.0f,-1.0f,0xFF0000FF},
};
这是缓冲区:
//*************************** Vertex Buffer ****************************
if( FAILED( g_pd3dDevice->CreateVertexBuffer( numberOfVertecies*sizeof(CUSTOMVERTEX),
0 /* Usage */, D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED, &g_pVB, NULL ) ) )
MessageBox(hwnd,"Vertex Buffer problem",NULL,NULL);
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
MessageBox(hwnd,"Vertex Lock Problem",NULL,NULL);
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();
这是正方形:
g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 20);
我只想看看如何对正方形进行纹理处理,以便我可以继续对整个环境进行纹理处理?
Hey guys can anybody help me with texture mapping in Direct3D C++. I have created a basic game and want to texture the enviroment. I have looked at numerous online tutorials but have had no luck so far
I am creating a custom vertex for my drawing code:
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The position for the vertex
DWORD color; // The vertex color
};
This is how I would draw a square:
CUSTOMVERTEX g_Vertices[] =
{
{-1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f, 1.0f,-1.0f,0xFF0000FF},
{ 1.0f, 1.0f,-1.0f,0xFF0000FF}, { 1.0f, 1.0f,-1.0f,0xFF0000FF},
{ 1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f,-1.0f,-1.0f,0xFF0000FF},
};
Here is the buffer:
//*************************** Vertex Buffer ****************************
if( FAILED( g_pd3dDevice->CreateVertexBuffer( numberOfVertecies*sizeof(CUSTOMVERTEX),
0 /* Usage */, D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED, &g_pVB, NULL ) ) )
MessageBox(hwnd,"Vertex Buffer problem",NULL,NULL);
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
MessageBox(hwnd,"Vertex Lock Problem",NULL,NULL);
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();
and here is the square:
g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 20);
I just want to see how to texture the square so I can go on to texturing my whole enviroment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要实现纹理映射,则必须将顶点结构更改为
创建顶点时将颜色值更改为纹理坐标(不要忘记(0,0)坐标对应于纹理贴图的左上角。
您还必须调整顶点流声明:
还必须告诉设备使用加载的纹理。
使用 D3DXCreateTextureFromFile() 加载纹理,并且如果您只想应用, 纹理(而不是纹理映射和每个顶点的颜色,如果您可以简单地应用纹理,为什么要给每个顶点一种颜色???)所以使用我编写的顶点结构而不是教程中的顶点结构。
If you want to implement texture mapping you have to change your vertex structure to
When you create the vertices change that color values to texture coordinates (dont forget that the (0,0) coordinate corresponds to the top-left corner of the texture map.
You also have to adapt your vertex stream declaration:
Load a texture using D3DXCreateTextureFromFile(). And you also have to tell the device to use the loaded texture. Check DirectX SDK Tutorial 5 to learn how to do that.
If you just want to apply a texture (and not texture mapping and color per vertex, why would you want to give each vertex a color if you can simply apply the texture???) so use the vertex struct I wrote instead of the one in the tutorial.