<p>It looks like most of the errors in your image are due to the lack of one critical property. That is that, for any <em>edge</em> (two out of three vertices of a triangle), <strong>the rasterization algorithm must have a consistent opinion on which side of the line each pixel falls on, even for different triangles, if those triangles share an edge</strong> (have the same vertex coordinates for 2 vertices).</p>
<p>For example, on the blue face, you have two triangles forming the surface. There are visible gaps, which indicate that the two blue triangles are not meeting perfectly at the edge, which happens because the two rasterizations are not agreeing on which triangle is responsible for filling a given pixel — there's probably some overlap (which is invisible) in addition to the gaps.</p>
<p>Fixing this should also help with the yellow edge on the right and the wiggliness of the red-green edge.</p>
<p>(I do not have any advice on how to implement this property in rasterization, as I haven't dealt with triangle-based software rendering myself; I just know it's necessary.)</p>
<hr />
<p>Additionally, you should implement “back-face culling”: do not draw a triangle if it is facing away from the viewer. Facing is usually defined by triangle “winding order”, clockwise or counterclockwise; one way to understand this is that you take the first two vertices of the triangle as a line with a direction (in screen space), and ask whether the third vertex is to the left (CCW order) or the right (CW order) of that line. That turns out to be equivalent to asking which face of the triangle is being viewed; you pick one such face to not draw, and make sure your mesh's vertex ordering matches that choice consistently.</p>
<p>When you have done this properly, the yellow back-face will not be drawn at all.</p>
<p>I'd recommend fixing your rasterization first, because implementing back-face culling will hide some of the problems. But culling will give you a big speedup since you can stop rendering half of the triangles in the scene (typically).</p>

CC BY-SA 4.0
2022-06-03T19:30:30.400Z
2022-06-04T13:58:50.507Z
2022-06-04T13:58:50.507Z
Kevin Reid
<p>I am currently creating my own 3D renderer. Here is a coloured cube created by the renderer.</p>
<p><a href="https://i.sstatic.net/HdodN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/HdodN.png" alt="cube" /></a></p>
<p>I modelled cube using a set of coloured triangles. Notice extraneous lines from edges and they does not look straight to me. Also there is a visible diagonal line from the bottom face as a result of using two triangles to represent square. How can I eliminate these deficiencies?</p>
<p>I used a variant of Bresenham's Algorithm to draw lines and <a href="http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html" rel="nofollow noreferrer">the standard algorithm</a> to render filled triangles.</p>
<p>I want to improve my triangle and line rendering functions. I wonder how OpenGL are able to render smooth lines and triangles. What kind algorithms they use?</p>

CC BY-SA 4.0
2022-06-02T10:55:18.517Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
<rendering>
How to draw smoother lines and triangles?
Jimmy Yang
# How to draw smoother lines and triangles?

(Asked by: username_0 on 2022-05-30)

I am currently creating my own 3D renderer. Here is a coloured cube created by the renderer.

[![cube](https://i.sstatic.net/HdodN.png)](https://i.sstatic.net/HdodN.png)

I modelled cube using a set of coloured triangles. Notice extraneous lines from edges and they does not look straight to me. Also there is a visible diagonal line from the bottom face as a result of using two triangles to represent square. How can I eliminate these deficiencies?

I used a variant of Bresenham's Algorithm to draw lines and [the standard algorithm](http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html) to render filled triangles.

I want to improve my triangle and line rendering functions. I wonder how OpenGL are able to render smooth lines and triangles. What kind algorithms they use?

**Tags:** rendering

> **Comments:**
> **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  

---

## 1 answers

### Answer by username_2 on 2022-06-03. Score: 4

It looks like most of the errors in your image are due to the lack of one critical property. That is that, for any *edge* (two out of three vertices of a triangle), **the rasterization algorithm must have a consistent opinion on which side of the line each pixel falls on, even for different triangles, if those triangles share an edge** (have the same vertex coordinates for 2 vertices).

For example, on the blue face, you have two triangles forming the surface. There are visible gaps, which indicate that the two blue triangles are not meeting perfectly at the edge, which happens because the two rasterizations are not agreeing on which triangle is responsible for filling a given pixel — there's probably some overlap (which is invisible) in addition to the gaps.

Fixing this should also help with the yellow edge on the right and the wiggliness of the red-green edge.

(I do not have any advice on how to implement this property in rasterization, as I haven't dealt with triangle-based software rendering myself; I just know it's necessary.)

---

Additionally, you should implement “back-face culling”: do not draw a triangle if it is facing away from the viewer. Facing is usually defined by triangle “winding order”, clockwise or counterclockwise; one way to understand this is that you take the first two vertices of the triangle as a line with a direction (in screen space), and ask whether the third vertex is to the left (CCW order) or the right (CW order) of that line. That turns out to be equivalent to asking which face of the triangle is being viewed; you pick one such face to not draw, and make sure your mesh's vertex ordering matches that choice consistently.

When you have done this properly, the yellow back-face will not be drawn at all.

I'd recommend fixing your rasterization first, because implementing back-face culling will hide some of the problems. But culling will give you a big speedup since you can stop rendering half of the triangles in the scene (typically).


---

