#### Archived

This topic is now archived and is closed to further replies.

# Modified Polygon Scan Conversion

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

## Recommended Posts

I'm using a modified version of the Polygon Scan Conversion algorithm to build a normal map for an arbitrary polygon. So far, it looks a bit like this: Hopefully you can see the effect I'm heading for: 'rounded' edges to a cave. Given a pixel, I work out how far left you have to move until you hit an edge, how far right, and the same for up and down. I then stick these values into a lookup table, and that gives me the necessary normal. Left and right are dead easy, because I can just iterate along each scanline, incrementing 'left' and decrementing 'right'. Similarly, the distance from the top edge isn't too bad, because I can just allocate an array of counters the same width as the polygon, set the counter at each edge to 0, and increment the counters in between each edge once per scanline. My problem is - how can I efficiently calculate the distance from the bottom edge? Anyone have any ideas? I'm starting to think that I may have to make a second pass, using the same technique as the top edge but working backwards, but that seems wasteful to me. Here's some of the code so far:
// nLength is the length of current scanline

unsigned int nWidth = max(1, min(nLength / 2, CAVE_EDGE_SIZE));
unsigned int nLeft = 0, nRight = nLength;

// Count along current scanline

for (unsigned int x = nStart; x < nStart + nLength; x ++)
{
int nLeftFactor = 0;
int nRightFactor = 0;
int nTopFactor = 0;

// Check if we are within left-hand curvy bit

if (nLeft < nWidth)
{
// Nearer to zero as we approach centre of cave

nLeftFactor = nWidth - nLeft;
}

// Check if we are within right-hand curvy bit

if (nRight < nWidth)
{
// Nearer to zero as we approach centre of cave

nRightFactor = nWidth - nRight;
}

// Check if we're within top curvy bit

if (nTopDistance[x] < CAVE_EDGE_SIZE)
{
// Nearer to zero as we approach centre of cave

nTopFactor = CAVE_EDGE_SIZE - nTopDistance[x];
}

// Interpolate final offset

int nOffset = CAVE_EDGE_SIZE *
(nLeftFactor - nRightFactor) / (int)nWidth;

// Set bump map value

// m_nNormal[y][x] is precalculated lookup table

((D3DCOLOR*)pBump)[x] = m_nNormal[nTopFactor + CAVE_EDGE_SIZE][nOffset + CAVE_EDGE_SIZE];

// Update distances from each edge

nLeft ++;
nRight --;
nTopDistance[x] ++;
}

[edited by - AndyGeers on June 7, 2004 10:05:30 AM] [edited by - AndyGeers on June 7, 2004 10:06:09 AM] [edited by - AndyGeers on June 7, 2004 11:57:07 AM]

##### Share on other sites
Interpolating stuff across arbitrary polygons can easily become hairy and isn''t even certain to be rotationally invariant in some cases. Not to mention what happens if the polygon is clipped.

I would triangulate the polygon and just use regular phong interpolation (interpolate vertex normals along each edge and then between these along the scanline).

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011702
• ### Who's Online (See full list)

There are no registered users currently online

×