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

标签 c++ visual-c++ winapi directx-9

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

// 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
}

最佳答案

查看三角形的纹理坐标:三角形 (4, 0, 6) 的纹理坐标为 (0, 0), (0, 0), (0, 1),这不会向右看!您可能必须创建仅具有不同纹理坐标的额外顶点才能实现您想要的效果。

关于c++ - 应用纹理另一个问题directx c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3809589/

相关文章:

c++ - Cc 套接字编程 select() 的第一个参数

c++ - 将头文件编译成 DLL

.net - C# WMI 中的软盘噪音 - Win32_LogicalDisk 类

java - JNA:将结构体指针作为 LPARAM 传递给 User32.dll 的 SendMessage 函数

c++ - 如何将 libssh 的 SCP 与 ssh::Session 一起使用

c++ - 如何在 Eclipse CDT 中切换调试和发布 Makefile?

c++ - "my_ifstream >> my_char"记录在哪里?

c++ - MSVC2015 中的固定基地址

c++ - 将 PUNICODE_STRING 写入文件,得到垃圾数据

c++ - 如何找出哪些包含的文件不是当前 VC++ 解决方案的一部分