Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I understand the benefits of GPU rendering and such, but for a certain limited application I need to render textured triangles purely using CPU.

I've built a 3D engine capable of object handling, transform, projection, culling and the likes ... now all I need is a little code snippet that draws a single textured triangle onto a bitmap... any language accepted!

  • Inputs: Texture bitmap, Triangle U/V/W coords, Triangle X/Y screen coords

  • Output: The textured triangle drawn at the given screen coords

enter image description here

I've currently been using a platform function to draw triangles to screen, but I'm looking to handle it myself to speeden up the process.

share|improve this question
    
Actually, if you want to do 3D you need to draw to 3D coordinates in order to transform the texture properly, same texture coordinates and screen coordinates does not equal the same rendering if the triangles are supposed to be oriented differently in space. –  eBusiness Nov 13 '11 at 8:01
    
Your question is also a bit broad, have you got problems with the maths? Or why can't you write this yourself? Also, what quality do you aim at? We could do anything from nearest pixel centre to integral sampling. –  eBusiness Nov 13 '11 at 8:07
    
@eBusiness - Correct. I have the X/Y/Z of the screen coordinates and also the U/V/T of the the texture coordinates. Maths? I'm not a math expert, although I can manage moderately complex things such as vectors and planes. I'm looking at something quite fast, performance first, some sampling would be useful but not necessary. Nearest pixel should be great for starts so I can check the performance. –  Jarvis Nov 13 '11 at 10:56
    
I'm not sure if it's possible to write a faster implementation of drawTriangle in Actionscript, as the platform function is most likely a "native" implementation. I might be wrong though. Maybe have a look how other 3D engines like Away3D do it? –  bummzack Nov 13 '11 at 11:28
    
@Nicol - I don't have the time or the expertise to invent my own textured triangle renderer. I'm not asking for a full scanline renderer, just a simple function to rasterize a single triangle. This isn't generic behaviour, review my other questions if you think I'm an idiot who simply steals the work of someone else's sweat. –  Jarvis Nov 15 '11 at 12:35
add comment

1 Answer

up vote 8 down vote accepted

With full source (or even just a thorough explanation) of a perspective correct textured triangle rasterizer being too long for an answer, I'll gladly refer you to Chris Hecker's classic series of articles on the topic, including source:

http://chrishecker.com/Miscellaneous_Technical_Articles

From your link I assume you are using Flash as a platform, so I sure hope you know how to efficiently turn on/off individual pixels there.

share|improve this answer
    
thanks, but I was looking at a more complete code snippet that I could simply port and run. Is this possible anyhow? and what do you mean by turn "on and off" pixels? I'm aware that you can draw into Bitmaps and place those onstage to have them rendered. Stage quality can also be modified to increase speed. Anything else? –  Jarvis Nov 13 '11 at 10:57
2  
There is full (complete, working) sample code included with the articles (last download) It's just not "a short and easy to port snippet", because manually rasterizing textured triangles isn't short and easy. –  Paul-Jan Nov 13 '11 at 12:17
    
and what do you mean by turn "on and off" pixels? I'm aware that you can draw into Bitmaps and place those onstage to have them rendered. Stage quality can also be modified to increase speed. Anything else? –  Jarvis Nov 15 '11 at 12:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.