.. Copyright (c) 2018-2024 William Emerison Six Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Adding Depth - Z axis Demo 14 ============================= Purpose ^^^^^^^ Do the same stuff as the previous demo, but use 3D coordinates, where the negative z axis goes into the screen (because of the right hand rule). Positive z comes out of the monitor towards your face. Things that this demo doesn't end up doing correctly: * The blue square is always drawn, even when its z-coordinate in world space is less than the paddle's. The solution will be z-buffering https://en.wikipedia.org/wiki/Z-buffering, and it is implemented in the next demo. .. figure:: static/screenshots/demo14.png :align: center :alt: Demo 14 :figclass: align-center Demo 14 .. figure:: static/screenshots/camera1.png :align: center :alt: Camera Space :figclass: align-center Camera Space .. figure:: static/screenshots/camera2.png :align: center :alt: Camera Space :figclass: align-center Camera Space How to Execute ^^^^^^^^^^^^^^ Load src/demo14/demo.py in Spyder and hit the play button Move the Paddles using the Keyboard ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ============== ============================================== Keyboard Input Action ============== ============================================== *w* Move Left Paddle Up *s* Move Left Paddle Down *k* Move Right Paddle Down *i* Move Right Paddle Up *d* Increase Left Paddle's Rotation *a* Decrease Left Paddle's Rotation *l* Increase Right Paddle's Rotation *j* Decrease Right Paddle's Rotation *UP* Move the camera up, moving the objects down *DOWN* Move the camera down, moving the objects up *LEFT* Move the camera left, moving the objects right *RIGHT* Move the camera right, moving the objects left *q* Rotate the square around its center *e* Rotate the square around paddle 1's center ============== ============================================== Description ^^^^^^^^^^^ .. TODO -- see if this text can fit. Other things added Added rotations around the x axis, y axis, and z axis. https://en.wikipedia.org/wiki/Rotation_matrix Added translate in 3D. Added scale in 3D. These are just like the 2D versions, just with the same process applied to the z axis. They direction of the rotation is defined by the right hand rule. * Vertex data will now have an X, Y, and Z component. * Rotations around an angle in 3D space follow the right hand rule_. Here's a link to them in matrix_ form, which we have not yet covered. .. _rule: https://en.wikipedia.org/wiki/Right-hand_rule .. _matrix: https://en.wikipedia.org/wiki/Rotation_matrix .. figure:: static/ccbysa3.0/wikipedia-Acdx/220px-Right_hand_rule_cross_product.svg.png :align: center :alt: Right hand rule :figclass: align-center * With open palm, fingers on the x axis, rotating the fingers to y axis, means that the positive z axis is in the direction of the thumb. Positive Theta moves in the direction that your fingers did. * starting on the y axis, rotating to z axis, thumb is on the positive x axis. * starting on the z axis, rotating to x axis, thumb is on the positive y axis. .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define vertex2d :end-before: doc-region-end define vertex2d :linenos: :lineno-match: :caption: src/demo14/demo.py Rotate Z ~~~~~~~~ Rotate Z is the same rotate that we've used so far, but doesn't affect the z component at all. .. figure:: static/cc0/Stephan_Kulla/Sinus_und_Kosinus_am_Einheitskreis_2.svg :align: center :alt: Rotate Z :figclass: align-center .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define rotate z :end-before: doc-region-end define rotate z :linenos: :lineno-match: :caption: src/demo14/demo.py Rotate X ~~~~~~~~ .. figure:: static/cc0/Stephan_Kulla/Sinus_und_Kosinus_am_Einheitskreis_4.svg :align: center :alt: Rotate X :figclass: align-center .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define rotate x :end-before: doc-region-end define rotate x :linenos: :lineno-match: :caption: src/demo14/demo.py Rotate Y ~~~~~~~~ .. figure:: static/cc0/Stephan_Kulla/Sinus_und_Kosinus_am_Einheitskreis_5.svg :align: center :alt: Rotate Y :figclass: align-center .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define rotate y :end-before: doc-region-end define rotate y :linenos: :lineno-match: :caption: src/demo14/demo.py Scale ~~~~~ .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define uniform scale :end-before: doc-region-end define uniform scale :linenos: :lineno-match: :caption: src/demo14/demo.py Code ^^^^ The only new aspect of the code below is that the paddles have a z-coordinate of 0 in their modelspace. .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin instantiate paddle 1 :end-before: doc-region-end instantiate paddle 1 :linenos: :lineno-match: :caption: src/demo14/demo.py The only new aspect of the square below is that the paddles have a z-coordinate of 0 in their modelspace. N.B that since we do a sequence transformations to the modelspace data to get to world-space coordinates, the X, Y, and Z coordinates are subject to be different. .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin define camera class :end-before: doc-region-end define camera class :linenos: :lineno-match: :caption: src/demo14/demo.py The camera now has a z-coordinate of 0 also. .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin instantiate square :end-before: doc-region-end instantiate square :linenos: :lineno-match: :caption: src/demo14/demo.py Event Loop ~~~~~~~~~~ .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin begin event loop :end-before: doc-region-end begin event loop :linenos: :lineno-match: :caption: src/demo14/demo.py :: ... * Draw Paddle 1 .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin draw paddle 1 :end-before: doc-region-end draw paddle 1 :linenos: :lineno-match: :caption: src/demo14/demo.py The square should not be visible when hidden behind the paddle1, as we do a translate by -1. But in running the demo, you see that the square is always drawn over the paddle. * Draw the Square .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin draw square :end-before: doc-region-end draw square :linenos: :lineno-match: :caption: src/demo14/demo.py This is because without depth_ buffering, the object drawn last clobbers the color of any previously drawn object at the pixel. Try moving the square drawing code to the beginning, and you will see that the square can be hidden behind the paddle. .. _depth: https://en.wikipedia.org/wiki/Fragment_(computer_graphics) * Draw Paddle 2 .. literalinclude:: ../../src/demo14/demo.py :language: python :start-after: doc-region-begin draw paddle 2 :end-before: doc-region-end draw paddle 2 :linenos: :lineno-match: :caption: src/demo14/demo.py Added translate in 3D. Added scale in 3D. These are just like the 2D versions, just with the same process applied to the z axis. They direction of the rotation is defined by the right hand rule. https://en.wikipedia.org/wiki/Right-hand_rule