cs193p - lecture 19 - stanford...

56
CS193P - Lecture 19 iPhone Application Development OpenGL ES 1 Monday, March 15, 2010

Upload: doandien

Post on 25-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

CS193P - Lecture 19iPhone Application Development

OpenGL ES

1Monday, March 15, 2010

Page 2: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Announcements• Final projects due in 7 days

■ Tuesday, March 16th 11:59 pm■ Submit:

■ Code■ Power-point/Keynote slides■ ReadMe file

• Final project demos■ March 18, from 12:15 - 3:15 pm in Hewlett 201■ 2 minute presentation, followed by demo-fair

■ Rapid-fire!!■ Time limit strictly enforced

■ Let us know if you do not want to be recorded

2Monday, March 15, 2010

Page 3: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Today’s Topics

AdvancedNovice

OpenGL on Desktop

OpenGL ES on iPhone

3Monday, March 15, 2010

Page 4: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Today’s Topics

AdvancedNovice

OpenGL on Desktop

OpenGL ES on iPhone

OpenGL Tutorialson the Internet

3Monday, March 15, 2010

Page 5: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Today’s Topics

AdvancedNovice

OpenGL on Desktop

OpenGL ES on iPhone

OpenGL Tutorialson the Internet

This Lecture

3Monday, March 15, 2010

Page 6: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• OpenGL Overview• Coordinate Systems and Transformations• Drawing Geometry• Using Textures• Other Details

Today’s Topics

4Monday, March 15, 2010

Page 7: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview

5Monday, March 15, 2010

Page 8: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview

6Monday, March 15, 2010

Page 9: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview• Software interface for graphics hardware

6Monday, March 15, 2010

Page 10: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview• Software interface for graphics hardware• Quickly render 2D or 3D graphics

6Monday, March 15, 2010

Page 11: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview• Software interface for graphics hardware• Quickly render 2D or 3D graphics• Hardware implementation agnostic

6Monday, March 15, 2010

Page 12: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview• Software interface for graphics hardware• Quickly render 2D or 3D graphics• Hardware implementation agnostic

6Monday, March 15, 2010

Page 13: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL Overview• Software interface for graphics hardware• Quickly render 2D or 3D graphics• Hardware implementation agnostic

• OpenGL ES is a subset of OpenGL■ GLUT and GLU are not available on the iPhone

6Monday, March 15, 2010

Page 14: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL is a state machine

7Monday, March 15, 2010

Page 15: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL is a state machine• Change Machine State

• Issue Drawing Commands

• Read Back State, Drawing Results

glEnable(); glDisable(); glMatrixMode(); glBindFramebufferOES();glViewport(); glVertexPointer(); glColorPointer(); glTranslatef() ... !

glDrawArrays(); glDrawElements(); ...

glGetBooleanv(); glGetFloatv(); glReadPixels(); ...

8Monday, March 15, 2010

Page 16: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Coordinate Systems

9Monday, March 15, 2010

Page 17: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

The Coordinate System Onion

Window Coordinates

Normalized Device Coordinates

Clip and Eye Coordinates

World Coordinates

Object Coordinates

10Monday, March 15, 2010

Page 18: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Window Coordinates

(0,0)

(320, 480)

11Monday, March 15, 2010

Page 19: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Normalized Device Coordinates

(0,0)

(1, 1)

(-1,-1)

12Monday, March 15, 2010

Page 20: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Normalized Device Coordinates

(0,0,0)

(1,1,1)

(-1,-1,-1)

13Monday, March 15, 2010

Page 21: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Clip Coordinates

ILLUSTRATIONNOT FOUND

14Monday, March 15, 2010

Page 22: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Eye Coordinates

(0,0)

(right, top)

(left, bottom)

15Monday, March 15, 2010

Page 23: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Eye Coordinates

(0,0,0)

(left, top, -zNear)

16Monday, March 15, 2010

Page 24: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

World Coordinates

17Monday, March 15, 2010

Page 25: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

World Coordinates

17Monday, March 15, 2010

Page 26: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Object Coordinates

18Monday, March 15, 2010

Page 27: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

DemoTransformations

19Monday, March 15, 2010

Page 28: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Transformations Cheat Sheet

glMatrixMode(GL_PROJECTION);glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glOrthof(left, right, bottom, top, zNear, zFar);glFrustumf(left, right, bottom, top, zNear, zFar);

glRotatef(degrees, x, y, z);glTranslatef(x, y, z);glScalef(x, y, z);glMultMatrixf(matrix);

glPushMatrix();glPopMatrix();

20Monday, March 15, 2010

Page 29: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Drawing Geometry

21Monday, March 15, 2010

Page 30: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Vertices• OpenGL ES draws triangles, lines, and points• Object space vertices mapped into window space• Rasterize the shapes to get pixels

22Monday, March 15, 2010

Page 31: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Colors• Each vertex can have a color associated• RGBA

■ Alpha usually means opacity

• Lines and triangles interpolate colors

23Monday, March 15, 2010

Page 32: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Drawing Modes• Vertices are passed to OpenGL ES in arrays• Drawing modes determine how vertices are interpreted to

produce shapes• Vertex order matters

24Monday, March 15, 2010

Page 33: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Drawing Modes• Vertices are passed to OpenGL ES in arrays• Drawing modes determine how vertices are interpreted to

produce shapes• Vertex order matters

1

2

3

4

5

00

1

GL_TRIANGLES

24Monday, March 15, 2010

Page 34: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Drawing Modes• Vertices are passed to OpenGL ES in arrays• Drawing modes determine how vertices are interpreted to

produce shapes• Vertex order matters

1

2

3

4

5

00

1

GL_TRIANGLES

0 23

45

10123

GL_TRIANGLE_FAN

24Monday, March 15, 2010

Page 35: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Drawing Modes• Vertices are passed to OpenGL ES in arrays• Drawing modes determine how vertices are interpreted to

produce shapes• Vertex order matters

1

2

3

4

5

00

1

GL_TRIANGLES

0 23

45

10123

GL_TRIANGLE_FAN GL_TRIANGLE_STRIP

1

2

3

4

5

001

23

24Monday, March 15, 2010

Page 36: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 37: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 38: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0

1

TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 39: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0

12

TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 40: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0

12

3

TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 41: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0

12

3

4TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 42: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

• Use a triangle strip

Drawing a Tetrahedron

0

12

3

4

5

TETRA_TOP

TETRA_LEFTTETRA_RIGHT

TETRA_FRONT

25Monday, March 15, 2010

Page 43: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

DemoGeometry

26Monday, March 15, 2010

Page 44: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Geometry Cheat Sheet

GLfloat vertexArray[] = {x1,y1,z1,x2,y2,z2,x3,y3,z3, ... };

GLubyte colorArray[] = {r1,g1,b1,a1,r2,g2,b2,a2,r3,g3,b3,a3, ... };

glVertexPointer(dimOfVertices, GL_FLOAT, arrayOffset, vertexArray);glEnableClientState(GL_VERTEX_ARRAY);glColorPointer(4, GL_UNSIGNED_BYTE, arrayOffset, colorArray);glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, arrayOffset, numberOfVertices);! // or GL_TRIANGLE_FAN, GL_TRIANGLES

glDisableClientState(GL_VERTEX_ARRAY);glDisableClientState(GL_COLOR_ARRAY);

27Monday, March 15, 2010

Page 45: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Using Textures

28Monday, March 15, 2010

Page 46: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Mapping• Color pixels according to an image in memory• Almost always 2D (3D textures are possible though)• Vertices are given texture coordinates (u,v)

(0,0)

(1,1)

u

v

29Monday, March 15, 2010

Page 47: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Atlasing• Swapping textures often is inefficient• Instead make one giant shared texture

30Monday, March 15, 2010

Page 48: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Atlasing• Swapping textures often is inefficient• Instead make one giant shared texture

30Monday, March 15, 2010

Page 49: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Atlasing• Swapping textures often is inefficient• Instead make one giant shared texture

• Vertices may not have consistent (u,v) coordinates

31Monday, March 15, 2010

Page 50: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Atlasing• Swapping textures often is inefficient• Instead make one giant shared texture

• Vertices may not have consistent (u,v) coordinates

UPRIGHT_ONE

TWOTHREE

FOUR RIGHT_ONELEFT_ONE

TETRA_TOP

TETRA_LEFT

TETRA_RIGHT

TETRA_FRONT

32Monday, March 15, 2010

Page 51: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

DemoTextures

33Monday, March 15, 2010

Page 52: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Texture Cheat Sheetbash$ export PATH=${PATH}:/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/bash$ texturetool -f PVR -e PVRTC image.png -o image.pvrtcbash$ # image.png must be square with power of side length -- e.g. 64, 256, 1024

#import "PVRTexture.h"// From Apple's PVRTextureLoader Example Project

NSString * path = [[NSBundle mainBundle] pathForResource: @"image" ofType:@"pvrtc"];PVRTexture * texture = [[PVRTexture alloc] initWithContentsOfFile: path];

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texture.name);

GLFloat textureCoordArray[] = { u1,v1, u2,v2, u3,v3, ... };

glTexCoordPointer(2, GL_FLOAT, arrayOffset, textureCoordArray);glEnableClientState(GL_TEXTURE_COORD_ARRAY);glDrawArrays(GL_TRIANGLE_STRIP, arrayOffset, numberOfVertices);glDisableClientState(GL_TEXTURE_COORD_ARRAY);

34Monday, March 15, 2010

Page 53: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Other Details

35Monday, March 15, 2010

Page 54: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

OpenGL ES 1.1 vs. ES 2.0• This lecture described OpenGL ES1.1• ES 2.0 is drastically different

■ Uses shader based approach■ More flexible, harder to wrap your head around

• ES 2.0 not available in iPhones before 3GS

36Monday, March 15, 2010

Page 55: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Want to know more?• Apple OpenGL Programming Guide• OpenGL Redbook• The Internets• Stanford CS 148, 248

• Topics of interest■ Framebuffers■ Depth Testing■ Backface Culling■ Animation (not inherently part of OpenGL)■ Transparency and Blending■ Lighting and Shading

37Monday, March 15, 2010

Page 56: CS193P - Lecture 19 - Stanford Universityweb.stanford.edu/class/cs193p/cgi-bin/drupal/system/...•Quickly render 2D or 3D graphics •Hardware implementation agnostic •OpenGL ES

Questions?

38Monday, March 15, 2010