Sign in to follow this  
showin

Why is my terrain a line?

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

// [I removed all your boilerplate code, leaving just the terrain class - Ed]

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



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.

I haven't looked at your code, but have you tried all the usual stuff?
In particular, try moving the camera back a bit, disabling backface-culling, depth-testing and alpha-blending.

Admiral

Share this post


Link to post
Share on other sites
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
[s ource]but don't use the gap
[/s ource]

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
I fixed it in the end.the piece of code
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;
}


shoule be
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);
i++;//pay attention
}
j++;//pay attention
}
m_pVB->Unlock();
return true;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this