Jump to content
  • Advertisement
Sign in to follow this  
hallgeir

Rendering a 2d fractal terrain to a texture

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I have made a simple random fractal terrain generator (2d). The plan is to try making something like Scorched Earth (don't know who remembers that classic, but for you who don't, think Worms). Anyway, I have managed to render the terrain. I use OpenGL for graphics, and every "vertex" needed to render the terrain is stored in an array. Basically the array contains every start/end point for the last generation of lines. The terrain is then rendered by making GL_QUADs. Anyhow, the problem. I would like a destroyable terrain. I think I know how to do that, BUT then I need to somehow get the terrain I generated into a texture, or at least into an array so I can make a texture out of it. So can anyone give me a point in the right direction on how to render my nice little fractal terrain onto a texture? :) Any help is appreciated. [Edited by - ziggwarth on May 13, 2007 11:30:25 AM]

Share this post


Link to post
Share on other sites
Advertisement
The term you are looking for is 'Rasterisation'
This is what your 3d accelerator card normally does, but the same methods can be used by yourself to convert your line segments into the texture map representation for your level data...

'Bresenham's algorithm'
and 'flood fill' should be useful for you.

Share this post


Link to post
Share on other sites
Alright I almost got it... but I've run into a problem: The slopes that go down & right don't get 100% continous... it's missing a few pixels here and there, which could be a little bad when I'm going to use flood fill to fill the entire thing. For the record, the pixels that go UP and to the right are drawn perfectly. It's only those down to the right-slopes that get holes.

I have uploaded a screenshot of the bug (a little zoomed in so it's easier to see) here:
http://img209.imageshack.us/img209/3653/buguy5.jpg
The white line is the terrain, and the green dots are the pixels on the texture, that is supposed to match the white line perfectly..

Here's the code I use to create the image I use for the texture:

//draw the pixels to a texture
for (unsigned int i = 0; i < m_VertexList.GetCount()-1; i++)
{
//vertices for the current line
pos_t p_Vertex1 = *m_VertexList;
pos_t p_Vertex2 = *m_VertexList[i+1];

p_Vertex1.x -= dX;
p_Vertex2.x -= dX;
p_Vertex1.y -= dY;
p_Vertex2.y -= dY;

bool p_bSteep = abs((int)p_Vertex2.y - (int)p_Vertex1.y) > abs((int)p_Vertex2.x - (int)p_Vertex1.x);
//if it's a steep slope, the X and Y values must be switched.
if (p_bSteep)
{
double temp;

temp = p_Vertex1.y;
p_Vertex1.y = p_Vertex1.x;
p_Vertex1.x = temp;

temp = p_Vertex2.y;
p_Vertex2.y = p_Vertex2.x;
p_Vertex2.x = temp;
}

if (p_Vertex1.x > p_Vertex2.x)
{
double temp;

temp = p_Vertex1.x;
p_Vertex1.x = p_Vertex2.x;
p_Vertex2.x = temp;

temp = p_Vertex1.y;
p_Vertex1.y = p_Vertex2.y;
p_Vertex2.y = temp;
}

double p_dDeltaX = abs(p_Vertex2.x - p_Vertex1.x);
double p_dDeltaY = abs(p_Vertex2.y - p_Vertex1.y);

double y = p_Vertex1.y;
int p_nYstep;

double p_dError = 0;
double p_dDeltaErr = (p_dDeltaY / p_dDeltaX);

if (p_Vertex1.y < p_Vertex2.y)
{
p_nYstep = 1;
}
else
{
p_nYstep = -1;
}

for (int x = (int)p_Vertex1.x; x < (int)p_Vertex2.x; x++)
{
p_dError += p_dDeltaErr;
int p_nRowLength = nWidth * 4;

if (p_bSteep)
{
if (x >= 0)
{
p_pcImageData[x * p_nRowLength + ((int)y * 4)] = 0;
p_pcImageData[x * p_nRowLength + ((int)y * 4) + 1] = 255;
p_pcImageData[x * p_nRowLength + ((int)y * 4) + 2] = 0;
p_pcImageData[x * p_nRowLength + ((int)y * 4) + 3] = 255;
}
}
else
{
if (y >= 0)
{
p_pcImageData[(int)y * p_nRowLength + (x * 4)] = 0;
p_pcImageData[(int)y * p_nRowLength + (x * 4) + 1] = 255;
p_pcImageData[(int)y * p_nRowLength + (x * 4) + 2] = 0;
p_pcImageData[(int)y * p_nRowLength + (x * 4) + 3] = 255;
}
}

if (p_dError >= 0.5)
{
y += p_nYstep;
p_dError -= 1.0;
}
}
}


Do anyone have any idea what could be wrong? I've been trying to tweak my code for a couple of hours now with no success at all...

Any help is appreciated, and thanks so far for the help I've already gotten. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!