www.cs.uakron.edu

Download Report

Transcript www.cs.uakron.edu

Game Engine Development
Yingcai Xiao
Game Engine Development
What do we need?
What tools do we have?
How can we design and implement?
We will answer those questions in an agile way.
What do we need?
Video Game: Interactive animation
Display
Device
Driver
(GDI)
Input
Device
Driver
Game
(Software)
Video Game: Interactive animation
Similar to all other programs:
data, algorithms, input, output.
Data: Game Objects
Algorithms: Animation
Input: Interactive Events
Output: Display
Game Objects (GO)
GO = Geometry + Attribute
Game Objects (GO)
Geometry:
primitives:
points, lines, triangles, polygons, spheres
tetrahedrons, hexahedrons, …
meshes:
grids: elevation, uniform, rectlinear, structured,
tin: triangular integrated network
Game Objects (GO)
Geometry:
meshes:
indexed
Free/Smooth curves and surfaces:
B-Splines
Game Objects (GO)
Geometry:
Stroked:
stroked primitives
stroked free curves and surfaces
composited:
avatars, prefabs, packages, …
Game
Objects
(GO)
Conversions
to indexed
meshes:
Data Set
Topology
Geometry
i
d
x
0
1
2
3
4
5
Point List
x
y
z
Cell List
# of
points
&
Point
idx in
Point
List
Data Point
Data List
i
Attribute
d
Values
x
Cell
Types
Cell
Type &
Offset
In Cell
List
Game
Objects
(GO)
Geometry
i
d
x
Convert a
plane to an
indexed
mesh:
0
y = 0;
0<=x<=10
0<=z<=10
Data Set
Topology
1
2
3
4
5
Point List
x
y
z
Cell List
# of
points
&
Point
idx in
Point
List
Data Point
Data List
i
Attribute
d
Values
x
Cell
Types
Cell
Type &
Offset
In Cell
List
Graphics Engine /
Display Engine /
Shading Engine
OpenGL: https://www.opengl.org
• Open Graphics Library
• SGI
• Open Source
• De facto Industry Standard
OpenGL: https://www.opengl.org
• With shaders to support GPU.
• Without shaders for easy learning.
• We are going to start without shaders.
OpenGL: https://www.opengl.org
• Tutorial with shaders:
http://www.opengl-tutorial.org
• Tutorial without shaders:
http://www.cs.uccs.edu/~ssemwal/indexGLTuto
rial.html
• We are going to have examples for both but
start without shaders.
Computer Graphics Textbooks
Edward Angel, University of New Mexico
Dave Shreiner, ARM, Inc
Interactive Computer Graphics: A Top-Down Approach
Using OpenGL, 5E. (no shaders)
Interactive Computer Graphics: A Top-Down
Approach with Shader-Based OpenGL, 6/E
Interactive Computer Graphics: A Top-Down
Approach with WebGL, 7/E
An Example for Game Programming
• The “Color Cube” example
• Users: interactive 3D animation
• Programmers: event-driven 3D animation of
geometric transformations.
• C++
• OpenGL for display
• Glut (GL Utility Toolkit) for interaction
• https://www.opengl.org/resources/libraries/glut/
The “Color Cube” example
• The “main” function:
•
•
•
initializes windows, display settings, and event
handlers.
Starts the event loop.
Post the first Redisplay event.
The “Color Cube” example
•
The event loop:
While (1) { // infinite loop
event = getEvent(); // from the queue
switch (event) {
case Redisplay: display (); break;
case Keyboard: keyboard(); break;
case WindowResize: resize(); postEvent(Redisplay); break;
default: idle();
}}
The “Color Cube” example
• The “display” function:
•
•
•
•
draws the game objects
OpenGL is a state machine: set the active parameters
before specifying vertices.
Per vertex setting: color, normal, texture, …
New vertices will use the current settings until they are
changed.
The “Color Cube” example
• The “keyboard” function:
•
•
•
•
The event handler for keyboard events.
Change display parameters to control the animation.
E.g. rotation angles, parametric variable t, simulation
variables such as speed.
Don’t directly call the “display” function.
Post the Redisplay event after everything is set.
glutPostRedisplay();
The “Color Cube” example
• The “idle” function:
•
•
•
•
•
The event handler for the “idle” event.
Invoked when no more event in the event queue.
Change display parameters to control the background
animation. E.g. rotation angles, parametric variable t,
simulation variables such as speed.
Don’t directly call the “display” function.
Post the Redisplay event after everything is set.
glutPostRedisplay();
The “Color Cube” example
• The “resize/reshape” function:
•
•
•
The event handler for the “WindowResize” event.
Need to reset the viewport so that the display still fits in
the viewport.
Not need to post the Redisplay event. Glut does it fro
you.
A More Advanced OpenGL Example
UA.cpp
The “UA” example
•
•
•
•
•
Matrix Stack
Matrix Mode
Display Aspect Ratio
Lighting
Material Properties of GOs.
OpenGL Matrix Stack
•
•
•
Matrices for geometric transformations are stacked.
Stack: first come last serve. The last matrix will be
applied first.
The following code segment rotate before translation
glTranslated(-2,-1.0,-6);
glRotatef(rotation.x, 1, 0, 0);
•
•
Use glPushMatrix to save the current stack.
Use glPopMatrix to throw away newly added matrices
and recover to the last pushed stack.
OpenGL Matrix Mode
•
•
•
•
•
Two types of geometric transformation matrices:
Projection and ModelView
Two types of projection matrices: perspective and
parallel.
Perspective projection: foreshortening effect for realistic
landscape display.
Parallel projection: no foreshortening effect for accurate
measurements in, e.g., CAD.
ModelView mode take care all non projection matrices
(translation, scaling, rotation, view orientation, …)
Perspective Projection:
Foreshortening: The size of the projected object becomes
smaller when the object moves away from the eye.
P1
P2
P’1
z
P’2
Default: eye is at a negative z location and looks towards the positive z direction.
'
M per
1
0

 0

0

0
0
1
0
0
0
0
1
d
0
0 
0 ;

1

'
P '  M per
P;
Perspective Projection
void glFrustum(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal
);
http://tildegarro.info
Parallel (Orthoganal) Projection
void glOrtho(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal
);
http://www.cs.sun.ac.za/
~lvzijl
gluLookAt:
eye location and direction
void gluLookAt(
GLdouble eyeX,
GLdouble eyeY,
GLdouble eyeZ,
GLdouble centerX,
GLdouble centerY,
GLdouble centerZ,
GLdouble upX,
GLdouble upY,
GLdouble upZ);
Edward Angel
Display Aspect Ratio
•
•
•
•
To keep round objects round.
ar = display-width / display-height;
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
Shading
•
•
•
Rendering of shades on 3D objects.
Lighting.
Material Properties of GOs.
Game Objects (GO)
Attributes: Appearance
color,
normal,
transparency,
texture,
bumpy map,
normal map,
…
Game Objects (GO)
Physical Properties:
rigid (fixed geometry)
deformable (changeable geometry)
breakable (changeable topology)
intangible (no defined geometry)
roughness,
friction, reflection and refraction coefficients,
weight, …
Animation
Keyframe:
Most commonly used.
Need to provide GUI for entering the keyframes.
Procedural:
For advanced physical simulation.
Tracking live actor:
Can be modified and saved.
Input Devices
CLI:
• Command Line Input
• Keyboard based
GUI:
• Graphical User Interface
• Mouse, touch screen, …
NUI:
• Natural User Interface
• Kinect, Leap Motion, …
Output
Hardware:
• Video Controller
• Graphics Card (2D/3D)
• GPU (graphics processing unit, a
dedicated processing unit for
rendering 3D graphics)
• GPGPU (general purpose GPU,
mostly used as servers for high
performance computing / HPC)
• Printers (2D / 3D)
Output
Software:
• GDI: Graphics Device Interface
• OpenGL (WebGL, OpenGL-ES)
• OpenCL / Vulkan for GPU
• ActiveX 3D