Fastest way to render many triangles


#1

Warning: I am 100% new to SDL and game design and haven’t tried anything before asking this question.

I have this idea to make a game that uses triangular pixel art. So all the graphics will be 100% triangles of different colors. I could do this with saved textures that consist of colored triangles, but I was thinking there should be a faster, more memory efficient, better way to do it.

I was thinking I could save my textures as some image format where each square pixel corresponds to the color of a full triangle, saving the maximum amount of memory and using existing image manipulation stuff to make it efficient and easy to deal with. Then, to render it to the screen, I have a few ideas. I could have one (actually two since there are upside down triangles) triangle texture that I change the color of to render each triangle. I could just separately fill all the individual pixels of each triangle. Or maybe it’s worth dealing with OpenGL for this task, although I would prefer to avoid dealing with that if possible.

But there could be some better way to do this I haven’t thought of. Thanks for any input!


#2

Well, there are many triangle-drawing alorithms out there.
Here is a web page describing a couple of algorithms.

A good algorithm I know is to keep in memory a buffer with two values for each y in the screen: xMin and xMax.

And then, you sort the vertices of the triangle and determine which are to be in xMin, and which in xMax.
And then you fill out the vertices as lines in the buffer. And you call a function that will fill horizontal lines by reading the xMin and xMax values for each Y in the triangle(From the min Y point of the triangle to the max Y point of the triangle).
The pros with this method is that it is fast and does not require to split the triangle.
However, you have to empty the xMin/xMax buffer at the start of each frame. But you could fill them out while you draw the horizontal lines, and it could achieve a little speed gain that way.
The reason why you should use horizontal lines is because they can quickly be filled using memset().

I hope I was clear. I’m not good explaining these things.