| 1 | <?xml version="1.0" encoding="UTF-8"?>
|
|---|
| 2 | <!-- key 1222139 -->
|
|---|
| 3 | <!--
|
|---|
| 4 | Document : DevelopmentInOpenSceneGraph.xml
|
|---|
| 5 | Created on : 20th May 2004
|
|---|
| 6 | Author : Robert Osfield
|
|---|
| 7 | Description: Lecture at AVR III 2004.
|
|---|
| 8 | -->
|
|---|
| 9 |
|
|---|
| 10 | <presentation character_size="0.02">
|
|---|
| 11 | <name>Siggraph Menu</name>
|
|---|
| 12 | <textcolor>YELLOW</textcolor>
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | <path>${DATA_DIR}</path>
|
|---|
| 17 | <path>${DATA_DIR}/OpenSceneGraph-Data</path>
|
|---|
| 18 | <path>${DATA_DIR}/Images</path>
|
|---|
| 19 | <path>${DATA_DIR}/Models</path>
|
|---|
| 20 | <path>${DATA_DIR}/Earth</path>
|
|---|
| 21 |
|
|---|
| 22 | <title-settings character_size="0.04"></title-settings>
|
|---|
| 23 | <text-settings character_size="0.03"></text-settings>
|
|---|
| 24 |
|
|---|
| 25 | <template_slide name="front">
|
|---|
| 26 | <background>Images/default_blue.jpg</background>
|
|---|
| 27 | <title></title>
|
|---|
| 28 | <base>
|
|---|
| 29 | <click_to_event>Home</click_to_event>
|
|---|
| 30 | <bullet character_size="0.02" position="0.5 0.02 0.05">Home</bullet>
|
|---|
| 31 | <click_to_event>Next</click_to_event>
|
|---|
| 32 | <bullet character_size="0.02" position="0.9 0.02 0.05">Next</bullet>
|
|---|
| 33 | </base>
|
|---|
| 34 | </template_slide>
|
|---|
| 35 |
|
|---|
| 36 | <template_slide name="middle">
|
|---|
| 37 | <background>Images/default_blue.jpg</background>
|
|---|
| 38 | <title></title>
|
|---|
| 39 | <base>
|
|---|
| 40 | <click_to_event>Previous</click_to_event>
|
|---|
| 41 | <bullet character_size="0.02" position="0.1 0.02 0.05">Previous</bullet>
|
|---|
| 42 | <click_to_event>Home</click_to_event>
|
|---|
| 43 | <bullet character_size="0.02" position="0.5 0.02 0.05">Home</bullet>
|
|---|
| 44 | <click_to_event>Next</click_to_event>
|
|---|
| 45 | <bullet character_size="0.02" position="0.9 0.02 0.05">Next</bullet>
|
|---|
| 46 | </base>
|
|---|
| 47 | </template_slide>
|
|---|
| 48 |
|
|---|
| 49 | <template_slide name="end">
|
|---|
| 50 | <background>Images/default_blue.jpg</background>
|
|---|
| 51 | <title></title>
|
|---|
| 52 | <base>
|
|---|
| 53 | <click_to_event>Previous</click_to_event>
|
|---|
| 54 | <bullet character_size="0.02" position="0.1 0.02 0.05">Previous</bullet>
|
|---|
| 55 | <click_to_event>Home</click_to_event>
|
|---|
| 56 | <bullet character_size="0.02" position="0.5 0.02 0.05">Home</bullet>
|
|---|
| 57 | </base>
|
|---|
| 58 | </template_slide>
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | <slide inherit="front">
|
|---|
| 62 | <background>Images/default_blue.jpg</background>
|
|---|
| 63 | <title> </title>
|
|---|
| 64 | <layer>
|
|---|
| 65 | <paragraph position="0.5 0.5 0" alignment="CENTER_CENTER" character_size="0.07">
|
|---|
| 66 | C++ and OpenSceneGraph</paragraph>
|
|---|
| 67 | <image fade="0 0.99 1 0.99" position="0.5 0.90 0" scale="0.55">Images/logops.rgb</image>
|
|---|
| 68 | </layer>
|
|---|
| 69 | </slide>
|
|---|
| 70 |
|
|---|
| 71 | <page inherit="middle" title="Standard C++">
|
|---|
| 72 | C++ is very powerful
|
|---|
| 73 |
|
|---|
| 74 | C++ can be optimised to very fast code
|
|---|
| 75 |
|
|---|
| 76 | Faster than standard C once inline methods and templates kick in
|
|---|
| 77 |
|
|---|
| 78 | Multiple Programming Paradigms:
|
|---|
| 79 |
|
|---|
| 80 | - Functional
|
|---|
| 81 | - Object Orientated
|
|---|
| 82 | - Generic Programming
|
|---|
| 83 |
|
|---|
| 84 | C++ can be very very complicated :-|
|
|---|
| 85 |
|
|---|
| 86 | C++ offers you a lot of rope to hang yourself with ;-z
|
|---|
| 87 |
|
|---|
| 88 | It takes experience and care to write robust C++..
|
|---|
| 89 | </page>
|
|---|
| 90 |
|
|---|
| 91 | <page inherit="middle" title="OpenSceneGraph aims for 'Good Practices'">
|
|---|
| 92 | One of my goals when writing the OpenSceneGraph was to provide an example of how to write and manage a C++ project.
|
|---|
| 93 |
|
|---|
| 94 | For others to learn from, cut short all the many frustrating hours of learning the hard way
|
|---|
| 95 |
|
|---|
| 96 | Takes inspiration from Design Patterns, Effective C++ etc.
|
|---|
| 97 |
|
|---|
| 98 | Make the project easier to contribute to.
|
|---|
| 99 |
|
|---|
| 100 | Make it easier to manage
|
|---|
| 101 |
|
|---|
| 102 | Make it more robust
|
|---|
| 103 | </page>
|
|---|
| 104 |
|
|---|
| 105 | <page inherit="middle" title="Inheritance">
|
|---|
| 106 | Single Inheritance:
|
|---|
| 107 |
|
|---|
| 108 | class A {};
|
|---|
| 109 | class B : public A {} ;
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | Multiple inheritance:
|
|---|
| 113 |
|
|---|
| 114 | class C {};
|
|---|
| 115 | class D : public A, public C {};
|
|---|
| 116 |
|
|---|
| 117 | Conflicting inheritance:
|
|---|
| 118 |
|
|---|
| 119 | class E : public B, public D {};
|
|---|
| 120 |
|
|---|
| 121 | What's the problem here??
|
|---|
| 122 |
|
|---|
| 123 | </page>
|
|---|
| 124 |
|
|---|
| 125 | <page inherit="middle" title="Inheritance cont.">
|
|---|
| 126 | Conflicting inheritance:
|
|---|
| 127 |
|
|---|
| 128 | class A {}
|
|---|
| 129 | class B : public A {};
|
|---|
| 130 | class C : public A {};
|
|---|
| 131 | class E : public B, public C {};
|
|---|
| 132 |
|
|---|
| 133 | E has A appear twice in its inheritance tree, compiler doesn't know which one to use.
|
|---|
| 134 |
|
|---|
| 135 | The solution is "Virtual Inheritance":
|
|---|
| 136 |
|
|---|
| 137 | class A {};
|
|---|
| 138 | class B : public virtual A {};
|
|---|
| 139 | class C : public virtual A {};
|
|---|
| 140 | class E : public A, public B {};
|
|---|
| 141 |
|
|---|
| 142 | Go find where Virtual inheritance is used, and work out why!
|
|---|
| 143 | </page>
|
|---|
| 144 |
|
|---|
| 145 | <page inherit="end" title="Containers">
|
|---|
| 146 | For many years everybody implemented their own containers
|
|---|
| 147 |
|
|---|
| 148 | They didn't integrate with each other.
|
|---|
| 149 |
|
|---|
| 150 | They weren't all efficient.
|
|---|
| 151 |
|
|---|
| 152 | They weren't all robust.
|
|---|
| 153 |
|
|---|
| 154 | But they all wasted time!
|
|---|
| 155 | </page>
|
|---|
| 156 |
|
|---|
| 157 | <page inherit="end" title="std::containers">
|
|---|
| 158 | The Standard Template Library is a watershed in C++.
|
|---|
| 159 |
|
|---|
| 160 | Finally robust, efficient and highly interoperable containers were available to all
|
|---|
| 161 |
|
|---|
| 162 | But still people waste time "Not invented here syndrome"
|
|---|
| 163 |
|
|---|
| 164 | OpenSceneGraph adopts Standard C++, container and all.
|
|---|
| 165 |
|
|---|
| 166 | - Saves coding, debugging and maintainence time
|
|---|
| 167 | - Reuse exising skill base
|
|---|
| 168 | - Cuts down on need to documention
|
|---|
| 169 | </page>
|
|---|
| 170 |
|
|---|
| 171 | <page inherit="end" title="Templates">
|
|---|
| 172 | Positives:
|
|---|
| 173 |
|
|---|
| 174 | - Templates are very powerful
|
|---|
| 175 | - Templates can compile down to efficient code
|
|---|
| 176 |
|
|---|
| 177 | Negatives:
|
|---|
| 178 |
|
|---|
| 179 | - Templates can be very hard to understand.
|
|---|
| 180 | - Templates can be very hard to decider at compile time.
|
|---|
| 181 | - Templates can be slow to compile.
|
|---|
| 182 | - Complex templates can restrict portability.
|
|---|
| 183 |
|
|---|
| 184 | So use sparingly:
|
|---|
| 185 |
|
|---|
| 186 | - When you need type safety on a generic type
|
|---|
| 187 | - When you need inline call efficency
|
|---|
| 188 | - When you can avoid recreating code many times
|
|---|
| 189 | - When the code remains readable
|
|---|
| 190 | </page>
|
|---|
| 191 |
|
|---|
| 192 | <page inherit="middle" title="Design Patterns">
|
|---|
| 193 | OpenSceneGraph use Design Patterns :
|
|---|
| 194 |
|
|---|
| 195 | - To produce clear designs
|
|---|
| 196 | - To uses designs that are familiar
|
|---|
| 197 | - That are already documented
|
|---|
| 198 | - And with standard terminology
|
|---|
| 199 |
|
|---|
| 200 | - Promotes collaboration!
|
|---|
| 201 |
|
|---|
| 202 | Design Patterns used include:
|
|---|
| 203 |
|
|---|
| 204 | - Composite -> osg::Group
|
|---|
| 205 | - Visitor -> osg::NodeVisitor
|
|---|
| 206 | - Chain of Responsibility -> Plugins
|
|---|
| 207 | - Singlon -> osgDB::Registry
|
|---|
| 208 | - Observer -> see end of talk ;-)
|
|---|
| 209 |
|
|---|
| 210 | </page>
|
|---|
| 211 |
|
|---|
| 212 | <page inherit="end" title="Memory management">
|
|---|
| 213 | Standard C++ has constructors and destructors, but...
|
|---|
| 214 |
|
|---|
| 215 | No compiler or automatically runtime support for ensuring that no dangling pointers or leak memory occurs.
|
|---|
| 216 |
|
|---|
| 217 | Partial solution:
|
|---|
| 218 |
|
|---|
| 219 | - Reference counting
|
|---|
| 220 | - Automatic deletion when reference count goes to zero.
|
|---|
| 221 |
|
|---|
| 222 | More complete solution:
|
|---|
| 223 |
|
|---|
| 224 | - Smart pointers automatically increment and decrement.
|
|---|
| 225 | - Smart pointers are robust in the presence of exceptions.
|
|---|
| 226 | - Smart pointers make handling of scope easy
|
|---|
| 227 | </page>
|
|---|
| 228 |
|
|---|
| 229 | <page inherit="end" title="ref_ptr">
|
|---|
| 230 | Templated smart pointer to the rescue!!
|
|---|
| 231 |
|
|---|
| 232 | Simple scope example:
|
|---|
| 233 |
|
|---|
| 234 | {
|
|---|
| 235 | // construct an Node, and automatically
|
|---|
| 236 | // increment it's reference count by one.
|
|---|
| 237 | osg::ref_ptr(osg::Node) node = new osg::Node;
|
|---|
| 238 |
|
|---|
| 239 | // do stuff (that doesn't increment it ref count)
|
|---|
| 240 |
|
|---|
| 241 | }; // ref_ptr goes out of scope and its destructor
|
|---|
| 242 | // automatically decrements ref count and if it goes to
|
|---|
| 243 | // zero then its deleted.
|
|---|
| 244 |
|
|---|
| 245 | </page>
|
|---|
| 246 |
|
|---|
| 247 | <page inherit="end" title="ref_ptr() cont.">
|
|---|
| 248 | More complex scope example:
|
|---|
| 249 |
|
|---|
| 250 | osg::ref_ptr(osg::Group) group = new osg::Group;
|
|---|
| 251 |
|
|---|
| 252 | {
|
|---|
| 253 | // construct an Node, and automatically
|
|---|
| 254 | // increment it's reference count to one.
|
|---|
| 255 | osg::ref_ptr(osg::Node) node = new osg::Node;
|
|---|
| 256 |
|
|---|
| 257 | // note we have convert to C pointer using .get()
|
|---|
| 258 | // addChild increments ref count to two
|
|---|
| 259 | group->addChild(node.get())
|
|---|
| 260 |
|
|---|
| 261 | }; // ref_ptr goes out of scope and its destructor
|
|---|
| 262 | // automatically decrements ref count down to 1
|
|---|
| 263 | // so it doesn't get deleted, group still keeps
|
|---|
| 264 | // its copy.
|
|---|
| 265 | </page>
|
|---|
| 266 |
|
|---|
| 267 | <page inherit="end" title="ref_ptr cont.">
|
|---|
| 268 | Problems... circular references...
|
|---|
| 269 |
|
|---|
| 270 | If two objects both has ref_ptr to each other then they never can be deleted!
|
|---|
| 271 |
|
|---|
| 272 | Exercise 2d_observer_ptr explores this issue.
|
|---|
| 273 | </page>
|
|---|
| 274 |
|
|---|
| 275 | <page inherit="end" title="observer_ptr">
|
|---|
| 276 | Solution to circular references - osg::observer_ptr
|
|---|
| 277 |
|
|---|
| 278 | osg::observer_ptr doesn't increment the reference count, but does automatically get reset to 0 when the object its
|
|---|
| 279 | observing get deleted.
|
|---|
| 280 |
|
|---|
| 281 | Now onto the Exercises/02_memory!
|
|---|
| 282 | </page>
|
|---|
| 283 | </presentation>
|
|---|