本文介绍了DirectX纹理旋转立方体不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好----

我想知道下面的代码到底在做什么错.我希望它制作一个旋转的纹理立方体,但是,当它旋转时,立方体的侧面会消失,然后重新出现.我正在关注directxtutorial.com上的教程.在本教程的后面的有关照明的部分中,他有一个可以正常工作的带刺纹理的多维数据集的示例,但是我只想要没有照明的多维数据集.我在做什么错?


这是我的"代码:

Hello----

I was wondering what exactly I am doing wrong with the code below. I would like it to make a spinning textured cube, but, when it is spinning the sides of the cube disappear and then reappear. I am following the tutorial available from directxtutorial.com. In a later section of the tutorial dealing with lighting, he has an example of a spinnign textured cube that works correctly, but I just want the cube without the lighting. What am I doing wrong?


This is "my" code:

#include.< d3d9.h >
#include``stdafx .h"
#include < windows.h >
#include < windowsx.h >
#包括< d3dx9.h >
#pragma注释(lib,"d3d9.lib")
#pragma注释(lib,"d3dx9.lib")
#defineKEY_DOWN(vk_code)(((GetAsyncKeyState(vk_code)& 0x8000)? 1:0)
#defineKEY_UP(vk_code)(((GetAsyncKeyState(vk_code)& 0x8000)?0:1)
LPDIRECT3D9d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 vbuf = NULL ;
LPDIRECT3DTEXTURE9 tex1;
避免使用initD3D(HWND hWnd) ;
void́render_frame(void);
void́cleanD3D(void);
voidinit_gfx(int r,int g,int b);
struct CUSTOMVERTEX {FLOATX, Y,Z; DWORD颜色; FLOATU,V;};
#define CUSTOMFVF(D3DFVF_XYZ || D3DFVF_NORMAL | D3DFVF_TEX1)
{-5,0,-5,0xffffffff,0,0,},
{5,-5,-5,0xffffffff,1,1,},
{-5,-5,-5,0xffffffff,1,0 ,},
5
{5,0,-5,0xffffffff,0,1,},
{-5,0,-5,0xffffffff,0,0,},
{5,0,5,0xffffffff,1,1,},
{-5,0,5,0xffffffff,1,0,},
第6
{5,0,- 5、0xffffffff,0、1,
{-5、0,-5、0xffffffff,0、0,},
{5,0,5,0xffffffff,1,1,},
{-5,0,5,0xffffffff,1,0,},
};
创建一个顶点缓冲区称为t_buffer的接口
d3ddev- > CreateVertexBuffer(24 * sizeof(CUSTOMVERTEX),
> CUSTOMFVF,
t
VOID * pVoid; //空指针
//锁定t_buffer并将顶点加载到其中
vbuf- > 锁定(0,0,(void **)& pVoid,0);
memcpy(pVoid,t_vert,sizeof(t_vert));
vbuf- > Unlock();
返回;
}
int WINAPI WinMain(HINSTANCE hInstance,
/td>
LPSTR lpCmdLine
/td>
{
.HWNDhWnd;
WNDCLASSEX wc;
ZeroMemory(& wc, sizeof(WNDCLASSEX));
wc.cbSize == sizeof (WNDCLASSEX);
wc.style == CS_HREDRAW CS_VREDRAW;
wc.lpfnWndProc (WNDPROC)WindowProc;
wc.hInstance == hInstance;
wc.hCursor == LoadCursor (NULL,IDC_ARROW) ;
)COLOR_WINDOW;
wc.lpszClassName ; = L " WindowClass; ;
RegisterClassEx( & wc);
hWnd = CreateWindowEx (NULL,
-color:rgb(247,247,247)> 300,300,300,300,
"background-color:rgb(247,247,247)"> NULL,
. -color:rgb(247、247、247)> hInstance,
ShowWindow(hWnd,nCmdShow) ;
设置和初始化Direct3D
initD3D(hWnd);
///进入主循环:
MSG msg;
while(TRUE)
{{
DWORD DWORD starting_point == GetTickCount ();
PeekMessage(& msg,NULL,0,0,PM_REMOVE))
rgb(247,247,247)>如果(( msg.message == WM_QUIT)
中断;
;
DispatchMessage(& msg);
}
if(KEY_DOWN(VK_ESCAPE) )//{
PostMessage(hWnd,WM_DESTROY,0,0);
///vbuf- > Release();
////init_gfx(40,78,50);
同时((((GetTickCount()-starting_point))< 10 );
}}
//清理DirectX和COM
cleanD3D();
返回msg.wParam;
}
LRESULTCALLBACKWindowProc(HWND,hWnd,UINT消息,WPARAM wParam,LPARAM lParam)
{
.
{
案例WM_DESTROY:
{
PostQuitMessage(0);
返回0;
}中断;
返回DefWindowProc(hWnd,消息,wParam,lParam);
}
#include "stdafx.h" 
#include <windows.h> 
#include <windowsx.h> 
#include <d3d9.h> 
#include <d3dx9.h> 
 
#pragma comment (lib,"d3d9.lib") 
#pragma comment (lib,"d3dx9.lib") 
 
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1: 0) 
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0: 1) 
 
LPDIRECT3D9 d3d; 
LPDIRECT3DDEVICE9 d3ddev; 
LPDIRECT3DVERTEXBUFFER9 vbuf=NULL
LPDIRECT3DTEXTURE9 tex1; 
 
void initD3D(HWND hWnd); 
void render_frame(void); 
void cleanD3D(void); 
void init_gfx(int r, int g, int b); 
 
struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR; FLOAT U,V;}; 
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1) 
 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 
 
void initD3D(HWND hWnd) { 
    d3d=Direct3DCreate9(D3D_SDK_VERSION); 
 
    D3DPRESENT_PARAMETERS d3dpp; 
 
    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.Windowed=true
    d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD
    d3dpp.hDeviceWindow=hWnd
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8
    d3dpp.BackBufferWidth = 640
    d3dpp.BackBufferHeight = 480
    d3dpp.EnableAutoDepthStencil = TRUE
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16
 
    d3d->CreateDevice( 
        D3DADAPTER_DEFAULT, 
        D3DDEVTYPE_HAL, 
        hWnd, 
        D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
        &d3dpp, 
        &d3ddev); 
 
     
 
    init_gfx(70,68,40); 
 
    d3ddev->SetRenderState(D3DRS_LIGHTING,FALSE); 
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); 
     
    return; 
 
void render_frame(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(); 
 
    // select which vertex format we are using 
        d3ddev->SetFVF(CUSTOMFVF); 
 
        D3DXMATRIX matRotateY;    // a matrix to store the rotation information 
 
    static float index = 0.0f; index+=0.03f;    // an ever-increasing float value 
 
    // build a matrix to rotate the model based on the increasing float value 
    D3DXMatrixRotationY(&matRotateY, index); 
 
    // tell Direct3D about our matrix 
    d3ddev->SetTransform(D3DTS_WORLD, &matRotateY); 
 
        D3DXMATRIX matView; 
 
        D3DXMatrixLookAtLH(&matView, 
            &D3DXVECTOR3 (0.0f, 8.0f, 35.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); 
 
        D3DXMATRIX matProjection; 
 
        D3DXMatrixPerspectiveFovLH(&matProjection, 
            D3DXToRadian(45), 
            (FLOAT)640/(FLOAT)480, 
            1.0f, 
            100.0f); 
 
        d3ddev->SetTransform(D3DTS_PROJECTION,&matProjection); 
 
        // select the vertex buffer to display 
        d3ddev->SetStreamSource(0, vbuf, 0, sizeof(CUSTOMVERTEX)); 
         
        d3ddev->SetTexture(0,tex1); 
        // copy the vertex buffer to the back buffer 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2); 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2); 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2); 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2); 
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);  
 
    d3ddev->EndScene(); 
 
    d3ddev->Present(NULL,NULL,NULL,NULL); 
    return; 
 
 
void cleanD3D(void) 
    vbuf->Release(); 
    tex1->Release(); 
    d3ddev->Release(); 
    d3d->Release(); 
     
 
    return; 
 
void init_gfx(int r, int g, int b) 
    D3DXCreateTextureFromFile(d3ddev, L"Metal.png",&tex1); 
    // create the vertices using the CUSTOMVERTEX struct 
    /*CUSTOMVERTEX t_vert[] = 
    { 
        { 320.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(r, g, b), }, 
        { 520.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(r, g, b), }, 
        { 120.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(r, g, b), }, 
    };*/ 
 
    // create the vertices using the CUSTOMVERTEX struct 
    CUSTOMVERTEX t_vert[] = 
    { 
        //{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(r, g, b), }, 
        //{ 0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(r, g, b), }, 
        //{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(r, g, b), }, 
            // side 1 
        {5, 0, -5, 0xffffffff, 0, 1,}, 
        {-5, 0, -5, 0xffffffff, 0, 0,}, 
        {5, 0, 5, 0xffffffff, 1, 1,}, 
        {-5, 0, 5, 0xffffffff, 1, 0,}, 
 
        // side 2 
        {5, -5, -5, 0xffffffff, 0, 1,}, 
        {-5, -5, -5, 0xffffffff, 0, 0,}, 
        {5, -5, 5, 0xffffffff, 1, 1,}, 
        {-5, -5, 5, 0xffffffff, 1, 0,}, 
 
        // side 3 
        {5, 0, 5, 0xffffffff, 0, 1,}, 
        {-5, 0, 5, 0xffffffff, 0, 0,}, 
        {5, -5, 5, 0xffffffff, 1, 1,}, 
        {-5, -5, 5, 0xffffffff, 1, 0,}, 
 
        // side 4 
        {5, 0, -5, 0xffffffff, 0, 1,}, 
        {-5, 0, -5, 0xffffffff, 0, 0,}, 
        {5, -5, -5, 0xffffffff, 1, 1,}, 
        {-5, -5, -5, 0xffffffff, 1, 0,}, 
 
        // side 5 
        {5, 0, -5, 0xffffffff, 0, 1,}, 
        {-5, 0, -5, 0xffffffff, 0, 0,}, 
        {5, 0, 5, 0xffffffff, 1, 1,}, 
        {-5, 0, 5, 0xffffffff, 1, 0,}, 
 
        // side 6 
        {5, 0, -5, 0xffffffff, 0, 1,}, 
        {-5, 0, -5, 0xffffffff, 0, 0,}, 
        {5, 0, 5, 0xffffffff, 1, 1,}, 
        {-5, 0, 5, 0xffffffff, 1, 0,}, 
    }; 
 
    // create a vertex buffer interface called t_buffer 
    d3ddev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX), 
                               0, 
                               CUSTOMFVF, 
                               D3DPOOL_MANAGED, 
                               &vbuf, 
                               NULL); 
 
    VOID* pVoid;    // a void pointer 
 
    // lock t_buffer and load the vertices into it 
    vbuf->Lock(0, 0, (void**)&pVoid, 0); 
    memcpy(pVoid, t_vert, sizeof(t_vert)); 
    vbuf->Unlock(); 
 
    return; 
 
int WINAPI WinMain(HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance, 
                   LPSTR lpCmdLine, 
                   int nCmdShow) 
    HWND hWnd; 
    WNDCLASSEX wc; 
 
    ZeroMemory(&wc, sizeof(WNDCLASSEX)); 
 
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = (WNDPROC)WindowProc; 
    wc.hInstance = hInstance; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW; 
    wc.lpszClassName = L"WindowClass"
 
    RegisterClassEx(&wc); 
 
    hWnd = CreateWindowEx(NULL, 
                          L"WindowClass", 
                          L"Our First Direct3D Program", 
                          WS_OVERLAPPEDWINDOW, 
                          300, 300, 
                          640, 480, 
                          NULL, 
                          NULL, 
                          hInstance, 
                          NULL); 
 
    ShowWindow(hWnd, nCmdShow); 
 
    // set up and initialize Direct3D 
    initD3D(hWnd); 
 
    // enter the main loop: 
 
    MSG msg; 
 
    while(TRUE) 
    { 
        DWORD starting_point = GetTickCount(); 
 
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { 
            if (msg.message == WM_QUIT) 
                break; 
 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 
 
        render_frame(); 
         
        if(KEY_DOWN(VK_ESCAPE)) //{ 
            PostMessage(hWnd, WM_DESTROY, 0, 0); 
        //} else if (KEY_DOWN(VK_UP)) { 
            //vbuf->Release(); 
            //init_gfx(40,78,50); 
        //} 
 
        while ((GetTickCount() - starting_point) < 10); 
    } 
 
    // clean up DirectX and COM 
    cleanD3D(); 
 
    return msg.wParam; 
 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
    switch(message) 
    { 
        case WM_DESTROY: 
            { 
                PostQuitMessage(0); 
                return 0; 
            } break; 
    } 
 
    return DefWindowProc (hWnd, message, wParam, lParam); 

推荐答案


这篇关于DirectX纹理旋转立方体不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 00:28