Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007

Similar presentations


Presentation on theme: "OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007"— Presentation transcript:

1 OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007 http://graphics.stanford.edu/courses/cs248-07/

2 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL n Is a mechanism to create images in a frame buffer n Is an API to access that mechanism n Is well specified OpenGL n Is not a window system n Is not a user interface n Is not a display mechanism n Does not even own the framebuffer n It is owned by the window system so it can be shared n But OpenGL defines its attributes carefully

3 CS248 Lecture 2Kurt Akeley, Fall 2007 White-square code // Draw a white square against a black background #include #define GLUT_DISABLE_ATEXIT_HACK // yuck! #include void draw() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glOrtho(0, 4, 0, 4, -1, 1); glBegin(GL_POLYGON); glVertex2i(1, 1); glVertex2i(3, 1); glVertex2i(3, 3); glVertex2i(1, 3); glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutCreateWindow("whitesquare"); glutDisplayFunc(draw); glutMainLoop(); } OpenGL GLUT

4 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL portion of white-square code glClear(GL_COLOR_BUFFER_BIT); // black background glLoadIdentity();// allow multipass glOrtho(0, 4, 0, 4, -1, 1);// int cast to double glBegin(GL_POLYGON);// draw white square glVertex2i(1, 1); glVertex2i(3, 1); glVertex2i(3, 3); glVertex2i(1, 3); glEnd(); glFlush();// force completion

5 CS248 Lecture 2Kurt Akeley, Fall 2007 Red-book example 1-1 OpenGL code glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, 0.75, 0.0); glVertex3f(0.25, 0.75, 0.0); glEnd(); glFlush();// force completion

6 CS248 Lecture 2Kurt Akeley, Fall 2007 State tables OpenGL 2.0 Spec, Table 6.21. Framebuffer Control COLOR_CLEAR_VALUE0,0,0,0

7 CS248 Lecture 2Kurt Akeley, Fall 2007 State tables OpenGL 2.0, Table 6.5. Current Values and Associated Data

8 CS248 Lecture 2Kurt Akeley, Fall 2007 A snippet from the OpenGL 2.0 spec Vertices are specified by giving their coordinates in two, three, or four dimensions. This is done using one of several versions of the Vertex command: void Vertex{234}{sifd}( T coords ); void Vertex{234}{sifd}v( T coords ); A call to any Vertex command specifies four coordinates: x, y, z, and w. The x coordinate is the first coordinate, y is second, z is third, and w is fourth. A call to Vertex2 sets the x and y coordinates; the z coordinate is implicitly set to zero and the w coordinate to one.

9 CS248 Lecture 2Kurt Akeley, Fall 2007 The OpenGL Vertex Pipeline

10 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL shaded-quad code glClearColor(1, 1, 1, 1);// white glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glOrtho(0, 100, 0, 100, -1, 1); glBegin(GL_TRIANGLE_STRIP); glColor3f(0, 0.5, 0); // dark green glVertex2i(11, 31); glVertex2i(37, 71); glColor3f(0.5, 0, 0); // dark red glVertex2i(91, 38); glVertex2i(65, 71); glEnd(); glFlush();

11 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL vertex pipeline Emphasis is on data types Diagram ignores n Pixel pipeline n Texture memory n Display lists n … Display is not part of OpenGL Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

12 CS248 Lecture 2Kurt Akeley, Fall 2007 Vertex assembly (data types) struct { float x,y,z,w; float r,g,b,a; } vertex; Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

13 CS248 Lecture 2Kurt Akeley, Fall 2007 Vertex assembly (OpenGL) Vertex assembly n Force input to canonical format n Convert to internal representation –E.g., x, y to float n Initialize unspecified values –E.g., z = 0, w=1 n Insert current modal state –E.g., color to 0,0.5,0,1 n Or create using evaluators Error detection n INVALID_ENUM n INVALID_VALUE n INVALID_OPERATION n Especially between Begin and End Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

14 CS248 Lecture 2Kurt Akeley, Fall 2007 Vertex assembly (in our case) struct { float x,y,z,w; // 11, 31, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1 } vertex; Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations glColor3f(0, 0.5, 0); glVertex2i(11, 31); glVertex2i(37, 71); glColor3f(0.5, 0, 0); // no effect struct { float x,y,z,w; // 37, 71, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1 } vertex; Framebuffer

15 CS248 Lecture 2Kurt Akeley, Fall 2007 Vertex operations OpenGL n Transform coordinates n 4x4 matrix arithmetic n Compute (vertex) lighting n Compute texture coordinates n … In our case: n Scale (arbitrary 100x100) coordinates to fit window n No lighting, no texture coordinates Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

16 CS248 Lecture 2Kurt Akeley, Fall 2007 Primitive assembly (data types) Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations struct { float x,y,z,w; float r,g,b,a; } vertex; struct { vertex v0,v1,v2; } triangle; struct { vertex v0,v1; } line; struct { vertex v0; } point; or Framebuffer

17 CS248 Lecture 2Kurt Akeley, Fall 2007 Primitive assembly OpenGL n Group vertexes into primitives: n points, n lines, or n triangles n Decompose polygons to triangles n Duplicate vertexes in strips or fans In our case: n Create two triangles from a strip: Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations glBegin(GL_TRIANGLE_STRIP); glColor(green); glVertex2i(…); // 0 glVertex2i(…); // 1 glColor(red); glVertex2i(…); // 2 glVertex2i(…); // 3 glEnd(); 0 13 2 Framebuffer

18 CS248 Lecture 2Kurt Akeley, Fall 2007 Primitive operations OpenGL n Clip to the window boundaries n Actually to the frustum surfaces n Perform back-face / front-face ops n Culling n Color assignment for 2-side lighting In our case n Nothing happens Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

19 CS248 Lecture 2Kurt Akeley, Fall 2007 Rasterization (data types) Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations struct { float x,y,z,w; float r,g,b,a; } vertex; struct { vertex v0,v1,v2 } triangle; struct { short int x,y; float depth; float r,g,b,a; } fragment; Framebuffer

20 CS248 Lecture 2Kurt Akeley, Fall 2007 Rasterization OpenGL n Determine which pixels are included in the primitive n Generate a fragment for each such pixel n Assign attributes (e.g., color) to each fragment In our case: Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

21 CS248 Lecture 2Kurt Akeley, Fall 2007 Fragment operations OpenGL n Texture mapping n Fragment lighting (OpenGL 2.0) n Fog n Scissor test n Alpha test In our case, nothing happens: Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

22 CS248 Lecture 2Kurt Akeley, Fall 2007 Framebuffer (2-D array of pixels) Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations struct { float x,y,z,w; float r,g,b,a; } vertex; struct { vertex v0,v1,v2 } triangle; struct { short int x,y; float depth; float r,g,b,a; } fragment; struct { int depth; byte r,g,b,a; } pixel; Framebuffer

23 CS248 Lecture 2Kurt Akeley, Fall 2007 Fragment  framebuffer operations OpenGL n Color blending n Depth testing (aka z-buffering) n Conversion to pixels In our case, conversion to pixels: Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Key idea: images are built in the framebuffer, not just placed there! Framebuffer

24 CS248 Lecture 2Kurt Akeley, Fall 2007 Discussion

25 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL is mechanism Programmer specifies operation modes: n Vertex n Transformation n Lighting n Primitive n Back-face culling n Fragment n Shading (texture and lighting) n Framebuffer (blending and depth testing) Great example is the diagram at the back of the old (out of print) “blue book” … Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer

26 CS248 Lecture 2Kurt Akeley, Fall 2007

27 CS248 Lecture 2Kurt Akeley, Fall 2007 Abstraction From the OpenGL specification, Section 1.5: Thus OpenGL is an abstraction n It doesn’t specify what does happen n It specifies what appears to happen “An implementation must produce results conforming to those produced by the specified methods, but there may be ways to carry out a particular computation that are more efficient than the one specified.”

28 CS248 Lecture 2Kurt Akeley, Fall 2007 Implementation = abstraction L2 FB SP L1 TF Thread Processor Vtx Thread Issue Setup / Rstr / ZCull Geom Thread IssuePixel Thread Issue Data Assembler Host SP L1 TF SP L1 TF SP L1 TF SP L1 TF SP L1 TF SP L1 TF SP L1 TF L2 FB L2 FB L2 FB L2 FB L2 FB Vertex assembly Primitive assembly Rasterization Fragment operations Vertex operations Application Primitive operations NVIDIA GeForce 8800OpenGL Pipeline Framebuffer

29 CS248 Lecture 2Kurt Akeley, Fall 2007 Declarative vs. imperative abstractions Declarative (what, not how) n Descriptive: specify input and desired result n E.g., OmitHiddenSurfaces(); n Example systems: n RenderMan scene description n Inventor and Performer scene graphs Imperative (how, not what) n Procedural: specify actions to create the result n E.g., EnableDepthBuffer(); n Example systems n PostScript and Xlib n OpenGL and Direct3D

30 CS248 Lecture 2Kurt Akeley, Fall 2007 Direct3D 10OpenGL 2.1 Graphics software stacks GeForce 8800 VRML web application Radeon 9600 Unreal engine Gears of War GPU graphics API scene graph application

31 CS248 Lecture 2Kurt Akeley, Fall 2007 Direct3D 10OpenGL 2.1 Levels of abstraction GeForce 8800 VRML web application Radeon 9600 Unreal engine Gears of War Imperative Declarative Specific, high leverage General, low leverage

32 CS248 Lecture 2Kurt Akeley, Fall 2007 An imperative API defines an architecture We mean architecture in the Brooks/Blaauw sense: n Separate from and above implementation OpenGL defines an architecture: Can separate architecture’s interface and mechanism: n Direct3D 10 (interface) n WGF 2.0 (mechanism) OpenGL 2.1 GeForce 8800

33 CS248 Lecture 2Kurt Akeley, Fall 2007 Additional OpenGL mechanisms Pixel pipeline Client-server operation Display lists Error behavior and reporting

34 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL pixel pipeline Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer Texture memory Pixel assembly (unpack) Pixel operations Pixel pack Vertex pipelinePixel pipeline Application

35 CS248 Lecture 2Kurt Akeley, Fall 2007 Client-server operation OpenGL Client OpenGL Server Network Application Display (you) Client side Server side

36 CS248 Lecture 2Kurt Akeley, Fall 2007 Display lists glNewList( listname, …); glColor3f(…); glVertex3f(…); … glEndList(); glCallList( listname ); OpenGL Client OpenGL Server Network Application Display (you) Client side Server side Display list state

37 CS248 Lecture 2Kurt Akeley, Fall 2007 Error behavior and reporting No undefined operations n Architectural integrity n Application portability Queried, not returned n Why ? Enumerated error types n INVALID_ENUM n INVALID_VALUE n INVALID_OPERATION Should query after rendering OpenGL Client OpenGL Server Network Application Display (you)

38 CS248 Lecture 2Kurt Akeley, Fall 2007 Check for errors after rendering void CheckErrors() { bool got_error = false; while (int i = glGetError()) { got_error = true; fprintf(stderr, "ERROR: 0x%x\n", i); } if (got_error) exit(1); } Example application-defined error routine

39 CS248 Lecture 2Kurt Akeley, Fall 2007 OpenGL abstraction properties Immediate mode Orthogonality Programmability Details n Ordered operation n Modal state model n Synchronous operation

40 CS248 Lecture 2Kurt Akeley, Fall 2007 Immediate mode Different meanings to different people n Begin/End command sequences n Abstraction doesn’t maintain object descriptions n Vertex arrays stored on client side n Display lists don’t count n Implementation doesn’t queue indefinitely n E.g., doesn’t wait for all primitives before beginning to render OpenGL Client OpenGL Server Network Application Display (you)

41 CS248 Lecture 2Kurt Akeley, Fall 2007 Orthogonality Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer Texture memory Pixel assembly (unpack) Pixel operations Pixel pack Vertex pipelinePixel pipeline Application All primitives (including pixels) are rasterized All vertexes are treated equally (e.g., lighted) All fragments are treated equally (e.g., z-buffered)

42 CS248 Lecture 2Kurt Akeley, Fall 2007 Programmability Programmable “shaders,” of course Multi-pass composition n Intended from the start n Requires repeatability n Invariance and equivariance n Appendix A in OpenGL spec Vertex assembly Primitive assembly Rasterization Fragment operations Vertex operations Application Primitive operations Framebuffer  Application programmable

43 CS248 Lecture 2Kurt Akeley, Fall 2007 Abstraction details Ordered operation n Implementation may be out of order n But results must be as though computed in order Modal state model n Also in-order n Pipeline implementations require care Synchronous operation (as in synchronous i/o) n Data bound at call n Queries stall until data are ready and returned

44 CS248 Lecture 2Kurt Akeley, Fall 2007 Interface details Object naming (programmer-assigned, why) Lots of geometry-specification entry points (why)

45 CS248 Lecture 2Kurt Akeley, Fall 2007 Other things you should know Programming tips (Red book, Appendix G) n Correctness tips (e.g., pixel-exact 2-D rendering) n Performance tips (user/implementer contract) Extensions (www.opengl.org)www.opengl.org n Recent ones include history and examples n Great way to understand graphics algorithms

46 CS248 Lecture 2Kurt Akeley, Fall 2007 Summary OpenGL is an imperative abstraction (aka architecture) n Mechanism (the OpenGL pipeline) n Interface (API) to feed and manage the mechanism The pipeline is best understood in terms of data types: n Vertex n Primitive (point, line, triangle) n Fragment n Pixel You should trust and be familiar with the OpenGL specification

47 CS248 Lecture 2Kurt Akeley, Fall 2007 Reading assignment Before Tuesday’s class, read n FvD 14.10 Aliasing and antialiasing n Return to the OpenGL specification n E.g., state tables Optional: n Set up your OpenGL/GLUT programming environment

48 CS248 Lecture 2Kurt Akeley, Fall 2007 End


Download ppt "OpenGL Kurt Akeley CS248 Lecture 2 27 September 2007"

Similar presentations


Ads by Google