terrain looks terrible

Started by
24 comments, last by steg 21 years, 1 month ago
Many thanks AikonIV,

I''ve been drawing this on a white board to try to better understand it, it seems so easy to do in OpenGL (this is an example I have) but Direct3D sets vertices up differently.
I guess I''m just thinking that by just putting the vertices into the vertex buffer they should be drawn correctly, guess this is completely wrong.

I will email you a screen shot.

Many thanks again.
Steve

If it isn't working, take a bath, have a think and try again...

Advertisement
In case there was any question, it definitely is your triangle strips. If you want, you can try doing it the way I showed you using triangle lists.

If you''re tired of trying to get this working, I could email you a small demo of how I do it (that is, using an index buffer containing triangle list information.)

Just tell me if you want me to.
Hiyas,
Just a gut reaction, and if I had more time I''d look into this more thoroughly, but if you say vertices are stretching across the screen, chances are the variables are initialised incorrectly or at some point set to an incorrect value. I remember having the same thing when coding a worldcraft *.map viewer, and the cause was, if I recall correctly, that some vertices were being calculated incorrectly, prolly a division by zero or something daft which of course made them stretch out to what looked like infinity or near as dammit
Cheers,SteveLiquidigital Online
Thanks guys,

It''s defo the triangle strips. I wasn''t even making the buffer size big enough to hold them! i.e.for 32x32 should be 32x32x4, 4 being number of vertices for a quad using lists method.
One question though, if I was drawing the quads row by row I''m thinking that the next quad has to have the same y,x value of the previous one ? Two quads below (1 and 2!) I''m taking it that quad 2 has to have same x,y veritces as quad vertices labelled 1 ? Does this make sense ?

| /|| /|
|/ ||/ |
12

AikonIV, your demo code would be much appreciated, I think I do know how to do this now, took me long time for it to sink in!

Kind regards,
Steve

If it isn't working, take a bath, have a think and try again...

OK, I threw together a little demo using the D3D Matrices tutorial as the base code. I am doing it with multiple triangle strips, which I believe is how you are currently doing it. This code should compile and run with no problem, though it looks kinda stupid b/c I'm just generating random values for my height map rather than reading them in from a file. Will be no problem for you to change. If there are any parts you don't understand/would like explained in greater detail, just say so.


  #include <d3dx8.h>#include <mmsystem.h>LPDIRECT3D8             g_pD3D       = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE8       g_pd3dDevice = NULL; // Our rendering deviceLPDIRECT3DVERTEXBUFFER8 g_pVB        = NULL; // Buffer to hold verticesstruct CUSTOMVERTEX{  FLOAT x, y, z;      // The untransformed, 3D position for the vertex  DWORD color;        // The vertex color};#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)FLOAT fHeightMap[33*33];HRESULT InitD3D( HWND hWnd ){    // Create the D3D object.    if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )        return E_FAIL;    // Get the current desktop display mode, so we can set up a back    // buffer of the same format    D3DDISPLAYMODE d3ddm;    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )        return E_FAIL;    // Set up the structure used to create the D3DDevice    D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = d3ddm.Format;    // Create the D3DDevice    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                                      &d3dpp, &g_pd3dDevice ) ) )    {        return E_FAIL;    }    // Turn off culling, so we see the front and back of the triangle    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    // Turn off D3D lighting, since we are providing our own vertex colors    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );    g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);    return S_OK;}//-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Creates the scene geometry//-----------------------------------------------------------------------------HRESULT InitGeometry(){  for(int i=0;i<33;i++)    for(int j=0;j<33;j++)      fHeightMap[i+(j*33)] = rand()/25000;  if(FAILED(g_pd3dDevice->CreateVertexBuffer(2178*sizeof(CUSTOMVERTEX),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB))) return E_FAIL;  CUSTOMVERTEX* pVertices;  if(FAILED(g_pVB->Lock(0,0,(BYTE**)&pVertices,0))) return E_FAIL;  DWORD dwBaseVertex = 0;  for(int z=0;z<32;z++)  {    for(int x=0;x<33;x++)    {      pVertices[dwBaseVertex+(x*2)+0].x = x*1.0;      pVertices[dwBaseVertex+(x*2)+0].z = (z+0)*1.0;      pVertices[dwBaseVertex+(x*2)+0].y = fHeightMap[x+((z+0)*33)];      pVertices[dwBaseVertex+(x*2)+0].color = 0xFFFFFFFF;      pVertices[dwBaseVertex+(x*2)+1].x = x*1.0;      pVertices[dwBaseVertex+(x*2)+1].z = (z+1)*1.0;      pVertices[dwBaseVertex+(x*2)+1].y = fHeightMap[z+((z+1)*33)];      pVertices[dwBaseVertex+(x*2)+1].color = 0xFFFFFFFF;    }    dwBaseVertex += 66; //Using 66 vertices per strip  }  /*for(int x=0;x<33;x++)  {    for(int y=0;y<33;y++)    {      pVertices[x+(y*33)].x = x*1.0;      pVertices[x+(y*33)].y = rand()/25000.0f;//RAND_MAX;      pVertices[x+(y*33)].z = y*1.0;      pVertices[x+(y*33)].color = 0xFFFFFFFF;    }  }*/  g_pVB->Unlock();  return S_OK;}VOID Cleanup(){  if( g_pVB != NULL ) g_pVB->Release();  if( g_pd3dDevice != NULL ) g_pd3dDevice->Release();  if( g_pD3D != NULL ) g_pD3D->Release();}VOID SetupMatrices(){  // For our world matrix, we will just rotate the object about the y-axis.  D3DXMATRIX matWorld;  //D3DXMATRIX matWorld_Trans;  //D3DXMATRIX matWorld_Rot;  D3DXMatrixTranslation(&matWorld,-16.0,0.0,-16.0);  //D3DXMatrixRotationY(&matWorld_Rot,timeGetTime()/500.0f);  //D3DXMatrixMultiply(&matWorld,&matWorld_Trans,&matWorld_Rot);  g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );  D3DXMATRIX matView;  D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3((-sin(timeGetTime()/1000.0f))*30, 20.0f,(-cos(timeGetTime()/1000.0f))*30),                                &D3DXVECTOR3(0.0f,0.0f,0.0f),                                &D3DXVECTOR3(0.0f, 1.0f,0.0f));  g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );  D3DXMATRIX matProj;  D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );  g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );}VOID Render(){  // Clear the backbuffer to a black color  g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );  // Begin the scene  g_pd3dDevice->BeginScene();  // Setup the world, view, and projection matrices  SetupMatrices();  // Render the vertex buffer contents  g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );  g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );  for(int i=0;i<32;i++) g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,i*66,64);//g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );  // End the scene  g_pd3dDevice->EndScene();  // Present the backbuffer contents to the display  g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            PostQuitMessage( 0 );            return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}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,                      "D3D Tutorial", NULL };    RegisterClassEx( &wc );    // Create the application's window    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,                              GetDesktopWindow(), NULL, (HINSTANCE)wc.hInstance, NULL );    // Initialize Direct3D    if( SUCCEEDED( InitD3D( hWnd ) ) )    {        // Create the scene geometry        if( SUCCEEDED( InitGeometry() ) )        {            // 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();            }        }    }    // Clean up everything and exit the app    Cleanup();    UnregisterClass( "D3D Tutorial", (HINSTANCE)wc.hInstance );    return 0;}  


[edited by - AikonIV on March 22, 2003 6:26:17 PM]
Thanks AikonIV.

I''ll take a look at it later today.

Kind regards,
Steve

If it isn't working, take a bath, have a think and try again...

This topic is closed to new replies.

Advertisement