游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3335|回复: 6

编写纹理时,遇到困难了,请求帮忙

[复制链接]

12

主题

41

帖子

60

积分

注册会员

Rank: 2

积分
60
发表于 2005-2-9 05:36:00 | 显示全部楼层 |阅读模式
代码如下:
#include <d3dx8.h>

LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices

LPDIRECT3DTEXTURE8 m_pTexture;

struct CUSTOMVERTEX
{
    FLOAT x, y, z;
    DWORD colour;
        FLOAT tu, tv;
};


//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}

//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

HRESULT InitialiseD3D(HWND hWnd)
{
    //First of all, create the main D3D object. If it is created successfully we
    //should get a pointer to an IDirect3D8 interface.
    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
    if(g_pD3D == NULL)
    {
        return E_FAIL;
    }

    //Get the current display mode
    D3DDISPLAYMODE d3ddm;
    if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    {
        return E_FAIL;
    }

    //Create a structure to hold the settings for our device
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    //Fill the structure.
    //We want our program to be windowed, and set the back buffer to a format
    //that matches our current display mode
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
    d3dpp.BackBufferFormat = d3ddm.Format;

    //Create a Direct3D device.
    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp,     
                                   &g_pD3DDevice)))
    {
        return E_FAIL;
    }
   
    //Turn on back face culling.
    //This is because we want to hide the back of our polygons
    g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

    //Turn off lighting because we are specifying that our vertices have colour
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

       
    return S_OK;
}


HRESULT InitialiseVertexBuffer()
{
    VOID* pVertices;


    //Store each point of the cube together with it's colour
    //Make sure that the points of a polygon are specified in a clockwise direction,
    //this is because anti-clockwise faces will be culled
    //We will use a three triangle strips to render these polygons
    //(Top, Sides, Bottom).
    CUSTOMVERTEX cvVertices[] =
    {
        //Top Face
        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f, 1.0f,}, //Vertex 0 - Blue
        {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0), 0.0f, 0.0f,}, //Vertex 1 - Red
        {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f, 1.0f,}, //Vertex 2 - Red
        {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f, 0.0f,}, //Vertex 3 - Green

        //Face 1
        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f, 1.0f,}, //Vertex 4 - Red
        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f, 0.0f,}, //Vertex 5 - Blue
        {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,}, //Vertex 6 - Green
        {5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f, 0.0f,}, //Vertex 7 - Red

        //Face 2
        {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f, 1.0f,}, //Vertex 8 - Blue
        {5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),0.0f, 0.0f,}, //Vertex 9 - Green
        
        //Face 3
        {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f, 1.0f,}, //Vertex 10 - Green
        {-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),1.0f, 0.0f,}, //Vertex 11 - Red

        //Face 4
        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0),0.0f, 1.0f,}, //Vertex 12 - Red
        {-5.0f, 5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,}, //Vertex 13 - Blue

        //Bottom Face
        {5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0),0.0f, 1.0f,}, //Vertex 14 - Green
        {5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),0.0f, 0.0f,}, //Vertex 15 - Blue
        {-5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,}, //Vertex 16 - Red
        {-5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),1.0f, 0.0f,}, //Vertex 17 - Green
    };

    //Create the vertex buffer from our device.
    if(FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX),
                                               0, D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, &g_pVertexBuffer)))
    {
        return E_FAIL;
    }


    //Get a pointer to the vertex buffer vertices and lock the vertex buffer
    if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
    {
        return E_FAIL;
    }

    //Copy our stored vertices values into the vertex buffer
    memcpy(pVertices, cvVertices, sizeof(cvVertices));

    //Unlock the vertex buffer
    g_pVertexBuffer->Unlock();


   

    return S_OK;
}



void SetupRotation()
{
    //Here we will rotate our world around the x, y and z axis.
    D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;
   
    //Create the transformation matrices
    D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);
    D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);
    D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);

    //Combine the transformations by multiplying them together
    D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
    D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);

    //Apply the tansformation
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}

void SetupCamera()
{
    //Here we will setup the camera.
    //The camera has three settings: "Camera Position", "Look at Position" and
    //"Up Direction"
    //We have set the following:
    //Camera Position: (0, 0, -30)
    //Look at Position: (0, 0, 0)
    //Up direction: Y-Axis.
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-30.0f), //Camera Position
                                 &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
    g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}


void SetupPerspective()
{
    //Here we specify the field of view, aspect ration and near and
    //far clipping planes.
    D3DXMATRIX matProj;
        static float i= 2.0f;
        static int flag = 0;
        if (flag == 0)
        {
                i += 0.1f;
        }
        else if (flag ==1)
        {
                i -= 0.1f;
        }
       
       
                    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/i,1.0f, 1.0f, 500.0f);

                if (i > 5.0f)
                {
                         
                         //i=2.0f;
                        flag =1;
                }
                else if (i<2.0f)
                {
                        flag =0;
                }
       
    g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}



void Render()
{
    if(g_pD3DDevice == NULL)
    {
        return;
    }

    //Clear the backbuffer to black
    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
   
    //Begin the scene
    g_pD3DDevice->BeginScene();
   

    //Setup the rotation, camera, and perspective matrices
    SetupRotation();
    SetupCamera();
    SetupPerspective();
////////////////////

        D3DXCreateTextureFromFile(g_pD3DDevice, "12.bmp",&m_pTexture);
        if(m_pTexture != NULL)
        {
    //A texture has been set. We don't want to blend our texture with
    //the colours of our vertices, so use D3DTOP_SELECTARG1
    g_pD3DDevice->SetTexture(0, m_pTexture);
    g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
        }
        else
        {
                //No texture has been set. So we will disable texture rendering.
                g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
        }

    //Rendering our objects
    g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
    g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top
    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides
    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom



    //End the scene
    g_pD3DDevice->EndScene();
   
    //Filp the back and front buffers so that whatever has been rendered on the
    //back buffer will now be visible on screen (front buffer).
    g_pD3DDevice-&gtresent(NULL, NULL, NULL, NULL);
}


void CleanUp()
{
        SafeRelease(m_pTexture);
        SafeRelease(g_pVertexBuffer);
    SafeRelease(g_pD3DDevice);
    SafeRelease(g_pD3D);

}

void GameLoop()
{
    //Enter the game loop
    MSG msg;
    BOOL fMessage;

    PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
   
    while(msg.message != WM_QUIT)
    {
        fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

        if(fMessage)
        {
            //Process message
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            //No message to process, so render the current scene
            Render();
        }

    }
}


//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        break;
        case WM_KEYUP:
            switch (wParam)
            {
                case VK_ESCAPE:
                    //User has pressed the escape key, so quit
                    DestroyWindow(hWnd);
                    return 0;
                break;
            }
        break;

    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
    //Register the window class
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
                     GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                     "DX Project 3", NULL};
    RegisterClassEx(&wc);

    //Create the application's window
    HWND hWnd = CreateWindow("DX Project 3", "www.andypike.com: Tutorial 3",
                              WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL);

    //Initialize Direct3D
    if(SUCCEEDED(InitialiseD3D(hWnd)))
    {
        //Show our window
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);

        //Initialize Vertex Buffer
        if(SUCCEEDED(InitialiseVertexBuffer()))
        {
            //Start game running: Enter the game loop
            GameLoop();
        }
    }
   
    CleanUp();

    UnregisterClass("DX Project 3", wc.hInstance);
   
    return 0;
}

错误提示:
--------------------Configuration: d3d8 - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol _D3DXCreateTextureFromFileA@12
Debug/d3d8.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

d3d8.exe - 2 error(s), 0 warning(s)

12

主题

41

帖子

60

积分

注册会员

Rank: 2

积分
60
 楼主| 发表于 2005-2-9 13:59:00 | 显示全部楼层

Re: 编写纹理时,遇到困难了,请求帮忙

帮忙啊

0

主题

275

帖子

676

积分

高级会员

Rank: 4

积分
676
发表于 2005-2-9 15:57:00 | 显示全部楼层

Re: Re: 编写纹理时,遇到困难了,请求帮忙

tzyux: Re: 编写纹理时,遇到困难了,请求帮忙

帮忙啊


把d3dx9.lib或是d3dx8.lib 加入??
看你使用的是哪??版本

17

主题

454

帖子

470

积分

中级会员

Rank: 3Rank: 3

积分
470
发表于 2005-2-14 01:44:00 | 显示全部楼层

Re:编写纹理时,遇到困难了,请求帮忙

好长.看不完.不过应该是,d3dx库没加进去..严重的基础不扎实...不是不扎实,而是太不扎实

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2005-2-14 14:51:00 | 显示全部楼层

Re:编写纹理时,遇到困难了,请求帮忙

楼上的话说的太重了吧。不过这个错误出现了不能解决的话,是不是对那个什么编译环境太不熟悉了

20

主题

398

帖子

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2005-2-14 17:16:00 | 显示全部楼层

Re:编写纹理时,遇到困难了,请求帮忙

“无法解释的外部符号 _D3DXCreateTextureFromFileA@12”
也就是说你定义并使用了函数D3DXCreateTextureFromFile(),但是编译程序找不到该函数的函数体。有两种可能性,一是你没有将d3dx库加载到编译环境中,二是directX的版本不匹配。

0

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
QQ
发表于 2008-12-5 18:56:00 | 显示全部楼层

Re:编写纹理时,遇到困难了,请求帮忙

包含d3dx9.lib链接
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 18:49

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表