Jump to content
  • Advertisement
Sign in to follow this  

Draw 2D Traingle Pixel by Pixel

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, does anyone know how to draw a simple equalateral triangle pixel by pixel? The reason I need to draw it pixel by pixel is that each pixel could have a different colour or effect applied to it. I can draw a circle (or donut) easily cause there's a simple formula, but the formula to draw a triangle evades me. What I currently do, is create a CRgn and creates a polygon region using 3 points. With this I test whether a point is inside the region and to draw it if it is. Unfortunately, this means I cannot interpolate the edges with the background because I don't know where the edges are. Please help. :) Thanks.

Share this post


Link to post
Share on other sites
Advertisement
When I made something a few years ago that drew in the insides of a triangle it stored "scanlines" as it was drawing the line and then used those scanlines to perform the drawing. This is commonly used for polygon filling, you can search with google.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrominium
Hi, does anyone know how to draw a simple equalateral triangle pixel by pixel? The reason I need to draw it pixel by pixel is that each pixel could have a different colour or effect applied to it.


Hrm. How are you going to specify the colour/effect for a given pixel, if you don't already know the set of pixels you're going to draw (in which case you wouldn't be asking)?

It's usual to assign effects to the corners or vertices of a triangle, and interpolate them internally - and 3D APIs normally do all of that sort of thing automatically. See what happens in OpenGL for example when you assign a different colour at each vertex of a triangle. You might also want to google things like "Gouraud shading" (there are others but they don't come to mind :( ).

Share this post


Link to post
Share on other sites
Thanks for the help! I didn't expect such a quick reply. :) I didn't know what to search for so I'm glad I know now. I need some kind of 2D graphics technique article or something.

Quote:
Original post by Zahlman
Hrm. How are you going to specify the colour/effect for a given pixel, if you don't already know the set of pixels you're going to draw (in which case you wouldn't be asking)?


The only thing I know are the 3 points in screen coordinates, and the colour for the various parts of the triangle. Working out what colour each pixel should be is easy, but I found it difficult to find out if that pixel should be drawn (indes or outside the triangle).

It's for a application and part of the UI, so I'm trying to avoid 3D APIs such as OpenGL or DirectX.

Share this post


Link to post
Share on other sites
Let v1, v2 and v3 be 3 vectors.
Every pixel on the triangel is given with
v=a.v1+b.v2+c.v3 where a,b and c are posetiv reals and a+b+c=1

Share this post


Link to post
Share on other sites
So, you'll need to write a software Gouraud triangle rasterizer. This is how 3D games were done before accelerated hardware. It isn't too hard, fortunately, and the Net is full of good resources for it. Check out, for instance, the legendary (for Finnish demo coders, anyway :p) 3DIca 3D programming tutorial (the link takes you directly to the relevant portion of the document).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!