High-level real-time 3D graphics development with...

50
High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Transcript of High-level real-time 3D graphics development with...

Page 1: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

High-level real-time 3D graphics development with Coin3D

Karin Kosina (vka kyrah)

Page 2: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

2

Introduction

* Karin Kosina (vka kyrah)

* Computer graphics programmer and lecturer

* Maintainer of Coin3D Mac OS X port

* Feel free to get in touch with me at: [email protected]

PGP fingerprint:10EA 9B79 1DDB 7535 1AAB 4E8D C2B8 C0AC BE32 63FE

Page 3: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

3

Introduction (your turn)

* Programmers?* C++?

* Python?

* Computer graphics?* OpenGL?

* High-level toolkits?

* Coin3D/Open Inventor?

Page 4: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

4

What is 3D Graphics?

* Relatively new scientific field (first papers from the 1960s)

* We want to display a virtual (3D) scene on a (2D) screen* Concept: taking a picture with a virtual camera

* This process is often called rendering.

* 3D model (mathematical description of an object)* e.g. a sphere: defined by its radius

* Relationship between the objects in the scene* position of the objects in “world space”

* Attributes* colour, lighting,...

Page 5: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

5

High-Level 3D Graphics

Page 6: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

6

Page 7: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

7

So how does this work?

* Most widely used library for 3D graphics is OpenGL.* Cross-platform standard, very flexible, very fast.

* Direct3D* Microsoft Windows' proprietary 3D library.

Page 8: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

8

So how does this work?

* Most widely used library for 3D graphics is OpenGL.* Cross-platform standard, very flexible, very fast.

* Direct3D* Microsoft Windows' proprietary 3D library.

Page 9: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

9

So how does this work?

* Most widely used library for 3D graphics is OpenGL.* Cross-platform standard, very flexible, very fast.

* OpenGL works great, but it is very low-level.* You have to think in triangles, not in objects.

Page 10: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

10

Page 11: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

11

So how does this work?

* Most widely used library for 3D graphics is OpenGL.* Cross-platform standard, very flexible, very fast.

* OpenGL works great, but it is very low-level.* You have to think in triangles, not in objects.

* You have to organise the scene yourself.

* You have to optimise for different graphics cards.

* Alternative: High-level 3D graphics* Scene arranged in hierarchical structure

* Higher abstraction level

* Ease of use, programmer convenience

Page 12: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

12

Let me show you what I mean...

Page 13: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

13

A simple textured cube:

* Coin: 37 loc

* OpenGL: 560 loc

texturedcube/opengl/footexturedcube/coin/bar

Page 14: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

14

High-Level 3D Graphics APIs

* All 3D graphics projects end up using some kind of scene abstraction...

* Option #1: Re-invent the wheel and write your own...* especially popular in games

* Option #2: Use an existing SDK* Open Inventor/Coin3D

* Performer, OpenSG, Java3D,...

Page 15: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

15

Coin3D Overview* C++ object oriented high-level 3D graphics API

* Available as Free Software under the GNU GPL

* Portable across a wide range of platforms

* GNU/Linux, Mac OS X, Windows, SGI Irix,...

* Uses OpenGL for accelerated rendering

* Native file format: Inventor files (.iv)

* GUI bindings available for various toolkits

* SoQt for cross-platform applications using Qt

* Others (SoXt, SoWin, Sc21)

* C++ SDK

* Bindings to other languages available, e.g. Pivy for Python

Page 16: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

16

Hello World

Page 17: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

17

#include <Inventor/Qt/SoQt.h>#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoCube.h>

int main(int argc, char ** argv){ QWidget * mainwin = SoQt::init(argc, argv, argv[0]);

SoSeparator * root = new SoSeparator; root->ref(); SoCube *cube = new SoCube; root->addChild(cube);

SoQtExaminerViewer * eviewer = new SoQtExaminerViewer(mainwin); eviewer->setSceneGraph(root); eviewer->show(); SoQt::show(mainwin); SoQt::mainLoop();

root->unref(); delete eviewer; return 0;}

basic/helloworld.cpp

Page 18: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

18

Page 19: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

19

The Scenegraph

Page 20: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

A very simple scene

Page 21: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

A very simple scene

Separator { Group { Material { diffuseColor 0 0 1 } Cube { } } Translation { translation 3 2 0 } Cube { }}

Page 22: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

22

Inventor files vs. C++ code

Separator { Group { Material { diffuseColor 0 0 1 } Cube { } } Translation { translation 3 2 0 } Cube { }}

SoSeparator *root = new SoSeparator;root->ref();

SoGroup *group = new SoGroup;root->addChild(group);SoMaterial *mat = new SoMaterial;mat->diffuseColor.setValue(0,0,1);SoCube *cube = new SoCube;group->addChild(mat);group->addChild(cube);

SoTransform *trans = new SoTransform;trans->translation.setValue(3,2,0);root->addChild(trans);

SoCube *cube2 = new SoCube;root->addChild(cube2);

cpp-vs-iv/cpp.cppcpp-vs-iv/iv.iv

Page 23: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

23

Scenegraph components

Separator { Group { Material { diffuseColor 0 0 1 } Cube { } } Translation { translation 3 2 0 } Cube { }}

Page 24: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

24

Scenegraph components

* Shape nodes* Geometry in the scene

components/test.iv

Page 25: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

25

Scenegraph components

* Shape nodes* Geometry in the scene

components/test.iv

Page 26: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

26

Scenegraph components

* Shape nodes* Geometry in the scene

components/test.iv

Page 27: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

27

Scenegraph components

* Shape nodes* Geometry in the scene

components/test.iv

Page 28: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

28

Scenegraph components

* Shape nodes* Geometry in the scene

* Property nodes* OpenGL state

* Inventor state

components/test.iv

Page 29: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

29

Scenegraph components

* Shape nodes* Geometry in the scene

* Property nodes* OpenGL state

* Inventor state

components/test.iv

Page 30: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

30

Scenegraph components

* Shape nodes* Geometry in the scene

* Property nodes* OpenGL state

* Inventor state

components/test.iv

Page 31: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

31

Scenegraph components

* Shape nodes* Geometry in the scene

* Property nodes* OpenGL state

* Inventor state

components/test.iv

Page 32: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

32

Scenegraph components

* Shape nodes* Geometry in the scene

* Property nodes* OpenGL state

* Inventor state

* Group nodes* Collect groups of nodes

into a subtree to build a hierarchy

components/test.iv

Page 33: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

33

Scenegraph rendering

Page 34: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Actions

* Scenegraph is traversed from top to bottom and from left to right

* Each node can react to the action (behaviour depending on node type)

* Nodes inherit state from nodes visited earlier

* Rendering the scene is an action

Page 35: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

The SoGLRenderAction – behaviours

* Group nodes traverse their children

* Shape nodes draw their geometry

* Property nodes set the OpenGL state

* usually replacing the previous state

* except transformation (concatenated) and light sources (added)

Page 36: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

36

SoGroup vs. SoSeparator

Page 37: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Group Nodes Revisited

* SoGroup: Nodes inherit state from to the left and above

* SoSeparator saves the state before traversing its children, and restores it when done.

* cf. OpenGL push and pop

Page 38: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Group nodes revisited

group-vs-separator/group.iv

Page 39: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Group nodes revisited

group-vs-separator/separator.iv

Page 40: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Transformations revisited

* Transformations are property nodes in the scenegraph

* Convenience nodes for basic transformations:* SoRotation

* SoTranslation

* SoScale

* It is also possible to specify the transformation matrix directly

* SoMatrixTransform

Page 41: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

41

Achtung! Here lies dragons...

Page 42: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Transformations revisited

* Matrix multiplication is not commutative

* The order of operations is important

* Example: combined rotation and translation

rotate first, than translate

translate first, than rotate

Page 43: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

#Inventor V2.1 ascii

Separator { Separator { Rotation { rotation 0 0 1 0.785 } Translation { translation 8 0 0 } Material { diffuseColor 1 0 0 } Cube { } } Separator { Translation { translation 8 0 0 } Rotation { rotation 0 0 1 0.785 } Material { diffuseColor 0 1 0 } Cube { } }}

Transformations revisited

basic/03_transorder.iv

Page 44: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

44

Summary:

* Hierarchical scene description in a scenegraph.* Group nodes, shape nodes, property nodes.

* Created programmatically or via Inventor files.

* To render the scene, the scenegraph is traversed top-down and left-right by the render action.

* State is inherited from nodes visited earlier.

* The order of operations is important.

Page 45: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

45

Qt integration

* Qt is a cross-platform GUI toolkit developed by Trolltech* Free Software edition available under the GPL

* excellent solution for cross-platform development

* The SoQt library provides integration with the Qt toolkit* OpenGL setup

* Event translation

* SoQt also provides a set of convenient viewer components* e.g. the SoQtExaminerViewer

Page 46: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

46

Getting started

Page 47: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

47

First steps

* Download Coin3D from http://www.coin3d.org.

* Build and install the Coin, SoQt, and simage libraries.

* Play around with the sample code.

* Documentation:

* API documentation: http://doc.coin3d.org

* The “Inventor Mentor” and “Inventor Toolmaker” books

* coin-discuss open mailing list hosted by SIM https://www.coin3d.org/mailman/listinfo/coin-discuss/

Page 48: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

Other Coin3D features...

* Easy way to read and write files* Including animations

* Field connections, notification mechanism* Automatic update based on scenegraph changes

* Interactive components* Draggers and manipulators

* Picking* Select and identify objects in the scene

* Event handling

* Sensors

io/readfile.cppio/writefile.cpp

drama/dragger.cpp

Page 49: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

49

Questions?

Page 50: High-level real-time 3D graphics development with Coin3Dkyrah.net/scratch/coin3d/coin3d_kkosina.pdf · High-level real-time 3D graphics development with Coin3D Karin Kosina (vka kyrah)

50

Legal

This presentation is licensed under the Creative Commons Attribution-Share Alike License.

To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Basically this means that you are free:

to Share – to copy, distribute, and transmit the work

to Remix – to adapt the work

under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor.

Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

For any reuse or distribution, you must make clear to others the license terms of this work.