Till KTH:s startsida Till KTH:s startsida

Lab hints and tips

Methodology

1) Do selective unit testing. Don't implement a lot of code and then expect it all to work. If it doesn't (likelihood: it won't ...;-) how will you locate the error? Divide and conquer.

2) Use a pen and paper to solve problem subsets first. Try to work a math problem out on paper first, or manually simulate how an algorithm works for a simple situation. If you sit down and try to code straight away, you are trying to solve multiple difficult issues at once. First solve and understand the model/math using pen and paper. Then solve the programming issues. Especially, if you solve the programming issues in the sense that your program compiles, you will still need to think deeply about the algorithm if your rendering has errors in it.

3) Be critical of instructions and attempt to understand equations. Trying to directly implement equations without understanding what they do or their relationship to the overall algorithm will guarantee many hours of wasted time in the labs and the real world.

Lab setup

For some, the most difficult initial aspect of the labs is getting the programming environment up and running with the skeleton code. The large number of various operating systems, development environments and versions make it difficult to provide a single solution.

Step-by-step set-up: https://docs.google.com/presentation/d/1790zNttJpSovlXfgExV9mggE9iJ9VOaMRGZPAlxIylg/edit?usp=sharing (30 March 2021)

The following details may therefore be helpful for setting the labs up on your particular configuration:

Ubuntu

A quick tutorial on Ubuntu system can be found here.

Windows and Microsoft Visual Studio

VS2013

A useful guide can be found here by previous DH2323 student Victor Dahlin on conducting a setup in Microsoft Visual Studio 2013. Fangkai has a newer version that runs on Visual Studio 2013 (VS2013) and which can be found here. Note that in order to easily enable cout output to the console, you may need to set the project to be a console application in the properties menu.

VS2015 (or higher)

For the students using VS2015, you may encounter the problems as posted in the News feedThe reason is in Visual Studio 2015, stdin, stderr, stdout are defined differently. The lib files downloaded from SDL homepage were compiled in VS version lower than VS2015. We recompiled the SDL lib files for VS2015 (download here), which you should use instead of those in the original download. Henrik Dahlberg made a template for VS2015 for those who want to use SDL2.0. SDL2 GLM VS15 Template and SDL2-2.0.4. Place the folder in the second link directly under C:\ and it should work.

Updated (2022): SDL2-2.0.4 with .vcxproj file

CMake

For students using cmake to generate project, new lab1 source code should be used (for the following labs, you could modify CMakeLists file). The only change is the CMakeLists file where 'find_package' is not used considering the installation of SDL in Windows is a bit rusty. Fangkai tested it on VS2017 using the new compiled SDL libs.

MacOS

Installing SDL on Mac OSX:

https://wiki.libsdl.org/Installation#Mac_OS_X

In order to get some of the lab code working with Mac OS, you will need to use SDL 2.0 rather than SDL 1.2. Here's example code for how to do this (also includes instructions for Linux): https://github.com/lemonad/DH2323-Skeleton

MacOS (with XCode)

A useful guide by previous DH2323 student Andreas Kramer can be found here. Chengqiu Zhang posted a follow-up tutorial using Mac OSX (Version 10.11.1) with Xcode 7.0.

Update (2020 March 24): The old setup is deprecated in the latest XCode and Mac OSX. Jonas Nockert have uploaded a template using SDL 2.0, it was tested to be working on the new Mac OSX 10.14+.

MacOS (without XCode)

Tutorial on setting up SDL2 on your Mac without XCode:

https://medium.com/@edkins.sarah/set-up-sdl2-on-your-mac-without-xcode-6b0c33b723f7

Previous DH2323 student Mikael Hedin recommends the following:

Install fink (http://www.finkproject.org/)
fink install sdl
Include in your Makefile: CXXFLAGS=$(shell sdl-config --libs) $(shell sdl-config --cflags)
make

Finally, if you are not so interested in getting the build process working, you can find an SDL2 skeleton for the lab assignments here.

General hints and tips

Lab 3 Rasterisation (lab track 1)


There are some misprints in the Lab 3 documentation (not difficult to spot, hopefully...):

1. Eqn. (3) and (4) on page 2 should refer to W/2 instead of W2 and H/2 instead of H2, respectively.

2. In section 5 on page 11, it repeatedly mentions the inverse "1=z" - it should be "1/z"

3. On page 14, the reflectance element of the Vertex struct should be represented a vec3 and not vec2 as printed.

Hints:
Issues with triangle borders (or gaps) being drawn may relate to missing out the drawing of pixels in one of the loops related to triangle filling operations. Check the starting and finishing conditions of those loops.
On page 14, the Light power of 1.1f * vec3(1,1,1) is not enough to give clear illumination. Try the value from the previous lab of 14.1f * vec3(1,1,1), especially if you are not getting good results for the illumination questions at the end of the lab.
Per vertex illumination is implemented in the vertex shader using eq. 10 and 11. The normal vectors of vertices are identical to those of triangles in this scene.

Perspective correct interpolation (last section of lab 3)

Per pixel illumination involves 3D position stored in the pixel structure. Do not interpolate the original 3D positions linearly, interpolate the transformed position (which is multiplied by inverse z value).

More detailed explanation:

Since a 2D point (x, y) in the screen space image is derived by x = fX/Z+W2; y = fY/Z+H2; (the perspective view), the interpolated points in the 2D screen space image change linearly with 1/z. For example, consider a triangle edge AB in world-space, with A(Xa, Ya, Za), B(Xb,Yb,Zb) and the equivalent perspective projected edge in screen space ab, with a(xa, ya), b(xb, yb). If we create linearly interpolated points between A and B in 3D world-space and then project them into the 2D screen space, the results will not be linear. This creates distortion in terms of lighting and so on.
Therefore, we should do a linear interpolation on the 2D screen space between a and b first. We then use these 2D interpolated points to reconstruct 3D interpolated points in 3D space.
The solution to this involves finding a variable in 2D screen space which changes linearly. Given x = fX/Z+W2 and y = fY/Z+H2, the variable 1/Z changes linearly. This is because x and y change linearly and (x, y) is the linear combination of 1/Z in 2D screen space. 1/Z should then be inverted to obtain Z in the 3D position.

In code, that translates to:

1) The illumination and position vectors of the Pixel struct should be multiplied by 1/Z in VertexShader to be interpolated correctly.

2) The values are then restored in the DrawPolygonRows function (by being divided by the zinverse)
before being sent to PixelShader.

More detailed descriptions of the problem and derivation of the solution is available here:

http://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/perspective-correct-interpolation-vertex-attributes

https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf

You should try to understand the problem, although deriving the solution is a lot more involved. You do not need to fully understand the derivation of the solution in order to implement, although it is good to understand.

Be careful of the following:
Accidentally converting floats to integers: Make sure your floats are being evaluated as such, and not being converted to integers. E.g. be careful of using "zinv = 1/vtx.z" instead of the correct "zinv = 1.0f/vtx.z"
If you are having segmentation fauls, you need to implement some form of bounds checking i.e. if it is possible for a vertex of a triangle be placed outside of the view, it would access the depth buffer out of bounds resulting in a segmentation fault.

Resources

  1. Unity Student Plan (includes Snap student bundle containing 3D asset library worth $489)
    https://store.unity.com/academic/unity-student 
  2. GitHub student developer pack (Access to pycharm, gitkranken, heroku, etc)
    https://education.github.com/pack 
  3. Build your project demonstrations
    https://www.wix.com/, https://wordpress.com/, and similarly other such platforms. (Previous project blogs)

Lärare Christopher Peters skapade sidan 23 mars 2015

Lärare Christopher Peters ändrade rättigheterna 22 april 2015

Kan därmed läsas av alla och ändras av lärare.
Christopher Peters redigerade 4 april 2016

Methodology 1) Do selective unit testing. Don't implement a lot of code and then expect it all to work. If it doesn't (it won't ...) how will you locate the error? Divide and conquer.

2) Use a pen and paper to solve problem subsets first. Mathematics programming is more than maths and programming combined. You are trying to solve both issues at once when you move directly to the computer. First solve and understand the math using pen and paper. Then solve the programming issues.

3) Be critical of instructions. Real instructions are always inaccurate and/or vague. You need to correct them and fill in the gaps where necessary. Implementing equations without trying to understand them (see step 2), will guarantee many hours of wasted time in these labs and the real world.

Lab setup Arguably one of the most difficult aspects of the labs is getting your programming environment up and running with the skeleton code. The large number of various operating systems, development environments and versions make it difficult to provide a single solution. The following details may therefore be helpful for setting the labs up on your particular configuration:

Windows and Microsoft Visual Studio A useful guide can be found here by previous DH2323 student Victor Dahlin on conducting a setup in Microsoft Visual Studio 2013. Note that in order to easily enable cout output to the console, you may need to set the project to be a console application in the properties menu.

MacOS (with XCode) A useful guide by previous DH2323 student Andreas Kramer can be found here. Chengqiu Zhang posted another follow-up tutorial this year using the latest Mac OSX (Version 10.11.1) with Xcode 7.0.

MacOS (without XCode) Previous DH2323 student Mikael Hedin recommends the following:

Install fink (http://www.finkproject.org/)fink install sdlInclude in your Makefile: CXXFLAGS=$(shell sdl-config --libs) $(shell sdl-config --cflags)make

General hints and tips

Lab 3 Rasterisation (lab track 1) There are some misprints in the Lab 3 documentation (not difficult to spot, hopefully...):

1. Eqn. (3) and (4) on page 2 should refer to W/2 instead of W2 and H/2 instead of H2, respectively.

2. In section 5 on page 11, it repeatedly mentions the inverse "1=z" - it should be "1/z"

3. On page 14, the reflectance element of the Vertex struct should be represented a vec3 and not vec2 as printed.Hints:Issues with triangle borders (or gaps) being drawn may relate to missing out the drawing of pixels in one of the loops related to triangle filling operations. Check the starting and finishing conditions of those loops.On page 14, the Light power of 1.1f * vec3(1,1,1) is not enough to give clear illumination. Try the value from the previous lab of 14.1f * vec3(1,1,1), especially if you are not getting good results for the illumination questions at the end of the lab.Per vertex illumination is implemented in the vertex shader using eq. 10 and 11. The normal vectors of vertices are identical to those of triangles in this scene.Per pixel illumination involves 3D position stored in the pixel structure. Do not interpolate the original 3D positions linearly, interpolate the transformed position (which is multiplied by inverse z value).Be careful of the following:Accidentally converting floats to integers: Make sure your floats are being evaluated as such, and not being converted to integers. E.g. be careful of using "zinv = 1/vtx.z" instead of the correct "zinv = 1.0f/vtx.z"If you are having segmentation fauls, you need to implement some form of bounds checking i.e. if it is possible for a vertex of a triangle be placed outside of the view, it would access the depth buffer out of bounds resulting in a segmentation fault.

kommenterade 20 maj 2016

Hi!

This only applies to the animation track.

Lab 2

I heard that a lot of people had problems getting the particleLab to work in Visual Studio 2015, the solution seemed to be to just install VS2010 to get it to work. I thought there had to be a better solution to this and decided to figure it out.

I have replaced the (very old) glaux library with SDL and changed the code a bit to make it work. 

The updated lab files can be downloaded from here: https://drive.google.com/open?id=0B0yEGehsNLYASmhQb3lROTNzemc

(I will do the same for Lab 3)

~Ewoud

Lärare kommenterade 20 maj 2016

Very nice, thanks!

As far as I know, the old version of particleLab should work on any version of VS up to (and including) VS 2013 (which is installed on University computers), so this problem is just with VS2015?

Also, don't forget to see the fix to this issue that I previously posted (the fix itself is somewhat obscure, but quite straightforward and quick to apply). Let me know if you do not know what I'm talking about.

kommenterade 20 maj 2016

I see, I missed that post. 

I didn't try it myself with older versions of Visual Studio. But from what I heard it does indeed work with 2013, my mistake.

Maybe these updated solutions can make life a little bit easier next year.

Since Lab3 just reuses the code from lab2 there is not much that needs to change, I've made a new, updated solution that includes the SDL library so it is easier to use. 

https://drive.google.com/open?id=0B0yEGehsNLYAaUxpS3g2LWRzWVk

kommenterade 23 mars 2017

What workloads should i choose while installing VisualStudio? I chose the Desktop development with C++ and Universal Windows Platform Development, but can't seem to find the same configuration alternatives as the tutorial uses. 

Assistent kommenterade 23 mars 2017

We will have a lab session next Monday. I will be there to check what is the problem. However, I think choose default settings will work.

kommenterade 23 mars 2017

CONFIGURATION.png

Alright, I might not be able to attend the lab-session on monday since I have lecture in Kista about the same time, but here is what i see while trying to configure in VisualStudio. I have the 2017-version, do you think that might be of importance?

kommenterade 24 mars 2017

Hi Tomas, 

Do this: After you have completed step two in Fangkai Yangs tutorial, jump straight to step 12 and 13, then jump back to step 3 and continue as normal, you will se that the "C/C++" tab how now appeared. (Microsoft apparently decided to do some changes in how VS2017 handles project files etc compared to previous versions). Also, don't forget to use the new compiled lib-files (see link on this page), these work on VS2017 as well.

I plan on doing a renewed tutorial covering VS2017 and post on this page (no promises though...), one with fewer steps.

Assistent kommenterade 24 mars 2017

Hi Joakim,

Great job! If the tutorial for VS2017 is done, email me a link and I will update our "hints and tips".

kommenterade 5 april 2017

Hi Im using mac and xcode 7.01 as in the tutorial. But I don't have a "MainMenu" file I have instead something called "Main.storyboard" should I delete that one ?

kommenterade 5 april 2017

Its working. but there is some question marks next to the imported libraries. Just want to make sure that it won't cause a problem. Screen Shot 2017-04-05 at 11.59.05.png  

En användare har tagit bort sin kommentar
Kiran Chhatre redigerade 29 mars 2021

Methodology 1) Do selective unit testing. Don't implement a lot of code and then expect it all to work. If it doesn't (likelihood: it won't ...;-) how will you locate the error? Divide and conquer.

2) Use a pen and paper to solve problem subsets first. Try to work a math problem out on paper first, or manually simulate how an algorithm works for a simple situation. If you sit down and try to code straight away, you are trying to solve multiple difficult issues at once. First solve and understand the model/math using pen and paper. Then solve the programming issues. Especially, if you solve the programming issues in the sense that your program compiles, you will still need to think deeply about the algorithm if your rendering has errors in it.

3) Be critical of instructions and attempt to understand equations. Trying to directly implement equations without understanding what they do or their relationship to the overall algorithm will guarantee many hours of wasted time in the labs and the real world.

Lab setup For some, the most difficult initial aspect of the labs is getting the programming environment up and running with the skeleton code. The large number of various operating systems, development environments and versions make it difficult to provide a single solution. The following details may therefore be helpful for setting the labs up on your particular configuration:

Ubuntu A quick tutorial on Ubuntu system can be found here.

Windows and Microsoft Visual Studio VS2013

A useful guide can be found here by previous DH2323 student Victor Dahlin on conducting a setup in Microsoft Visual Studio 2013. Fangkai has a newer version that runs on Visual Studio 2013 (VS2013) and which can be found here. Note that in order to easily enable cout output to the console, you may need to set the project to be a console application in the properties menu.

VS2015 (or higher)

For the students using VS2015, you may encounter the problems as posted in the News feed. The reason is in Visual Studio 2015, stdin, stderr, stdout are defined differently. The lib files downloaded from SDL homepage were compiled in VS version lower than VS2015. We recompiled the SDL lib files for VS2015 (download here), which you should use instead of those in the original download. Henrik Dahlberg made a template for VS2015 for those who want to use SDL2.0. SDL2 GLM VS15 Template and SDL2-2.0.4. Place the folder in the second link directly under C:\ and it should work.

CMake

For students using cmake to generate project, new lab1 source code should be used (for the following labs, you could modify CMakeLists file). The only change is the CMakeLists file where 'find_package' is not used considering the installation of SDL in Windows is a bit rusty. Fangkai tested it on VS2017 using the new compiled SDL libs.

MacOS Installing SDL on Mac OSX:

https://wiki.libsdl.org/Installation#Mac_OS_X

In order to get some of the lab code working with Mac OS, you will need to use SDL 2.0 rather than SDL 1.2. Here's example code for how to do this (also includes instructions for Linux): https://github.com/lemonad/DH2323-Skeleton

MacOS (with XCode) A useful guide by previous DH2323 student Andreas Kramer can be found here. Chengqiu Zhang posted a follow-up tutorial using Mac OSX (Version 10.11.1) with Xcode 7.0.

Update (2020 March 24): The old setup is deprecated in the latest XCode and Mac OSX. Jonas Nockert have uploaded a template using SDL 2.0, it was tested to be working on the new Mac OSX 10.14+.

MacOS (without XCode) Tutorial on setting up SDL2 on your Mac without XCode:

https://medium.com/@edkins.sarah/set-up-sdl2-on-your-mac-without-xcode-6b0c33b723f7

Previous DH2323 student Mikael Hedin recommends the following:

Install fink (http://www.finkproject.org/)fink install sdlInclude in your Makefile: CXXFLAGS=$(shell sdl-config --libs) $(shell sdl-config --cflags)make

General hints and tips

Lab 3 Rasterisation (lab track 1) There are some misprints in the Lab 3 documentation (not difficult to spot, hopefully...):

1. Eqn. (3) and (4) on page 2 should refer to W/2 instead of W2 and H/2 instead of H2, respectively.

2. In section 5 on page 11, it repeatedly mentions the inverse "1=z" - it should be "1/z"

3. On page 14, the reflectance element of the Vertex struct should be represented a vec3 and not vec2 as printed.Hints:Issues with triangle borders (or gaps) being drawn may relate to missing out the drawing of pixels in one of the loops related to triangle filling operations. Check the starting and finishing conditions of those loops.On page 14, the Light power of 1.1f * vec3(1,1,1) is not enough to give clear illumination. Try the value from the previous lab of 14.1f * vec3(1,1,1), especially if you are not getting good results for the illumination questions at the end of the lab.Per vertex illumination is implemented in the vertex shader using eq. 10 and 11. The normal vectors of vertices are identical to those of triangles in this scene.

Perspective correct interpolation (last section of lab 3)

Per pixel illumination involves 3D position stored in the pixel structure. Do not interpolate the original 3D positions linearly, interpolate the transformed position (which is multiplied by inverse z value).

More detailed explanation:

Since a 2D point (x, y) in the screen space image is derived by x = fX/Z+W2; y = fY/Z+H2; (the perspective view), the interpolated points in the 2D screen space image change linearly with 1/z. For example, consider a triangle edge AB in world-space, with A(Xa, Ya, Za), B(Xb,Yb,Zb) and the equivalent perspective projected edge in screen space ab, with a(xa, ya), b(xb, yb). If we create linearly interpolated points between A and B in 3D world-space and then project them into the 2D screen space, the results will not be linear. This creates distortion in terms of lighting and so on.Therefore, we should do a linear interpolation on the 2D screen space between a and b first. We then use these 2D interpolated points to reconstruct 3D interpolated points in 3D space.The solution to this involves finding a variable in 2D screen space which changes linearly. Given x = fX/Z+W2 and y = fY/Z+H2, the variable 1/Z changes linearly. This is because x and y change linearly and (x, y) is the linear combination of 1/Z in 2D screen space. 1/Z should then be inverted to obtain Z in the 3D position.

In code, that translates to:

1) The illumination and position vectors of the Pixel struct should be multiplied by 1/Z in VertexShader to be interpolated correctly. 2) The values are then restored in the DrawPolygonRows function (by being divided by the zinverse)before being sent to PixelShader.

More detailed descriptions of the problem and derivation of the solution is available here:

http://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/perspective-correct-interpolation-vertex-attributes

https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf

You should try to understand the problem, although deriving the solution is a lot more involved. You do not need to fully understand the derivation of the solution in order to implement, although it is good to understand.Be careful of the following:Accidentally converting floats to integers: Make sure your floats are being evaluated as such, and not being converted to integers. E.g. be careful of using "zinv = 1/vtx.z" instead of the correct "zinv = 1.0f/vtx.z"If you are having segmentation fauls, you need to implement some form of bounds checking i.e. if it is possible for a vertex of a triangle be placed outside of the view, it would access the depth buffer out of bounds resulting in a segmentation fault.



Resources
* Unity Student Plan (includes Snap student bundle containing 3D asset library worth $489)https://store.unity.com/academic/unity-student
* GitHub student developer pack (Access to pycharm, gitkranken, heroku, etc)https://education.github.com/pack
* Build your project demonstrationshttps://www.wix.com/, https://wordpress.com/, and similarly other such platforms. (Previous project blogs)


Kiran Chhatre redigerade 30 mars 2021

Methodology 1) Do selective unit testing. Don't implement a lot of code and then expect it all to work. If it doesn't (likelihood: it won't ...;-) how will you locate the error? Divide and conquer.

2) Use a pen and paper to solve problem subsets first. Try to work a math problem out on paper first, or manually simulate how an algorithm works for a simple situation. If you sit down and try to code straight away, you are trying to solve multiple difficult issues at once. First solve and understand the model/math using pen and paper. Then solve the programming issues. Especially, if you solve the programming issues in the sense that your program compiles, you will still need to think deeply about the algorithm if your rendering has errors in it.

3) Be critical of instructions and attempt to understand equations. Trying to directly implement equations without understanding what they do or their relationship to the overall algorithm will guarantee many hours of wasted time in the labs and the real world.

Lab setup For some, the most difficult initial aspect of the labs is getting the programming environment up and running with the skeleton code. The large number of various operating systems, development environments and versions make it difficult to provide a single solution.Step-by-step set-up guide: https://docs.google.com/presentation/d/1790zNttJpSovlXfgExV9mggE9iJ9VOaMRGZPAlxIylg/edit?usp=sharing (30 March 2021) The following details may therefore be helpful for setting the labs up on your particular configuration:

Ubuntu A quick tutorial on Ubuntu system can be found here.

Windows and Microsoft Visual Studio VS2013

A useful guide can be found here by previous DH2323 student Victor Dahlin on conducting a setup in Microsoft Visual Studio 2013. Fangkai has a newer version that runs on Visual Studio 2013 (VS2013) and which can be found here. Note that in order to easily enable cout output to the console, you may need to set the project to be a console application in the properties menu.

VS2015 (or higher)

For the students using VS2015, you may encounter the problems as posted in the News feed. The reason is in Visual Studio 2015, stdin, stderr, stdout are defined differently. The lib files downloaded from SDL homepage were compiled in VS version lower than VS2015. We recompiled the SDL lib files for VS2015 (download here), which you should use instead of those in the original download. Henrik Dahlberg made a template for VS2015 for those who want to use SDL2.0. SDL2 GLM VS15 Template and SDL2-2.0.4. Place the folder in the second link directly under C:\ and it should work.

CMake

For students using cmake to generate project, new lab1 source code should be used (for the following labs, you could modify CMakeLists file). The only change is the CMakeLists file where 'find_package' is not used considering the installation of SDL in Windows is a bit rusty. Fangkai tested it on VS2017 using the new compiled SDL libs.

MacOS Installing SDL on Mac OSX:

https://wiki.libsdl.org/Installation#Mac_OS_X

In order to get some of the lab code working with Mac OS, you will need to use SDL 2.0 rather than SDL 1.2. Here's example code for how to do this (also includes instructions for Linux): https://github.com/lemonad/DH2323-Skeleton

MacOS (with XCode) A useful guide by previous DH2323 student Andreas Kramer can be found here. Chengqiu Zhang posted a follow-up tutorial using Mac OSX (Version 10.11.1) with Xcode 7.0.

Update (2020 March 24): The old setup is deprecated in the latest XCode and Mac OSX. Jonas Nockert have uploaded a template using SDL 2.0, it was tested to be working on the new Mac OSX 10.14+.

MacOS (without XCode) Tutorial on setting up SDL2 on your Mac without XCode:

https://medium.com/@edkins.sarah/set-up-sdl2-on-your-mac-without-xcode-6b0c33b723f7

Previous DH2323 student Mikael Hedin recommends the following:

Install fink (http://www.finkproject.org/)fink install sdlInclude in your Makefile: CXXFLAGS=$(shell sdl-config --libs) $(shell sdl-config --cflags)make

General hints and tips

Lab 3 Rasterisation (lab track 1) There are some misprints in the Lab 3 documentation (not difficult to spot, hopefully...):

1. Eqn. (3) and (4) on page 2 should refer to W/2 instead of W2 and H/2 instead of H2, respectively.

2. In section 5 on page 11, it repeatedly mentions the inverse "1=z" - it should be "1/z"

3. On page 14, the reflectance element of the Vertex struct should be represented a vec3 and not vec2 as printed.Hints:Issues with triangle borders (or gaps) being drawn may relate to missing out the drawing of pixels in one of the loops related to triangle filling operations. Check the starting and finishing conditions of those loops.On page 14, the Light power of 1.1f * vec3(1,1,1) is not enough to give clear illumination. Try the value from the previous lab of 14.1f * vec3(1,1,1), especially if you are not getting good results for the illumination questions at the end of the lab.Per vertex illumination is implemented in the vertex shader using eq. 10 and 11. The normal vectors of vertices are identical to those of triangles in this scene.

Perspective correct interpolation (last section of lab 3)

Per pixel illumination involves 3D position stored in the pixel structure. Do not interpolate the original 3D positions linearly, interpolate the transformed position (which is multiplied by inverse z value).

More detailed explanation:

Since a 2D point (x, y) in the screen space image is derived by x = fX/Z+W2; y = fY/Z+H2; (the perspective view), the interpolated points in the 2D screen space image change linearly with 1/z. For example, consider a triangle edge AB in world-space, with A(Xa, Ya, Za), B(Xb,Yb,Zb) and the equivalent perspective projected edge in screen space ab, with a(xa, ya), b(xb, yb). If we create linearly interpolated points between A and B in 3D world-space and then project them into the 2D screen space, the results will not be linear. This creates distortion in terms of lighting and so on.Therefore, we should do a linear interpolation on the 2D screen space between a and b first. We then use these 2D interpolated points to reconstruct 3D interpolated points in 3D space.The solution to this involves finding a variable in 2D screen space which changes linearly. Given x = fX/Z+W2 and y = fY/Z+H2, the variable 1/Z changes linearly. This is because x and y change linearly and (x, y) is the linear combination of 1/Z in 2D screen space. 1/Z should then be inverted to obtain Z in the 3D position.

In code, that translates to:

1) The illumination and position vectors of the Pixel struct should be multiplied by 1/Z in VertexShader to be interpolated correctly. 2) The values are then restored in the DrawPolygonRows function (by being divided by the zinverse)before being sent to PixelShader.

More detailed descriptions of the problem and derivation of the solution is available here:

http://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/perspective-correct-interpolation-vertex-attributes

https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf

You should try to understand the problem, although deriving the solution is a lot more involved. You do not need to fully understand the derivation of the solution in order to implement, although it is good to understand.Be careful of the following:Accidentally converting floats to integers: Make sure your floats are being evaluated as such, and not being converted to integers. E.g. be careful of using "zinv = 1/vtx.z" instead of the correct "zinv = 1.0f/vtx.z"If you are having segmentation fauls, you need to implement some form of bounds checking i.e. if it is possible for a vertex of a triangle be placed outside of the view, it would access the depth buffer out of bounds resulting in a segmentation fault.

Resources
* Unity Student Plan (includes Snap student bundle containing 3D asset library worth $489)https://store.unity.com/academic/unity-student
* GitHub student developer pack (Access to pycharm, gitkranken, heroku, etc)https://education.github.com/pack
* Build your project demonstrationshttps://www.wix.com/, https://wordpress.com/, and similarly other such platforms. (Previous project blogs)