Presentation is loading. Please wait.

Presentation is loading. Please wait.

03/19/2002(c) University of Wisconsin, CS 559 Last Time BSP Tree rendering and exact visibility in mazes Local Shading –Diffuse term –Specular term.

Similar presentations


Presentation on theme: "03/19/2002(c) University of Wisconsin, CS 559 Last Time BSP Tree rendering and exact visibility in mazes Local Shading –Diffuse term –Specular term."— Presentation transcript:

1 03/19/2002(c) University of Wisconsin, CS 559 Last Time BSP Tree rendering and exact visibility in mazes Local Shading –Diffuse term –Specular term

2 03/19/2002(c) University of Wisconsin, CS 559 Today Shading Interpolation OpenGL and lighting/shading More on light sources Texture Mapping

3 03/19/2002(c) University of Wisconsin, CS 559 Next Time Midterm In class One sheet of paper (double sided) Anything on the to-know sheet is possible Last office hour (for me) before exam is this afternoon

4 03/19/2002(c) University of Wisconsin, CS 559 Putting It Together Global ambient intensity, I a : –Gross approximation to light bouncing around of all other surfaces –Modulated by ambient reflectance ka Just sum all the terms If there are multiple lights, sum contributions from each light Several variations, and approximations …

5 03/19/2002(c) University of Wisconsin, CS 559 Color Do everything for three colors, r, g and b (r is shown) Note that some terms (the expensive ones) are constant For reasons we will not go into, this is an approximation, but few graphics practitioners realize it –Aliasing in color space –Better results use 9 color samples

6 03/19/2002(c) University of Wisconsin, CS 559 Distant Light Approximation The viewer direction, V, and the light direction, L, depend on the surface position being considered, x Distant light approximation: –Assume L is constant for all x –Good approximation if light is distant, such as sun –Generally called a directional light source What aspects of surface appearance are affected by this approximation? –Diffuse? –Specular?

7 03/19/2002(c) University of Wisconsin, CS 559 Local Viewer Approximation Specularities require the viewing direction: –V(x) = ||c-x|| –Slightly expensive to compute Local viewer approximation uses a global V –Independent of which point is being lit –Use the view plane normal vector –Error depends on the nature of the scene Is the diffuse component affected?

8 03/19/2002(c) University of Wisconsin, CS 559 Describing Surfaces The various parameters in the lighting equation describe the appearance of a surface (k d,r,k d,g,k d,b ): The diffuse color, which most closely maps to what you would consider the “color” of a surface –Also called diffuse reflectance coefficient (k s,r,k s,g,k s,b ): The specular color, which controls the color of specularities –The same as the diffuse color for metals, white for plastics –Some systems do not let you specify this color separately (k a,r,k a,g,k a,b ): The ambient color, which controls how the surface looks when not directly lit –Normally the same as the diffuse color

9 03/19/2002(c) University of Wisconsin, CS 559 OpenGL Model Allows emission, E: Light being emitted by surface Allows separate light intensity for diffuse and specular Ambient light can be associated with light sources Allows spotlights that have intensity that depends on outgoing light direction Allows attenuation of light intensity with distance Can specify coefficients in multiple ways Too many variables and commands to present in class The OpenGL programming guide goes through it all (the red book)

10 03/19/2002(c) University of Wisconsin, CS 559 OpenGL Commands (1) glMaterial{if}(face, parameter, value) –Changes one of the coefficients for the front or back side of a face (or both sides) glLight{if}(light, property, value) –Changes one of the properties of a light (intensities, positions, directions, etc) –There are 8 lights: GL_LIGHT0, GL_LIGHT1, … glLightModel{if}(property, value) –Changes one of the global light model properties (global ambient light, for instance) glEnable(GL_LIGHT0) enables GL_LIGHT0 –You must enable lights before they contribute to the image –You can enable and disable lights at any time

11 03/19/2002(c) University of Wisconsin, CS 559 OpenGL Commands (2) glColorMaterial(face, mode) –Causes a material property, such as diffuse color, to track the current glColor() –Speeds things up, and makes coding easier glEnable(GL_LIGHTING) turns on lighting –You must enable lighting explicitly – it is off by default Don’t use specular intensity if you don’t have to –It’s expensive - turn it off by giving 0,0,0 as specular color of the lights Don’t forget normals Many other things to control appearance

12 03/19/2002(c) University of Wisconsin, CS 559 Shading Interpolation The models we have discussed give the intensity of a single point –Computing these models for every point that is displayed is expensive –Normals may not be explicitly stated for every point Several options: –Flat shading –Gouraud interpolation –Phong interpolation New hardware provides other options

13 03/19/2002(c) University of Wisconsin, CS 559 Flat shading Compute shading at a representative point and apply to whole polygon –OpenGL uses one of the vertices Advantages: –Fast - one shading computation per polygon Disadvantages: –Inaccurate –What are the artifacts?

14 03/19/2002(c) University of Wisconsin, CS 559 Gourand Shading Shade each vertex with it’s own location and normal Linearly interpolate the color across the face Advantages: –Fast - incremental calculations when rasterizing –Much smoother - use one normal per shared vertex to get continuity between faces Disadvantages: –What are the artifacts? –Is it accurate?

15 03/19/2002(c) University of Wisconsin, CS 559 Phong Interpolation Interpolate normals across faces Shade each pixel Advantages: –High quality, narrow specularities Disadvantages: –Expensive –Still an approximation for most surfaces Not to be confused with Phong’s specularity model

16 03/19/2002(c) University of Wisconsin, CS 559

17 03/19/2002(c) University of Wisconsin, CS 559 Shading and OpenGL OpenGL defines two particular shading models –Controls how colors are assigned to pixels –glShadeModel(GL_SMOOTH) interpolates between the colors at the vertices (the default) –glShadeModel(GL_FLAT) uses a constant color across the polygon

18 03/19/2002(c) University of Wisconsin, CS 559 The Current Generation Current hardware allows you to break from the standard illumination model Programmable Vertex Shaders allow you to write a small program that determines how the color of a vertex is computed –Your program has access to the surface normal and position, plus anything else you care to give it (like the light) –You can add, subtract, take dot products, and so on

19 03/19/2002(c) University of Wisconsin, CS 559 The Full Story We have only touched on the complexities of illuminating surfaces –The common model is hopelessly inadequate for accurate lighting (but it’s fast and simple) Consider two sub-problems of illumination –Where does the light go? Light transport –What happens at surfaces? Reflectance models Other algorithms address the transport or the reflectance problem, or both –Much later in class, or a separate course

20 03/19/2002(c) University of Wisconsin, CS 559 Light Sources Two aspects of light sources are important for a local shading model: –Where is the light coming from (the L vector)? –How much light is coming (the I values)? Various light source types give different answers to the above questions: –Point light source: Light from a specific point –Directional: Light from a specific direction –Spotlight: Light from a specific point with intensity that depends on the direction –Area light: Light from a continuum of points (later in the course)

21 03/19/2002(c) University of Wisconsin, CS 559 Point and Directional Sources Point light: L(x) = ||p light - x|| –The L vector depends on where the surface point is located –Must be normalized - slightly expensive –To specify an OpenGL light at 1,1,1: Directional light: L(x) = L light –The L vector does not change over points in the world –OpenGL light traveling in direction 1,1,1 (L is in opposite direction): Glfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); Glfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

22 03/19/2002(c) University of Wisconsin, CS 559 Spotlights Point source, but intensity depends on L: –Requires a position: the location of the source –Requires a direction: the center axis of the light –Requires a cut-off: how broad the beam is –Requires and exponent: how the light tapers off at the edges of the cone Intensity scaled by (L·D) n glLightfv(GL_LIGHT0, GL_POSITION, light_posn); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_dir); glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, 1.0); cut-off direction

23 03/19/2002(c) University of Wisconsin, CS 559 Mapping Techniques Consider the problem of rendering a soup can –The geometry is very simple - a cylinder –But the color changes rapidly, with sharp edges –With the local shading model, so far, the only place to specify color is at the vertices –To do a soup can, would need thousands of polygons for a simple shape –Same things goes for an orange: simple shape but complex normal vectors Solution: Mapping techniques use simple geometry modified by a mapping of some type

24 03/19/2002(c) University of Wisconsin, CS 559 Texture Mapping The soup tin is easily described by pasting a label on the plain cylinder Texture mapping associates the color of a point with the color in an image: the texture –Soup tin: Each point on the cylinder get the label’s color Question to address: Which point of the texture do we use for a given point on the surface? Establish a mapping from surface points to image points –Different mappings are common for different shapes –We will, for now, just look at triangles (polygons)

25 03/19/2002(c) University of Wisconsin, CS 559 Basic Mapping The texture lives in a 2D space –Parameterize points in the texture with 2 coordinates: (s,t) –These are just what we would call (x,y) if we were talking about an image, but we wish to avoid confusion with the world (x,y,z) Define the mapping from (x,y,z) in world space to (s,t) in texture space –To find the color in the texture, take an (x,y,z) point on the surface, map it into texture space, and use it to look up the color of the texture With polygons: –Specify (s,t) coordinates at vertices –Interpolate (s,t) for other points based on given vertices

26 03/19/2002(c) University of Wisconsin, CS 559 Texture Interpolation Specify where the vertices in world space are mapped to in texture space Linearly interpolate the mapping for other points in world space –Straight lines in world space go to straight lines in texture space Texture map s t Triangle in world space

27 03/19/2002(c) University of Wisconsin, CS 559 Interpolating Coordinates (x 1, y 1 ), (s 1, t 1 )(x 2, y 2 ), (s 2, t 2 ) (x 3, y 3 ), (s 3, t 3 )

28 03/19/2002(c) University of Wisconsin, CS 559 Pipelines and Texture Mapping Texture mapping is done in canonical screen space as the polygon is rasterized When describing a scene, you assume that texture interpolation will be done in world space Which property of perspective projection means that the “wrong thing” will happen if we apply our simple interpolations from the previous slide? Perspective correct texture mapping does the right thing, but at a cost Is it a problem with orthographic viewing?

29 03/19/2002(c) University of Wisconsin, CS 559 Basic OpenGL Texturing Specify texture coordinates for the polygon: –Use glTexCoord2f(s,t) before each vertex: Eg: glTexCoord2f(0,0); glVertex3f(x,y,z); Create a texture object and fill it with texture data: –glGenTextures(num, &indices) to get identifiers for the objects –glBindTexture(GL_TEXTURE_2D, identifier) to bind the texture Following texture commands refer to the bound texture –glTexParameteri(GL_TEXTURE_2D, …, …) to specify parameters for use when applying the texture –glTexImage2D(GL_TEXTURE_2D, ….) to specify the texture data (the image itself) MORE…

30 03/19/2002(c) University of Wisconsin, CS 559 Basic OpenGL Texturing (cont) Enable texturing: glEnable(GL_TEXTURE_2D) State how the texture will be used: –glTexEnvf(…) Texturing is done after lighting You’re ready to go…

31 03/19/2002(c) University of Wisconsin, CS 559 Nasty Details There are a large range of functions for controlling the layout of texture data: –You must state how the data in your image is arranged –Eg: glPixelStorei(GL_UNPACK_ALIGNMENT, 1) tells OpenGL not to skip bytes at the end of a row –You must state how you want the texture to be put in memory: how many bits per “pixel”, which channels,… For project 3, when you use this stuff, there will be example code, and the Red Book contains examples

32 03/19/2002(c) University of Wisconsin, CS 559 Controlling Different Parameters The “pixels” in the texture map may be interpreted as many different things. For example: –As colors in RGB or RGBA format –As grayscale intensity –As alpha values only The data can be applied to the polygon in many different ways: –Replace: Replace the polygon color with the texture color –Modulate: Multiply the polygon color with the texture color or intensity –Similar to compositing: Composite texture with base color using operator

33 03/19/2002(c) University of Wisconsin, CS 559 Example: Diffuse shading and texture Say you want to have an object textured and have the texture appear to be diffusely lit Problem: Texture is applied after lighting, so how do you adjust the texture’s brightness? Solution: –Make the polygon white and light it normally –Use glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE) –Use GL_RGB for internal format –Then, texture color is multiplied by surface (fragment) color, and alpha is taken from fragment

34 03/19/2002(c) University of Wisconsin, CS 559 Next Next Time After spring break… More on textures – there’s a whole lot more still to come Next Next Next time (April 4) –No class


Download ppt "03/19/2002(c) University of Wisconsin, CS 559 Last Time BSP Tree rendering and exact visibility in mazes Local Shading –Diffuse term –Specular term."

Similar presentations


Ads by Google