DirectX 9 Fehler bei der Textur



  • Hi

    Ich bin daran ein Viereck mit einer Textur zu versehen. Doch ich habe jetzt schon mehrmals geschaut, warum dieser Fehler auftritt und fand nicht woran es liegen könnte. Die Textur wird nicht auf dem Viereck angezeigt.

    Hier ist der Code:

    // main.cpp
    //
    // include the basic windows header files and the Direct3D header file
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    
    // define the screen resolution
    #define SCREEN_WIDTH  800
    #define SCREEN_HEIGHT 600
    
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
    
    // include the Direct3D Library file
    #pragma comment (lib, "d3d9.lib")
    #pragma comment (lib, "d3dx9.lib")
    
    bool bDone = false;
    
    // global declarations
    LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
    LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
    
    // function prototypes
    void initD3D(HWND hWnd); // sets up and initializes Direct3D
    void initgraphics(void);
    void render_frame(void); // renders a single frame
    void cleanD3D(void); // closes Direct3D and releases memory
    
    const DWORD D3DFVF = D3DFVF_XYZ | D3DFVF_TEX1;
    const DWORD CUSTOMFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
    
    struct D3DVERTEX
    {
    	float x, y, z;
    	float tu, tv;
    };
    
    struct CUSTOMVERTEX
    {
    	D3DXVECTOR3 position;
    	D3DCOLOR color;
    	float tu, tv;
    };
    
    // the WindowProc function prototype
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    LPDIRECT3DVERTEXBUFFER9	v_buffer;
    LPDIRECT3DVERTEXBUFFER9	vertex_buffer;
    LPDIRECT3DTEXTURE9		pTexture;
    
    // the entry point for any Windows program
    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 = WindowProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        // wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    // not needed any more
        wc.lpszClassName = "WindowClass";
    
        RegisterClassEx(&wc);
    
        hWnd = CreateWindowEx(NULL,
                              "WindowClass",
                              "Our Direct3D Program",
                              WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
                              0, 0,    // the starting x and y positions should be 0
                              SCREEN_WIDTH, SCREEN_HEIGHT,    // set the window to 640 x 480
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        ShowWindow(hWnd, nCmdShow);
    
        // set up and initialize Direct3D
        initD3D(hWnd);
    
        // enter the main loop:
    
        MSG msg;
    
        while(TRUE)
        {
            while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
    		if(KEY_DOWN(VK_ESCAPE))
    		{
    			bDone = true;
    			PostMessage(hWnd, WM_CLOSE, 0, 0);
    		}
    
            if(msg.message == WM_QUIT)
                break;
    
            render_frame();
        }
    
        // clean up DirectX and COM
        cleanD3D();
    
        return msg.wParam;
    }
    
    // this is the main message handler for the program
    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);
    }
    
    // this function initializes and prepares Direct3D for use
    void initD3D(HWND hWnd)
    {
        d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
    
        D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
    
        ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
        d3dpp.Windowed = FALSE;    // program fullscreen, not windowed
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
        d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D
        d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
        d3dpp.BackBufferWidth = SCREEN_WIDTH;    // set the width of the buffer
        d3dpp.BackBufferHeight = SCREEN_HEIGHT;    // set the height of the buffer
    
        // create a device class using this information and the info from the d3dpp stuct
        d3d->CreateDevice(D3DADAPTER_DEFAULT,
                          D3DDEVTYPE_HAL,
                          hWnd,
                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                          &d3dpp,
                          &d3ddev);
    
    	initgraphics();
    }
    
    void initgraphics()
    {
    	HRESULT hResult;
    
    		// load the texture
    
    		if(FAILED(hResult = D3DXCreateTextureFromFile(d3ddev, "weltall.jpg", &pTexture)))
    		{
    			MessageBox(NULL, "Fehler beim erstellen der Cube-Texture!", "Fehler aufgetreten",
    					   MB_OK | MB_ICONEXCLAMATION);
    		}
    
    		if(FAILED(hResult = d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
                                   D3DUSAGE_WRITEONLY,
    							   CUSTOMFVF,
                                   D3DPOOL_MANAGED,
                                   &v_buffer,
                                   NULL)))
    		{
    			MessageBox(NULL, "Fehler beim erstellen des VertexBuffers!", "Fehler aufgetreten",
    					   MB_OK | MB_ICONEXCLAMATION);
    		}
    
    		CUSTOMVERTEX * pVertices;
    
    		v_buffer->Lock(0, 0, (VOID**)&pVertices, 0); // Quad erzeugen
    
    		pVertices[0].position	= D3DXVECTOR3(-10.0f, 10.0f, 0.0f);
    		pVertices[0].color		= D3DCOLOR_XRGB(255, 255, 255);
    		pVertices[0].tu			= 0.0f;
    		pVertices[0].tv			= 0.0f;
    
    		pVertices[1].position	= D3DXVECTOR3(10.0f, 10.0f, 0.0f);
    		pVertices[1].color		= D3DCOLOR_XRGB(255, 255, 255);
    		pVertices[1].tu			= 1.0f;
    		pVertices[1].tv			= 0.0f;
    
    		pVertices[2].position	= D3DXVECTOR3(-10.0f, -10.0f, 0.0f);
    		pVertices[2].color		= D3DCOLOR_XRGB(255, 255, 255);
    		pVertices[2].tu			= 0.0f;
    		pVertices[2].tv			= 1.0f;
    
    		pVertices[3].position	= D3DXVECTOR3(10.0f, -10.0f, 0.0f);
    		pVertices[3].color		= D3DCOLOR_XRGB(255, 255, 255);
    		pVertices[3].tu			= 1.0f;
    		pVertices[3].tv			= 1.0f;
    
    		v_buffer->Unlock();
    }
    
    // this is the function used to render a single frame
    void render_frame(void)
    {
        // clear the window to a deep blue
    	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 100, 0), 1.0f, 0);
    
        d3ddev->BeginScene();    // begins the 3D scene
    
    	d3ddev->SetFVF(CUSTOMFVF);
    
        // 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
    
    	HRESULT hResult;
    
    		d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
    
    		if(FAILED(hResult = d3ddev->SetTexture(0, pTexture)))
    		{
    			MessageBox(NULL, "Fehler beim Rendern der Cube-Texture!", "Fehler aufgetreten",
    					   MB_OK | MB_ICONEXCLAMATION);
    		}
    
    		d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
    
    		// draw the cube
    		d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    
        d3ddev->EndScene();    // ends the 3D scene
    
        d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen
    }
    
    // this is the function that cleans up Direct3D and COM
    void cleanD3D(void)
    {
        d3ddev->Release(); // close and release the 3D device
        d3d->Release(); // close and release Direct3D
    	v_buffer->Release();
    	pTexture->Release();
    }
    

    Kann mir jemand helfen?

    Gruss Patrick



  • Ich habe den Fehler bereits selber herausgefunden.

    Ich musste noch Materials zu dem Quad hinzufügen.

    Tortzdem danke für das Hirnen xD


Anmelden zum Antworten