1 lecture 5 rendering 3d graphical primitives. 2 3d rendering example:
Post on 22-Dec-2015
229 Views
Preview:
TRANSCRIPT
5
Rendering
How to render in different modes to get either a solid or an outlined object?
OpenGL allows both sides of an object to be rendered in a different mode.
Example: render a square solid on the front, and outlined on the back.
A question appears: “How does OpenGL know which side of an object is the front?”
Rule: OpenGL assumes that anything drawn in counter-clock-wise direction is the front side by default.
6
Example: program fragment drawing a red square with front solid and back outlined
glPolygonMode( GL_FRONT, GL_FILL ); // Front filledglPolygonMode( GL_BACK, GL_LINE ); // Back OutlinedglColor3f( 1.0, 0.0, 0.0 );glBegin( GL_QUAD ); // Square drawn counter clockwiseglVertex2d( -1.0, -1.0 );glVertex2d( 1.0, -1.0 );glVertex2d( 1.0, 1.0 );glVertex2d( -1.0, 1.0 );
glEnd();glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); // return the drawing mode
7
Example: program fragment drawing a red square with front solid and back outlined
Notice that the points are ordered from the bottom left to the top left in a counter-clock-wise direction. This means that we will never see the outline back unless we rotate the camera.
8
GLFrontFace(): changes the default rule
A call to glFrontFace( enum Mode ) can change the default counter-clock-wise front facing polygons to back facing.
The accepted Modes are GL_CCW for counter-clock-wise, and GL_CW for clock-wise front facing polygons.
This function is rarely used, but may be useful for porting applications from different graphic environments.
For example, Microsoft’s DirectX uses clock-wise as front facing. In this case, making a call to this function may be easier than reordering the position of the vertices.
9
void glCullFace (enum Mode);
Culling is a term in three-dimensional graphics that means not drawing.
When an object is drawn to the screen, all vertices are mapped to where they would be on the screen even if they are not going to be seen.
If it is not necessary to draw a face of an object, calling the glCullFace function will save rendering if you know that a face will not be seen.
The possible Modes correlate to the faces of the object. They are GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK. In order to use this function, you must enable the culling with a call to
glEnable( GL_CULL_FACE ).
10
Hidden Surface Removal
How does OpenGL know what objects are in front of other objects?
Assume, two objects are rendered. Object 1 is behind object 2 and object 2 is partially obscuring object 1. The display function would draw the object 1 first and then object 2.
What if the camera rotated 180 degrees behind object 1? Now the tables are turned, and object 1 is in front with the respects to the camera. If the objects are drawn in the same order, object 2 will always look like it is in front of object 1 no matter what the camera location is.
11
Hidden Surface Removal
OpenGL uses Z-buffering to solve this problem of hidden surface removal.
Z-Buffering is a method in which every pixel’s z-coordinate is compared to every other pixel’s z-coordinate on a line from the camera.
Only the pixel that is not covered by any other pixel is actually drawn to the screen.
12
Hidden Surface Removal
To enable this powerful feature:
First, in your setup function
call to glClear should also contain GL_DEPTH_BUFFER_BIT along with any
other buffer bit you wish to clear.
glClear( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
Second, your application should include GLUT_DEPTH in the main
function’s glutInitDisplayMode. This would look something like this:
glutInitDisplayMode(GLUT_DEPTH | … );
Third, a call to glEnable( GL_DEPTH_TEST ) to tell the API to use depth
testing.
13
3D GLUT predefined shapes
void glutSolidSphere( double radius, int slices, int stacks );
void glutWireSphere ( double radius, int slices, int stacks );
Radius is the radius of the sphere. Slices are the number of subdivisions around the z-axis. Stacks are the number of subdivisions along the z-axis.
16
3D GLUT predefined shapes
void glutSolidCube( double size ); void glutWireCube ( double size );
Size is the length of each side
17
3D GLUT predefined shapes
void glutSolidCone(double base,double height,int slices, int stacks );
void glutWireCone ( double base,double height,int slices,int stacks );
Radius is the radius of the cone. Slices are the number of subdivisions around the z-axis. Stacks are the number of subdivisions along the z-axis.
18
3D GLUT predefined shapes
void glutSolidTorus( double InnerRadius,
double OuterRadius,
int sides,
int rings ); void glutWireTorus( double InnerRadius,
double OuterRadius,
int sides,
int rings ); InnerRadius is the inner radius of the torus. OuterRadius is the outer radius of the torus. Sides is the number of sides for each radial section. Rings are the number of radial divisions for the torus.
19
3D GLUT predefined shapes
void glutSolidTeapot( double size ); void glutWireTeapot ( double size );
Size is the relative size of the teapot.
20
3D GLUT predefined shapes
The prototype for four more shapes are shown below. void glutSolidIcosahedron(); void glutWireIcosahedron (); void glutSolidOctahedron(); void glutWireOctahedron (); void glutSolidTetrahedron(); void glutWireTetrahedron (); void glutSolidDodecahedron(); void glutWireDodecahedron ();
21
3D GLUT predefined shapes
By changing the Display function of the triangle program, we can get some good three-dimensional graphics.
Example: a program fragment that draws a yellow teapot to the screen:
top related