Sign in to follow this  
Kiryn

Terrain Lines

Recommended Posts

I'm not the same guy as the other but similar issues. My terrain is coming out funky. it suppose to look like I just borrow the sample.raw from that tutorial. But different code. Heres my terrain code.
#include "FlareXTerrain.h"

FlareXTerrain::FlareXTerrain(const unsigned int height, const unsigned int width)
{
	this->height = height;
	this->width = width;
	FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
}

IDirect3DVertexBuffer9 *FlareXTerrain::LoadTerrain(IDirect3DDevice9 *device, std::string filename, D3DCOLOR color)
{
	IDirect3DVertexBuffer9 *buffer = NULL;
	std::vector<TerrainFVF> tfvf(width * height);
	std::ifstream terrainFile(filename.c_str(), std::ios::binary);

	if(terrainFile.is_open())
	{
		for(int x = 0; x != width; ++x)
		{
			for(int y = 0; y != height; ++y)
			{
				tfvf[y * width + x].x = (float)-x;
				tfvf[y * width + x].y = (float)y;
				tfvf[y * width + x].z = (float)terrainFile.get() / 50;
				tfvf[y * width + x].color = color;
			}
		}
	}
	else
	{
		::MessageBox(NULL, L"file cannot be found", L"File not found", MB_OK | MB_ICONERROR);
	}

	terrainFile.close();

	if(FAILED(device->CreateVertexBuffer(width * height * sizeof(TerrainFVF), 0, FVF, D3DPOOL_MANAGED, &buffer, NULL)))
	{
		::MessageBox(NULL, L"Buffer creation failed", L"Failed buffer", MB_OK | MB_ICONERROR);
	}

	void *pVoid;

	buffer->Lock(0, 0, (void**)&pVoid, 0);
	::memcpy(pVoid, &tfvf[0], tfvf.size() * sizeof(TerrainFVF));
	buffer->Unlock();
	pVoid = NULL;

	return buffer;
}

IDirect3DIndexBuffer9 *FlareXTerrain::LoadIndices(IDirect3DDevice9 *device)
{
	IDirect3DIndexBuffer9 *buffer;

	std::vector<unsigned short> terrainIndex((width - 1) * (height - 1) * 6);
	unsigned counter = 0;
	for(unsigned x = 0; x < (width - 1); x++)
	{
		for(unsigned y = 0; y < (height - 1); y++)
		{
			terrainIndex[counter++] = x + y * width;
			terrainIndex[counter++] = (x + 1) + y * width;
			terrainIndex[counter++] = (x + 1) + (y + 1) * width;
			terrainIndex[counter++] = (x + 1) + (y + 1) * width;
			terrainIndex[counter++] = x + y * width;
			terrainIndex[counter++] = x + (y + 1) * width;
		}
	}

	if(FAILED(device->CreateIndexBuffer((width - 1) * (height - 1) * 6 * sizeof(unsigned short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
		D3DPOOL_MANAGED, &buffer, NULL)))
	{
		::MessageBox(NULL, L"Buffer creation failed", L"Failed buffer", MB_OK | MB_ICONERROR);
	}

	void *pVoid;

	buffer->Lock(0, 0, (void**)&pVoid, 0);
	::memcpy(pVoid, &terrainIndex[0], terrainIndex.size() * sizeof(unsigned short));
	buffer->Unlock();
	pVoid = NULL;

	return buffer;
}


And the code where i'm calling it from.
[source code="cpp"]
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "FlareX.h"
#include "FlareXFPS.h"
#include "FlareXTerrain.h"
#include "Win32.h"

IDirect3DDevice9 *d3ddev;
ID3DXFont *fpsFont;
IDirect3DVertexBuffer9 *vertBuffer;
IDirect3DIndexBuffer9 *indexBuffer;
FlareX flareX(480, 640);
FlareXFPS flareXFPS;
FlareXTerrain flareXTerrain(64, 64);

void RenderScene();
void Camera();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	flareX.InitD3D(hInstance, true, &d3ddev, MsgProc);
	flareXFPS.StartTimer();

	D3DXCreateFont(d3ddev, 20, 0, 700, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH |
				   FF_DONTCARE, L"Arial", &fpsFont);
	std::wstring filename = L"sample.raw";
	vertBuffer = flareXTerrain.LoadTerrain(d3ddev, filename, D3DCOLOR_XRGB(255, 255, 255));
	indexBuffer = flareXTerrain.LoadIndices(d3ddev);

	d3ddev->SetRenderState(D3DRS_LIGHTING, false);
	d3ddev->SetRenderState(D3DRS_ZENABLE, true);
	d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

	MessageLoop(&RenderScene, hInstance);
	flareX.Release<IDirect3DDevice9*>(d3ddev);
	flareX.Release<ID3DXFont*>(fpsFont);
	flareX.Release<IDirect3DIndexBuffer9*>(indexBuffer);
	flareX.Release<IDirect3DVertexBuffer9*>(vertBuffer);
}

void RenderScene()
{
	RECT fpsRECT;
	std::wstring fpsString;
	std::wstringstream tempFPSString;
	tempFPSString << "FPS: " << int(flareXFPS.GetFPS());
	fpsString = tempFPSString.str();
	
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 51, 102), 1.0f, 0);
	
	if(SUCCEEDED(d3ddev->BeginScene()))
	{
		d3ddev->SetFVF(flareXTerrain.FVF);
		Camera();
		d3ddev->SetStreamSource(0, vertBuffer, 0, sizeof(flareXTerrain.FVF));
		d3ddev->SetIndices(indexBuffer);

		::SetRect(&fpsRECT, 5, 4, 40, 40);
		fpsFont->DrawText(NULL, fpsString.c_str(), -1, &fpsRECT, DT_LEFT | DT_NOCLIP, D3DCOLOR_XRGB(0, 0, 0));
		d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, flareXTerrain.width * flareXTerrain.height, 0,
			((flareXTerrain.width - 1) * (flareXTerrain.height - 1) * 2));
		d3ddev->EndScene();
	}

	d3ddev->Present(NULL, NULL, NULL, NULL);
	flareXFPS.Flip();
}

void Camera()
{
	D3DXMATRIX matView;
	D3DXMATRIX matProj;
	D3DXMATRIX matTran;
	D3DXMATRIX matRot;

	static float location = 0.0f;
	location += 0.05f;

	D3DXMatrixRotationY(&matRot, location);

	D3DXMatrixTranslation(&matTran, 30.0f, -32.0f, 0.0f);

	//d3ddev->SetTransform(D3DTS_WORLD, &matTran);

	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(-60.0f, 0.0f, 50.0f),
		&D3DXVECTOR3(-5.0f, 0.0f, 0.0f),
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	d3ddev->SetTransform(D3DTS_VIEW, &matView);

	D3DXMatrixPerspectiveFovLH(&matProj,
		D3DX_PI / 4,
		640/480, 1.0f, 100.0f);
	d3ddev->SetTransform(D3DTS_PROJECTION, &matProj);
}





[Edited by - Kiryn on June 5, 2007 6:27:02 AM]

Share this post


Link to post
Share on other sites
I'm not sure if it's causing the problem, but I do think your calls to memcpy pass the wrong size parameter, since memcpy wants the size in bytes. You could multiply the 3rd parameter by sizeof(TerrainFVF), but that's still not "safe".

I would suggest that instead of declaring pVoid as a void*, you declare it as a TerrainFVF* and simply cast it to void** when you lock the vertex buffer. Then you can use std::copy or some similar method. (You could probably lock it directly into the first element of the vector, but I'd avoid that [wink])

Share this post


Link to post
Share on other sites
I've tired all that, its a no go, I've spent 5 hours trying to figure this thing out and I cannot figure it out to save my life :'(

Share this post


Link to post
Share on other sites
There's clearly something wrong with the creation of your vertex- or index-buffer (or both). These things are a real pain to debug, especially when you only have a piece of source to look at, but the best way to get things fixed is to trace through the creation of each buffer one step at a time and manually verify the results for at least the first pass of the outer loop. It sounds daunting, but it's better to spend a half hour tracing and sort things out, than a half hour randomly changing loop bounds and failing [wink].

It may also be worth temporarily modifying the DrawIndexedPrimitive call to only draw the first few polygons. It's easier to visualise the problem when you can see the trees for the forest, so to speak.

Admiral

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