11/4/09 OpenGL OpenGL • OpenGL – Is a mechanism to create images in a frame buffer – Is an API to access that mechanism – Is well specified • OpenGL – Is not a window system – Is not a user interface – Is not a display mechanism – Does not even own the framebuffer • It is owned by the window system so it can be shared • But OpenGL defines its aAributes carefully White‐Square Code // Draw a white square against a black background #include <windows.h> #include <stdio.h> #define GLUT_DISABLE_ATEXIT_HACK // yuck! #include <GL/glut.h> void draw() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glOrtho(0, 4, 0, 4, -1, 1); glBegin(GL_POLYGON); glVertex2i(1, 1); OpenGL 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); GLUT glutCreateWindow("whitesquare"); glutDisplayFunc(draw); glutMainLoop(); } 1
11/4/09 OpenGL PorGon of White‐Square Code glClear(GL_COLOR_BUFFER_BIT); // black background glLoadIdentity(); 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 Red‐Book Example 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 State tables COLOR_CLEAR_VALUE 0,0,0,0 OpenGL 2.0 Spec, Table 6.21. Framebuffer Control 2
11/4/09 State tables OpenGL 2.0, Table 6.5. Current Values and Associated Data Snippet From the OpenGL 2.0 Spec • VerGces 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. OpenGL Philosophy • PlaXorm and window system independent • Rendering only • Aims to be real‐Gme • Takes advantage of graphics hardware where it exists • State system • Client‐server system • Immediate mode 3
11/4/09 The OpenGL Pipeline 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(); OpenGL Pipeline Application • Emphasis is on data types Vertex assembly • Diagram ignores Vertex operations – Pixel pipeline Primitive assembly – Texture memory – Display lists Primitive operations – … Rasterization Fragment operations • Display is not part of OpenGL Framebuffer Display 4
11/4/09 Vertex Assembly (data types) Application struct { Vertex assembly float x,y,z,w; float r,g,b,a; Vertex operations } vertex; Primitive assembly Primitive operations Rasterization Fragment operations Framebuffer Display Vertex Assembly (OpenGL) Application • Vertex assembly Vertex assembly – Force input to canonical format • Convert to internal representaGon Vertex operations – E.g., x, y to float • IniGalize unspecified values Primitive assembly – E.g., z = 0, w=1 • Insert current modal state Primitive operations – E.g., color to 0,0.5,0,1 – Or create using evaluators Rasterization • Error detecGon Fragment operations – INVALID_ENUM – INVALID_VALUE Framebuffer – INVALID_OPERATION • Especially between Begin and End Display Vertex Assembly (OpenGL) Application glColor3f(0, 0.5, 0); glVertex2i(11, 31); Vertex assembly glVertex2i(37, 71); glColor3f(0.5, 0, 0); // no effect Vertex operations Primitive assembly Primitive operations struct { float x,y,z,w; // 11, 31, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1 Rasterization } vertex; Fragment operations struct { float x,y,z,w; // 37, 71, 0, 1 Framebuffer float r,g,b,a; // 0, 0.5, 0, 1 } vertex; Display 5
11/4/09 Vertex OperaGons Application • OpenGL Vertex assembly – Transform coordinates Vertex operations • 4x4 matrix arithmeGc – Compute (vertex) lighGng Primitive assembly – Compute texture coordinates – … Primitive operations • In our case: Rasterization – Scale (arbitrary 100x100) coordinates to fit window Fragment operations – No lighGng, no texture coordinates Framebuffer Display PrimiGve Assembly (data types) Application struct { Vertex assembly float x,y,z,w; float r,g,b,a; Vertex operations } vertex; Primitive assembly struct { vertex v0,v1,v2; Primitive operations } triangle; or Rasterization struct { vertex v0,v1; Fragment operations } line; or Framebuffer struct { Display vertex v0; } point; PrimiGve Assembly Application OpenGL • Vertex assembly – Group verGces into primiGves: • points, Vertex operations • lines, or • triangles Primitive assembly – Decompose polygons to triangles – Duplicate verGces in strips or fans Primitive operations In our case: • Rasterization – Create two triangles from a strip: 1 3 Fragment operations glBegin(GL_TRIANGLE_STRIP); glColor(green); glVertex2i(…); // 0 Framebuffer glVertex2i(…); // 1 glColor(red); glVertex2i(…); // 2 glVertex2i(…); // 3 Display 2 glEnd(); 0 6
11/4/09 PrimiGve OperaGons Application • OpenGL Vertex assembly – Clip to the window boundaries Vertex operations • Actually to the frustum surfaces – Perform back‐face / front‐face ops Primitive assembly • Culling Primitive operations • Color assignment for 2‐side lighGng • In our case Rasterization – Nothing happens Fragment operations Framebuffer Display RasterizaGon (data types) Application struct { Vertex assembly float x,y,z,w; float r,g,b,a; Vertex operations } vertex; Primitive assembly struct { vertex v0,v1,v2 Primitive operations } triangle; Rasterization struct { short int x,y; Fragment operations float depth; float r,g,b,a; Framebuffer } fragment; Display RasterizaGon Application OpenGL – Determine which pixels are included in Vertex assembly the primiGve Vertex operations • Generate a fragment for each such pixel – Assign aAributes (e.g., color) to each Primitive assembly fragment Primitive operations In our case: Rasterization Fragment operations Framebuffer Display 7
11/4/09 Fragment OperaGons Application OpenGL – Texture mapping Vertex assembly – Fragment lighGng (OpenGL 2.0) Vertex operations – Fog Primitive assembly – Scissor test – Alpha test Primitive operations In our case, nothing happens: Rasterization Fragment operations Framebuffer Display Framebuffer (2‐D array of pixels) Application struct { float x,y,z,w; Vertex assembly float r,g,b,a; } vertex; Vertex operations struct { vertex v0,v1,v2 Primitive assembly } triangle; Primitive operations struct { short int x,y; Rasterization float depth; float r,g,b,a; Fragment operations } fragment; Framebuffer struct { int depth; Display byte r,g,b,a; } pixel; Fragment Framebuffer Ops Application OpenGL Vertex assembly – Color blending – Depth tesGng (aka z‐buffering) Vertex operations – Conversion to pixels Primitive assembly In our case, conversion to pixels: Primitive operations Rasterization Key idea: images are Fragment operations built in the framebuffer, not just Framebuffer placed there! Display 8
Recommend
More recommend