pool billiard - an opengl-based billiard simulation · pdf filepool billiard an opengl-based...

34
Pool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHR ¨ OCKER Fachbereich Computerwissenschaften Universit ¨ at Salzburg June 10, 2009 S. Huber, K. Safdar, A. Schr¨ ocker: Pool Billiard, An OpenGL-based billiard simulation 1/34

Upload: lyxuyen

Post on 14-Feb-2018

280 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Pool BilliardAn OpenGL-based billiard simulation

Stefan HUBERKamran SAFDAR

Andreas SCHROCKER

Fachbereich ComputerwissenschaftenUniversitat Salzburg

June 10, 2009

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 1/34

Page 2: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Table of Contents

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 2/34

Page 3: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the BallsAn Introduction to Texture Mapping

What is Texture Mapping?2D Texture MappingTexture Mapping a Sphere

Texturing the Billiard BallsDrawing the Billiard Balls using OpenGL

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 ControlS. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 3/34

Page 4: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

The Ball

Rendered using a sphere (gluSphere)with unit radius, 50 slices and 50 stacks.

A texture image is wrapped around it togive it a look of billiard ball.

Figure: Texture image

Figure: Wire Sphere

Figure: Textured Sphere

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 4/34

Page 5: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Texture Mapping

Mapping of a function, called a texturemap, onto a surface.

Can be understood as gluing a 2Dimage onto the surface of a 3D object.

Pioneered by Edwin Catmull in 1974.

Can be used to enhance the aestheticappearance of an object at a relativelysmall computational cost. Figure: A 2D image mapped onto a

3D cylinder.Image source: http://prosjekt.ffi.no/unik-4660/lectures04/chapters/Basic.html

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 5/34

Page 6: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

2D Texture Mapping

The 2D texture space is defined in terms of parameters s and t eachranging from 0 to 1.

For each point in the 3D object space, a corresponding point in the 2Dtexture space, called a texel, is mapped.

While rendering the 3D object, each point (x ,y ,z) is assigned a colorvalue computed from the corresponding 2D texel value (s, t).

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 6/34

Page 7: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Geometry of a Sphere

Geometrically stating, a sphere withradius r centred at C(xc,yc,zc) isthe locus of pointsPi(x ,y ,z),(0≤ i ≤ n), such that

(x−xc)2 +(y−yc)

2 +(z−zc)2 = r2

For any point P(x ,y ,z) on surfaceof the sphere, let s be the angle

from +x-axis to−−→CP ′, and t be the

angle from +z-axis to−→CP, such

that (0≤ s ≤ 2π) and (0≤ t ≤ π).

y

P

t

x

z

s

C

r

P ′

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 7/34

Page 8: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Geometry of a Sphere

The parametric representation canbe explained mathematically as:

x = xc + r · sin t · cossy = yc + r · sin t · sinsz = zc + r · cos t

with s ∈ [0,2π] and t ∈ [0,π]

So, if we know the coordinates of apoint P(x ,y ,z) on surface of thesphere, then we can easily find outthe values of parameters s and t .

y

P

t

x

z

s

C

r

P ′

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 8/34

Page 9: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Billiard Ball Textures

The textures are 512×256 RAW images.

The lines marked as North pole and South pole are mapped onto singlepoints at the poles.

s

t

0 1

1

0

512

256

North pole

South pole

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 9/34

Page 10: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Mapping the Ball Textures

Texture coordinates are assigned suchthat:

s = 0.00 at y = +rs = 0.25 at x = +rs = 0.50 at y =−rs = 0.75 at x =−rs = 1.00 at y = +r

t = 0.0 at z =−rt = 1.0 at z = +r

s = 0s = 1

t = 0

t = 1

North pole

South pole

s = 0.5

+x

−x

−y

+y

−z

+z

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 10/34

Page 11: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

The Basic Algorithm

Draw the Billiard BallLoad the texture map in memory

Create a quadric object

Enable texturing of the quadric object

Draw a quadric Sphere, generate texture coordinates for it as per thescheme already known and load the texels

Do garbage collection and house keeping

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 11/34

Page 12: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Drawing the Billiard Balls using OpenGL

1 void DrawBall()2 {3 GLuint texture_map;4 GLUquadric *qobj = gluNewQuadric();5

6 texture_map = LoadTextureRAW("textures/2.raw", true , 512, 256);7 glEnable(GL_TEXTURE_2D);8 glBindTexture(GL_TEXTURE_2D , texture_map);9 gluQuadricTexture(qobj ,GL_TRUE);

10 gluSphere(qobj ,1,50,50);11 gluDeleteQuadric(qobj);12 glDeleteTextures( 1, &texture_map );13 glDisable(GL_TEXTURE_2D);14 }

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 12/34

Page 13: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the Balls

2 Modelling the TableDrawing the TableTexturing the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 13/34

Page 14: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Structure of the Table

The Table is axisaligned.Rectangular planes

GL QUADS

PocketsGL QUAD STRIPSGL TRIANGLE FANGL TRIANGLES

rectangle rectangle

quad strip quad strip

triangle fan

triangle

triangle

quad strip

trapezoid

trapezoid

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 14/34

Page 15: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Drawing the Edge

Draw a half of and a quarter of a circlewith brute-force scan-conversionalgorithm

Save the points in two arrays

Connect the points withGL QUAD STRIPS

For the hole: Draw an arc and useGL TRIANGLE FAN

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 15/34

Page 16: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Drawing the Legs

A cylinder is drawn

void gluCylinder()

And closed with

void gluDisk()

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 16/34

Page 17: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Symmetric Construction of the Table

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 17/34

Page 18: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Full Table

We make useof thesymmetry inthe table’sgeometry.

One quarterof a table isdrawn 4 timeswithglScalef().

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 18/34

Page 19: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Table Texture

Two different raw files: Wooden texture and green cloth texture

The texture coordinates are specified with glTexCoord2d(u,v)

For GL QUADS: Set u,v to (0.0,0.0), (0.0,1.0), (1.0,0.0), (1.0,1.0)

For the rest: Calculate u,v so that it looks properly

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 19/34

Page 20: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 20/34

Page 21: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Force Simulation

Orthogonal projection for drawing in 2D

glOrtho(0, 10.0, 0.0, 10.0, -1, 1);

We used three GL QUADS with differentblending functions

Black background:glBlendFunc(GL ZERO,GL ZERO)Color slide: glBlendFunc(GL ONE,GL ZERO)Transparent slide:glBlendFunc(GL DST COLOR,GL SRC ALPHA)

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 21/34

Page 22: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 ViewingCamera Movement

5 Animation

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 22/34

Page 23: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

A Top View

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 23/34

Page 24: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

A Strike Direction View

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 24/34

Page 25: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Looking at the Center of the Table

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 25/34

Page 26: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Setting Camera in Display Function

1 switch (nCameraState)2 {3 case CAM_SHOOTING:4 gluLookAt(eye_x , eye_y , eye_z ,5 arrBalls[0].pos.x,0.285,arrBalls[0].pos.z,6 0.0, 1.0, 0.0);7 break;8

9 case CAM_OVERVIEW:10 gluLookAt(0,35,0, 0,0,0 , 1.0, 0.0, 0.0);11 break;12

13 case CAM_CENTER:14 gluLookAt(eye_x , eye_y , eye_z , 0,0,0, 0.0, 1.0, 0.0);15 break;16 }

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 26/34

Page 27: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 AnimationMovement and FrictionRotation of BallsCollision with the CushionCollision of Balls

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 27/34

Page 28: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Movement and Rotation of Balls

Movement: Adding a constant vector to the position.

Friction: Simulated by subtracting a part of the movement vector.

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 28/34

Page 29: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Rotation of Balls

In real life, movement of a ball is understood as:Displacement from start positionRevolution around its center in the direction of movement

Rotation matrix is stored for every ball and applied when drawing - onevery step of animation a new rotation matrix is multiplied on this storedmatrix

x2(1− c)+ c xy(1− c)− zs xz(1− c)+ ys 0yx(1− c)+ zs y2(1− c)+ c yz(1− c)− xs 0xz(1− c)− ys yz(1− c)+ xs z2(1− c)+ c 0

0 0 0 1

c = cos(angle),s = sin(angle),‖(x ,y ,z)‖= 1

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 29/34

Page 30: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Collision with the Cushion

The ball is reflected back when it strikes the cushion.

Reflection is calculated by changing the sign of x or z component of themovement vector (remember, cushions are parallel to x or z axis).

A

R

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 30/34

Page 31: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Collision of Balls

a

b

A

B

(a-b)

-(a-b)

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 31/34

Page 32: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Collision of Balls

1 void ball_check_collision(struct BALL* b1, struct BALL* b2) {2 GLdouble b1b2_norm;3 Vertex v1, v2, vdiff , b1b2;4

5 vec_diff(&b2->pos, &b1->pos, &b1b2);6 b1b2_norm = norm(&b1b2);7 if (b1b2_norm < BALL_DIAMETER) {8 GLdouble s1 = vec_dotp(&b1->move , &b1b2)/b1b2_norm;9 GLdouble s2 = vec_dotp(&b2->move , &b1b2)/b1b2_norm;

10 GLdouble sd = fabs(s1-s2);11

12 vec_mult(&b1b2 , 1/b1b2_norm , &b1b2); // build unit-vector of b1b213

14 vec_mult(&b1b2 , -sd, &v1);15 vec_mult(&b1b2 , sd, &v2);16

17 vec_add(&b1->move , &v1, &b1->move); // new speed of the balls18 vec_add(&b2->move , &v2, &b2->move);19 }20 }

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 32/34

Page 33: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 33/34

Page 34: Pool Billiard - An OpenGL-based billiard simulation · PDF filePool Billiard An OpenGL-based billiard simulation Stefan HUBER Kamran SAFDAR Andreas SCHROCKER¨ Fachbereich Computerwissenschaften

Control

Left and right arrow keys: Rotate around the y-axis

Up and down arrow keys: Move towards the ball andbackwards (eye y does not change)

The keys ’y’ and ’Y’: Decrease and increase eye y

Left mouse button: Push the ball

S. Huber, K. Safdar, A. Schrocker: Pool Billiard, An OpenGL-based billiard simulation 34/34