I cant get this terrain code to work

Started by
9 comments, last by kingpinzs 20 years, 10 months ago
************************************************************************************************************************** * Function fills the vertex buffer. To do this, we need to Lock() the Vertex Buffer to * gain access to the vertices. This mechanism is required becuase vertex buffers may be in device memory. **************************************************************************************************************************/ HRESULT CTerrain::FeedVertexBuffer() { if( FAILED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) ) return E_FAIL; D3DXVECTOR3 NormalVector; TERRAINVERTEX* pVertex; int iVertexBuffOffset = 0; int terrScale = 8; // loop as many times as there are vertices along x & Z axes calculating the coordinates for each terrain vertex (X,Y,Z) and store // in VB in a linear fashion. NOTE: the 2D regular grid (X,Y) is being transformed into 3D (X,Y,Z) terrain grid with height equal to 0. for (int z = 0; z < m_iNumOfVerticesZ; z++) //vertical rows { for (int x = 0; x < m_iNumOfVerticesX; x++) //horizontal rows { iVertexBuffOffset = x +( z * m_iNumOfVerticesX); pVertex = &( m_pVertices[ iVertexBuffOffset ] ); // 1. Calculate each terrain vertex coordinates pVertex->posX = (float)x * terrScale; pVertex->posY = 0.0; //constant terrain height pVertex->posZ = (float)z * terrScale; } } m_pTerrVertexBuffer->Unlock(); return S_OK; } What kind of verible is m_iNumOfVerticesX and m_iNumOfVerticesy Got this from http://www.uniqsoft.co.uk/directx/ tut3 Can any one help please
Advertisement
From context, it appears that those variables should be int, counting how wide your terrain is in X and Z directions.
Those variables use a widely used format for members of a class. The ''m_'' signifies it''s a class memeber, and the letter before the variable name, in this case ''i'' stands for it''s type, which is an int. Hope that makes things a little easier.
thinks that is what i did but every time i run the program it comiles but then when it is ran it crashes,. I have no idea why.
#include <d3d8.h>#include <d3dx8.h>#include <stdio.h>#include <mmsystem.h>#include <stdlib.h>////////////////////////////////////////////////////////////////////////////////LPDIRECT3D8 gpD3D = NULL;LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;LPDIRECT3DVERTEXBUFFER8 m_pTerrVertexBuffer;		// Terrain vertex buffer to hold vertices XYZ used to render triangles LPDIRECT3DINDEXBUFFER8  m_pTerrIndexBuffer;		int m_iTotalNumOfVertices;int m_iTotalNumOfPrimitives;int m_iNumOfVerticesZ ; int m_iNumOfVerticesX ; ////////////////////////////////////////////////////////////////////////////////	// Denfines my own custom D3D vertex format used by the vertex buffer  	struct TERRAINVERTEX 	{ 		float posX, posY, posZ; // Vertex XYZ position 	}; 	#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ) //////////////////////////////////////////////////////////////////////////////////Initialisetion of d3d/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////HRESULT InitialiseD3D(HWND hWnd){if( NULL == ( gpD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ){return E_FAIL;}D3DDISPLAYMODE d3ddm;if(FAILED(gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))){return E_FAIL;}D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;d3dpp.BackBufferFormat = d3ddm.Format;d3dpp.EnableAutoDepthStencil = TRUE ;  d3dpp.AutoDepthStencilFormat = D3DFMT_D16;if(FAILED(gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))){return E_FAIL;}//g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, 255);return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT FeedVertexBuffer(){		  TERRAINVERTEX*          m_pVertices; if( FAILED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) ) return E_FAIL;	D3DXVECTOR3 NormalVector;	TERRAINVERTEX* pVertex;	int iVertexBuffOffset = 0;    	int terrScale = 8;	// loop as many times as there are vertices along x & Z axes calculating the coordinates for each terrain vertex (X,Y,Z) and store 	// in VB in a linear fashion. NOTE: the 2D regular grid (X,Y) is being transformed into 3D (X,Y,Z) terrain grid with height equal to 0.	for (int z = 0; z < m_iNumOfVerticesZ; z++)	//vertical rows	{		for (int x = 0; x < m_iNumOfVerticesX; x++) //horizontal rows		{			iVertexBuffOffset = x +( z * m_iNumOfVerticesX);			pVertex = &( m_pVertices[ iVertexBuffOffset ] );                        			// 1. Calculate each terrain vertex coordinates			pVertex->posX = (float)x * terrScale;            			pVertex->posY = 0.0;                    		//constant terrain height			pVertex->posZ = (float)z * terrScale;		}	}	m_pTerrVertexBuffer->Unlock();	return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT CreateTerrainIndexBuffer(){	int iTotalNumOfIndices;	WORD	*pIndices;	UINT	Index;	// Calculate the number of indices	iTotalNumOfIndices = ((m_iNumOfVerticesX * 2) + 2) * (m_iNumOfVerticesZ - 1);	// Create the Index Buffer	if( FAILED( g_pD3DDevice->CreateIndexBuffer( iTotalNumOfIndices *sizeof(WORD),											D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,                                            D3DPOOL_MANAGED, &m_pTerrIndexBuffer ) ) )      		return E_FAIL;	if( FAILED( m_pTerrIndexBuffer->Lock(   0,  // Fill from start of the buffer.                                            0,	// Size of the data to load.                            (BYTE**)&pIndices,	// Returned index data.                                       0 ) ) )	// Send default flags to the lock.		return E_FAIL;	int IndexOffset = 0;    	// Use the following equation to get the number of primitives to draw	m_iTotalNumOfPrimitives = ( (m_iNumOfVerticesX + 1) * (m_iNumOfVerticesZ - 1) * 2 ) - 2;	// Fill in the index buffer with vertex ID linking the vertex buffer	for( int z = 0; z < m_iNumOfVerticesZ-1; z++ )	{		for( int x = 0; x < m_iNumOfVerticesX; x++ )		{			Index = x + ( z * m_iNumOfVerticesX );			pIndices[IndexOffset] = (WORD)Index;	    	IndexOffset++;			Index = x +( (z+1) * m_iNumOfVerticesX );			pIndices[IndexOffset] =(WORD)Index;			IndexOffset++;		}		//Place in copy of previous one		pIndices[IndexOffset] =(WORD)Index;		IndexOffset++;		//Place in first one for next row		Index = 0 + ( (z+1) * m_iNumOfVerticesX );		pIndices[IndexOffset] = (WORD)Index;		IndexOffset++;	}	m_pTerrIndexBuffer->Unlock();	return S_OK;} //////////////////////////////////////////////////////////////////////////////////the Render////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void Render(){D3DXMATRIX matView;	D3DXMATRIX TranslationMatrix; 	    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,                                       D3DCOLOR_XRGB(0,255,0), 1.0f, 0); // What ever this does it is important if(SUCCEEDED(g_pD3DDevice->BeginScene())){g_pD3DDevice->SetStreamSource( 0, m_pTerrVertexBuffer, sizeof(TERRAINVERTEX) );	    g_pD3DDevice->SetVertexShader( D3DFVF_TERRAINVERTEX );        g_pD3DDevice->SetIndices( m_pTerrIndexBuffer, 0 );		//Renders the specified geometric primitive, based on indexing into an array of vertices.		g_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP , 0, m_iTotalNumOfVertices, 0, m_iTotalNumOfPrimitives);g_pD3DDevice->EndScene();}g_pD3DDevice->Present(NULL, NULL, NULL, NULL);}//////////////////////////////////////////////////////////////////////////////////shuts down d3d////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void CleanUp(){if (g_pD3DDevice !=NULL){g_pD3DDevice->Release();g_pD3DDevice = NULL;}if(gpD3D != NULL){gpD3D->Release();gpD3D = NULL;}}////////////////////////////////////////////////////////////////////////////////LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            PostQuitMessage( 0 );            return 0;break;        case WM_KEYUP:        switch (wParam)        {        case VK_ESCAPE:        DestroyWindow(hWnd);        return 0;        break;        }                  }    return DefWindowProc( hWnd, msg, wParam, lParam );}//////////////////////////////////////////////////////////////////////////////////Win Main//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "D3D Tutorial", NULL };       RegisterClassEx( &wc );    // Create the application''s window    HWND hWnd = CreateWindow( "D3D Tutorial", "My window",                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,                              GetDesktopWindow(), NULL,(HINSTANCE)wc.hInstance, NULL );////////////////////////////////////////////////////////////////////////////////  //The Game Loop/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////               	if( SUCCEEDED( InitialiseD3D( hWnd  ) ) ) {         MessageBox(NULL, "Initd3d worked" , "error",MB_OK|MB_ICONEXCLAMATION);                        // Create the scene geometry                              ShowWindow( hWnd, SW_SHOWDEFAULT );                UpdateWindow( hWnd );         MSG msg;         ZeroMemory(&msg, sizeof(msg));        while(1)   {       if (PeekMessage( &msg, NULL, 0, 0,PM_REMOVE ) )    {       if(msg.message == WM_QUIT)       break;            TranslateMessage( &msg );            DispatchMessage( &msg );     }        else        Render();        } } CleanUp(); UnregisterClass( "D3D Tutorial", (HINSTANCE) wc.hInstance ); return 0;}

can any one help me please and thanks.
Ok I change the variables to int but it wont display anything on the screen but a window

#include <d3d8.h>#include <d3dx8.h>#include <stdio.h>#include <mmsystem.h>#include <stdlib.h>////////////////////////////////////////////////////////////////////////////////LPDIRECT3D8 gpD3D = NULL;LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;LPDIRECT3DVERTEXBUFFER8 m_pTerrVertexBuffer;		// Terrain vertex buffer to hold vertices XYZ used to render triangles LPDIRECT3DINDEXBUFFER8  m_pTerrIndexBuffer;		int m_iTotalNumOfVertices;int m_iTotalNumOfPrimitives;int m_iNumOfVerticesZ = 32 ; int m_iNumOfVerticesX  = 32 ; ////////////////////////////////////////////////////////////////////////////////	// Denfines my own custom D3D vertex format used by the vertex buffer  	struct TERRAINVERTEX 	{ 		float posX, posY, posZ; // Vertex XYZ position 	}; 	#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ) //////////////////////////////////////////////////////////////////////////////////Initialisetion of d3d/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////HRESULT InitialiseD3D(HWND hWnd){if( NULL == ( gpD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ){return E_FAIL;}D3DDISPLAYMODE d3ddm;if(FAILED(gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))){return E_FAIL;}D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;d3dpp.BackBufferFormat = d3ddm.Format;d3dpp.EnableAutoDepthStencil = TRUE ;  d3dpp.AutoDepthStencilFormat = D3DFMT_D16;if(FAILED(gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))){return E_FAIL;}//g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, 255);return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT FeedVertexBuffer(){		  TERRAINVERTEX*          m_pVertices; if( SUCCEEDED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) )   { MessageBox(NULL, " FeedVertex" , "error",MB_OK|MB_ICONEXCLAMATION); return S_OK;}	D3DXVECTOR3 NormalVector;	TERRAINVERTEX* pVertex;	int iVertexBuffOffset = 0;	int terrScale = 8;	// loop as many times as there are vertices along x & Z axes calculating the coordinates for each terrain vertex (X,Y,Z) and store 	// in VB in a linear fashion. NOTE: the 2D regular grid (X,Y) is being transformed into 3D (X,Y,Z) terrain grid with height equal to 0.	for (int z = 0; z < m_iNumOfVerticesZ; z++)	//vertical rows	{		for (int x = 0; x < m_iNumOfVerticesX; x++) //horizontal rows		{			iVertexBuffOffset = x +( z * m_iNumOfVerticesX);			pVertex = &( m_pVertices[ iVertexBuffOffset ] );                        			// 1. Calculate each terrain vertex coordinates			pVertex->posX = (float)x * terrScale;            			pVertex->posY = 0.0;                    		//constant terrain height			pVertex->posZ = (float)z * terrScale;		}	}	m_pTerrVertexBuffer->Unlock();	return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT CreateTerrainIndexBuffer(){	int iTotalNumOfIndices;	WORD	*pIndices;	UINT	Index;	// Calculate the number of indices	iTotalNumOfIndices = ((m_iNumOfVerticesX * 2) + 2) * (m_iNumOfVerticesZ - 1);	// Create the Index Buffer	if( FAILED( g_pD3DDevice->CreateIndexBuffer( iTotalNumOfIndices *sizeof(WORD),											D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,                                            D3DPOOL_MANAGED, &m_pTerrIndexBuffer ) ) )      		return E_FAIL;	if( FAILED( m_pTerrIndexBuffer->Lock(   0,  // Fill from start of the buffer.                                            0,	// Size of the data to load.                            (BYTE**)&pIndices,	// Returned index data.                                       0 ) ) )	// Send default flags to the lock.		return E_FAIL;	int IndexOffset = 0;    	// Use the following equation to get the number of primitives to draw	m_iTotalNumOfPrimitives = ( (m_iNumOfVerticesX + 1) * (m_iNumOfVerticesZ - 1) * 2 ) - 2;	// Fill in the index buffer with vertex ID linking the vertex buffer	for( int z = 0; z < m_iNumOfVerticesZ-1; z++ )	{		for( int x = 0; x < m_iNumOfVerticesX; x++ )		{			Index = x + ( z * m_iNumOfVerticesX );			pIndices[IndexOffset] = (WORD)Index;	    	IndexOffset++;			Index = x +( (z+1) * m_iNumOfVerticesX );			pIndices[IndexOffset] =(WORD)Index;			IndexOffset++;		}		//Place in copy of previous one		pIndices[IndexOffset] =(WORD)Index;		IndexOffset++;		//Place in first one for next row		Index = 0 + ( (z+1) * m_iNumOfVerticesX );		pIndices[IndexOffset] = (WORD)Index;		IndexOffset++;	}	m_pTerrIndexBuffer->Unlock();	return S_OK;} //////////////////////////////////////////////////////////////////////////////////the Render////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void Render(){D3DXMATRIX matView;	D3DXMATRIX TranslationMatrix; 	    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,                                       D3DCOLOR_XRGB(0,0,0), 1.0f, 0); // What ever this does it is important if(SUCCEEDED(g_pD3DDevice->BeginScene())){    g_pD3DDevice->SetStreamSource( 0, m_pTerrVertexBuffer, sizeof(TERRAINVERTEX) );	    g_pD3DDevice->SetVertexShader( D3DFVF_TERRAINVERTEX );        g_pD3DDevice->SetIndices( m_pTerrIndexBuffer, 0 );		//Renders the specified geometric primitive, based on indexing into an array of vertices.		g_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP , 0, m_iTotalNumOfVertices, 0, m_iTotalNumOfPrimitives);g_pD3DDevice->EndScene();}g_pD3DDevice->Present(NULL, NULL, NULL, NULL);}//////////////////////////////////////////////////////////////////////////////////shuts down d3d////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void CleanUp(){if (g_pD3DDevice !=NULL){g_pD3DDevice->Release();g_pD3DDevice = NULL;}if(gpD3D != NULL){gpD3D->Release();gpD3D = NULL;}}////////////////////////////////////////////////////////////////////////////////LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            PostQuitMessage( 0 );            return 0;break;        case WM_KEYUP:        switch (wParam)        {        case VK_ESCAPE:        DestroyWindow(hWnd);        return 0;        break;        }                  }    return DefWindowProc( hWnd, msg, wParam, lParam );}//////////////////////////////////////////////////////////////////////////////////Win Main//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "D3D Tutorial", NULL };       RegisterClassEx( &wc );    // Create the application''s window    HWND hWnd = CreateWindow( "D3D Tutorial", "My window",                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,                              GetDesktopWindow(), NULL,(HINSTANCE)wc.hInstance, NULL );////////////////////////////////////////////////////////////////////////////////  //The Game Loop/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////               	if( SUCCEEDED( InitialiseD3D( hWnd  ) ) ) {         MessageBox(NULL, "Initd3d worked" , "error",MB_OK|MB_ICONEXCLAMATION);                        // Create the scene geometry                                              ShowWindow( hWnd, SW_SHOWDEFAULT );                UpdateWindow( hWnd );         MSG msg;         ZeroMemory(&msg, sizeof(msg));        while(1)   {       if (PeekMessage( &msg, NULL, 0, 0,PM_REMOVE ) )    {       if(msg.message == WM_QUIT)       break;            TranslateMessage( &msg );            DispatchMessage( &msg );     }        else        Render();        } } CleanUp(); UnregisterClass( "D3D Tutorial", (HINSTANCE) wc.hInstance ); return 0;}

So what am I doing wrong if any one could help me I would be very happy. Please and thank you
No matter what I try it wont work can some one try the code and tell me why it wont work. This is so frustrating!! Please and Thank you ?
Afternoon, kingpinzs.

This bit is wrong:

TERRAINVERTEX* m_pVertices;
if( SUCCEEDED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) )


m_pTerrVertexBuffer hasn't been created yet.

Try placing this before that^ bit:

m_pTerrVertexBuffer = NULL;
g_pD3DDevice->CreateVertexBuffer( ((sizeof TERRAINVERTEX) * m_iNumOfVerticesX * m_iNumOfVerticesZ), 0, D3DFVF_TERRAINVERTEX, D3DPOOL_DEFAULT, &m_pTerrVertexBuffer);


...and don't forget to release any created vertex/index buffers ( inside CleanUp() ):

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


I've also started a terrain renderer. Still got a fair way to go with it (LOD/texturing everything/etc).
http://www.scrontsoft.com/ww1/ww1_ss1.png
http://www.scrontsoft.com/ww1/ww1_ss2.png
The height data is obtained from:
http://edcdaac.usgs.gov/gtopo30/gtopo30.html

I've had to build a utility proggy to cut up the data into 128*128 chunks and use 127*127*2 index buffers (screenshot#2 is rendering 56 127*127*2 chunks which is about 1.8meg of triangles).

Cheers,
Scronty


[edited by - Scronty on June 20, 2003 8:20:16 AM]
- "Has the world gone mad, or am I at work?"
NO, that is not it at all, it is correct to get a pointer, and lock it in that fashion.

What I am confused about is this:
if( SUCCEEDED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) )   {     MessageBox(NULL, "FeedVertex" , "error",MB_OK|MB_ICONEXCLAMATION); return S_OK;}	  


So, if the lock was successful, you print a box and return S_OK?

If you return out of the function, how can your vertices be filled? I bet you are saying "But I never see the box"

correct, because you VB is still locked.

EDIT:

Maybe I was partially wrong, do you create the VB before you attempt to lock it?

[edited by - RhoneRanger on June 20, 2003 4:19:53 PM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Still nothing on the screen
#include <d3d8.h>#include <d3dx8.h>#include <stdio.h>#include <mmsystem.h>#include <stdlib.h>////////////////////////////////////////////////////////////////////////////////LPDIRECT3D8 gpD3D = NULL;LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;/////////////////////////////////////////////LPDIRECT3DVERTEXBUFFER8 m_pTerrVertexBuffer = NULL;		// Terrain vertex buffer to hold vertices XYZ used to render triangles LPDIRECT3DINDEXBUFFER8  m_pTerrIndexBuffer = NULL ;	///////////////////////////////////////////int m_iTotalNumOfVertices ;int m_iTotalNumOfPrimitives;int m_iNumOfVerticesZ ; int m_iNumOfVerticesX  ; int m_iTotalNumOfVertices  = m_iNumOfVerticesZ * m_iNumOfVerticesZ;////////////////////////////////////////////////////////////////////////////////	// Denfines my own custom D3D vertex format used by the vertex buffer  	struct TERRAINVERTEX 	{ 		float posX, posY, posZ; // Vertex XYZ position 	}; 	#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ) //////////////////////////////////////////////////////////////////////////////////Initialisetion of d3d/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////HRESULT InitialiseD3D(HWND hWnd){if( NULL == ( gpD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ){return E_FAIL;}D3DDISPLAYMODE d3ddm;if(FAILED(gpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))){return E_FAIL;}D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;d3dpp.BackBufferFormat = d3ddm.Format;d3dpp.EnableAutoDepthStencil = TRUE ;  d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//////////////////////////////////////////////////////////////if(FAILED(gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))){return E_FAIL;}g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);g_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, 255);return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT FeedVertexBuffer(){	TERRAINVERTEX*         m_pVertices;g_pD3DDevice->CreateVertexBuffer( ((sizeof m_pVertices) * m_iNumOfVerticesX * m_iNumOfVerticesZ), 0, D3DFVF_TERRAINVERTEX, D3DPOOL_DEFAULT, &m_pTerrVertexBuffer);if( SUCCEEDED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) )  { MessageBox(NULL, " FeedVertex" , "error",MB_OK|MB_ICONEXCLAMATION); return S_OK;}    int z = 32;    int x = 32;    D3DXVECTOR3 NormalVector;	TERRAINVERTEX* pVertex;	int iVertexBuffOffset = 0;	int terrScale = 8;// loop as many times as there are vertices along x & Z axes calculating the coordinates for each terrain vertex (X,Y,Z) and store // in VB in a linear fashion. NOTE: the 2D regular grid (X,Y) is being transformed into 3D (X,Y,Z) terrain grid with height equal to 0.	for (int z = 0; z < m_iNumOfVerticesZ; z++)	//vertical rows	{		for (int x = 0; x < m_iNumOfVerticesX; x++) //horizontal rows		{			iVertexBuffOffset = x +( z * m_iNumOfVerticesX);			pVertex = &( m_pVertices[ iVertexBuffOffset ] );            // 1. Calculate each terrain vertex coordinates			pVertex->posX = (float)x * terrScale;            			pVertex->posY = 0.0; //constant terrain height			pVertex->posZ = (float)z * terrScale;		}	}m_pTerrVertexBuffer->Unlock();	return S_OK;}////////////////////////////////////////////////////////////////////////////////HRESULT CreateTerrainIndexBuffer(){	int iTotalNumOfIndices;	WORD	*pIndices;	UINT	Index;	int IndexOffset = 0;// Calculate the number of indices	iTotalNumOfIndices = ((m_iNumOfVerticesX * 2) + 2) * (m_iNumOfVerticesZ - 1);// Create the Index Buffer	if( FAILED( g_pD3DDevice->CreateIndexBuffer( iTotalNumOfIndices *sizeof(WORD),											D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,                                            D3DPOOL_MANAGED, &m_pTerrIndexBuffer ) ) )      {		  return E_FAIL;}	if( FAILED( m_pTerrIndexBuffer->Lock(   0,  // Fill from start of the buffer.                                            0,	// Size of the data to load.                            (BYTE**)&pIndices,	// Returned index data.                                       0 ) ) )	// Send default flags to the lock.{		  return E_FAIL;}// Use the following equation to get the number of primitives to drawm_iTotalNumOfPrimitives = ( (m_iNumOfVerticesX + 1) * (m_iNumOfVerticesZ - 1) * 2 ) - 2;// Fill in the index buffer with vertex ID linking the vertex buffer	for( int z = 0; z < m_iNumOfVerticesZ-1; z++ )	{		for( int x = 0; x < m_iNumOfVerticesX; x++ )		{			Index = x + ( z * m_iNumOfVerticesX );			pIndices[IndexOffset] = (WORD)Index;	    	IndexOffset++;			Index = x +( (z+1) * m_iNumOfVerticesX );			pIndices[IndexOffset] =(WORD)Index;			IndexOffset++;		}//Place in copy of previous one		pIndices[IndexOffset] =(WORD)Index;		IndexOffset++;//Place in first one for next row		Index = 0 + ( (z+1) * m_iNumOfVerticesX );		pIndices[IndexOffset] = (WORD)Index;		IndexOffset++;	}	m_pTerrIndexBuffer->Unlock();	return S_OK;} ////////////////////////////////////////////////////////////////////////////////VOID SetupMatrices(){     // For our world matrix, we will just leave it as the identity    D3DXMATRIX matWorld;    D3DXMatrixRotationY( &matWorld, 0);    g_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );    // Set up our view matrix. A view matrix can be defined given an eye point,    // a point to lookat, and a direction for which way is up. Here, we set the    // eye five units back along the z-axis and up three units, look at the     // origin, and define "up" to be in the y-direction.    D3DXMATRIX matView;    D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( -5.0f, 0.0f,-3.0f ),//x turns it side ways,y moves it away,z moves it closer                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );    g_pD3DDevice->SetTransform( D3DTS_VIEW, &matView );    // For the projection matrix, we set up a perspective transform (which    // transforms geometry from 3D view space to 2D viewport space, with    // a perspective divide making objects smaller in the distance). To build    // a perpsective transform, we need the field of view (1/4 pi is common),    // the aspect ratio, and the near and far clipping planes (which define at    // what distances geometry should be no longer be rendered).    D3DXMATRIX matProj;    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );    g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );}//////////////////////////////////////////////////////////////////////////////////the Render////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void Render(){D3DXMATRIX matView;	D3DXMATRIX TranslationMatrix; /////////////////////////////////////////////////////////////g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,                                       D3DCOLOR_XRGB(0,0,0), 1.0f, 0); // What ever this does it is important ////////////////////////////////////////////////// if(SUCCEEDED(g_pD3DDevice->BeginScene())){SetupMatrices();g_pD3DDevice->SetStreamSource( 0, m_pTerrVertexBuffer, sizeof(TERRAINVERTEX) );g_pD3DDevice->SetVertexShader( D3DFVF_TERRAINVERTEX );g_pD3DDevice->SetIndices( m_pTerrIndexBuffer, 0 );//Renders the specified geometric primitive, based on indexing into an array of vertices.g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, m_iTotalNumOfVertices, 0, m_iTotalNumOfPrimitives);//////////////////////////////////////////////g_pD3DDevice->EndScene();}g_pD3DDevice->Present(NULL, NULL, NULL, NULL);}//////////////////////////////////////////////////////////////////////////////////shuts down d3d////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void CleanUp(){if( m_pTerrVertexBuffer != NULL) m_pTerrVertexBuffer->Release();if (g_pD3DDevice !=NULL){g_pD3DDevice->Release();g_pD3DDevice = NULL;}if(gpD3D != NULL){gpD3D->Release();gpD3D = NULL;}}////////////////////////////////////////////////////////////////////////////////LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {        case WM_DESTROY:            PostQuitMessage( 0 );            return 0;break;        case WM_KEYUP:        switch (wParam)        {        case VK_ESCAPE:        DestroyWindow(hWnd);        return 0;        break;        }        }    return DefWindowProc( hWnd, msg, wParam, lParam );}//////////////////////////////////////////////////////////////////////////////////Win Main//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,                      "D3D Tutorial", NULL };   RegisterClassEx( &wc );// Create the application's window    HWND hWnd = CreateWindow( "D3D Tutorial", "My window",                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,                              GetDesktopWindow(), NULL,(HINSTANCE)wc.hInstance, NULL );////////////////////////////////////////////////////////////////////////////////  //The Game Loop/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////               	if( SUCCEEDED( InitialiseD3D( hWnd  ) ) ) {         MessageBox(NULL, "Initd3d worked" , "error",MB_OK|MB_ICONEXCLAMATION);        // Create the scene geometry          if( SUCCEEDED(  CreateTerrainIndexBuffer()));          {          MessageBox(NULL, "createTerain worked" , "error",MB_OK|MB_ICONEXCLAMATION);               ShowWindow( hWnd, SW_SHOWDEFAULT );                UpdateWindow( hWnd );         MSG msg;         ZeroMemory(&msg, sizeof(msg));        while(1)   {       if (PeekMessage( &msg, NULL, 0, 0,PM_REMOVE ) )    {       if(msg.message == WM_QUIT)       break;            TranslateMessage( &msg );            DispatchMessage( &msg );     }        else        Render();        } } } CleanUp(); UnregisterClass( "D3D Tutorial", (HINSTANCE) wc.hInstance ); return 0;}

This is what I have for but it diplays only a window and a black screen and nothing else what do you think

[edited by - kingpinzs on June 20, 2003 9:25:52 PM]
if( SUCCEEDED( m_pTerrVertexBuffer->Lock( 0, 0, (BYTE**)&m_pVertices, 0 ) ) )  {     MessageBox(NULL, " FeedVertex" , "error",MB_OK|MB_ICONEXCLAMATION);     return S_OK;}   


Again I say, look at this part of code,

what you are saying is this "If my lock was succesful, then do the message box and return out of the function"

EDIT: to be even more clear, if you return out of the function if your VB->Lock is succesful, you never fill the buffer with data.

You dont need this chunk of code at all, cause if you are succesful at creating a VB, it WILL lock.



[edited by - RhoneRanger on June 21, 2003 4:02:48 AM]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno

This topic is closed to new replies.

Advertisement