• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
timothyjlaird

problem with heightmapping

5 posts in this topic

I am trying to do some heightmapping with XNA. Basically I'm making a grid of verticies and then linking them together with indicies to form triangles. I then read the red values from all the pixels in a JPG or PNG, divide by 255, multiply by a scale factor and then set each of the 'y' value in the verticies in the grid to that value...giving a rudimentary heightmap. My problem is that the geometry does not look quite right in the render. Also if there is a better way to generate terrain I would like to hear it...because I can't go beyond a 512x512 heightmap without exceeding XNA's max triangle count. I know terrain generation with heightmaps is an ancient problem...and I'm hoping there is a better way to do what I am trying to do. Any ideas on how to make this work better (at least from a geometry standpoint).


The code I use to generate the triangle grid and read the heightmap image:
[CODE]
private float[] getVertexYFromImage(Texture2D displacementmap, int scalefactor)
{
Color[] pixels = new Color[displacementmap.Width * displacementmap.Height];
displacementmap.GetData<Color>(pixels);
float[] vertexy = new float[pixels.Length];
for (int cursor = 0; cursor < pixels.Length; cursor++)
{
//use red channel to get vert height (y)
vertexy[cursor] = ((float)pixels[cursor].R / 255.0f) * scalefactor;
}
return vertexy;
}
private void getTerrainVertexIndexData(int size, int spread, out int[] indexData, out VertexPositionNormalTexture[] vertexData)
{
vertexData = new VertexPositionNormalTexture[size*size];
indexData = new int[3 * (2 * (size - 1)) * (size - 1)];
//get height data for verticies
float[] vertexYData = getVertexYFromImage(displaceT2D, 32);
//build grid of verts
int xcounter = 0;
int zcounter = 0;
int x_vert = 0;
float y_vert = 0;
int z_vert = 0;
float u = 0.0f;
float v = 0.0f;
for (int index = 0; index < vertexData.Length; index++)
{
y_vert = vertexYData[index];
vertexData[index].Position = new Vector3(x_vert, y_vert, z_vert);
vertexData[index].Normal = new Vector3(0.0f, 0.0f, 0.0f);
vertexData[index].TextureCoordinate = new Vector2(u, v);
if (xcounter < (size - 1))
{
x_vert = x_vert + spread;
xcounter++;
}
else
{
x_vert = 0;
xcounter = 0;
z_vert = z_vert + spread;
zcounter++;
}
u = (float)xcounter / (size - 1);
v = (float)zcounter / (size - 1);
}
//build indixies to form triangles from verts
int icounter = 0;
int cursorx = 0;
int skipcounter = size-1;
for (int currentstrip = 0; currentstrip < (size - 1); currentstrip++)
{
for (int count = 0; count < (size - 1); count++)
{
indexData[icounter] = cursorx;
indexData[icounter + 1] = cursorx + 1;
indexData[icounter + 2] = cursorx + 1 + (size);
icounter = icounter + 3;
indexData[icounter] = cursorx;
indexData[icounter + 1] = cursorx + 1 + size;
indexData[icounter + 2] = cursorx + size;
icounter = icounter + 3;
cursorx++;
}
if (cursorx == skipcounter)
{
cursorx++;
skipcounter = skipcounter + size;
}
}
//calculate normals for smooth shading
for(int counter=0; counter<indexData.Length / 3; counter++)
{
Vector3 firstvec = vertexData[indexData[counter * 3 + 1]].Position - vertexData[indexData[counter * 3]].Position;
Vector3 secondvec = vertexData[indexData[counter * 3]].Position - vertexData[indexData[counter * 3 + 2]].Position;
Vector3 normal = Vector3.Cross(firstvec, secondvec);
normal.Normalize();
vertexData[indexData[counter * 3]].Normal += normal;
vertexData[indexData[counter * 3 + 1]].Normal += normal;
vertexData[indexData[counter * 3 + 2]].Normal += normal;
}
for (int counter = 0; counter < vertexData.Length; counter++)
{
vertexData[counter].Normal.Normalize();
}
}
[/CODE]

Thanks,
John
0

Share this post


Link to post
Share on other sites
[quote name='timothyjlaird' timestamp='1339727529' post='4949401']
JPG or PNG
[/quote]
For accurate data you need a lossless compression format, so avoid JPG and stick to lossless compressed PNG to avoid artifacts.

[quote name='timothyjlaird' timestamp='1339727529' post='4949401']
because I can't go beyond a 512x512 heightmap without exceeding XNA's max triangle count
[/quote]
Instead of creating your whole map at once, subdivide it in patches (i.e. 64x64) and create them on-the-fly once you get close to them.
0

Share this post


Link to post
Share on other sites
you could also generate your heightmap/noise in an application and store the heights across all the channels giving you much greater range of depth:

[url="http://www.gamerendering.com/2008/09/25/packing-a-float-value-in-rgba/"]http://www.gamerendering.com/2008/09/25/packing-a-float-value-in-rgba/[/url]

I think that is derived from a post on this forum anyway, should help smooth out the results!
0

Share this post


Link to post
Share on other sites
I figured out how to magnfiy the problem. I just increased my red channel scale factor and this is what happened:

[img]http://i295.photobucket.com/albums/mm151/coffeeaddict21/terrainproblem.jpg[/img]

I found a vertex smoothing algorithm. I'll let you know how it works out...

http://www.nfostergames.com/Lessons/TerrainSmoothing.htm
0

Share this post


Link to post
Share on other sites
fixed...

[img]http://i295.photobucket.com/albums/mm151/coffeeaddict21/terrainproblem_fixed.jpg[/img]

and some of the code that I used (4 passes of it seems to be about right), in the rare chance anyone else runs into the same set of problems...

[CODE]
private float[] getSmoothedVertexData(float[] yvalues, int size)
{
for (int count = 0; count < yvalues.Length; count++)
{
float adjSum = 0.0f;
float adjAvg = 0.0f;
if (count == 0) //left bottom corner
{
adjSum = yvalues[1] + yvalues[size] + yvalues[size+1];
adjAvg = adjSum / 3.0f;
yvalues[count] = (yvalues[count] + adjAvg) / 2.0f;
}
else if(count == (size - 1)) //right bottom corner
{
adjSum = yvalues[size - 2] + yvalues[(size -2)+size] + yvalues[(size -1)+size];
adjAvg = adjSum / 3.0f;
yvalues[count] = (yvalues[count] + adjAvg) / 2.0f;
}
else if(count == (yvalues.Length - size)) //left top corner
{
adjSum = yvalues[(yvalues.Length - size) + 1] + yvalues[(yvalues.Length - size) - size] + yvalues[(yvalues.Length - size) - size + 1];
adjAvg = adjSum / 3.0f;
yvalues[count] = (yvalues[count] + adjAvg) / 2.0f;
}
else if(count == (yvalues.Length - 1)) //right top corner
{
adjSum = yvalues[(yvalues.Length - 2)] + yvalues[(yvalues.Length - 1) - size] + yvalues[(yvalues.Length - 1) - size - 1];
adjAvg = adjSum / 3.0f;
yvalues[count] = (yvalues[count] + adjAvg) / 2.0f;
}
else if (count > 0 && count < size) //bottom strip
{
adjSum = yvalues[count - 1] + yvalues[count + 1] + yvalues[count + size] + yvalues[count + size + 1] + yvalues[count + size - 1];
adjAvg = adjSum / 5.0f;
yvalues[count] = adjAvg;
}
else if (count > (yvalues.Length - size) && (count < (yvalues.Length - 1))) //top strip
{
adjSum = yvalues[count - 1] + yvalues[count + 1] + yvalues[count - size] + yvalues[count - size - 1] + yvalues[count - size + 1];
adjAvg = adjSum / 5.0f;
yvalues[count] = adjAvg;
}
else if ((count % size) == 0) //left strip
{
adjSum = yvalues[count + 1] + yvalues[count - size] + yvalues[count + size] + yvalues[count + size + 1] + yvalues[count - size + 1];
adjAvg = adjSum / 5.0f;
yvalues[count] = adjAvg;
}
else if ((count + 1) % size == 0) //right strip
{
adjSum = yvalues[count - 1] + yvalues[count - size] + yvalues[count + size] + yvalues[count - size - 1] + yvalues[count + size - 1];
adjAvg = adjSum / 5.0f;
yvalues[count] = adjAvg;
}
else
{
int[] adjacentVerts = getAdjacentVertPositions(count, size);
for (int index = 0; index < 8; index++)
{
adjSum = adjSum + yvalues[adjacentVerts[index]];
}
adjAvg = adjSum / 8.0f;
yvalues[count] = (yvalues[count] + adjAvg) / 2.0f;
}
}
return yvalues;
}
private int[] getAdjacentVertPositions(int arrayPos, int size)
{
int[] adjacentVertPositions = new int[8];
adjacentVertPositions[0] = arrayPos - 9;
adjacentVertPositions[1] = arrayPos - 8;
adjacentVertPositions[2] = arrayPos - 7;
adjacentVertPositions[3] = arrayPos + 1;
adjacentVertPositions[4] = arrayPos + 9;
adjacentVertPositions[5] = arrayPos + 8;
adjacentVertPositions[6] = arrayPos + 7;
adjacentVertPositions[7] = arrayPos - 1;
return adjacentVertPositions;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[url="http://skytiger.wordpress.com/2010/11/28/xna-large-terrain/"]http://skytiger.wordpress.com/2010/11/28/xna-large-terrain/[/url]
0

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  
Followers 0