Download presentation
Presentation is loading. Please wait.
1
1 3D modelling with OpenGL Brian Farrimond Robina Hetherington
2
2 A standard 3D OpenGL Program Draws a wire cube Ex02
3
3 Looking at the wire cube Default position
4
4 Program overview Drawing the scene Initialising the graphics Program start
5
5 Program overview What to do when the window is created, resized or moved Response to keyboard presses
6
6 main
7
7 Always there
8
8 main Setting up the window
9
9 main Initialise the graphics
10
10 init
11
11 init Does nothing in this case
12
12 main Name of the function that will draw the scene
13
13 main Name of the function that will respond to the keyboard
14
14 keyboard ASCII code of pressed key ASCII code for Esc key Terminate the program
15
15 main Name of the function that will be called when the window is created, resized or moved
16
16 reshape
17
17 Getting OpenGL to view the wire cube Two separate tasks: –Controlling the camera settings –Positioning the camera and the objects to be photographed Achieved by using matrices Matrix = Mathematical representation of a sequence of operations
18
18 OpenGL Matrices Controlling the camera settings –PROJECTION MATRIX Positioning the camera and the objects –MODELVIEW MATRIX
19
19 Ex02 The state of the matrices after the program has drawn the wire cube
20
20 Using orthographic projection The state of the matrices after the program has drawn the wire cube with orthographic projection
21
21 reshape Always the same
22
22 reshape PROJECTION Setting up the camera settings: perspective projection
23
23 reshape MODELVIEW Getting ready to position the camera and objects
24
24 Nate Robin’s Tutorial example Projection
25
25 display Position the camera
26
26 Where the camera is
27
27 Where the camera is looking
28
28 gluLookAt Always the same
29
29 gluLookAt
30
30 Changing the camera position gluLookAt(0, 5, 6, 0, 0, 0, 0, 1, 0);
31
31 display Position the camera Draw the object
32
32 Interacting with OpenGL
33
33 Interacting through the keyboard We have already met: Use Esc key to stop the program
34
34 Ex03 Now look at this … If key ‘a’ is pressed Increase variable g_yeye by 1 Redraw the scene
35
35 display Camera position stored in three variables
36
36 Declaring and initialising the camera position
37
37 Exercise Modify the code so that, in addition, ‘n’ moves the viewer left and ‘m’ moves the viewer right.
38
38 Using special keys
39
39 Using special keys GLUT codes for special keys
40
40 Other special keys
41
41 Exercise Modify Ex03.cpp to use the arrow keys to move the viewer left, right, up and down.
42
42 Animation Objects change position and appearance over time In an animation program: Repeat change the position/appearance data redraw the scene Until end of program
43
43 Example – rotating a cube Ex04 Note the poor quality of the animation
44
44 Example – rotating a cube If this continually changes then redrawing would visualise the change
45
45 The matrices Camera settings Camera and object positions
46
46 Repeatedly calling spin Tells OpenGL to call spin whenever there is nothing else to do
47
47 Handling the rotation angle Calling this function changes the rotation angle and redraws the scene This sets the initial rotation angle to 0 The rotation angle
48
48 The overall main function
49
49 Improving the quality – double buffering The problem –The OpenGL program continually writes new image data into the frame buffer –Before previous version has been fully displayed
50
50 Improving the quality – double buffering The solution –Two buffers (double buffering) –The OpenGL program writes new image data into one buffer –Digital to Analogue Converter reads from the other buffer –Buffers swapped as DAC prepares to draw another picture
51
51 Double buffering in OpenGL
52
52 Transformations Positioning objects in a scene is achieved by combinations of –Translation –Scaling –Rotation
53
53 Translation This performs a movement: –3 units in the positive x direction, –7 units in the negative y direction and –2 units in the positive z direction
54
54 Scaling stretches the scene –twofold in the x direction –and leaves the other directions unchanged.
55
55 Rotation Performs a rotation about an axis defined by a line joining the origin to the point (x, y, z). Rotations are usually made around one of the axes
56
56 Applying sets of transformations Object vertices can be expressed as column vectors Here is point P with coordinates x, y, z:
57
57 Applying sets of transformations The extra 1 at the bottom of the column vector enables the all transformations to be represented by 4x4 matrices.
58
58 Applying sets of transformations A 3D transformation is performed mathematically as a matrix multiplication. Here a transformation by matrix A moves the point P to (x’, y’, z’)
59
59 Applying sets of transformations We can represent this more neatly as P’ = A.P If another transformation, B, is carried out on P’ we get P’’ P’’ = B.P’ Hence we have: P’’ = B.A.P
60
60 Applying sets of transformations Modelling transformations are held in the modelview matrix In a sequence of transformations, the modelview matrix is multiplied by the corresponding sequence of 4x4 transformation matrices Order is crucial –A.B is not the same as B.A
61
61 Example 1 The cube is drawn Wire cube MODELVIEW MATRIX
62
62 Example 2 The cube is drawn, the cube is scaled Scale / Wire cube MODELVIEW MATRIX
63
63 Example 3 The cube is drawn, the cube is scaled, the cube is rotated, the cube is translated Translate / Rotate / Scale / Wire cube MODELVIEW MATRIX
64
64 Example 4 The cube is drawn, the cube is scaled, the cube is translated, the cube is rotated Rotate / Translate / Scale / Wire cube MODELVIEW MATRIX
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.