Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng

Similar presentations


Presentation on theme: "Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng"— Presentation transcript:

1 Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com

2 2 Objectives Introduce the OpenGL shading functions Discuss polygonal shading ­Flat ­Smooth ­Gouraud

3 3 Steps in OpenGL shading 1.Enable shading and select model 2.Specify normals 3.Specify material properties 4.Specify lights

4 4 Normals In OpenGL the normal vector is part of the state Set by glNormal*() ­glNormal3f(x, y, z); ­glNormal3fv(p); Usually we want to set the normal to have unit length so cosine calculations are correct ­Length can be affected by transformations ­Note that scaling does not preserved length ­glEnable(GL_NORMALIZE) allows for auto- normalization at a performance penalty

5 5 Normal for Triangle p0p0 p1p1 p2p2 n Plane n · (p - p 0 ) = 0 n = (p 2 - p 0 ) × (p 1 - p 0 ) normalize n  n/ |n| p Note that right-hand rule determines outward face

6 6 Enabling Shading Shading calculations are enabled by ­glEnable(GL_LIGHTING) ­Once lighting is enabled, glColor() ignored ­glEnable(GL_COLOR_MATERIAL); Must enable each light source individually ­glEnable(GL_LIGHTi) i=0,1….. Can choose light model parameters ­glLightModeli(parameter, GL_TRUE) GL_LIGHT_MODEL_LOCAL_VIEWER do not use simplifying distant viewer assumption in calculation (infinite viewer) GL_LIGHT_MODEL_TWO_SIDED shades both sides of polygons independently

7 7 Defining a Point Light Source For each light source, we can set an RGBA for the diffuse, specular, and ambient components, and for the position GLfloat diffuse0[]={1.0, 0.0, 0.0, 1.0}; GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0}; GLfloat specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos); glLightv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

8 8 Distance and Direction The source colors are specified in RGBA The position is given in homogeneous coordinates ­If w = 1.0, we are specifying a finite location ­If w = 0.0, we are specifying a parallel source with the given direction vector The coefficients in the distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms ). Change by a = 0.80; glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a); 1/(a+bd L +cd L 2 )

9 9 Spotlights Use glLightv to set ­Direction GL_SPOT_DIRECTION ­Cutoff GL_SPOT_CUTOFF ­Attenuation GL_SPOT_EXPONENT Proportional to cos     

10 10 Global Ambient Light Ambient light depends on color of light sources ­A red light in a white room will cause a red ambient term that disappears when the light is turned off OpenGL also allows a global ambient term that is often helpful for testing ­glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

11 11 glLightfv(light, pname, param)

12 12 LIGHT_TYPE_SPOT(Demo Ogl-lighting) case LIGHT_TYPE_SPOT:{ GLfloat diffuse_light2[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat position_light2[] = { 2.0f*x, 2.0f*y, 2.0f*z, 1.0f }; GLfloat spotDirection_light2[] = { x, y, z }; GLfloat constantAttenuation_light2[] = { 1.0f }; glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse_light2 ); glLightfv( GL_LIGHT2, GL_POSITION, position_light2 ); glLightfv( GL_LIGHT2, GL_SPOT_DIRECTION, spotDirection_light2 ); glLightfv( GL_LIGHT2, GL_CONSTANT_ATTENUATION, constantAttenuation_light2 ); glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, 45.0f ); glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, 25.0f ); } case LIGHT_TYPE_SPOT:{ GLfloat diffuse_light2[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat position_light2[] = { 2.0f*x, 2.0f*y, 2.0f*z, 1.0f }; GLfloat spotDirection_light2[] = { x, y, z }; GLfloat constantAttenuation_light2[] = { 1.0f }; glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse_light2 ); glLightfv( GL_LIGHT2, GL_POSITION, position_light2 ); glLightfv( GL_LIGHT2, GL_SPOT_DIRECTION, spotDirection_light2 ); glLightfv( GL_LIGHT2, GL_CONSTANT_ATTENUATION, constantAttenuation_light2 ); glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, 45.0f ); glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, 25.0f ); }

13 13 Moving Light Sources (Demo) Light sources are geometric objects whose positions or directions are affected by the model-view matrix Depending on where we place the position (direction) setting function, we can ­Move the light source(s) with the object(s) ­Fix the object(s) and move the light source(s) ­Fix the light source(s) and move the object(s) ­Move the light source(s) and object(s) independently

14 14 void display(GLint spin){ ­GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; ­glPushMatrix(); ­glTranslatef(0.0, 0.0, -5.0); ­glPushMatrix(); ­glRotated((GLdouble) spin, 1.0, 0.0, 0.0); ­glLightfv(GL_LIGHT0, GL_POSITION, light_position); ­glPopMatrix(); ­auxSolidTorus(0.275, 0.85); ­glPopMatrix(); ­glFlush(); } // 旋转光源 void display(GLint spin){ ­GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; ­glPushMatrix(); ­glTranslatef(0.0, 0.0, -5.0); ­glPushMatrix(); ­glRotated((GLdouble) spin, 1.0, 0.0, 0.0); ­glLightfv(GL_LIGHT0, GL_POSITION, light_position); ­glPopMatrix(); ­auxSolidTorus(0.275, 0.85); ­glPopMatrix(); ­glFlush(); } // 旋转光源

15 15 Material Properties Material properties are also part of the OpenGL state and match the terms in the modified Phong model Set by glMaterialv() GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shine = 100.0 glMaterialf(GL_FRONT, GL_AMBIENT, ambient); glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialf(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shine);

16 16 Front and Back Faces The default is shade only front faces which works correctly for convex objects If we set two sided lighting, OpenGL will shade both sides of a surface Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf back faces not visibleback faces visible

17 17 Emissive Term We can simulate a light source in OpenGL by giving a material an emissive component This component is unaffected by any sources or transformations GLfloat emission[] = 0.0, 0.3, 0.3, 1.0); glMaterialf(GL_FRONT, GL_EMISSION, emission);

18 18 Transparency Material properties are specified as RGBA values The A value can be used to make the surface translucent The default is that all surfaces are opaque regardless of A Later we will enable blending and use this feature

19 19 Efficiency Because material properties are part of the state, if we change materials for many surfaces, we can affect performance We can make the code cleaner by defining a material structure and setting all materials during initialization We can then select a material by a pointer typedef struct materialStruct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness; } MaterialStruct;

20 20 IMPLEMENTATION I

21 21 Objectives Introduce basic implementation strategies Clipping Scan conversion

22 22 Overview At end of the geometric pipeline, vertices have been assembled into primitives Must clip out primitives that are outside the view frustum ­Algorithms based on representing primitives by lists of vertices Must find which pixels can be affected by each primitive ­Fragment generation ­Rasterization or scan conversion

23 23 Required Tasks Clipping Rasterization or scan conversion We can get the pixels based on vertices Some tasks deferred until fragement processing ­Hidden surface removal ­Antialiasing Projection Fragments Clipping Shading Surface hidden Texture Transparency

24 24 Rasterization Meta Algorithms Consider two approaches to rendering a scene with opaque objects For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel ­Ray tracing paradigm For every object, determine which pixels it covers and shade these pixels ­Pipeline approach ­Must keep track of depths

25 25 Clipping 2D against clipping window 3D against clipping volume Easy for line segments polygons Hard for curves and text ­Convert to lines and polygons first

26 26 Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window ­Inefficient: one division per intersection

27 27 Cohen-Sutherland Algorithm Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window x = x max x = x min y = y max y = y min

28 28 The Cases Case 1: both endpoints of line segment inside all four lines ­Draw (accept) line segment as is Case 2: both endpoints outside all lines and on same side of a line ­Discard (reject) the line segment x = x max x = x min y = y max y = y min

29 29 The Cases Case 3: One endpoint inside, one outside ­Must do at least one intersection Case 4: Both outside ­May have part inside ­Must do at least one intersection x = x max x = x min y = y max

30 30 Defining Outcodes For each endpoint, define an outcode Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions (8 times for two endpoint) b0b1b2b3b0b1b2b3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise

31 Outcodes application Case I: o 1, o 2 AB: o 1 = o 2 = 0 31

32 Outcodes application Case II: o 1, o 2 CD: o 1 ≠ 0, o 2 = 0 32

33 Outcodes application Case III: o 1, o 2 EF: o 1 & o 2 ≠ 0 33

34 Outcodes application Case IV: o 1, o 2 GH and IJ: o 1 & o 2 = 0 Compute the intersection points; Re-compute the code of intersection points. 34

35 35 The Cohen-Sutherland Line- Clipping Algorithm (2) To perform trivial and reject tests, we extend the edges of the clip rectangle to divide the plane into nine regions, and encode each region a 4-bit code. If both 4-bit codes of the endpoint are zero, the line is Trivially Accepted If both endpoints lie in the outside halfplane of a particular edge, the line is Trivially rejected, and the logical AND of the endpoints is not zero.

36 36 The Cohen-Sutherland Line- Clipping Algorithm(3) Compute the codes of both endpoints and check for trivial acceptance and rejection. If neither test is successful, find an endpoint that lies outside, and then test the code to find the edge that is crossed and to determine the corresponding intersection point. Clip off the line segment from the outside endpoint to the intersection point by replacing the outside endpoint with the intersection point, and compute the code of this new endpoint to prepare for the next iteration.

37 37 Efficiency In many applications, the clipping window is small relative to the size of the entire data base ­Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step

38 38 Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes

39 39 Parametric Lines and Intersections For L 1 x=x0 l1 + t(x1 l1 – x0 l1 ) y=y0 l1 + t(y1 l1 – y0 l1 ) For L 2 x=x0 l2 + t(x1 l2 – x0 l2 ) y=y0 l2 + t(y1 l2 – y0 l2 ) The Intersection Point: x0 l1 + t 1 (x1 l1 – x0 l1 ) = x0 l2 + t 2 (x1 l2 – x0 l2 ) y0 l1 + t 1 (y1 l1 – y0 l1 ) = y0 l2 + t 2 (y1 l2 – y0 l2 )

40 40 Clipping Lines Clipping Lines by Solving Simultaneous Equations ­Two sets of simultaneous equations of this parametric form can be solved for parameters t edge for the edge and t line for the line segment. ­The value of t edge and t line can be checked to see whether both lie in [0,1]; if they do, the intersection point is a true clip- rectangle intersection. ­Shortcoming: considerable calculation and testing

41 41 A Parametric Line-Clipping Algorithm(1) –Liang-Barsky 1.For each clip edge (to upright rectangle), we can easily calculate a value t corresponding to the intersection point between the edge and the line segment. 2.We get four values of in t total. 3.Intersections can be classified as PE or PL ( How ?) PE: Point Entering the clipping rectangle PL: Point Leaving the clipping rectangle

42 42 A Parametric Line-Clipping Algorithm(2) –Liang-Barsky 3.on the basis of the angle between P 0 P 1 and the normal of the edge. ­ 0 ) ­>90, PE ( N i. D <0 ) 4.Divide t into two groups({t 0 ’, t 0 ” } {t 1 ’, t 1 ” }) according to its corresponding intersection belongs to PE or PL. 5.We get ­t 0 = max (t 0 ’, t 0 ”,0) ­t 1 = min(t 1 ’, t 1 ”,1) If t 0 < t 1, the line segment between [t 0, t 1 ] is visible, otherwise, the whole line is invisible

43 43 A Parametric Line-Clipping Algorithm(3) –Liang-Barsky precalculate N i for each edge for (each line segment to be clipped){ ­If (P 0 = P 1 ) Line is degenerate so clip as a point; ­else{ t e =0; t l =1; for (each candidate intersection with a clip edge){ –if (N i * D !=0) { // ignore edges parallel to line calculate t; use sign of N i * D to categorize as PE or PL; if (PE) t e =max(t e. t); if (PL) t l =min(t l. t); –} } if (t e > t l ) return nil; else return P(t e ) and P(t l ) as true clip intersections; ­} }

44 44 Advantages Can accept/reject as easily as with Cohen-Sutherland Using values of , we do not have to use algorithm recursively as with C-S Extends to 3D

45 45 Clipping and Normalization General clipping in 3D requires intersection of line segments against arbitrary plane Example: oblique view

46 46 Plane-Line Intersections

47 47 Normalized Form before normalizationafter normalization Normalization is part of viewing (pre clipping) but after normalization, we clip against sides of right parallelepiped Typical intersection calculation now requires only a floating point subtraction, e.g. is x > x max top view


Download ppt "Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng"

Similar presentations


Ads by Google