0% found this document useful (0 votes)
78 views34 pages

Document 1

This document describes an OpenGL-based billiards simulation. It discusses modeling the billiard balls by texturing spheres and mapping the textures. It also covers modeling the billiards table by drawing its rectangular and trapezoidal surfaces, as well as the pockets using quad strips, fans, and triangles. The document outlines how the simulation handles force simulation, viewing, animation, and user control.

Uploaded by

semabay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views34 pages

Document 1

This document describes an OpenGL-based billiards simulation. It discusses modeling the billiard balls by texturing spheres and mapping the textures. It also covers modeling the billiards table by drawing its rectangular and trapezoidal surfaces, as well as the pockets using quad strips, fans, and triangles. The document outlines how the simulation handles force simulation, viewing, animation, and user control.

Uploaded by

semabay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Pool Billiard

An OpenGL-based billiard simulation

Stefan HUBER
Kamran SAFDAR
Andreas SCHRÖCKER

Fachbereich Computerwissenschaften
Universität Salzburg

June 10, 2009

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


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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 2/34


Outline

1 Modelling the Balls


An Introduction to Texture Mapping
What is Texture Mapping?
2D Texture Mapping
Texture Mapping a Sphere
Texturing the Billiard Balls
Drawing the Billiard Balls using OpenGL

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 ControlS. Huber, K. Safdar, A. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 3/34
The Ball

Rendered using a sphere (gluSphere)


with unit radius, 50 slices and 50 stacks.
A texture image is wrapped around it to
give it a look of billiard ball.

Figure: Wire Sphere

Figure: Texture image

Figure: Textured Sphere

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


Texture Mapping

Mapping of a function, called a texture


map, onto a surface.
Can be understood as gluing a 2D
image onto the surface of a 3D object.
Pioneered by Edwin Catmull in 1974.
Can be used to enhance the aesthetic
appearance of an object at a relatively
Figure: A 2D image mapped onto a
small computational cost. 3D cylinder.
Image source: http://prosjekt.ffi.no/unik-4660/
lectures04/chapters/Basic.html

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


2D Texture Mapping

The 2D texture space is defined in terms of parameters s and t each


ranging from 0 to 1.
For each point in the 3D object space, a corresponding point in the 2D
texture space, called a texel, is mapped.
While rendering the 3D object, each point (x , y , z ) is assigned a color
value computed from the corresponding 2D texel value (s, t ).

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


Geometry of a Sphere

z
Geometrically stating, a sphere with
radius r centred at C (xc , yc , zc ) is
the locus of points
Pi (x , y , z ), (0 ≤ i ≤ n), such that P
t r

(x − xc )2 +(y − yc )2 +(z − zc )2 = r 2 C
s
For any point P (x , y , z ) on surface y
P0
of the sphere, let s be the angle
−−→
from +x-axis to CP 0 , and t be the x
−→
angle from +z-axis to CP, such
that (0 ≤ s ≤ 2π) and (0 ≤ t ≤ π).

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


Geometry of a Sphere

z
The parametric representation can
be explained mathematically as:

x = xc + r · sin t · cos s P
y = yc + r · sin t · sin s t r
z = zc + r · cos t
C
s
with s ∈ [0, 2π] and t ∈ [0, π] y
P0
So, if we know the coordinates of a
x
point P (x , y , z ) on surface of the
sphere, then we can easily find out
the values of parameters s and t.

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


Billiard Ball Textures

The textures are 512 × 256 RAW images.


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

0 s 1
North pole
0

t 256

1
512 South pole

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


Mapping the Ball Textures

−z
t=0
North pole
−y
s = 0.5
Texture coordinates are assigned such
that: −x

s = 0.00 at y = +r
s = 0.25 at x = +r +x
t = 0.0 at z = −r
s = 0.50 at y = −r
t = 1.0 at z = +r
s = 0.75 at x = −r
s = 1.00 at y = +r s=1
+y s = 0
t=1
South pole
+z

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


The Basic Algorithm

Draw the Billiard Ball


Load 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 the
scheme already known and load the texels
Do garbage collection and house keeping

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


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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 12/34


Outline

1 Modelling the Balls

2 Modelling the Table


Drawing the Table
Texturing the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

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


Structure of the Table

quad strip quad strip

rectangle rectangle
The Table is axis
aligned.
Rectangular planes
trapezoid
GL QUADS
Pockets
GL QUAD STRIPS
GL TRIANGLE FAN triangle
GL TRIANGLES
quad strip

triangle fan
trapezoid
triangle
S. Huber, K. Safdar, A. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 14/34
Drawing the Edge

Draw a half of and a quarter of a circle


with brute-force scan-conversion
algorithm
Save the points in two arrays
Connect the points with
GL QUAD STRIPS
For the hole: Draw an arc and use
GL TRIANGLE FAN

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


Drawing the Legs

A cylinder is drawn
void gluCylinder()
And closed with
void gluDisk()

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


Symmetric Construction of the Table

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


Full Table

We make use
of the
symmetry in
the table’s
geometry.
One quarter
of a table is
drawn 4 times
with
glScalef().

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


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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 19/34


Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

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


Force Simulation

Orthogonal projection for drawing in 2D


glOrtho(0, 10.0, 0.0, 10.0, -1, 1);
We used three GL QUADS with different
blending 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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 21/34


Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing
Camera Movement

5 Animation

6 Control

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


A Top View

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


A Strike Direction View

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


Looking at the Center of the Table

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


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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 26/34


Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation
Movement and Friction
Rotation of Balls
Collision with the Cushion
Collision of Balls

6 Control
S. Huber, K. Safdar, A. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 27/34
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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 28/34


Rotation of Balls

In real life, movement of a ball is understood as:


Displacement from start position
Revolution around its center in the direction of movement
Rotation matrix is stored for every ball and applied when drawing - on
every step of animation a new rotation matrix is multiplied on this stored
matrix

x 2 (1 − c ) + c xy (1 − c ) − zs xz (1 − c ) + ys
 
0
yx (1 − c ) + zs y 2 (1 − c ) + c yz (1 − c ) − xs 0
xz (1 − c ) − ys yz (1 − c ) + xs z 2 (1 − c ) + c
 
0
0 0 0 1

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

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


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 the
movement vector (remember, cushions are parallel to x or z axis).

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


Collision of Balls

-(a-b)

A
a

(a-b)

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


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 b1b2
13
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 balls
18 vec_add (& b2 -> move , &v2 , &b2 -> move );
19 }
20 }

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


Outline

1 Modelling the Balls

2 Modelling the Table

3 Force Simulation

4 Viewing

5 Animation

6 Control

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


Control

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


Up and down arrow keys: Move towards the ball and
backwards (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. Schröcker: Pool Billiard, An OpenGL-based billiard simulation 34/34

You might also like