Upload
lora-may
View
246
Download
0
Tags:
Embed Size (px)
Citation preview
JOGL in MS WindowsJOGL in MS Windows
GDI – Graphics Device Interface (Windows-specific)
OpenGL Lib
JOGL Commands
GDI Processor
GraphicsHardware
JOGL Lib
Manipulating OpenGL StateManipulating OpenGL State
OpenGL state values can be specified within OpenGL state values can be specified within glBeginglBegin and and glEndglEnd pairs.pairs.
Appearance is controlled by current stateAppearance is controlled by current state
for each ( primitive to render ) {for each ( primitive to render ) {
update OpenGL stateupdate OpenGL state
render primitiverender primitive
}} Manipulating vertex attributes is mostManipulating vertex attributes is most
common way to manipulate state common way to manipulate state
glColor*() / glIndex*()glColor*() / glIndex*()
glNormal*()glNormal*()
glTexCoord*()glTexCoord*()
OpenGL state values can be specified within OpenGL state values can be specified within glBeginglBegin and and glEndglEnd pairs.pairs.
Appearance is controlled by current stateAppearance is controlled by current state
for each ( primitive to render ) {for each ( primitive to render ) {
update OpenGL stateupdate OpenGL state
render primitiverender primitive
}} Manipulating vertex attributes is mostManipulating vertex attributes is most
common way to manipulate state common way to manipulate state
glColor*() / glIndex*()glColor*() / glIndex*()
glNormal*()glNormal*()
glTexCoord*()glTexCoord*()
Controlling current stateControlling current state
Setting StateSetting StateglPointSize( glPointSize( sizesize ); );
glLineStipple( glLineStipple( repeatrepeat, , pattern pattern ););
glShadeModel( glShadeModel( GLGL__SMOOTHSMOOTH ); );
Enabling FeaturesEnabling FeaturesglEnable( glEnable( GLGL__LIGHTING LIGHTING ););
glDisable( glDisable( GL_TEXTURE_2D GL_TEXTURE_2D ););
Setting StateSetting StateglPointSize( glPointSize( sizesize ); );
glLineStipple( glLineStipple( repeatrepeat, , pattern pattern ););
glShadeModel( glShadeModel( GLGL__SMOOTHSMOOTH ); );
Enabling FeaturesEnabling FeaturesglEnable( glEnable( GLGL__LIGHTING LIGHTING ););
glDisable( glDisable( GL_TEXTURE_2D GL_TEXTURE_2D ););
Color and Gray ScaleColor and Gray Scale
Number of colors available depends on the Number of colors available depends on the amount of storage/pixel in frame buffer.amount of storage/pixel in frame buffer.– Store RGB color codes directly in bufferStore RGB color codes directly in buffer– Store color codes in a separate color tablesStore color codes in a separate color tables
Resolution of 1024 x 1024 pixelsResolution of 1024 x 1024 pixels– Full color, 24-bits/pixelFull color, 24-bits/pixel– 3MBytes of storage required for Frame Buffer3MBytes of storage required for Frame Buffer– Reference next slide for exampleReference next slide for example
Color TablesColor Tables
Gray ScaleGray Scale
Color System are commonColor System are common Use RGB functions to set shades of Use RGB functions to set shades of
graygray When an RGB color settings have When an RGB color settings have
equal amounts of R, G, and B equal amounts of R, G, and B Shade of GrayShade of Gray
Values close to Zero Values close to Zero dark gray, dark gray, values close to One values close to One light gray light gray
OpenGL Color FunctionsOpenGL Color Functions
Set color display mode to RGBSet color display mode to RGBglutInitDisplayMode( glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBGLUT_SINGLE | GLUT_RGB ); );
// The 1// The 1stst argument states we are using a single buffer argument states we are using a single buffer
// The 2// The 2ndnd argument sets the RGB mode (default) argument sets the RGB mode (default)
// To use a color table set GLUT_INDEX | GLUT_RGB// To use a color table set GLUT_INDEX | GLUT_RGB
glColor3f( glColor3f( 0.0, 1.0, 1.0 0.0, 1.0, 1.0 ););
glColor3fv( glColor3fv( colorArraycolorArray););
glColor3i( 0, 255, 255);glColor3i( 0, 255, 255);
OpenGL Color-Index ModeOpenGL Color-Index Mode
Color-index mode reference values in Color-index mode reference values in color tablecolor table– We set the color by specifying an index We set the color by specifying an index
into a color tableinto a color tableglIndex* ( colorIndex);glIndex* ( colorIndex);glIndexi (196);glIndexi (196);glutSetColor( index, R, G, B);glutSetColor( index, R, G, B);
– Color parameters R,G,B are assigned floating-Color parameters R,G,B are assigned floating-point valuespoint values
ExampleExample
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -0.5f, -4.0f); // A glVertex3f(-1.0f, -0.5f, -4.0f); // A
glColor3f(0.0f, 1.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -0.5f, -4.0f); // B glVertex3f( 1.0f, -0.5f, -4.0f); // B
glColor3f(0.0f, 0.0f, 1.0f); glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( 0.0f, 0.5f, -4.0f); // C glVertex3f( 0.0f, 0.5f, -4.0f); // C
glEnd(); glEnd();
Color BlendingColor Blending
Combine colors of overlapping Combine colors of overlapping objects.objects.– Blend the object colors with backgroundBlend the object colors with background
How?How?– Load 1Load 1stst object into frame buffer object into frame buffer– Combine the color of the 2Combine the color of the 2ndnd object with object with
the frame-buffer colorthe frame-buffer color
Color BlendingColor Blending
To enable blending in an applicationTo enable blending in an application– glEnable (GL_BLEND);glEnable (GL_BLEND);
To disable blendingTo disable blending– glDisable (GL_BLEND);glDisable (GL_BLEND);
If blending is not active, an objects If blending is not active, an objects color simply replaces the frame-color simply replaces the frame-buffer contents at the object’s buffer contents at the object’s locationlocation
Other Color FunctionsOther Color Functions
Select RGB color components for a Select RGB color components for a display windowdisplay window– glClearColor( R, G, B);glClearColor( R, G, B);
Clear color to the color buffers withClear color to the color buffers with– glClear ( GL_COLOR_BUFFER_BIT);glClear ( GL_COLOR_BUFFER_BIT);
In color-index mode, we use the In color-index mode, we use the following to set the display window following to set the display window colorcolor– glClearIndex (index);glClearIndex (index);
Point & Line AttributesPoint & Line Attributes
Set the size for an OpenGL PointSet the size for an OpenGL Point– glPointSize (size);glPointSize (size);
Size is a positive floating point value which Size is a positive floating point value which is rounded to an integer.is rounded to an integer.
Line width is setLine width is set– glLineWidth ( width);glLineWidth ( width);
Width is a positive floating point value Width is a positive floating point value rounded to the nearest positive integer.rounded to the nearest positive integer.
Line Style FunctionLine Style Function void void glLineStippleglLineStipple(GLint factor, GLushort pattern);(GLint factor, GLushort pattern); Sets the current stippling pattern for lines. The pattern argument Sets the current stippling pattern for lines. The pattern argument
is a 16-bit series of 0s and 1s, and it's repeated as necessary to is a 16-bit series of 0s and 1s, and it's repeated as necessary to stipple a given line. A 1 indicates that drawing occurs, and 0 that stipple a given line. A 1 indicates that drawing occurs, and 0 that it does not, on a pixel-by-pixel basis, beginning with the low-order it does not, on a pixel-by-pixel basis, beginning with the low-order bit of the pattern. The pattern can be stretched out by using bit of the pattern. The pattern can be stretched out by using factor, which multiplies each subseries of consecutive 1s and 0s. factor, which multiplies each subseries of consecutive 1s and 0s. Thus, if three consecutive 1s appear in the pattern, they're Thus, if three consecutive 1s appear in the pattern, they're stretched to six if factor is 2. factor is clamped to lie between 1 stretched to six if factor is 2. factor is clamped to lie between 1 and 255. Line stippling must be enabled by passing and 255. Line stippling must be enabled by passing GL_LINE_STIPPLE to glEnable(); it's disabled by passing the same GL_LINE_STIPPLE to glEnable(); it's disabled by passing the same argument to glDisable(). argument to glDisable().
Line Stipple Line Stipple ExampleExample
glLineStippleglLineStipple(1, 0x3F07); (1, 0x3F07); glEnable(GL_LINE_STIPPLE); glEnable(GL_LINE_STIPPLE);
What is shading?What is shading?
A point has only one vertex, and whatever color you specify for that vertex A point has only one vertex, and whatever color you specify for that vertex is the resulting point.is the resulting point.
A line, however, has two vertices and each can be set to a different color. A line, however, has two vertices and each can be set to a different color. The color of the line depends on the shading model. Shading is simply The color of the line depends on the shading model. Shading is simply defined as the smooth transition from one color to the next.defined as the smooth transition from one color to the next.
Shading MathematicallyShading Mathematically You can do shading mathematically by finding the You can do shading mathematically by finding the
equation of the line connecting two points in the equation of the line connecting two points in the three-dimensional RGB colorspace. Then, simply three-dimensional RGB colorspace. Then, simply loop through from one end of the line to the loop through from one end of the line to the other, retrieving coordinates along the way to other, retrieving coordinates along the way to provide the color of each pixel on the screen.provide the color of each pixel on the screen.
This shading exercise becomes slightly more This shading exercise becomes slightly more complex for polygons. Luckily, OpenGL does all complex for polygons. Luckily, OpenGL does all of this work for you!of this work for you!
Polygon Shading Methods Provided by Polygon Shading Methods Provided by
OpenGLOpenGL
Flat ShadingFlat Shading Interpolative Shading (Gouraud)Interpolative Shading (Gouraud) Phong ShadingPhong Shading
Flat ShadingFlat Shading Flat shading means that no shading Flat shading means that no shading
calculations are performed on the interior calculations are performed on the interior of primitives. Generally, with flat shading, of primitives. Generally, with flat shading, the color of the primitive’s interior is the the color of the primitive’s interior is the color that was specified for the last vertex. color that was specified for the last vertex. The only exception is for a GL_POLYGON The only exception is for a GL_POLYGON primitive, in which case the color is that of primitive, in which case the color is that of the first vertex.the first vertex.
glShadeModel (GL_FLAT);glShadeModel (GL_FLAT);
Example of Flat ShadingExample of Flat ShadingglShadeModel (GL_FLAT);glShadeModel (GL_FLAT);
glBegin(GL_TRIANGLES);glBegin(GL_TRIANGLES);
glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0);glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0);glVertex3f(0.0f, 200, 0f, 0.0f); // Red Apex glVertex3f(0.0f, 200, 0f, 0.0f); // Red Apex
glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0);glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0);glVertex3f(200.0f, -70.0f, 0.0f); // Green right glVertex3f(200.0f, -70.0f, 0.0f); // Green right
bottom cornerbottom corner
glColor3ub((Glubyte)0, (Glubyte)0, (Glubyte)255);glColor3ub((Glubyte)0, (Glubyte)0, (Glubyte)255);glVertex3f(-200.0f, -70.0f, 0.0f); // Blue left glVertex3f(-200.0f, -70.0f, 0.0f); // Blue left
bottom cornerbottom corner
glEnd();glEnd();
Benefits of Flat ShadingBenefits of Flat Shading Flat shading uses very little CPU Flat shading uses very little CPU
resources and leaves more of the resources and leaves more of the graphical pipeline free to handle graphical pipeline free to handle other tasks.other tasks.
Problems with Flat ShadingProblems with Flat Shading If our polygon model has been If our polygon model has been
designed to model a smooth designed to model a smooth surface, flat shading will almost surface, flat shading will almost always be disappointing, because always be disappointing, because we can see even small differences we can see even small differences in shading between adjacent in shading between adjacent polygons.polygons.
According to Edward Angel, “this According to Edward Angel, “this phenomenon is a consequence of phenomenon is a consequence of how the cones in the eye are how the cones in the eye are connected to the optic nerve” connected to the optic nerve” (259). Simply brilliant.(259). Simply brilliant.
Interpolative and Gouraud ShadingInterpolative and Gouraud Shading
Gouraud shading is a method for linearly Gouraud shading is a method for linearly interpolating a color or shade across a polygon. It interpolating a color or shade across a polygon. It was invented by Gouraud in 1971. It is a very was invented by Gouraud in 1971. It is a very simple and effective method of adding a curved simple and effective method of adding a curved feel to a polygon that would otherwise appear feel to a polygon that would otherwise appear flat.flat.
Unlike a flat shaded polygon, you can specify a Unlike a flat shaded polygon, you can specify a different shade for each vertex of a polygon, the different shade for each vertex of a polygon, the rendering engine then smoothly interpolates the rendering engine then smoothly interpolates the shade across the surface.shade across the surface.
glShadeModel(GL_SMOOTH);glShadeModel(GL_SMOOTH);
Example of Gouraud ShadingExample of Gouraud ShadingglShadeModel (GL_SMOOTH);glShadeModel (GL_SMOOTH);
glBegin(GL_TRIANGLES);glBegin(GL_TRIANGLES);
glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0);glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0);glVertex3f(-200.0f, -70.0f, 0.0f); // Blue topglVertex3f(-200.0f, -70.0f, 0.0f); // Blue top
glColor3ub((Glubyte)0, (Glubyte), (Glubyte)255);glColor3ub((Glubyte)0, (Glubyte), (Glubyte)255);glVertex3f(200.0f, -70.0f, 0.0f); // Green left bottom glVertex3f(200.0f, -70.0f, 0.0f); // Green left bottom
cornercorner
glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0);glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0);glVertex3f(0.0f, 200, 0f, 0.0f); // Red Bottom RightglVertex3f(0.0f, 200, 0f, 0.0f); // Red Bottom Right
glEnd();glEnd();
Benefits of Gouraud ShadingBenefits of Gouraud Shading
Polygons, more complex than triangles, Polygons, more complex than triangles, can also have different colors specified for can also have different colors specified for each vertex. In these instances, the each vertex. In these instances, the underlying logic for shading can become underlying logic for shading can become more intricate. Fortunately, you never more intricate. Fortunately, you never have to worry about it with OpenGL. No, have to worry about it with OpenGL. No, matter how complex your polygon, matter how complex your polygon, OpenGL successfully shades the interior OpenGL successfully shades the interior points between each vertex.points between each vertex.
Problems with Gouraud Problems with Gouraud ShadingShading
Even the smoothness introduced by Gouraud shading may Even the smoothness introduced by Gouraud shading may not prevent the appearance of the shading differences not prevent the appearance of the shading differences
between adjacent polygons.between adjacent polygons.
Gouraud shading is more CPU intensive and can become a Gouraud shading is more CPU intensive and can become a problem when rendering real time environments with many problem when rendering real time environments with many polygons.polygons.
T-Junctions with adjoining polygons can sometimes result in T-Junctions with adjoining polygons can sometimes result in visual anomalies. In general, T-Junctions should be visual anomalies. In general, T-Junctions should be avoided.avoided.