Upload
dangcong
View
217
Download
0
Embed Size (px)
Citation preview
CSc 165
Computer Game Architecture
10(a) – 3D Modeling
for Games
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
2
Overview
• Model Characteristics
• 3D Model File Formats
• Model Loaders
• Digital Content Creation (DCC) Tools
• Skinning and UV-unwrapping
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
3
Models
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
4
Static Data o 3D geometry (vertex data)
o Polygon (face) data
o Rendering attributes o Wireframe / Faceted / Smooth-shaded
o Lighting & Materials
o Texturing (“skinning”) data
Animation Data (sometimes) o Model structure (skeletons, joints)
o Model poses
o Animation sequences o walk / run / jump / die …
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
5
Common 3D Model File Formats
.3ds – 3D Studio Max format
.blend – Blender format
.dae – COLLADA Digital Asset Exchange format
.dem – USGS Standard for Digital Elevation Models
.dxf – Autodesk's AutoCAD format
.hdf – Hierarchical Data Format
.iges – Initial Graphics Exchange Specification
.iv – Open Inventor File Format Info
.lwlo, .lwob & .lwsc – Lightwave 3D file formats
.md2/.md3/.md4/.md5 – Quake Model Files
.ms3d – Milkshape 3D binary format
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
6
.msdl – Manchester Scene Description Language
.nff & .enff – (Extended) Neutral File Format
.obj – Alias|Wavefront Object Files
.off – 3D mesh Object File Format
.oogl – Object Oriented Graphics Library
.ply – Stanford Scanning Repository format
.pov – Persistence of Vision ray-tracer
.qd3d – Apple's QuickDraw 3D metafile format
.viz – used by Division's dVS/dVISE
.vrml – Virtual Reality Modeling Language
.x – Microsoft’s DirectX/Direct3D file format
.x3d – eXtensible 3D XML-based scene description format
See wikipedia – list of file formats
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
7
.OBJ File Commands
Vertex data o v – geometric data
o vt – texture data
o vn – vertex normals
Elements o p – point
o l – line
o f – face
o curv – curve
o surf – surface
Grouping o g – group name
o s – smoothing group
o mg – merging group
o o – object name
Render Attributes o usemtl – material name
o mtllib – material file name
o lod – level of detail
o shadow_obj – shadow casting
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
8
.OBJ Example # File: ‘cube.obj’
# This file uses “OBJ” format to define a cube which has a
# different “material” applied to each of its faces.
# Define 8 cube vertices
v -1.0 -1.0 -1.0
v -1.0 -1.0 1.0
v -1.0 1.0 -1.0
v -1.0 1.0 1.0
v 1.0 -1.0 -1.0
v 1.0 -1.0 1.0
v 1.0 1.0 -1.0
v 1.0 1.0 1.0
# Specify the file (library) containing materials
mtllib cube.mtl
#continued...
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
9
.OBJ Example (cont.)
# file ‘cube.obj’ (cont.)
# Define a group (g) and material for each face (f)
g front
usemtl red
f 2 6 8 4
g back
usemtl blue
f 1 3 7 5
g right
usemtl green
f 6 5 7 8
g top
usemtl yellow
f 4 8 7 3
g left
usemtl magenta
f 2 4 3 1
g bottom
usemtl cyan
f 2 1 5 6
#end of ‘cube.obj’
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
10
OBJ Face/Vertex Data Structures
Vertices Faces
1 x y z
2 x y z
3 x y z
4 x y z
5 x y z
6 x y z
. . .
1
V1
V2
V3
2 V1
V2
V3
3
V1
V4
# File ‘man.obj’
v -1.0 -1.0 -1.0
v -1.0 -1.0 1.0
v -1.0 1.0 -1.0
...
vt 0.72 0.32
vt 0.86 0.33
...
vn 1.0 0.0 1.0
vn -1.0 0.0 0.5
...
f 2/1/1 4/2/1 3/3/2
f 1/4/2 2/4/2 5/5/3
...
V4
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
11
Content Loaders in RAGE
Model
File
Read
Create
Insert
Scenegraph Object in some
specific file format
Format-specific
loader code
<<interface>>
MeshLoader
GenericEntity
WavefrontMeshLoader
+ loadAsset(fileName)
+ loadAsset(fileName)
<<interface>>
Entity
GenericSubEntity
- vertexBuffer
- TexCoordBuffer
- NormalsBuffer
<<interface>>
SubEntity
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
12
Example: .OBJ Content Loader
public class WavefrontMeshLoader implements MeshLoader
{ ...
public void loadAsset(Mesh mesh, Path path)
{ List<Float> verts = new ArrayList<>();
List<Float> norms = new ArrayList<>();
List<Float> texcoords = new ArrayList<>();
BufferedReader br = Files.newBufferedReader(path);
String line;
while ((line = br.readLine()) != null)
{ if (line.startsWith(VERTEX_COMMAND))
{ processVertexPositionCommand(line, verts); }
else if (line.startsWith(TEXTURE_COORD_COMMAND))
{ processVertexTextureCommand(line, texcoords); }
else if (line.startsWith(NORMAL_COMMAND))
{ processVertexNormalCommand(line, norms); }
else if (line.startsWith(FACE_COMMAND))
{ processVertexFaceCommand(line); }
else if (line.startsWith(MATERIAL_COMMAND))
{ materialLib = line.split(" ")[1]; }
}
SubMesh sm = mesh.createSubMesh(mesh.getName());
createVertexBuffers(verts, norms, texcoords);
sm.setVertexBuffer(toFloatBuffer(vertexPositionsList));
sm.setNormalBuffer(toFloatBuffer(vertexNormalsList));
sm.setTextureCoordBuffer(toFloatBuffer(vertexTexCoordsList));
sm.setIndexBuffer(toIntBuffer(vertexIndicesList)); } ...
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
13
Digital Content Creation (DCC) Tools
Too much work to do “by hand”
Model
File
Content
Loader Entity
Scenegraph
DCC
Tool
Model
File
Model
File
Model in Content-Loader format
Model in different format 1
Model in different format 2
commands
“Export”
“Import”
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
14
DCC Tools
Commercial o Maya
o 3DStudio Max
o Lightwave
o etc., etc. …
Free (or nearly) o Blender
o MilkShape3D
o Maya / 3DStudio Max for students
o etc. …
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
15
Blender Features:
Modeling, texturing, lighting, UV-mapping
Animation rigging/skinning
Particle, physics, soft-body, fluid & cloth simulations
Game engine, video post-processing
Python scripting/plugin support
Cross-platform (Windows, Linux, Mac OSX, FreeBSD, Solaris….)
Import/export for a wide variety of model file formats
Extensive online documentation/wiki support
Availability: Free (GNU GPL license), at http://www.blender.org
Tutorials: http://www.blender.org/support/tutorials
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Example Blender Models
16
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Basic Blender Controls
Viewing:
• Scroll-wheel: zoom
• MMB: orbit (rotate 3D view)
• Shift-MMB: pan
• Reset view to Top: NUM-7 Front: NUM-1 Side: NUM-3
• Z-key: toggle wireframe
• NUM-0: switch to camera view
• NUM-5: toggle ortho/perspective view
• HOME: set view to show all objects
• Ctrl-Z: undo
• Ctrl-S: save workspace
• Ctrl-N: reset (discard all; new session)
• F12: render
17
Object Manipulation (in object mode):
• RMB: select (SHIFT to add)
• A-key: toggle select-All
• B-key: activate border-selection
(rubber-band select)
• DEL-key: delete selected
• LMB: position 3D cursor
• Shift-A: add object (at cursor loc)
• Transform selection:
R-key: rotate S-key: scale G-key: grab
LMB to accept; RMB or ESC to cancel
• Quick move: RMB on object, drag,
LMB to drop
• Shift-D: duplicate selection
(drag to reposition)
• TAB: toggle object/edit mode
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
18
Building Models From Primitives
Extrude Multiple Extrusions
Scale Ends
Original Cube
Scale Inner
Surfaces
Add Bevels
and Smooth Model concept: The Essential Blender,
Roland Hess, Blender Foundation
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Model Exporting
Most DCC tools export a variety of formats Blender supports exporting to:
o Collada XML (“.dae”)
o Wavefront OBJ (“.obj”)
o Stanford Graphics Library (“.ply”)
o 3DStudio Max (“.3ds”)
o A host of others via “addons”
Including “OgreXML”
RAGE has ModelLoaders for o OBJ
o RAGE Skeletal Animation (custom Blender exporter) 19
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Exporting Models
20
# Blender3D v249 OBJ File:
CrossDemo.blend
# www.blender3d.org
mtllib CrossDemo.mtl
v 0.515410 2.745096 -0.496360
v 0.515410 2.745096 0.496359
v -0.477309 2.745096 0.496359
v -0.477309 2.745096 -0.496360
v 0.515410 3.737815 -0.496359
v 0.515410 3.737815 0.496360
v -0.477309 3.737815 0.496359
v -0.477309 3.737815 -0.496360
v -0.523100 6.135959 0.542150
v -0.523100 6.135958 -0.542150
v 0.561200 6.135959 0.542150
v 0.561201 6.135958 -0.542150
...#total 44 vertices
usemtl Material
s 1
f 2 6 7
f 5 1 4
f 12 10 9
f 13 14 15
f 1 42 44
f 21 24 44
...#total 42 faces
Export:
Ogre
OBJ
DAE
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Advanced Modeling: Sculpting
21
“Grab” brush
“Layer” brush “Inflate” brush
“Add” brush
applied to Sphere
“Texture” brush
Images from: The Essential Blender,
Roland Hess, Blender Foundation
Original sphere
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
22
Skinning
Applying “texture” to 3D models
Problem: texture mapping is per-polygon: Models are collections of polygons
u
v
Texture Map
(1,1)
(0,0)
Polygon
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
23
Texture Space Subdivision
Texturing meshes by dividing texture space:
u
v
Texture Map
Polygon Mesh
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
24
Skinning Example
Image credit: Practical Java Game Programming,
Clingman et.al., Charles River Media
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
25
Texture Subdivision Difficulties
Requires creating a complex mapping (and the model is often curved)
How do we create the texture map?
How do we determine the correct mapping?
Mapping texture locations
to model coordinates
Image credit: Practical Java Game Programming,
Clingman et.al., Charles River Media
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
26
Creating Texture Skins
Create the model
Flatten the model (“UV Unwrapping”) Mark seams
Cut along seams (“project”)
Save unwrapped (flattened) UV image
Save model with texture coordinates
Use UV image as a “pattern” to create tex map GIMP, PhotoShop, ...
Apply texture map to model
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
27
Example: Soup Can
Soup Can Model
(Cylinder)
Texture
Mapping
Image credit: 3D Game Programming All In One,
Kenneth Finney, Premier Press
Unwrap
Create
Texture
Image
GIMP, PhotoShop, …
Texture Image Model with Texture Coords
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Blender UV Unwrapping
28
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
29
Character Models
More complex, but same approach:
Create model
Mark/cut seams (some tools support groups)
Project UV’s
Save projection image
Use projection image to create texture
Apply texture to UV map/model
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
Blender: Character Example
30 Example from: 3D Game Programming All In One,
Kenneth Finney, Premier Press
Step 1: Load/Create Model
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
31
Step 2: Mark Seams
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
32
Step 3: Export UVs
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
33
Step 4: Paint Texture
GIMP,
PhotoShop,
MS Paint,
etc.
CSc 165 Lecture Note Slides
10a - 3D Modeling For Games
34
Step 5: Apply Texture to Model