Sign in to follow this  
  • entries
  • comments
  • views

Measurable Progress!

Sign in to follow this  


After making my D3D renderer look a bit more like reasonable C++ code, I decided I should actualy use it to show something on the screen (fancy that). After a few hours of work today, I have height mapping! Yay!

...Okay, its a little hard to get excited over something like height mapping, but at least its some form of visual feedback that my renderer works.


I also have texture support in. I support fancy stuff like multitexturing in my engine, but haven't done anything to really utilize it yet.

The code itself is probably not nearly as pretty or optimized as it could/should be, but I'll address that later. Here is my heighmap class:



#include "bitmap.h"
#include "Renderer.h"
#include "structs.h"

class Heightmap
Heightmap(unsigned numRows, unsigned numCols);

int LoadHeightmap(std::string filename, HDC hdc);

int Draw();

CBitmap *m_bitmap; //holds height info
int m_bufferIndex;

int m_numRows, m_numCols;
int m_xstep, m_ystep;

int CreateVertsFromBitmap();
MapVertex* CreateVertex(MapVertex *vert, int x, int z);

inline int GetIndexFromXZ(int x, int z);



#include "../Header Files/heightmap.h"
#include "../Header Files/defines.h"
#include "../Header Files/structs.h"
#include "../Header Files/Renderer.h"

extern CRenderInterface* g_renderer;

Heightmap::Heightmap(unsigned numRows, unsigned numCols) : m_numRows(numRows), m_numCols(numCols), m_bitmap(NULL), m_bufferIndex(-1)


int Heightmap::LoadHeightmap(std::string filename, HDC hdc)
m_bitmap = new CBitmap();

if ( !m_bitmap->LoadBitmap(filename, hdc) )
return FAIL;

int width = m_bitmap->GetWidth();
int height = m_bitmap->GetHeight();

m_xstep = width / m_numCols;
m_ystep = height / m_numRows;

return CreateVertsFromBitmap();

int Heightmap::Draw()

float camX = 20.0f;
float camZ = 40.0f;
float camY = 15.0f;
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 at(0.0f, 00.0f, 0.0f);
D3DXVECTOR3 eye( camX, camY, camZ );
D3DXMatrixLookAtLH(&view, &eye, &at, &up );

// Set our view matrix
reinterpret_cast(g_renderer)->GetDevice()->SetTransform( D3DTS_VIEW, &view );


return 0;

MapVertex* Heightmap::CreateVertex(MapVertex *vert, int x, int z)
BYTE *line = m_bitmap->GetLinePtr(z);
vert->tv = static_cast<float>(z) / (m_numRows - 1);
vert->tu = static_cast<float>(x) / (m_numCols - 1);

vert->x = static_cast<float>(x);
vert->y = line[x * m_xstep * m_bitmap->GetChannels()] / 30.f;
vert->z = static_cast<float>(z);

vert->color = COLOR_ARGB(255, line[x * m_xstep * m_bitmap->GetChannels()], 0, 100);

return vert;

int Heightmap::CreateVertsFromBitmap()
int numVerts = (m_numRows - 1) * (m_numCols - 1) * 6;;

MapVertex *verts = new MapVertex[numVerts];

MapVertex vert = {0};

for (int z = 0; z < m_numRows - 1; ++z)
for (int x = 0; x < m_numCols - 1; ++x)
int startIndex = GetIndexFromXZ(x, z) * 6;

verts[startIndex] = *(CreateVertex(&vert, x, z));
verts[startIndex+1] = *(CreateVertex(&vert, x, z+1));
verts[startIndex+2] = *(CreateVertex(&vert, x+1, z+1));
verts[startIndex+3] = *(CreateVertex(&vert, x, z));
verts[startIndex+4] = *(CreateVertex(&vert, x+1, z+1));
verts[startIndex+5] = *(CreateVertex(&vert, x+1, z));
return g_renderer->CreateStaticBuffer(MAP_FVF, TRIANGLE_LIST, numVerts, 0,
sizeof(MapVertex), (void**)&verts, NULL, &m_bufferIndex);

inline int Heightmap::GetIndexFromXZ(int x, int z)
return (z * (m_numCols-1)) + x;

As you can see, it is noticably devoid of comments, sorry about that. I also muck about with my lookat matrix when I draw my heightmap; next up on my agenda is a slightly more robust camera system. Hopefully that will be done by midnight or so, so I can work on model loading and animation tomorrow.

Oh, and this is also the first time I used my GDNet+ webspace. Hooray webspace!
Sign in to follow this  

1 Comment

Recommended Comments

Oye! Documentation!!! Things look really good and I only wish to be skilled enough to do that by myself. Great job, keep it up!

Share this comment

Link to comment

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