应用纹理另一个问题 directx c++

发布于 2024-09-24 20:32:35 字数 7452 浏览 11 评论 0原文

好吧,我一切正常。纹理仅适用于立方体的两侧,其他面不会显示正确的纹理。
这是我的代码

// Monopoly.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Monopoly.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
HWND hWnd;                                      // current window
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
LPDIRECT3D9 d3d;                                // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;                       // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 buffer= NULL;
LPDIRECT3DINDEXBUFFER9 i_buffer;
#define SCREEN_HEIGHT   600
#define SCREEN_WIDTH    800
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
float z = 0.0f;
float y = 0.0f;
float x = 0.0f;
float rz = 0.0f;
float ry = 0.0f;
float rx = 0.0f;


// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

VOID InitGraphic();
VOID InitMatrices();

// FVF structure
struct CUSTOMVERTEX
{
    FLOAT x, y, z;    // from the D3DFVF_XYZRHW flag
    DWORD color;    // from the D3DFVF_DIFFUSE flag
    FLOAT tx, ty;
};

IDirect3DTexture9 *g_texture=NULL;

// Initilizing direct3D 9
void InitD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

    InitGraphic();

    D3DXCreateTextureFromFile(d3ddev,   //Direct3D Device
                                _T("image.png"),       //File Name
                                &g_texture);    //Texture handle

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);    // turn off culling
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
    d3ddev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);   //Ignored

    d3ddev->SetTexture(0,g_texture);
}


//render a single frame
void RenderFrame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    d3ddev->SetFVF(CUSTOMFVF);

    InitMatrices();

    // set the world transform

    D3DXMATRIX matTran;    // a matrix to store the rotation for each triangle
    D3DXMATRIX matRotz;
    D3DXMATRIX matRoty;
    D3DXMATRIX matRotx;
    D3DXMatrixTranslation(&matTran, x, y, z);   
    D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz));
    D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry));
    D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx));

    d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx));    // set the world transform

     // select the vertex buffer to display
    d3ddev->SetStreamSource(0, buffer, 0, sizeof(CUSTOMVERTEX));
    d3ddev->SetIndices(i_buffer);

    // draw the Hypercraft
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); 

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

// cleans up Direct3D and COM
void CleanD3D(void)
{
    buffer->Release();
    i_buffer->Release();
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
    g_texture->Release();
    g_texture=NULL;
}

VOID InitGraphic()
{
    CUSTOMVERTEX vertices[] =
    {
        { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f, },
        { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0), 1.0, 0.0, },
        { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(255, 0, 0), 0.0, 1.0, },
        { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, },
        { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0, 0.0, },
        { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), 1.0, 0.0, },
        { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), 0.0, 1.0, },
        { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, },
    };

    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    buffer->Unlock();

    // create the indices using an int array
    short indices[] =
    {
        0, 1, 2,    // side 1
        2, 1, 3,
        4, 0, 6,    // side 2
        6, 0, 2,
        7, 5, 6,    // side 3
        6, 5, 4,
        3, 1, 7,    // side 4
        7, 1, 5,
        4, 5, 0,    // side 5
        0, 5, 1,
        3, 7, 2,    // side 6
        2, 7, 6,
    };

    // create an index buffer interface called i_buffer
    d3ddev->CreateIndexBuffer(36*sizeof(short),
                              0,
                              D3DFMT_INDEX16,
                              D3DPOOL_MANAGED,
                              &i_buffer,
                              NULL);

    // lock i_buffer and load the indices into it
    i_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    i_buffer->Unlock();
}

// Create metrices
VOID InitMatrices()
{
    // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,   // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
}

ok i got everything working. the texture only applys for two sides only of the cube the other sides won't display the correct texture.
here is my code

// Monopoly.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Monopoly.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
HWND hWnd;                                      // current window
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
LPDIRECT3D9 d3d;                                // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;                       // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 buffer= NULL;
LPDIRECT3DINDEXBUFFER9 i_buffer;
#define SCREEN_HEIGHT   600
#define SCREEN_WIDTH    800
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
float z = 0.0f;
float y = 0.0f;
float x = 0.0f;
float rz = 0.0f;
float ry = 0.0f;
float rx = 0.0f;


// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

VOID InitGraphic();
VOID InitMatrices();

// FVF structure
struct CUSTOMVERTEX
{
    FLOAT x, y, z;    // from the D3DFVF_XYZRHW flag
    DWORD color;    // from the D3DFVF_DIFFUSE flag
    FLOAT tx, ty;
};

IDirect3DTexture9 *g_texture=NULL;

// Initilizing direct3D 9
void InitD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

    InitGraphic();

    D3DXCreateTextureFromFile(d3ddev,   //Direct3D Device
                                _T("image.png"),       //File Name
                                &g_texture);    //Texture handle

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);    // turn off culling
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
    d3ddev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);   //Ignored

    d3ddev->SetTexture(0,g_texture);
}


//render a single frame
void RenderFrame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    d3ddev->SetFVF(CUSTOMFVF);

    InitMatrices();

    // set the world transform

    D3DXMATRIX matTran;    // a matrix to store the rotation for each triangle
    D3DXMATRIX matRotz;
    D3DXMATRIX matRoty;
    D3DXMATRIX matRotx;
    D3DXMatrixTranslation(&matTran, x, y, z);   
    D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz));
    D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry));
    D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx));

    d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx));    // set the world transform

     // select the vertex buffer to display
    d3ddev->SetStreamSource(0, buffer, 0, sizeof(CUSTOMVERTEX));
    d3ddev->SetIndices(i_buffer);

    // draw the Hypercraft
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); 

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

// cleans up Direct3D and COM
void CleanD3D(void)
{
    buffer->Release();
    i_buffer->Release();
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
    g_texture->Release();
    g_texture=NULL;
}

VOID InitGraphic()
{
    CUSTOMVERTEX vertices[] =
    {
        { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f, },
        { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0), 1.0, 0.0, },
        { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(255, 0, 0), 0.0, 1.0, },
        { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, },
        { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0, 0.0, },
        { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), 1.0, 0.0, },
        { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), 0.0, 1.0, },
        { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, },
    };

    // create a vertex buffer interface called v_buffer
    d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    buffer->Unlock();

    // create the indices using an int array
    short indices[] =
    {
        0, 1, 2,    // side 1
        2, 1, 3,
        4, 0, 6,    // side 2
        6, 0, 2,
        7, 5, 6,    // side 3
        6, 5, 4,
        3, 1, 7,    // side 4
        7, 1, 5,
        4, 5, 0,    // side 5
        0, 5, 1,
        3, 7, 2,    // side 6
        2, 7, 6,
    };

    // create an index buffer interface called i_buffer
    d3ddev->CreateIndexBuffer(36*sizeof(short),
                              0,
                              D3DFMT_INDEX16,
                              D3DPOOL_MANAGED,
                              &i_buffer,
                              NULL);

    // lock i_buffer and load the indices into it
    i_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    i_buffer->Unlock();
}

// Create metrices
VOID InitMatrices()
{
    // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,   // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2024-10-01 20:32:35

看一下三角形的纹理坐标:三角形 (4, 0, 6) 的纹理坐标为 (0, 0)、(0, 0)、(0, 1),这看起来不正确!您可能需要创建仅具有不同纹理坐标的其他顶点才能实现您想要的效果。

Take a look at the texture coordinates for your triangles: the triangle (4, 0, 6) has texture coordinates (0, 0), (0, 0), (0, 1), which isn't going to look right! You may have to create additional vertices with merely different texture coordinates to achieve what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文