Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
12 years ago

How to draw a triangle?

I'd like to draw a solid triangle either to screen or a bitmap that i can later use - how is this simple task to be accomplished?
Triangle will be a simple color (no pattern), partially transparent (yes alpha). I am looking at ifDraw2D and see no means for that. Graphic libraries usually have a polygon-fill primitive, which i's even better - but i could live with a mere triangle (any polygon can be broken down to triangles anyway). No flood-fill either.

Drawing it with line segments, one end pinned at vertex A while the other end loops from B to C won't work - color intensity near A will be higher than near BC due to alpha blending/addition of the lines. Don't want to use pre-rasterized images in consideration of different screen resolutions (don't want diagonal lines jagged by scaling) - also the number of images needed will be too big to package with channel.

18 Replies

  • Thank you for chiming in. Thread is not too old, i have neither solved, nor given up on the idea i have (just haven't got developing on it - i have small list of things i work on, on and off).

    I did think of scan-line rasterizing (it's not hard but will have to be careful with degenerate cases, like all 3 vertices on same horizontal line + alpha blending). Initially i was concerned that when i am working in non-native resolution (say if i use roScreen is 854x480 when player is in 720p or 1080p mode), that will lead to triangles turning into horizontal stripes, with gaps forming between the horizontal lines i draw due to zooming. I did not find discussion anywhere how the scaling is done - it could be vector or bitmap-wise, with different outcomes. I still don't know for sure and since it is not documented, it can potentially change (?!).

    But performance is my biggest concern - as an idea of speed i would hope to be able to draw something like this spinning tetrahedron:
    <snip>
    My gut feeling is line-by-line won't be able to. Was your routine close to such speed, ballpark?

    bump bump bump ( better late than never )

    why alpha blending? isn't alpha on roku 2D compositing alpha only? my memory is kinda fuzzy on alpha in 3D, but I vaguely rem it has something to do with raycasting, no?

    anyway, I bring it up as it might be a bottleneck. (maybe. maybe not.)

    as for performance, try this: http://bloggingwordpress.com/2012/08/ro ... ster-demo/

    and for those too lazy to click on the link, instead of real-time, why not have your triangle render engine be an offline one, creating sprite sheets for you at load time?

    "rokujoel" wrote:
    “can we draw to a bitmap directly and then show that bitmap?”

    Effectively, drawing to a bitmap is the same operation as drawing to the screen – the screen is sort of a giant bitmap, pretty much anything that works on the screen will work to a bitmap, I believe you will need to call bitmapname.finish() after drawing to bitmaps, just like to the screen,(weird stuff can happen if you don’t) or possibly bitmapname.swapbuffers() under double buffering situations (not sure if bitmaps other than the screen can be double buffered).


    but I'll be bold and say YES! that is possible, with backface culling ( alpha issue? ), I totally believe a Roku 2 can draw 2 (flat shaded) Triangles in realtime using BrightScript*... but probably not much else. 😉

    * disclaimer, I'm not even going to guess what the FPS would be.
  • Be aware that drawing first to a bitmap, then to the the screen requires twice as many draws, which will further slow down your render time.
  • "TheEndless" wrote:
    Be aware that drawing first to a bitmap, then to the the screen requires twice as many draws, which will further slow down your render time.

    good call.

    So, then shouldn't the pre-rendered sprite-sheet solution work? Assuming sufficient time between 3D objects are used?
  • "dev42" wrote:
    "TheEndless" wrote:
    Be aware that drawing first to a bitmap, then to the the screen requires twice as many draws, which will further slow down your render time.

    good call.

    So, then shouldn't the pre-rendered sprite-sheet solution work? Assuming sufficient time between 3D objects are used?

    In theory.
  • "dev42" wrote:
    why alpha blending? isn't alpha on roku 2D compositing alpha only? my memory is kinda fuzzy on alpha in 3D, but I vaguely rem it has something to do with raycasting, no?
    What 3D?! I never said 3D... did the animated GIF above mislead you - those just 2 triangles visible at any one time, showing the speed and size i was looking for. I am a simple, 2D person 8-)

    The Alpha is for see-through when two partially transparent figures overlap (fancification).

    as for performance, try this: http://bloggingwordpress.com/2012/08/ro ... ster-demo/
    That one is interesting - thanks, I need to look at the code - image is horribly grainy though, like drawn with a chainsaw.

    and for those too lazy to click on the link, instead of real-time, why not have your triangle render engine be an offline one, creating sprite sheets for you at load time?
    Nope, not really - the game should work offline, no justification to make it depend on outside server. Figures be moving and rotating, can't precompute it all. Theoretically there is ifDraw2D.DrawRotatedObject but that one is limited to 90 degree rotations (i.e. mostly useless). Unofficially, a few of the players (with GLES - 3xxx, 4200) will rotate to non-multiples of 90 but the rest just draw Nada.
  • and for those too lazy to click on the link, instead of real-time, why not have your triangle render engine be an offline one, creating sprite sheets for you at load time?
    Nope, not really - the game should work offline, no justification to make it depend on outside server. Figures be moving and rotating, can't precompute it all. Theoretically there is ifDraw2D.DrawRotatedObject but that one is limited to 90 degree rotations (i.e. mostly useless). Unofficially, a few of the players (with GLES - 3xxx, 4200) will rotate to non-multiples of 90 but the rest just draw Nada.

    by "offline" I meant to say precomputed. Like right before they are needed... like during a "loading" screen or something. If enough rotations of the object are precomputed, stored in your sprite sheet, you can pick and choose which frames to display. Movement is the same for "normal" sprites ( those loaded from disk ).

    As for triangle drawing code:

    http://www.sunshine2k.de/coding/java/Tr ... ation.html

    My guess is without anti-aliasing it'll look pretty jagged, but I still believe it's doable just not sure how much else can be moving on the screen at the same time. 😉
  • Update:

    I can now draw 2D triangles! With alpha!
    http://forums.roku.com/viewtopic.php?f=34&t=75545#p461149

    BTW, I forgot that there was a 3D Ray Casting thread that went along with that blog post:
    http://forums.roku.com/viewtopic.php?f=34&t=51299&p=348167&hilit=raycaster#p348115

    It's been a while since I played around with it. I'm not even sure if I can find the version with RokuJoel's updates. Does anybody remember how he's drawing the rectangles? Is he drawing all the walls? Even those that are occluded? In other words, is there any Hidden Surface Removal going on?

    As for performance improvements, I remember this running pretty slowly, but our Roku's have been updated since then. So, who knows?!

    more when I have it.

    peace & 42