游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2404|回复: 8

为什么我编的地形却是一条直线?

[复制链接]

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
发表于 2007-6-3 09:24:00 | 显示全部楼层 |阅读模式
我把代码贴上来,诸位帮忙运行一下看看是什么原因.

  1. #include <Windows.h>
  2. #include <mmsystem.h>
  3. #include <d3dx9.h>
  4. #pragma warning( disable : 4996 ) // disable deprecated warning
  5. #include <strsafe.h>
  6. #pragma warning( default : 4996 )

  7. #include "Camera8.h"
  8. #include "Terrain8.h"
  9. //-----------------------------------------------------------------------------
  10. // Global variables
  11. //-----------------------------------------------------------------------------
  12. LPDIRECT3D9             g_pD3D           = NULL; // Used to create the D3DDevice
  13. LPDIRECT3DDEVICE9       g_pd3dDevice     = NULL; // Our rendering device

  14. ID3DXMesh* mesh = 0;
  15. const int Width = 800;
  16. const int Height = 600;

  17.    
  18.     POINT          m_ptLastMouse;      // position of last mouse point

  19.         float Z=0.0f;
  20.         D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f);//( .5f, .55f, -.2f );//+Z
  21.         float X=0.0f;
  22.     D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );//( .5f+X,  .125f, .5f+Z );
  23.     D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
  24.     D3DXMATRIXA16 g_matWorld;
  25. Terrain* g_pTerrain;
  26. Camera* theCamera = new Camera(vEyePt,vLookatPt,vUpVec);
  27.        
  28. //-----------------------------------------------------------------------------
  29. // Name: InitD3D()
  30. // Desc: Initializes Direct3D
  31. //-----------------------------------------------------------------------------
  32. HRESULT InitD3D( HWND hWnd )
  33. {
  34.     // Create the D3D object.
  35.     if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  36.         return E_FAIL;

  37.     // Set up the structure used to create the D3DDevice. Since we are now
  38.     // using more complex geometry, we will create a device with a zbuffer.
  39.     D3DPRESENT_PARAMETERS d3dpp;
  40.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  41.     d3dpp.Windowed = TRUE;
  42.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  43.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  44.     d3dpp.EnableAutoDepthStencil = TRUE;
  45.     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

  46.     // Create the D3DDevice
  47.     if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  48.                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  49.                                       &d3dpp, &g_pd3dDevice ) ) )
  50.     {
  51.         return E_FAIL;
  52.     }

  53.     // Turn on the zbuffer
  54.     g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

  55.     // Turn on ambient lighting
  56.     g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
  57.     g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);

  58. //    m_pCamera1 = new Camera();
  59.         g_pTerrain = new Terrain(g_pd3dDevice,21, 21, 10);//, 15.0
  60. //    m_pTerrain1->SetTexture("Grass.bmp");
  61.     D3DXCreateTeapot(g_pd3dDevice,&mesh,0);
  62.         return S_OK;
  63. }


  64. //-----------------------------------------------------------------------------
  65. // Name: Cleanup()
  66. // Desc: Releases all previously initialized objects
  67. //-----------------------------------------------------------------------------
  68. VOID Cleanup()
  69. {
  70.     delete(theCamera);
  71.         mesh->Release();
  72.         mesh = 0;
  73.     delete(g_pTerrain);

  74.     if( g_pd3dDevice != NULL )
  75.         g_pd3dDevice->Release();

  76.     if( g_pD3D != NULL )
  77.         g_pD3D->Release();

  78. //SafeDelete(g_pTerrain);
  79. //SafeDelete(m_pCamera1);
  80. }


  81. void OnBegin( int nX, int nY)
  82.     {
  83. //       m_bDrag = true;
  84.                 //   m_qDown = m_qNow;
  85.         //m_vDownPt = ScreenToVector( (float)nX, (float)nY );
  86.     m_ptLastMouse.x =nX;
  87.         m_ptLastMouse.y =nY;
  88.         }

  89. //-----------------------------------------------------------------------------
  90. // Name: SetupMatrices()
  91. // Desc: Sets up the world, view, and projection transform matrices.
  92. //-----------------------------------------------------------------------------
  93. VOID SetupMatrices()
  94. {
  95.     //D3DXMatrixTranslation( &g_matWorld, -2.56f,0.0f,0.0f);

  96.         D3DXMatrixIdentity(&g_matWorld);
  97.     g_pd3dDevice->SetTransform( D3DTS_WORLD,&g_matWorld );

  98.     D3DXMATRIXA16 matView;
  99.     D3DXMatrixLookAtLH( &matView,&vEyePt, &vLookatPt , &vUpVec );
  100.     g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );


  101.         D3DXMATRIXA16 matProj;
  102.     D3DXMatrixPerspectiveFovLH( &matProj,D3DX_PI/4,  1.250f, 1.0f, 1000.0f);
  103.     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  104. }


  105. //-----------------------------------------------------------------------------
  106. // Name: Render()
  107. // Desc: Draws the scene
  108. //-----------------------------------------------------------------------------
  109. VOID Render()
  110. {
  111.     // Clear the backbuffer and the zbuffer
  112.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  113.                          D3DCOLOR_XRGB(250,250,250), 1.0f, 0 );
  114.    
  115.     // Begin the scene
  116.     if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
  117.     {
  118.         // Setup the world, view, and projection matrices
  119.         SetupMatrices();

  120.         // Meshes are divided into subsets, one for each material. Render them in
  121.             mesh->DrawSubset(0);
  122.        // {
  123.             g_pTerrain->Render();
  124.        // }
  125.         // End the scene
  126.         g_pd3dDevice->EndScene();
  127.     }

  128.     // Present the backbuffer contents to the display
  129.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  130. }


  131. //-----------------------------------------------------------------------------
  132. // Name: MsgProc()
  133. // Desc: The window's message handler
  134. //-----------------------------------------------------------------------------
  135. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  136. {
  137.         // Current mouse position
  138.     int iMouseX = (short)LOWORD(lParam);
  139.     int iMouseY = (short)HIWORD(lParam);

  140.         switch( msg )
  141.     {
  142.         case WM_DESTROY:
  143.             Cleanup();
  144.             PostQuitMessage( 0 );
  145.             return 0;
  146.         
  147.                         case WM_RBUTTONDOWN:
  148.                         case WM_LBUTTONDOWN:
  149.                 SetCapture( hWnd );
  150.                 OnBegin( iMouseX, iMouseY);//
  151.                 return TRUE;

  152. /*            case WM_LBUTTONUP:
  153.                 ReleaseCapture();
  154.                 return TRUE;

  155.             case WM_CAPTURECHANGED:
  156.                 if( (HWND)lParam != hWnd )
  157.                 {
  158.                     ReleaseCapture();
  159.                 }
  160.                 return TRUE;
  161. */
  162.             case WM_MOUSEMOVE:
  163. /*              if( MK_LBUTTON&wParam )
  164.                 {
  165.                   //D3DXMatrixRotationY( &g_matWorld, timeGetTime()/1000.0f );
  166.                                    OnMove( iMouseX, iMouseY);
  167.                   // D3DXMatrixRotationZ( &g_matWorld, Angle);
  168.                                    m_ptLastMouse.x = iMouseX;
  169.                    m_ptLastMouse.y = iMouseY;
  170.                                   
  171.                                 }
  172. */

  173.                 //ADD 摄象机的代码
  174.         if( MK_RBUTTON&wParam )//case WM_RBUTTONDBLCLK:
  175.         {
  176.                         // Compute the drag rectangle in screen coord.
  177.             POINT ptCursor = { (short)LOWORD(lParam), (short)HIWORD(lParam) };

  178.             // Update member var state
  179.             if( ( msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK ))// && PtInRect( &g_Camera.m_rcDrag, ptCursor ) )
  180. //               { g_Camera.m_bMouseRButtonDown = true; g_Camera.m_nCurrentButtonMask |= MOUSE_RIGHT_BUTTON; }

  181.             // Capture the mouse, so if the mouse button is
  182.             // released outside the window, we'll get the WM_LBUTTONUP message
  183.             SetCapture(hWnd);
  184.            // GetCursorPos( &g_Camera.m_ptLastMousePosition );
  185.         /*    g_Camera.GetInput(iMouseX, iMouseY );
  186.                         vLookatPt = g_Camera.m_vLookAt;

  187.                                    m_ptLastMouse.x = iMouseX;
  188.                    m_ptLastMouse.y = iMouseY;
  189.            */
  190. Z = (iMouseY-m_ptLastMouse.x)/10.0f;//
  191. X = (iMouseX-m_ptLastMouse.x)/10.0f;//
  192. //vEyePt = D3DXVECTOR3(0.0f, 3.0f,-5.0f+Z);
  193. //vLookatPt = D3DXVECTOR3( 0.0f+X, 0.0f, 0.0f+Z );//
  194.   theCamera->yaw(Z) ;//
  195. // TheCamera.walk(X) ;
  196.   D3DXMATRIX V;
  197.   theCamera->getViewMatrix(&V);
  198.   g_pd3dDevice->SetTransform(D3DTS_VIEW,&V);
  199.                
  200.                 }

  201.                return TRUE;


  202.         case WM_RBUTTONUP:   
  203.         {
  204.                ReleaseCapture();
  205.             break;
  206.         }

  207.    
  208.     }

  209.     return DefWindowProc( hWnd, msg, wParam, lParam );
  210. }


  211. //-----------------------------------------------------------------------------
  212. // Name: WinMain()
  213. // Desc: The application's entry point
  214. //-----------------------------------------------------------------------------
  215. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  216. {
  217.     // Register the window class
  218.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  219.                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  220.                       "Terrain8", NULL };
  221.     RegisterClassEx( &wc );

  222.     // Create the application's window
  223.     HWND hWnd = CreateWindow( "Terrain8", "Terrain8",
  224.                               WS_OVERLAPPEDWINDOW, 0, 0, 800, 800,
  225.                               NULL, NULL, wc.hInstance, NULL );

  226.     // Initialize Direct3D
  227.     if( SUCCEEDED( InitD3D( hWnd ) ) )
  228.     {
  229.             // Show the window
  230.             ShowWindow( hWnd, SW_SHOWDEFAULT );
  231.             UpdateWindow( hWnd );

  232.             // Enter the message loop
  233.             MSG msg;
  234.             ZeroMemory( &msg, sizeof(msg) );
  235.             while( msg.message!=WM_QUIT )
  236.             {
  237.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  238.                 {
  239.                     TranslateMessage( &msg );
  240.                     DispatchMessage( &msg );
  241.                 }
  242.                 else
  243.                     Render();
  244.             }

  245.     }

  246.     UnregisterClass( "Terrain8", wc.hInstance );
  247.     return 0;
  248. }
复制代码

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 09:25:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

#include "Camera8.h"
Camera::Camera(D3DXVECTOR3 pos,D3DXVECTOR3 look,D3DXVECTOR3 up)
{
_pos = pos;
_look = look;
_up = up;
_right = D3DXVECTOR3(1.0,0.0,0.0);
};
Camera::~Camera()
{};
void Camera::getViewMatrix(D3DXMATRIX* v)//
{
        D3DXVec3Normalize(&_look,&_look);
        D3DXVec3Cross(&_up,&_look,&_right);
        D3DXVec3Normalize(&_up,&_up);
        D3DXVec3Cross(&_right,&_up,&_look);
    D3DXVec3Normalize(&_right,&_right);

        float x=-D3DXVec3Dot(&_right,&_pos);
        float y=-D3DXVec3Dot(&_up,&_pos);
        float z=-D3DXVec3Dot(&_look,&_pos);

        (*v)(0,0)=_right.x;(*v)(0,1)=_up.x;(*v)(0,2)=_look.x;(*v)(0,3)=0.0f;
    (*v)(1,0)=_right.y;(*v)(1,1)=_up.y;(*v)(1,2)=_look.y;(*v)(1,3)=0.0f;
    (*v)(2,0)=_right.z;(*v)(2,1)=_up.z;(*v)(2,2)=_look.z;(*v)(2,3)=0.0f;
        (*v)(3,0)=x;       (*v)(3,1)=y;    (*v)(3,2)=z;      (*v)(3,3)=1.0f;
   

}

void Camera::pitch(float angle)//前倾
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_right,angle);
D3DXVec3TransformCoord(&_up,&_up,&T);
D3DXVec3TransformCoord(&_look,&_look,&T);

}

void Camera::yaw(float angle)//偏航
{
D3DXMATRIX T;
//if(_cameraType==LANDOBJECT)
D3DXMatrixRotationY(&T,angle);
//if(_cameraType==AIRCRAFT)
//D3DXMatrixRotationAxis(&T,&_up,angle);
D3DXVec3TransformCoord(&_right,&_right,&T);
D3DXVec3TransformCoord(&_look,&_look,&T);

}

void Camera::roll(float angle)//滚转
{
if(_cameraType==AIRCRAFT)
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_look,angle);
D3DXVec3TransformCoord(&_right,&_right,&T);
D3DXVec3TransformCoord(&_up,&_up,&T);

}
}

void Camera::walk(float units)//前移
{
//if(_cameraType==LANDOBJECT)
_pos+=D3DXVECTOR3(_look.x,0.0f,_look.z)*units;
//if(_cameraType==AIRCRAFT)
//_pos+=_look*units;
}

void Camera::strafe(float units)//平移
{
if(_cameraType==LANDOBJECT)
_pos+=D3DXVECTOR3(_right.x,0.0f,_right.z)*units;
if(_cameraType==AIRCRAFT)
_pos+=_right*units;
}

void Camera::fly(float units)
{
if(_cameraType==LANDOBJECT)
_pos.y+=units;
if(_cameraType==AIRCRAFT)
_pos+=_up*units;
}

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 09:26:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

#include <d3dx9.h>
class Camera
{
public:
        enum CameraType{LANDOBJECT,AIRCRAFT};
        Camera(D3DXVECTOR3,D3DXVECTOR3,D3DXVECTOR3);
        Camera(CameraType cameraType);
        ~Camera();

        void strafe(float units);
        void fly(float units);
        void walk(float units);
        void pitch(float angle);
        void yaw(float angle);
        void roll(float angle);

        void getViewMatrix(D3DXMATRIX* v);
        void setCameraType(CameraType cameraType);
        void getPosition(D3DXVECTOR3* pos);
    void setPosition(D3DXVECTOR3* pos);
        void getRight(D3DXVECTOR3* right);
    void getUp(D3DXVECTOR3* up);
        void getLook(D3DXVECTOR3* look);

private:
        CameraType _cameraType;
    D3DXVECTOR3 _right;
    D3DXVECTOR3 _up;
    D3DXVECTOR3 _look;
    D3DXVECTOR3 _pos;
};

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 09:26:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

#include "Terrain8.h"


Terrain::Terrain(IDirect3DDevice9* device,int numVertsPerRow,int numVertsPerCol,int cellSpacing)
{
     m_pDevice = device;
     m_pVB = 0;
     m_pIB = 0;   
         _numVertsPerRow = numVertsPerRow ;
     _numVertsPerCol = numVertsPerCol ;
     _cellSpacing = cellSpacing;
     _numCellsPerRow = _numVertsPerRow-1;
     _numCellsPerCol = _numVertsPerCol-1;
         _width = _numCellsPerRow*_cellSpacing;
     _depth = _numCellsPerCol*_cellSpacing;
         _numVertices = _numVertsPerRow*_numVertsPerCol;
         _numTriangles = _numCellsPerRow*_numCellsPerCol*2;
         computeVertices();
         computeIndices();
     mY = new float[_numVertices];
       
};
Terrain::~Terrain()
{
delete mY;
m_pVB->Release();
m_pIB->Release();

};

bool Terrain::computeVertices()
{
        HRESULT hr = 0;
        hr = m_pDevice->CreateVertexBuffer(_numVertices*sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&m_pVB,0);
    if(FAILED(hr))
                return false;
        int startX = -_width/2;
        int startZ = _depth/2;
        int endX   = _width/2;
        int endZ   = -_depth/2;
        float uCoordIncrementSize = 1.0f/(float)_numCellsPerRow;
    float vCoordIncrementSize = 1.0f/(float)_numCellsPerCol;
        Vertex* v = 0;
        /*m_pVB->Lock(0,0,(void**)&vertices,0);
vertices[0] = Vertex(-1.0,0.0,5.0);vertices[1] = Vertex(1.0,0.0,5.0);
vertices[2] = Vertex(-1.0,0.0,6.0);vertices[3] = Vertex(1.0,0.0,6.0);
vertices[4] = Vertex(-1.0,0.0,7.0);vertices[5] = Vertex(1.0,0.0,7.0);
vertices[6] = Vertex(-1.0,0.0,8.0);vertices[7] = Vertex(1.0,0.0,8.0);
   m_pVB->Unlock();*/
   
        m_pVB->Lock(0,0,(void**)&v,0);
        int i = 0;
        for(int z = startZ;z>=endZ;z-=_cellSpacing)
        {
         int j = 0;
          for(int x = startX;x<=endX;x+=_cellSpacing)
          {
           int index = i*_numVertsPerRow+j;
       v[index] = Vertex((float)x,(float)0,(float)z,(float)j*uCoordIncrementSize,(float)i*vCoordIncrementSize);//y
         
          }
          j++;
        }
    i++;
    m_pVB->Unlock();
    return true;
}
bool Terrain::computeIndices()
{
        HRESULT hr = 1;
        hr = m_pDevice->CreateIndexBuffer(3*_numTriangles*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&m_pIB,0);
    if(FAILED(hr))
                return false;
   WORD* indices = 0;
   /*m_pIB->Lock(0,0,(void**)&indices,0);
indices[0] = 0;indices[1] =2 ;indices[2] = 1;
indices[3] = 2;indices[4] = 3;indices[5] = 1;
indices[6] = 2;indices[7] = 4;indices[8] = 3;
indices[9] = 4;indices[10] = 5;indices[11] = 3;
indices[12] = 4;indices[13] = 6;indices[14] = 5;
indices[15] = 6;indices[16] = 7;indices[17] = 5;
   m_pIB->Unlock();*/
m_pIB->Lock(0,0,(void**)&indices,0);
int baseIndex = 0;
for(int i = 0;i<_numCellsPerCol;i++)
{
  for(int j = 0;j<_numCellsPerRow;j++)
  {
  indices[baseIndex] =       i*_numVertsPerRow+j;
  indices[baseIndex+1] =     i*_numVertsPerRow+j+1;
  indices[baseIndex+2] = (i+1)*_numVertsPerRow+j;
  
  indices[baseIndex+3] = (i+1)*_numVertsPerRow+j;
  indices[baseIndex+4] =     i*_numVertsPerRow+j+1;
  indices[baseIndex+5] = (i+1)*_numVertsPerRow+j+1;

  baseIndex +=6;
  }
}
m_pIB->Unlock();
   return true;
}

bool Terrain::Render()
{
D3DXMATRIXA16 m_matWorld;
//D3DXMatrixTranslation(&m_matWorld,-10.0f,0.0f,0.0f);
D3DXMatrixRotationZ(&m_matWorld,0.644f);
//m_pDevice->SetTransform( D3DTS_WORLD,&m_matWorld );
m_pDevice->SetStreamSource( 0,m_pVB,0,sizeof(Vertex) );
m_pDevice->SetIndices( m_pIB );
m_pDevice->SetFVF(Vertex::FVF);
m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,_numVertices,0,_numTriangles);//

return true;
}

float Terrain::getHeight(float x,float z)
{
x = ((float)_width/2.0f)+x;
z = ((float)_depth/2.0f)+z;
x /= (float)_cellSpacing;
z /= (float)_cellSpacing;
float col = ::floorf(x);
float row = ::floorf(z);
return 1;
}

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 09:27:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

#include <d3dx9.h>
class Terrain
{
public:
        Terrain(IDirect3DDevice9* device,int numVertsPerRow,int numVertsPerCol,int cellSpacing);//float heightScale,std::string heightmapFileName
    Terrain();
        ~Terrain();
        bool Render();
        float getHeight(float x,float z);
    struct Vertex
     {
        Vertex(){}
        Vertex(float x,float y,float z,float u,float v)
           {
                _x=x,_y=y,_z=z,_u=u,_v=v;
           }
        float _x,_y,_z;
        float _u,_v;
        static const DWORD FVF = D3DFVF_XYZ|D3DFVF_TEX1;
     };
    float* mY;

private:
        IDirect3DVertexBuffer9* m_pVB ;
    IDirect3DIndexBuffer9* m_pIB ;
    IDirect3DDevice9* m_pDevice;
        bool computeVertices();
        bool computeIndices();
    int _numVertsPerRow;
    int _numVertsPerCol;
    int _cellSpacing;
        int _numCellsPerRow;
        int _numCellsPerCol;
        int _width;
        int _depth;
        int _numVertices;
        int _numTriangles;
};

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 09:28:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

d3dxof.lib dxguid.lib d3dx9d.lib d3d9.lib winmm.lib

154

主题

4567

帖子

4579

积分

论坛元老

Rank: 8Rank: 8

积分
4579
QQ
发表于 2007-6-3 11:43:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

目前还没看3D,不过你这里有个基础知识上的问题
===========
float Terrain::getHeight(float x,float z)
{
x = ((float)_width/2.0f)+x;
z = ((float)_depth/2.0f)+z;
x /= (float)_cellSpacing;
z /= (float)_cellSpacing;
float col = ::floorf(x);
float row = ::floorf(z);
return 1; <-----------------------????
}

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-3 13:52:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

float Terrain::getHeight(float x,float z)
这个函数还没写完,所以让它暂时返回1.

48

主题

142

帖子

142

积分

注册会员

Rank: 2

积分
142
 楼主| 发表于 2007-6-7 13:54:00 | 显示全部楼层

Re:为什么我编的地形却是一条直线?

终于发现了自己的错误:
  1. bool Terrain::computeVertices()
  2. {
  3.         HRESULT hr = 0;
  4.         hr = m_pDevice->CreateVertexBuffer(_numVertices*sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&m_pVB,0);
  5.     if(FAILED(hr))
  6.                 return false;
  7.         int startX = -_width/2;//set point (0,0) the center of the terrain
  8.         int startZ = _depth/2;
  9.         int endX   = _width/2;
  10.         int endZ   = -_depth/2;
  11.         float uCoordIncrementSize = 1.0f/(float)_numCellsPerRow;//compute the increment size of texture coordinates
  12.     float vCoordIncrementSize = 1.0f/(float)_numCellsPerCol;//from one vertex to the next
  13.         Vertex* v = 0;
  14.         /*m_pVB->Lock(0,0,(void**)&vertices,0);
  15. vertices[0] = Vertex(-1.0,0.0,5.0);vertices[1] = Vertex(1.0,0.0,5.0);
  16. vertices[2] = Vertex(-1.0,0.0,6.0);vertices[3] = Vertex(1.0,0.0,6.0);
  17. vertices[4] = Vertex(-1.0,0.0,7.0);vertices[5] = Vertex(1.0,0.0,7.0);
  18. vertices[6] = Vertex(-1.0,0.0,8.0);vertices[7] = Vertex(1.0,0.0,8.0);
  19.    m_pVB->Unlock();*/
  20.    
  21.         m_pVB->Lock(0,0,(void**)&v,0);
  22.         int i = 0;
  23.         for(int z = startZ;z>=endZ;z-=_cellSpacing)//
  24.         {
  25.          int j = 0;
  26.           for(int x = startX;x<=endX;x+=_cellSpacing)
  27.           {
  28.            int index = i*_numVertsPerRow+j;//give every vertex its coordinate
  29.        v[index] = Vertex((float)x,(float)0,(float)z,(float)j*uCoordIncrementSize,(float)i*vCoordIncrementSize);//y
  30.          
  31.           }
  32.           j++;
  33.         }
  34.     i++;
  35.     m_pVB->Unlock();
  36.     return true;
  37. }
复制代码

应该是
  1. bool Terrain::computeVertices()
  2. {
  3.         HRESULT hr = 0;
  4.         hr = m_pDevice->CreateVertexBuffer(_numVertices*sizeof(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&m_pVB,0);
  5.     if(FAILED(hr))
  6.                 return false;
  7.         int startX = -_width/2;//set point (0,0) the center of the terrain
  8.         int startZ = _depth/2;
  9.         int endX   = _width/2;
  10.         int endZ   = -_depth/2;
  11.         float uCoordIncrementSize = 1.0f/(float)_numCellsPerRow;//compute the increment size of texture coordinates
  12.     float vCoordIncrementSize = 1.0f/(float)_numCellsPerCol;//from one vertex to the next
  13.         Vertex* v = 0;
  14.         /*m_pVB->Lock(0,0,(void**)&vertices,0);
  15. vertices[0] = Vertex(-1.0,0.0,5.0);vertices[1] = Vertex(1.0,0.0,5.0);
  16. vertices[2] = Vertex(-1.0,0.0,6.0);vertices[3] = Vertex(1.0,0.0,6.0);
  17. vertices[4] = Vertex(-1.0,0.0,7.0);vertices[5] = Vertex(1.0,0.0,7.0);
  18. vertices[6] = Vertex(-1.0,0.0,8.0);vertices[7] = Vertex(1.0,0.0,8.0);
  19.    m_pVB->Unlock();*/
  20.    
  21.         m_pVB->Lock(0,0,(void**)&v,0);
  22.         int i = 0;
  23.         for(int z = startZ;z>=endZ;z-=_cellSpacing)//
  24.         {
  25.          int j = 0;
  26.           for(int x = startX;x<=endX;x+=_cellSpacing)
  27.           {
  28.            int index = i*_numVertsPerRow+j;//give every vertex its coordinate
  29.        v[index] = Vertex((float)x,(float)0,(float)z,(float)j*uCoordIncrementSize,(float)i*vCoordIncrementSize);//y
  30.          j++;//注意
  31.           }
  32.           i++;//注意
  33.         }
  34.     m_pVB->Unlock();
  35.     return true;
  36. }

复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-26 05:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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