300 likes | 557 Views
JOGL Commands. Graphics Hardware. JOGL Lib. OpenGL Lib. GDI Processor. JOGL in MS Windows. GDI – Graphics Device Interface (Windows-specific). Manipulating OpenGL State. OpenGL state values can be specified within glBegin and glEnd pairs. Appearance is controlled by current state
E N D
JOGL Commands Graphics Hardware JOGL Lib OpenGL Lib GDI Processor JOGL in MS Windows GDI – Graphics Device Interface (Windows-specific)
Manipulating OpenGL State • OpenGL state values can be specified within glBegin and glEndpairs. • Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } • Manipulating vertex attributes is most common way to manipulate state glColor*() / glIndex*() glNormal*() glTexCoord*()
Controlling current state • Setting State glPointSize( size ); glLineStipple( repeat, pattern ); glShadeModel( GL_SMOOTH ); • Enabling Features glEnable( GL_LIGHTING ); glDisable( GL_TEXTURE_2D );
Color and Gray Scale • Number of colors available depends on the amount of storage/pixel in frame buffer. • Store RGB color codes directly in buffer • Store color codes in a separate color tables • Resolution of 1024 x 1024 pixels • Full color, 24-bits/pixel • 3MBytes of storage required for Frame Buffer • Reference next slide for example
Gray Scale • Color System are common • Use RGB functions to set shades of gray • When an RGB color settings have equal amounts of R, G, and B Shade of Gray • Values close to Zero dark gray, values close to One light gray
OpenGL Color Functions • Set color display mode to RGB glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); // The 1st argument states we are using a single buffer // The 2nd argument sets the RGB mode (default) // To use a color table set GLUT_INDEX | GLUT_RGB glColor3f( 0.0, 1.0, 1.0 ); glColor3fv( colorArray); glColor3i( 0, 255, 255);
OpenGL Color-Index Mode • Color-index mode reference values in color table • We set the color by specifying an index into a color table • glIndex* ( colorIndex); • glIndexi (196); • glutSetColor( index, R, G, B); • Color parameters R,G,B are assigned floating-point values
Example glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -0.5f, -4.0f); // A glColor3f(0.0f, 1.0f, 0.0f); glVertex3f( 1.0f, -0.5f, -4.0f); // B glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 0.0f, 0.5f, -4.0f); // C glEnd();
Color Blending • Combine colors of overlapping objects. • Blend the object colors with background • How? • Load 1st object into frame buffer • Combine the color of the 2nd object with the frame-buffer color
Color Blending • To enable blending in an application • glEnable (GL_BLEND); • To disable blending • glDisable (GL_BLEND); • If blending is not active, an objects color simply replaces the frame-buffer contents at the object’s location
Other Color Functions • Select RGB color components for a display window • glClearColor( R, G, B); • Clear color to the color buffers with • glClear ( GL_COLOR_BUFFER_BIT); • In color-index mode, we use the following to set the display window color • glClearIndex (index);
Point & Line Attributes • Set the size for an OpenGL Point • glPointSize (size); • Size is a positive floating point value which is rounded to an integer. • Line width is set • glLineWidth ( width); • Width is a positive floating point value rounded to the nearest positive integer.
Line Style Function • void glLineStipple(GLint factor, GLushort pattern); • 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 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 bit of the pattern. The pattern can be stretched out by using factor, which multiplies each subseries of consecutive 1s and 0s. 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 and 255. Line stippling must be enabled by passing GL_LINE_STIPPLE to glEnable(); it's disabled by passing the same argument to glDisable(). • Line Stipple Example • glLineStipple(1, 0x3F07); • glEnable(GL_LINE_STIPPLE);
What is shading? • A point has only one vertex, and whatever color you specify for that vertex is the resulting point. • 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 defined as the smooth transition from one color to the next.
Shading Mathematically • You can do shading mathematically by finding the equation of the line connecting two points in the three-dimensional RGB colorspace. Then, simply loop through from one end of the line to the other, retrieving coordinates along the way to provide the color of each pixel on the screen. • This shading exercise becomes slightly more complex for polygons. Luckily, OpenGL does all of this work for you!
Polygon Shading Methods Provided by OpenGL • Flat Shading • Interpolative Shading (Gouraud) • Phong Shading
Flat Shading • Flat shading means that no shading calculations are performed on the interior of primitives. Generally, with flat shading, the color of the primitive’s interior is the color that was specified for the last vertex. The only exception is for a GL_POLYGON primitive, in which case the color is that of the first vertex.
Example of Flat Shading glShadeModel (GL_FLAT); glBegin(GL_TRIANGLES); glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0); glVertex3f(0.0f, 200, 0f, 0.0f); // Red Apex glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0); glVertex3f(200.0f, -70.0f, 0.0f); // Green right bottom corner glColor3ub((Glubyte)0, (Glubyte)0, (Glubyte)255); glVertex3f(-200.0f, -70.0f, 0.0f); // Blue left bottom corner glEnd();
Benefits of Flat Shading • Flat shading uses very little CPU resources and leaves more of the graphical pipeline free to handle other tasks.
If our polygon model has been designed to model a smooth surface, flat shading will almost always be disappointing, because we can see even small differences in shading between adjacent polygons. According to Edward Angel, “this phenomenon is a consequence of how the cones in the eye are connected to the optic nerve” (259). Simply brilliant. Problems with Flat Shading
Interpolative and Gouraud Shading • Gouraud shading is a method for linearly interpolating a color or shade across a polygon. It was invented by Gouraud in 1971. It is a very simple and effective method of adding a curved feel to a polygon that would otherwise appear flat. • Unlike a flat shaded polygon, you can specify a different shade for each vertex of a polygon, the rendering engine then smoothly interpolates the shade across the surface.
Example of Gouraud Shading glShadeModel (GL_SMOOTH); glBegin(GL_TRIANGLES); glColor3ub((Glubyte)255, (Glubyte)0, Glubyte)0); glVertex3f(-200.0f, -70.0f, 0.0f); // Blue top glColor3ub((Glubyte)0, (Glubyte), (Glubyte)255); glVertex3f(200.0f, -70.0f, 0.0f); // Green left bottom corner glColor3ub((Glubyte)0, (Glubyte)255, (Glubyte)0); glVertex3f(0.0f, 200, 0f, 0.0f); // Red Bottom Right glEnd();
Benefits of Gouraud Shading • Polygons, more complex than triangles, can also have different colors specified for each vertex. In these instances, the underlying logic for shading can become more intricate. Fortunately, you never have to worry about it with OpenGL. No, matter how complex your polygon, OpenGL successfully shades the interior points between each vertex.
Problems with Gouraud Shading • Even the smoothness introduced by Gouraud shading may not prevent the appearance of the shading differences between adjacent polygons. • Gouraud shading is more CPU intensive and can become a problem when rendering real time environments with many polygons. • T-Junctions with adjoining polygons can sometimes result in visual anomalies. In general, T-Junctions should be avoided.