240 likes | 766 Views
Lecture 5 Rendering 3D graphical primitives 3D Rendering Example: 3D Rendering Pipeline 3D Rendering Pipeline 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.
E N D
Lecture 5 Rendering 3D graphical primitives
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.
Example: program fragment drawing a red square with front solid and back outlined glPolygonMode( GL_FRONT, GL_FILL ); // Front filled glPolygonMode( GL_BACK, GL_LINE ); // Back Outlined glColor3f( 1.0, 0.0, 0.0 ); glBegin( GL_QUAD ); // Square drawn counter clockwise glVertex2d( -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
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.
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.
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 ).
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.
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.
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.
3D GLUT predefined shapes • void glutSolidSphere( double radius, int slices, int stacks ); • void glutWireSphere ( double radius, int slices, int stacks ); • Radiusis 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.
3D GLUT predefined shapes • void glutSolidCube( double size ); • void glutWireCube ( double size ); • Size is the length of each side
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. • Slicesare the number of subdivisions around the z-axis. • Stacks are the number of subdivisions along the z-axis.
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 thetorus.
3D GLUT predefined shapes • void glutSolidTeapot( double size ); • void glutWireTeapot ( double size ); • Sizeis the relative size of the teapot.
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 ();
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:
3D GLUT predefined shapes void Display() { // Clear pixels in buffer glClear( GL_COLOR_BUFFER_BIT ); glColor3f( 1.0, 1.0, 0.0 ); glutSolidTeapot( 0.5 ); glFlush(); // Draw to the screen }