Why is my terrain a line?

Started by
9 comments, last by showin 16 years, 10 months ago
I post my code below,which I am going to use to create a terrain(wireframe).But I got just a line after I ran the code. Why is my terrain a line?Could anyone help me to find out anything wrong?

#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 ) 
#include "Camera8.h"
#include "Terrain8.h"
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D           = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice     = NULL; // Our rendering device
ID3DXMesh* mesh = 0;
const int Width = 800;
const int Height = 600;
POINT          m_ptLastMouse;      // position of last mouse point
float Z=0.0f;
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f);//( .5f, .55f, -.2f );//+Z 
float X=0.0f;
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );//( .5f+X,  .125f, .5f+Z );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 g_matWorld;
Terrain* g_pTerrain;
Camera* theCamera = new Camera(vEyePt,vLookatPt,vUpVec);
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;
    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
    // Turn on ambient lighting 
    g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
    g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
    //    m_pCamera1 = new Camera();
    g_pTerrain = new Terrain(g_pd3dDevice,21, 21, 10);//, 15.0
    //    m_pTerrain1->SetTexture("Grass.bmp");
    D3DXCreateTeapot(g_pd3dDevice,&mesh,0);
    return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    delete(theCamera);
mesh->Release();
mesh = 0;
    delete(g_pTerrain);

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
//SafeDelete(g_pTerrain);
//SafeDelete(m_pCamera1);
}
void OnBegin( int nX, int nY)
    {
 //       m_bDrag = true;
//   m_qDown = m_qNow;
        //m_vDownPt = ScreenToVector( (float)nX, (float)nY );
    m_ptLastMouse.x =nX;
m_ptLastMouse.y =nY;
}
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    //D3DXMatrixTranslation( &g_matWorld, -2.56f,0.0f,0.0f);

D3DXMatrixIdentity(&g_matWorld);
    g_pd3dDevice->SetTransform( D3DTS_WORLD,&g_matWorld );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView,&vEyePt, &vLookatPt , &vUpVec );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj,D3DX_PI/4,  1.250f, 1.0f, 1000.0f); 
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
                         D3DCOLOR_XRGB(250,250,250), 1.0f, 0 );  
    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();
        // Meshes are divided into subsets, one for each material. Render them in
            mesh->DrawSubset(0);
       // {
            g_pTerrain->Render();
       // }
        // End the scene
        g_pd3dDevice->EndScene();
    }
    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
        // Current mouse position
    int iMouseX = (short)LOWORD(lParam);
    int iMouseY = (short)HIWORD(lParam);
switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;    
case WM_RBUTTONDOWN:
case WM_LBUTTONDOWN:
                SetCapture( hWnd );
                OnBegin( iMouseX, iMouseY);// 
                return TRUE;
/*            case WM_LBUTTONUP:
                ReleaseCapture();
                return TRUE;
            case WM_CAPTURECHANGED:
                if( (HWND)lParam != hWnd )
                {
                    ReleaseCapture();
                }
                return TRUE;
*/
            case WM_MOUSEMOVE:
 /*              if( MK_LBUTTON&wParam )
                {
                  //D3DXMatrixRotationY( &g_matWorld, timeGetTime()/1000.0f );
   OnMove( iMouseX, iMouseY);
                  // D3DXMatrixRotationZ( &g_matWorld, Angle);
   m_ptLastMouse.x = iMouseX;
                   m_ptLastMouse.y = iMouseY;  
}
*/
//ADD &#25668;&#35937;&#26426;&#30340;&#20195;&#30721;
        if( MK_RBUTTON&wParam )//case WM_RBUTTONDBLCLK:
        {
// Compute the drag rectangle in screen coord.
            POINT ptCursor = { (short)LOWORD(lParam), (short)HIWORD(lParam) };
            // Update member var state
            if( ( msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK ))// && PtInRect( &g_Camera.m_rcDrag, ptCursor ) )
 //               { g_Camera.m_bMouseRButtonDown = true; g_Camera.m_nCurrentButtonMask |= MOUSE_RIGHT_BUTTON; }
            // Capture the mouse, so if the mouse button is 
            // released outside the window, we'll get the WM_LBUTTONUP message
            SetCapture(hWnd);
           // GetCursorPos( &g_Camera.m_ptLastMousePosition );
        /*    g_Camera.GetInput(iMouseX, iMouseY );
  vLookatPt = g_Camera.m_vLookAt;
   m_ptLastMouse.x = iMouseX;
                   m_ptLastMouse.y = iMouseY;
           */
Z = (iMouseY-m_ptLastMouse.x)/10.0f;//
X = (iMouseX-m_ptLastMouse.x)/10.0f;//
//vEyePt = D3DXVECTOR3(0.0f, 3.0f,-5.0f+Z);
//vLookatPt = D3DXVECTOR3( 0.0f+X, 0.0f, 0.0f+Z );//
  theCamera->yaw(Z) ;//
 // TheCamera.walk(X) ;
  D3DXMATRIX V;
  theCamera->getViewMatrix(&V);
  g_pd3dDevice->SetTransform(D3DTS_VIEW,&V);
}
               return TRUE;
        case WM_RBUTTONUP:   
        {
               ReleaseCapture();
            break;
        } 
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "Terrain8", NULL };
    RegisterClassEx( &wc );
    // Create the application's window
    HWND hWnd = CreateWindow( "Terrain8", "Terrain8", 
                              WS_OVERLAPPEDWINDOW, 0, 0, 800, 800,
                              NULL, NULL, wc.hInstance, NULL );
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    { 
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            // Enter the message loop
            MSG msg; 
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
     }
    UnregisterClass( "Terrain8", wc.hInstance );
    return 0;
}






#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)//&#21069;&#20542;
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_right,angle);
D3DXVec3TransformCoord(&_up,&_up,&T);
D3DXVec3TransformCoord(&_look,&_look,&T);
}
void Camera::yaw(float angle)//&#20559;&#33322;
{
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)//&#28378;&#36716;
{
if(_cameraType==AIRCRAFT)
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_look,angle);
D3DXVec3TransformCoord(&_right,&_right,&T);
D3DXVec3TransformCoord(&_up,&_up,&T);
}
}
void Camera::walk(float units)//&#21069;&#31227;
{
//if(_cameraType==LANDOBJECT)
_pos+=D3DXVECTOR3(_look.x,0.0f,_look.z)*units;
//if(_cameraType==AIRCRAFT)
//_pos+=_look*units;
}
void Camera::strafe(float units)//&#24179;&#31227;
{
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;
}






#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;
};





#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;
}




#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;
};


d3dxof.lib dxguid.lib d3dx9d.lib d3d9.lib winmm.lib [Edited by - showin on June 5, 2007 6:07:18 AM]
Advertisement
No. I can't help you.

A) no description what your actual problem is
B) see A, plus not even posting an error message
C) see A, plus not saying anything about what the program is supposed to do
D) you're not even using the code tags

I am sorry.I don't know how to use the code tags.
How could I use the tags,by the way?
I want to create a terrain,but after I ran my code above I got a line,not a terrain.
Could anyone build an object and run my code to try to find why the terrain is a line?
Thank you advanced.
the syntax of tagging is "[tagname]-your code goes here-[/tagname]", so you're looking for the "[ c o d e ] ... [ / c o d e ]"-tags (remove the blanks).

Hmm, I still don't get what you mean by your "landscape is a line".

Is it wireframe, or is there only one line at all?
It would be cool if you could post a screenshot ;)
Without reading through all of your code here's a few things to check.
Is your terrain object set up correctly, are you sure you are specifying X,Y,Z values for all of your verts.

Play with your camera, try to set a camera up first by making it look straight down on your terrain, X=0, Y=0, Z = +- 1000 (or whatever). Make sure your LookAt is 0,0,0 and make sure your object is put on or hear the world space origin.

Are you applying your matrix transformations in the proper order? view * world * projection is not the same as world * view * projection since matrix multiplication is not commutative.

If after these steps you still are seeing a line instead of a 2D Image then I'd suggest swapping out your terrain with a more simple model, try a single triangle. Get that on screen in your camera and then go back to your original terrain. If you still see a line then you know it's something in your terrain and not your camera.

Webby
I am so sorry again,I failed to use tags.
My God,I also failed to post a screenshot.
Run the program,then a teapot and a line show in the window. No terrain.
It's not too late to edit your original post. But for source this long, [code] tags are not much better than plain text. Use [source] tags, like this:

// </span><br><br><span class="cpp-directive">#include</span> <span class="cpp-literal">"Terrain8.h"</span><br>Terrain::Terrain(IDirect3DDevice9* device,<span class="cpp-keyword">int</span> numVertsPerRow,<span class="cpp-keyword">int</span> numVertsPerCol,<span class="cpp-keyword">int</span> cellSpacing)<br>{<br>	m_pDevice = device;<br>	m_pVB = <span class="cpp-number">0</span>;<br>	m_pIB = <span class="cpp-number">0</span>;<br>	_numVertsPerRow = numVertsPerRow ;<br>	_numVertsPerCol = numVertsPerCol ;<br>	_cellSpacing = cellSpacing;<br>	_numCellsPerRow = _numVertsPerRow-<span class="cpp-number">1</span>;<br>	_numCellsPerCol = _numVertsPerCol-<span class="cpp-number">1</span>;<br>	_width = _numCellsPerRow*_cellSpacing;<br>	_depth = _numCellsPerCol*_cellSpacing;<br>	_numVertices = _numVertsPerRow*_numVertsPerCol;<br>	_numTriangles = _numCellsPerRow*_numCellsPerCol*<span class="cpp-number">2</span>;<br>	computeVertices();<br>	computeIndices();<br>	mY = <span class="cpp-keyword">new</span> <span class="cpp-keyword">float</span>[_numVertices];<br>};<br>Terrain::~Terrain()<br>{<br>	<span class="cpp-keyword">delete</span> mY;<br>	m_pVB-&gt;Release();<br>	m_pIB-&gt;Release();<br>};<br><span class="cpp-keyword">bool</span> Terrain::computeVertices()<br>{<br>	HRESULT hr = <span class="cpp-number">0</span>;<br>	hr = m_pDevice-&gt;CreateVertexBuffer(_numVertices*<span class="cpp-keyword">sizeof</span>(Vertex),D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&amp;m_pVB,<span class="cpp-number">0</span>);<br>	<span class="cpp-keyword">if</span>(FAILED(hr))<br>		<span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br>	<span class="cpp-keyword">int</span> startX = -_width/<span class="cpp-number">2</span>;<br>	<span class="cpp-keyword">int</span> startZ = _depth/<span class="cpp-number">2</span>;<br>	<span class="cpp-keyword">int</span> endX = _width/<span class="cpp-number">2</span>;<br>	<span class="cpp-keyword">int</span> endZ = -_depth/<span class="cpp-number">2</span>;<br>	<span class="cpp-keyword">float</span> uCoordIncrementSize = <span class="cpp-number">1</span>.0f/(<span class="cpp-keyword">float</span>)_numCellsPerRow;<br>	<span class="cpp-keyword">float</span> vCoordIncrementSize = <span class="cpp-number">1</span>.0f/(<span class="cpp-keyword">float</span>)_numCellsPerCol;<br>	Vertex* v = <span class="cpp-number">0</span>;<br>	<span class="cpp-comment">/*m_pVB-&gt;Lock(0,0,(void**)&amp;vertices,0);<br>	vertices[0] = Vertex(-1.0,0.0,5.0);vertices[1] = Vertex(1.0,0.0,5.0);<br>	vertices[2] = Vertex(-1.0,0.0,6.0);vertices[3] = Vertex(1.0,0.0,6.0);<br>	vertices[4] = Vertex(-1.0,0.0,7.0);vertices[5] = Vertex(1.0,0.0,7.0);<br>	vertices[6] = Vertex(-1.0,0.0,8.0);vertices[7] = Vertex(1.0,0.0,8.0);<br>	m_pVB-&gt;Unlock();*/</span><br>	m_pVB-&gt;Lock(<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,(<span class="cpp-keyword">void</span>**)&amp;v,<span class="cpp-number">0</span>);<br>	<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> z = startZ;z&gt;=endZ;z-=_cellSpacing)<br>	{<br>		<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>;<br>		<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> x = startX;x&lt;=endX;x+=_cellSpacing)<br>		{<br>			<span class="cpp-keyword">int</span> index = i*_numVertsPerRow+j;<br>			v[index] = Vertex((<span class="cpp-keyword">float</span>)x,(<span class="cpp-keyword">float</span>)<span class="cpp-number">0</span>,(<span class="cpp-keyword">float</span>)z,(<span class="cpp-keyword">float</span>)j*uCoordIncrementSize,(<span class="cpp-keyword">float</span>)i*vCoordIncrementSize);<span class="cpp-comment">//y</span><br>		}<br>		j++;<br>	}<br>	i++;<br>	m_pVB-&gt;Unlock();<br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>}<br><span class="cpp-keyword">bool</span> Terrain::computeIndices()<br>{<br>	HRESULT hr = <span class="cpp-number">1</span>;<br>	hr = m_pDevice-&gt;CreateIndexBuffer(<span class="cpp-number">3</span>*_numTriangles*<span class="cpp-keyword">sizeof</span>(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&amp;m_pIB,<span class="cpp-number">0</span>);<br>	<span class="cpp-keyword">if</span>(FAILED(hr))<br>		<span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br>	WORD* indices = <span class="cpp-number">0</span>;<br>	<span class="cpp-comment">/*m_pIB-&gt;Lock(0,0,(void**)&amp;indices,0);<br>	indices[0] = 0;indices[1] =2 ;indices[2] = 1;<br>	indices[3] = 2;indices[4] = 3;indices[5] = 1;<br>	indices[6] = 2;indices[7] = 4;indices[8] = 3;<br>	indices[9] = 4;indices[10] = 5;indices[11] = 3;<br>	indices[12] = 4;indices[13] = 6;indices[14] = 5;<br>	indices[15] = 6;indices[16] = 7;indices[17] = 5;<br>	m_pIB-&gt;Unlock();*/</span><br>	m_pIB-&gt;Lock(<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,(<span class="cpp-keyword">void</span>**)&amp;indices,<span class="cpp-number">0</span>);<br>	<span class="cpp-keyword">int</span> baseIndex = <span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>;i&lt;_numCellsPerCol;i++)<br>	{<br>		<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>;j&lt;_numCellsPerRow;j++)<br>		{<br>			indices[baseIndex] = i*_numVertsPerRow+j;<br>			indices[baseIndex+<span class="cpp-number">1</span>] = i*_numVertsPerRow+j+<span class="cpp-number">1</span>;<br>			indices[baseIndex+<span class="cpp-number">2</span>] = (i+<span class="cpp-number">1</span>)*_numVertsPerRow+j;<br>			indices[baseIndex+<span class="cpp-number">3</span>] = (i+<span class="cpp-number">1</span>)*_numVertsPerRow+j;<br>			indices[baseIndex+<span class="cpp-number">4</span>] = i*_numVertsPerRow+j+<span class="cpp-number">1</span>;<br>			indices[baseIndex+<span class="cpp-number">5</span>] = (i+<span class="cpp-number">1</span>)*_numVertsPerRow+j+<span class="cpp-number">1</span>;<br>			baseIndex +=<span class="cpp-number">6</span>;<br>		}<br>	}<br>	m_pIB-&gt;Unlock();<br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>}<br><span class="cpp-keyword">bool</span> Terrain::Render()<br>{<br>	D3DXMATRIXA16 m_matWorld;<br>	<span class="cpp-comment">//D3DXMatrixTranslation(&amp;m_matWorld,-10.0f,0.0f,0.0f);</span><br>	D3DXMatrixRotationZ(&amp;m_matWorld,<span class="cpp-number">0</span>.644f);<br>	<span class="cpp-comment">//m_pDevice-&gt;SetTransform( D3DTS_WORLD,&amp;m_matWorld );</span><br>	m_pDevice-&gt;SetStreamSource( <span class="cpp-number">0</span>,m_pVB,<span class="cpp-number">0</span>,<span class="cpp-keyword">sizeof</span>(Vertex) );<br>	m_pDevice-&gt;SetIndices( m_pIB );<br>	m_pDevice-&gt;SetFVF(Vertex::FVF);<br>	m_pDevice-&gt;DrawIndexedPrimitive(D3DPT_TRIANGLELIST,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,_numVertices,<span class="cpp-number">0</span>,_numTriangles);<span class="cpp-comment">//</span><br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>}<br><span class="cpp-keyword">float</span> Terrain::getHeight(<span class="cpp-keyword">float</span> x,<span class="cpp-keyword">float</span> z)<br>{<br>	x = ((<span class="cpp-keyword">float</span>)_width/<span class="cpp-number">2</span>.0f)+x;<br>	z = ((<span class="cpp-keyword">float</span>)_depth/<span class="cpp-number">2</span>.0f)+z;<br>	x /= (<span class="cpp-keyword">float</span>)_cellSpacing;<br>	z /= (<span class="cpp-keyword">float</span>)_cellSpacing;<br>	<span class="cpp-keyword">float</span> col = ::floorf(x);<br>	<span class="cpp-keyword">float</span> row = ::floorf(z);<br>	<span class="cpp-keyword">return</span> <span class="cpp-number">1</span>;<br>}<br><br><br><br><br><span class="cpp-directive">#include</span> &lt;d3dx9.h&gt;<br><span class="cpp-keyword">class</span> Terrain<br>{<br><span class="cpp-keyword">public</span>:<br>	Terrain(IDirect3DDevice9* device,<span class="cpp-keyword">int</span> numVertsPerRow,<span class="cpp-keyword">int</span> numVertsPerCol,<span class="cpp-keyword">int</span> cellSpacing);<span class="cpp-comment">//float heightScale,std::string heightmapFileName</span><br>	Terrain();<br>	~Terrain();<br>	<span class="cpp-keyword">bool</span> Render();<br>	<span class="cpp-keyword">float</span> getHeight(<span class="cpp-keyword">float</span> x,<span class="cpp-keyword">float</span> z);<br>	<span class="cpp-keyword">struct</span> Vertex<br>	{<br>		Vertex(){}<br>		Vertex(<span class="cpp-keyword">float</span> x,<span class="cpp-keyword">float</span> y,<span class="cpp-keyword">float</span> z,<span class="cpp-keyword">float</span> u,<span class="cpp-keyword">float</span> v)<br>		{<br>			_x=x,_y=y,_z=z,_u=u,_v=v;<br>		}<br>		<span class="cpp-keyword">float</span> _x,_y,_z;<br>		<span class="cpp-keyword">float</span> _u,_v;<br>		<span class="cpp-keyword">static</span> <span class="cpp-keyword">const</span> DWORD FVF = D3DFVF_XYZ|D3DFVF_TEX1;<br>	};<br>	<span class="cpp-keyword">float</span>* mY;<br><span class="cpp-keyword">private</span>:<br>	IDirect3DVertexBuffer9* m_pVB ;<br>	IDirect3DIndexBuffer9* m_pIB ;<br>	IDirect3DDevice9* m_pDevice;<br>	<span class="cpp-keyword">bool</span> computeVertices();<br>	<span class="cpp-keyword">bool</span> computeIndices();<br>	<span class="cpp-keyword">int</span> _numVertsPerRow;<br>	<span class="cpp-keyword">int</span> _numVertsPerCol;<br>	<span class="cpp-keyword">int</span> _cellSpacing;<br>	<span class="cpp-keyword">int</span> _numCellsPerRow;<br>	<span class="cpp-keyword">int</span> _numCellsPerCol;<br>	<span class="cpp-keyword">int</span> _width;<br>	<span class="cpp-keyword">int</span> _depth;<br>	<span class="cpp-keyword">int</span> _numVertices;<br>	<span class="cpp-keyword">int</span> _numTriangles;<br>};<br><br></pre></div><!–ENDSCRIPT–><br><br>I don't mean to sound rude, but you're asking a lot and giving very little. The members of this forum are very helpful, but nobody wants to wade through pages and pages of uncommented code. You'll encourage more helpful responses if you isolate the pieces of code you suspect to be at fault (or at least remove those you're sure are fine) and give detailed accounts of what's right, what's wrong and what has changed.<br><br>I haven't looked at your code, but have you tried all the usual stuff?<br>In particular, try moving the camera back a bit, disabling backface-culling, depth-testing and alpha-blending.<br><br>Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Hey all, it did state what the problem was. it's terrain comes out as a Line.
And you use these to put your code in
but don't use the gap<br>[/s ource]<br>
Thank all of you.
The head file of class Terrain
//Terrain8.h#include <d3dx9.h>class Terrain{public:	Terrain(IDirect3DDevice9* device,int numVertsPerRow,int numVertsPerCol,int cellSpacing);    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;};
file Terrain8.cpp
//Terrain8.cpp#include "Terrain8.h"Terrain::Terrain(IDirect3DDevice9* device,int numVertsPerRow,int numVertsPerCol,int cellSpacing){     m_pDevice = device;       //quote globle variable     m_pVB = 0;                 //pointer to vertex buffer     m_pIB = 0;                 //pointer to index buffer  	 _numVertsPerRow = numVertsPerRow ;//number of vertices per row     _numVertsPerCol = numVertsPerCol ;    //number of vertices per column     _cellSpacing = cellSpacing;           //space between neighbor vertexs     _numCellsPerRow = _numVertsPerRow-1;   //number of cells per row     _numCellsPerCol = _numVertsPerCol-1;   //number of cells per column     _width = _numCellsPerRow*_cellSpacing; //terrain width     _depth = _numCellsPerCol*_cellSpacing;    //terrain depth	 _numVertices = _numVertsPerRow*_numVertsPerCol;//total nummber of vertexes	 _numTriangles = _numCellsPerRow*_numCellsPerCol*2;//total triangles of the terrain	 computeVertices();//deal with vertices	 computeIndices();//deal with indices     mY = new float[_numVertices];//store y coordinate of vertices,not completed	};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;//set point (0,0) the center of the terrain	int startZ = _depth/2;	int endX   = _width/2;	int endZ   = -_depth/2;	float uCoordIncrementSize = 1.0f/(float)_numCellsPerRow;//compute the increment size of texture coordinates     float vCoordIncrementSize = 1.0f/(float)_numCellsPerCol;//from one vertex to the next	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;//give every vertex its coordinate       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++)//assign every vertex a number,weave tiangles  {  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)//not completed{ 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;}

This topic is closed to new replies.

Advertisement