Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Similar presentations


Presentation on theme: "Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17."— Presentation transcript:

1 Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17

2 vertexvertex Modelview Matrix Projection Matrix Perspective Division Viewport Transform Modelview Projection object eye clip normalized device window other calculations here material  color shade model (flat) polygon rendering mode polygon culling clipping Transformation Pipeline

3 Matrix Operations Specify Current Matrix Stack glMatrixMode( GL_MODELVIEW or GL_PROJECTION ) Other Matrix or Stack OperationsglLoadIdentity()glPushMatrix()glPopMatrix() Viewport usually same as window size viewport aspect ratio should be same as projection transformation or resulting image may be distorted glViewport( x, y, width, height )

4 Projection Transformation Shape of viewing frustum Perspective projection gluPerspective( fovy, aspect, zNear, zFar ) glFrustum ( left, right, bottom, top, zNear, zFar ) Orthographic parallel projection glOrtho( left, right, bottom, top, zNear, zFar ) gluOrtho2D( left, right, bottom, top ) calls glOrtho with z values near zero

5 Applying Projection Transformations Typical use (orthographic projection) glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( left, right, bottom, top, zNear, zFar );

6 Viewing Transformations Position the camera/eye in the scene place the tripod down; aim camera To “fly through” a scene change viewing transformation and redraw scene gluLookAt( eye x, eye y, eye z, aim x, aim y, aim z, up x, up y, up z ) up vector determines unique orientation tripod

7 Modeling Transformations Move object glTranslate{fd}( x, y, z ) Rotate object around arbitrary axis glRotate{fd}( angle, x, y, z ) angle is in degrees Dilate (stretch or shrink) or mirror object glScale{fd}( x, y, z )

8 Connection: Viewing and Modeling Moving camera is equivalent to moving every object in the world towards a stationary camera Viewing transformations are equivalent to several modeling transformations gluLookAt() has its own command can make your own polar view or pilot view

9 Projection is left handed Projection transformations ( gluPerspective, glOrtho ) are left handed think of zNear and zFar as distance from view point Everything else is right handed, including the vertexes to be rendered x x y y z+ left handedright handed

10 Building geometry using coordinates Construct a pyramid by connecting the dots to form triangles

11 Winding Rule Using a right-handed coordinate system 3D coordinates are given in a right-handed coordinate system X = left-to-right. Y = bottom-to-top. Z = back-to-front. Distances are conventionally in meters 扶

12 Right Hand Rule

13 Using coordinate order Polygons have a front and back: By default, only the front side of a polygon is rendered. A polygon's winding order determines which side is the front. Most polygons only need one side rendered. You can turn on double-sided rendering, at a performance cost.

14 Using coordinate order Use the right-hand rule: Curl your right-hand fingers around the polygon perimeter in the order vertices are given (counter- clockwise) Your thumb sticks out the front of the polygon

15 Back-face elimination... culling Two Sides to a Polygon: Most polygonal meshes have a well-defined inside and outside. We use the right-hand rule (CCW counterclockwise winding) and specify vertices in a counterclockwise order to indicate the outside face. Culling in OpenGL: By default, OpenGL considers polygons with counterclockwise specification to be front facing. If we specify, or render, in the opposite direction, we are rendering the back face (inward-facing face of a solid).

16 We use the right-hand rule (CCW) to specify the vertices that make up the outside faces of a polygon.

17 Common Transformation Usage 3 examples of resize() routine restate projection & viewing transformations Usually called when window resized Registered as callback for glutReshapeFunc ()

18 resize() : Perspective & LookAt void resize( int w, int h ) { glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0, (GLdouble) w / h, 1.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); }

19 19 resize() : Perspective & Translate Same effect as previous LookAt void resize( int w, int h ) { glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0, (GLdouble) w/h, 1.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 0.0, 0.0, -5.0 ); }

20 resize() : Ortho void resize( int width, int height ) { GLdouble aspect = (GLdouble) width / height; GLdouble left = -2.5, right = 2.5; GLdouble bottom = -2.5, top = 2.5; glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); if ( aspect < 1.0 ) { left /= aspect; right /= aspect; } else { bottom *= aspect; top *= aspect; } glOrtho( left, right, bottom, top, near, far ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); }

21 Compositing Modeling Transformations Problem 1: hierarchical objects one position depends upon a previous position robot arm or hand; sub-assemblies Solution 1: moving local coordinate system modeling transformations move coordinate system post-multiply column-major matrices OpenGL post-multiplies matrices

22 Compositing Modeling Transformations Problem 2: objects move relative to absolute world origin my object rotates around the wrong origin make it spin around its center or something else Solution 2: fixed coordinate system modeling transformations move objects around fixed coordinate system pre-multiply column-major matrices OpenGL post-multiplies matrices must reverse order of operations to achieve desired effect

23 Lighting, Shading Lighting and shading give objects “shape” Important effects shading shiny highlights reflections shadows Local techniques simplify these effects to improve performance

24 Shading/ lighting Diffuse ambient light creates no shading... Simplest Illumination can vary by angle between N (normal to the polygon) and L (the source) Source of illumination can be a point or a region (expressed as cos n  ). The larger the n the narrower the beam Compute N and L across polygon face

25 Shading Can interpolate shade across a polygon Gouraud shading interpolates shade across edges, reduces effect of intensity change. Phong shading (and illumination) interpolates surface normal vector across polygons then interpolates illumination.

26 Shading A reflection (diffuse + specular + florescence) model describes the interaction of light with a surface, in terms of the properties of the surface and the nature of the incident light. Surface : surface normal + material (combination of k a, k d, k s and n) I = I a k a + I i [k d (LN) + k s (RV) n ]/(r+k)  1V1V 1R1R N L  

27 Shading Compute lighting based on angle of light on polygon surface. Surface normal

28 Gouraud Shading Compute shading for each pixel by averaging shading based on distance and shading of vertices.

29 Gouraud Each face has a normal. Vertex normals are the averages of the faces surrounding them. Note: with OpenGL, user supplies vertex normals directly.

30 Gouraud Once normals of vertices of face P known, then the illumination of those points can be computed with lighting equation. Then, when scan-converting polygon P, the pixel colour (lighting) is simply linearly interpolated from these vertex lighting values. In example right, only 4 lighting computations done; rest of face is interpolated from these values.

31 Different Illumination

32

33 Global illumination Global techniques provide more accuracy by simulating light propagation among all surfaces in a 3D world. Local shading (Gauroud shading, Phong shading) does not calculate global effect (shadow, reflection, refraction, scattering, etc) Technique ray tracing radiosity

34 Solid Modeling Which surfaces should be drawn? Object space methods Hidden Surface Removal Painters Algorithm BSP Trees Image space methods Z-Buffering Ray Casting

35 Volume Rendering Ray Traced Texture Mapped

36 Ray Tracing Image space technique that mimics physical processes of light Extremely computationally intensive, but beautiful Hidden surface removal Transparency Reflections Refraction Ambient lighting Point source lighting Shadows

37 Shadows and beyond Can be computationally intensive, project each polygon on light source find projection on other polygons 3D shadows, transparency, fog and atmospherics all complicate the computation.

38 Why Lighting? What light source is used and how the object response to the light makes difference Ocean looks bright bluish green in sunny day but dim gray green in cloudy day Lighting gives you a 3D view to an object A unlit sphere looks no different from a 2D disk To get realistic pictures, the color computation of pixels must include lighting calculations

39 Types of Light Ambient Light that’s been scattered so much by the environment that its direction is impossible to determine - it seems to come from all directions Diffuse Light that comes from one direction, but it gets scattered equally in all directions Specular Light comes from a particular direction, and its tends to bounce off the surface in a preferred direction

40 Important factors in illumination surface properties light properties (colour, intensity) position of light(s), viewer eye, object (polygon):

41 Diffuse and Specular Reflection n - normal of point/polygon s - direction to light r - reflection angle to s v - direction to user eye B - angle between v and r

42

43 Materials Colors A material’s color depends on the percentage of the incoming different lights it reflects Materials have different ambient, diffuse and specular reflectances Material can also have an emissive color which simulates light originating from an object Headlights on a automobile

44 OpenGL Lighting Model Lighting has four independent components that are computed independently Emission, Ambient, Diffuse, and Specular OpenGL approximates lighting as if light can be broken into red, green, and blue components The RGB values for lights mean different than for materials For light, the numbers correspond to a percentage of full intensity for each color For materials, the numbers correspond to the reflected proportions of those colors Total effect is a combination of corresponding components of incoming light and illuminated material surface (LR*MR, LG*MG, LB*MB)

45 Adding Lighting to the Scene Define normal vectors for each vertex of each object Create, select, and position one ore more light sources Create and select a lighting model Define material properties for the objects in the scene

46 Creating Light Sources Properties of light sources are color, position, and direction void glLight{if}(GLenum light, GLenum pname, TYPE param); void glLight{if}v(GLenum light, GLenum pname, TYPE *param); Creates the light specified by light that can be GL_LIGHT0, GL_LIGHT1, … or GL_LIGHT7 Pname specifies the characteristics of the light being set Param indicates the values to which the pname characteristic is set glEnable(GL_LIGHT0);

47 Color for a Light Source GLfloat light_ambient[] = {0.0,0.0,0.0,1.0}; GLfloat light_diffuse[] = {1.0,1.0,1.0,1.0}; GLfloat light_specular[] = {1.0,1.0,1.0,1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

48 Position of Light Source Positional light source (x, y, z) values specify the location of the light GLfloat light_position[] = {x, y, z, w}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); Directional light source (x, y, z) values specify the direction of the light located at the infinity No attenuation GLfloat light_position[] = {x, y, z, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

49 Multiple Lights You can define up to eight light sources Need to specify all the parameters defining the position and characteristics of the light OpenGL performs calculations to determine how much light each vertex from each source Increasing numbers of lights affects performance

50 Controlling a Light’s Position and Direction A light source is subject to the same matrix transformations as a geometric model Position or direction is transformed by the current modelview matrix and stored in eye coordinates Keeping the light stationary Specify the light position after modelview transformations Independently moving the light Set the light position after the modeling transformation that you want to apply for light Moving the light together with the viewpoint Set the light position before the viewing transformation

51 Selecting a Lighting Model How to specify a lighting model glLightModel{if}(GLenum pname, TYPE param); glLightModel(if}v(GLenum pname, TYPE *param); Sets properties of the lighting model Pname defines the characteristic of the model being set Param indicates the values to which the pname characteristic is set Needs to be enabled or disabled glEnable(GL_LIGHTING); glDisable(GL_LIGHTING);

52 Components of Lighting Model Global ambient light Ambient light from not any particular source Glfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0} glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); Local or Infinite viewpoint Whether the viewpoint position is local to the scene or whether it should be considered to be an infinite distance away glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); Default is an infinite viewpoint Two-sided lighting Whether lighting calculations should be performed differently for both the front and bacl faces of objects glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

53 Defining Material Properties Specifying the ambient, diffuse, and specular colors, the shininess, and the color of any emitted light void glMaterial{if}(GLenum face, GLenum pname, TYPE param); void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param); Specifies a current material property for use in lighting calculations Face can be GL_FRONT, GL_BLACK, or GL_FRONT_AND_BACK Pname identifies the particular material property being set Param defines the desired values for that property

54 Reflectances Diffuse and ambient reflection Gives color GLfloat mat_amb_diff[] = {0.1, 0.5,0.8,1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff); Specular reflection Produces highlights GLfloat mat_specular[] = {1.0,1.0,1.0,1.0); Glfloat low_shininess[] = {5.0}; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); Emission Make an object glow (to simulate lamps and other light sources GLfloat mat_emission[] = {0.3,0.2,0.2,0.0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);


Download ppt "Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17."

Similar presentations


Ads by Google