• 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
thewhiteaussie

Triangle rasterization troubles

3 posts in this topic

I'm trying to write a couple of functions to draw (filled) flat-top and flat-bottom triangles. For the large part they work however cracks are still sometimes seen between adjacent triangles. I use an interpolating technique whereby I raster the triangle line by line, calculating the new left and right limits at each step. Much of this is explained in the code, but here is the general idea (for a flat bottom):

1) Find dy (height)
2) Find dy/dx (slope) from the top point to each bottom point
3) Move to the starting row ( floor(top point) ), and find initial x-start and x-end coordinates
4) Interpolate down the triangle

I should note that cracks are only seen between triangles that join at the sides, not top/bottom joins. I've been at this so long I don't know what to try anymore. I think the logic is solid, maybe any cracks are due to floating point error. I'd really appreciate some feedback.

[source lang="cpp"]typedef unsigned int uint32;

Line(uint32 y, uint32 x_left, uint32 x_right); //Draws a horizontal line at these coords

struct vector2
{
float x,y;
};

//--------------------------------------------------------------------------------
// Draws a flat bottom triangle from top to bottom
//--------------------------------------------------------------------------------
void Draw_Bottom_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2)
{
//Point order:
//Bottom left: p0
//Bottom right: p1
//Top point: p2

//calculate dy
float dy = p2.y - p0.y;

//dx/dy for the left and right edges
float dxdy_left = (p0.x - p2.x)/dy;
float dxdy_right = (p1.x - p2.x)/dy;

//Since we start the raster process at floor(p2.y)
//we need to shift the x start and x end postions along
//by this factor:
float y_bump = p2.y - floor(p2.y);

//Initial start and end x values
float xs = p2.x + dxdy_left*y_bump; //x left (start)
float xe = p2.x + dxdy_right*y_bump; //x right (end)

uint32 yb = uint32(p0.y) + 1; //y bottom, +1 for top left fill convention
uint32 yt = uint32(p2.y); //y top, use casting instead of std::floor

//Draw lines
for (uint32 i = yt; i >= yb; i--)
{
//Set left and right limits, use casting instead of std::floor
uint32 left = uint32(xs) + 1; //+1 for top left fill convention
uint32 right = uint32(xe);

//Draw line, can also be std::fill or simply a for loop.
Line(i, left, right);

//Increment limits
xs += dxdy_left;
xe += dxdy_right;
}

} //End: Draw_Bottom_Tri_SOLID()


//--------------------------------------------------------------------------------
// Draws a flat top triangle from bottom to top
//--------------------------------------------------------------------------------
void Draw_Top_Tri_SOLID(Vector2 p0, Vector2 p1, Vector2 p2)
{
//Point order:
//Top left: p0
//Top right: p1
//Bottom point: p2

//calculate dy (height)
float dy = p0.y - p2.y;

//dx/dy for the left and right edges
float dxdy_left = (p0.x - p2.x)/dy;
float dxdy_right = (p1.x - p2.x)/dy;

//Find shifting factor
float y_bump = ceil(p2.y) - p2.y;

//Initial start and end x values
float xs = p2.x + dxdy_left*y_bump; //x left (start)
float xe = p2.x + dxdy_right*y_bump; //x right (end)

uint32 yb = uint32(p2.y) + 1; //y bottom, +1 for top left fill convention
uint32 yt = uint32(p0.y) ; //y top

//Draw lines
for (uint32 i = yb; i <= yt; i++)
{
//Set left and right limits
uint32 left = uint32(xs) + 1; //+1 for top left fill convention
uint32 right = uint32(xe);

//Draw line, can be std::fill or simply a for loop.
Line(i, left, right);

//Increment limits
xs += dxdy_left;
xe += dxdy_right;
}


} //End: Draw_Top_Tri_SOLID()


[/source]
0

Share this post


Link to post
Share on other sites
there are quite some possible reasons and they lead to different kind of bugs, if you could show the wireframe and the flatshaded rendering with cracks, it's easier to guess what might go wrong.

one problem might be line 50 and 51, every step you add something to xs/xe, every step after the add there is some rounding, this leads to an increasing error at every step. if you modify it to something like xs=y*dxdy_left+bs; you might already decrease the amount of error.

is that all the code? seems like it's just the top part of the triangle, going on in the 2nd part of the triangle with the wrong data is a common bug that leads to cracks.


I suggest you to check out http://devmaster.net/forums/topic/1145-advanced-rasterization/
it describes how to draw leak free solid triangles with a very simple algorithm, might not be as fast as yours, but it's good to have a working reference, right? :)
1

Share this post


Link to post
Share on other sites
Typically the way to fix this is to quantize your screen-space vertices to some fixed grid. This grid can be finer than the size of a pixel. Then when creating your interpolants you have a finer resolution so can you step using a finer resolution than this quantized grid. If done correctly then the scanline interpolation should never under or over shoot the endpoints. I use fixed point math to store screen space positions and interpolants, though you can use fp math when calculating intermediate values.
1

Share this post


Link to post
Share on other sites

Thanks guys. After much experimentation, the problem was with rounding errors. I have since switch to using a fixed point number representation for greater accuracy and have solved the issue, pretty much as AB suggested.

 

Also, Krypt0n you migh be right about the error introduced by incrementing xs and xe each iteration, I'll check it out.

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