Comments (3)
The idea is to make the code not use divisions and float. Note that if you have an inequality: a*b/c + d >= 0
, then as long as c>0
you can turn it into: a*b + c*d >=0
.
from tinyrenderer.
I remember having a hard time trying to grasp that part until eventually losing my trust to the writer and opening this issue angrily. Now I look again, I understand how the improvement is made and unfortunately, the explanation is really shallow and somewhat irrelevant to the change that is made.
'Taking the division out of the loop', after 'noticing that each division has the same divisor', would be just by calculating 1/{common divisor}
beforehand and then multiplying the dividend with this value in the loop. Something like this (compare with 3rd attempt):
float divisor = 1/(float)(x1-x0);
for (int x=x0; x<=x1; x++) {
float t = (x-x0)*divisor;
}
But the change writer made (4th attempt) does this:
float derror = std::abs(dy/float(dx));
float error = 0;
for (int x=x0; x<=x1; x++) {
error += derror;
}
The improvement made here (not multiplying at every step) is possible not only because of the common divisor, also because of unmentioned proportional increase of dividend(x-x0) in the loop. Actually, it is more about the latter and one only needs to see that t
variable in 3rd attempt is increasing by the same amount on every iteration, to easily implement the same optimization.
Whatever. My guess is, writer already had the resulting snippets (maybe from classes) and showed some impatience while filling the gaps. I also think the resulting explanation has something to do with trying to be smart (sorry, this costed me too much time and i am angry again).
from tinyrenderer.
While I do agree with your general feeling, we should also be fair considering who this tutorial was supposedly meant for. The important point being that the algorithm was most likely meant to be derived by readers (you can find my derivation here: https://github.com/vchizhov/ssloy_software_renderer/blob/master/src/line_bresenham.hpp). The algorithm description in wikipedia is also quite helpful (a bit different from mine, but ultimately equivalent): https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Algorithm_for_integer_arithmetic
from tinyrenderer.
Related Issues (20)
- Lighting code in lesson 2 is not working properly
- Drawing triangle compilation error HOT 1
- Why does the rendered line break when it is vertical —— lesson 1 HOT 2
- Question about transformation of normal vectors in Lesson5 HOT 2
- My picture is brighter
- Where is the function of face() HOT 1
- Typo in Lesson 5
- 我渲染得到的结果是这样的,有人能告诉我这是哪里出现问题了吗 HOT 2
- Question about zbuffer HOT 2
- How to do the shadow mapping after perspective deformations? HOT 1
- Confused about design of the articles
- In lesson2,something wrong in function `barycentric` when i run task in Vscode and i fix it. HOT 1
- The compilation error of lambda expressions in lesson 2 HOT 1
- Holes in fill a triangle using coordinates of pixel left bottom corner
- About the function of face() HOT 1
- LLVM linker error HOT 5
- Why not use the more universal Bresenham? HOT 2
- equations for lesson two are invisible due to githubs default dark theme
- slower result when using omp HOT 6
- Help, i have an error related to the vector library HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyrenderer.