Lectures Transparency Case Study

Similar documents
Lectures Display List

Graphics Pipeline & APIs

Lectures OpenGL Introduction

Graphics Pipeline & APIs

Buffers and Processing Fragments

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

INF3320 Computer Graphics and Discrete Geometry

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

Understand how real-world lighting conditions are approximated by OpenGL

Overview. Shading. Shading. Why we need shading. Shading Light-material interactions Phong model Shading polygons Shading in OpenGL

Display Lists in OpenGL

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

CS 432 Interactive Computer Graphics

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

CSCI 4620/8626. Primitives and Attributes

Computer graphics MN1

Exercise 1 Introduction to OpenGL

Why we need shading?

5.2 Shading in OpenGL

Lighting. Chapter 5. Chapter Objectives. After reading this chapter, you'll be able to do the following:

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Pixels and Buffers. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

A display list is a group of OpenGL commands that have been stored for later execution.

Lecture 2 2D transformations Introduction to OpenGL

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

Lecture 4 Advanced Computer Graphics (CS & SE )

Illumination and Reflection in OpenGL CS 460/560. Computer Graphics. Shadows. Photo-Realism: Ray Tracing. Binghamton University.

Computer Graphics. Three-Dimensional Graphics VI. Guoying Zhao 1 / 73

Lighting. CSC 7443: Scientific Information Visualization

Tutorial 4: Depth and Transparency

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

Computer Graphics. Bing-Yu Chen National Taiwan University

Lights, Colour and Materials In OpenGL

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

/opengl/docs/man_pages/hardcopy/gl/html/gl/

Display Issues Week 5

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Interaction Computer Graphics I Lecture 3

Image Processing. CSCI 420 Computer Graphics Lecture 22

Today s class. Simple shadows Shading Lighting in OpenGL. Informationsteknologi. Wednesday, November 21, 2007 Computer Graphics - Class 10 1

Image Processing. Alpha Channel. Blending. Image Compositing. Blending Errors. Blending in OpenGL

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Definition. Blending & Compositing. Front & Back Buffers. Left & Right Buffers. Blending combines geometric objects. e.g.

OpenGL refresher. Advanced Computer Graphics 2012

Introduction to OpenGL

Introduction to OpenGL

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

COMPSCI 373 S1 C - Assignment 2 Sample Solution

Visualizing Molecular Dynamics

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Modeling with Transformations

Rasterization Computer Graphics I Lecture 14. Scan Conversion Antialiasing Compositing [Angel, Ch , ]

Interaction. CSCI 480 Computer Graphics Lecture 3

Illumination and Shading

Rendering Pipeline/ OpenGL

Computer graphic -- Programming with OpenGL 2

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

Basic Graphics Programming

QUESTION 1 [10] 2 COS340-A October/November 2009

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

CS Surface Rendering

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

Computer graphics MN1


Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Light Sources. Spotlight model

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

hw1-2 review hw 3 questions?

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Better Interactive Programs

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

C OMPUTER G RAPHICS Thursday

Shading and Illumination

1 /* 4 C:\opencv\build\include. 6 C:\opencv\build\x86\vc10\lib

OpenGL An introduction

Scientific Visualization Basics

Basic Graphics Programming

Computer Graphics (CS 4731) Lecture 16: Lighting, Shading and Materials (Part 1)

Discrete Techniques. 11 th Week, Define a buffer by its spatial resolution (n m) and its depth (or precision) k, the number of

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Interaction. CSCI 420 Computer Graphics Lecture 3

Lessons Learned from HW4. Shading. Objectives. Why we need shading. Shading. Scattering

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

CS452/552; EE465/505. Lighting & Shading

Computer Graphics. Bing-Yu Chen National Taiwan University

OpenGL. White Square Code 11/4/09. OpenGL. OpenGL. OpenGL

Transcription:

Lectures Transparency Case Study By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma State University Rohnert Park, California 2004, Tom Duff and George Ledin Jr 1

How is transparency achieved in OpenGL? In OpenGL, we use blending of alpha value (opacity value) to create a translucent fragment that lets some of the previously stored color value show through. 2004, Tom Duff and George Ledin Jr 2

Enable/Disable blending of color To enable transparency, we need to explicitly enable blending: glenable(gl_blend) To disable blending: gldisable(gl_blend) 2004, Tom Duff and George Ledin Jr 3

Alpha Value void glclearcolor(glclampf red, GLclampf green, GLclampf blue, GLclampf alpha) Specify values for the color buffer void glcolor4f(glfloat red, GLfloat green, GLfloat blue, GLfloat alpha) Specify colors Transparent 0 <= alpha <= 1 opaque 2004, Tom Duff and George Ledin Jr 4

What happens when blending is enabled? When blending is enabled, the alpha value is used to combine the color value of the fragment being processed with that of the pixel already stored in the framebuffer. Blending Model Blending occurs after the scene has been rasterized and converted to fragments, but just before the final pixels are drawn in the framebuffer. Without blending, each new fragment overwrites any existing color values in the frame buffer, as if the fragment were opaque. With blending, one can control how much of the existing color value should be combined with the new fragment s value. 2004, Tom Duff and George Ledin Jr 5

Case Study 1 a real world scenario Scenario: There s a yellow circle and a blue circle. The yellow circle is 25% transparent. The blue circle is 75% transparent. Choice A Question: When the yellow circle is in front of the blue circle, what do we see, Choice A or Choice B? Choice B 2004, Tom Duff and George Ledin Jr 6

Case Study 1 Answer is A Reason: Because yellow circle is more opaque, therefore we see more of the yellow and less of the blue. Scenario: There s a yellow circle and blue circle. Choice A The yellow circle is 25% transparent. The blue circle is 75% transparent. This means that the yellow circle is 75% opaque and blue circle is 25% opaque. Choice B If the yellow circle is in front of the blue circle, only 25% of the blue blends with 75% of the yellow. (Choice A) If the blue circle is in front of the yellow circle, only 25% of yellow blends with 75% of the blue. (Choice B) 2004, Tom Duff and George Ledin Jr 7

The source and destination factors During blending, source is the color value of the incoming fragment. Blending Model Destination is the currently stored pixel value. 2004, Tom Duff and George Ledin Jr 8

How does OpenGL blend the source and destination color values? Assume: Source Color: (Rs, Gs, Bs, As) Destination Color: (Rd, Gd, Bd, Ad) Step 1: Specify the source and destination factors. Let source destination blending factors be: (Sr, Sg, Sb, Sa) Let destination blending factors be: (Dr, Dg, Db, Da) Step 2: Combine the corresponding components of source and destination. Final blended RGBA values are given by: (RsSr+RdDr, GsSg+GdDg, BsSb+BdDb, AsSa+AdDa) Note: Each of these quadruplets is clamped to [0,1] 2004, Tom Duff and George Ledin Jr 9

Blend Function void glblendfunc(glenum sfactor, GLenum dfactor) sfactor Specifies how the red, green, blue and alpha source blending factors are computed. Nine symbolic constants are accepted: GL_ZERO GL_ONE GL_DST_ COLOR GL_ONE_MINUS_DST_COLOR GL_SRC_ALPHA GL_ONE_MINUS_SRC_COLOR GL_DST_ALPHA GL_ONE_MINUS_DST_ALPHA GL_SRC_ALPHA_SATURATE dfactor Specifies how the red, green, blue and alpha destination blending factors are computed. Eight symbolic constants are accepted: GL_ZERO GL_ONE GL_SCR_COLOR GL_ONE_MINUS_SRC_COLOR GL_SRC_ALPHA GL_ONE_MINUS_SRC_COLOR, GL_DST_ALPHA GL_ONE_MINUS_DST_ALPHA. Blending factors lie in the range [0,1]. After the color values in the source and destination are combined, they re clamped to the range [0,1]. 2004, Tom Duff and George Ledin Jr 10

Source and Destination Blending Factors Table sfactor=(sr,sg,sb,sa) or dfactor=(dr,dg,db,da) 2004, Tom Duff and George Ledin Jr 11

Case Study 2 Source and Destination Blending Factors Case Study Setup: A yellow circle of radius 2, centered at (-1,0,0) static void drawleftcircle(void){ /* draw yellow triangle on LHS of screen */ glcolor4f(1.0, 1.0, 0.0, 0.75); //yellow, alpha=0.75 circle(-1,0,2); //x=-1,y=0,radius=2 A blue circle of radius 2, centered at (1,0,0) static void drawrightcircle(void){ Question: /* draw yellow triangle on LHS of screen */ glcolor4f(0.0, 0.0, 1.0, 0.75); //blue, alpha=0.75 circle(1,0,2); //x=1,y=0,radius=2 If we change the sfactor and dfactor of glblendfunc(sfactor, dfactor), what will be the blending effect? 2004, Tom Duff and George Ledin Jr 12

Init blending function in main /* We will change sfactor and dfactor values later on to test the blending effect.*/ //(Sr,Sg,Sb,Sa)=(As,As,As,As)= (0.75,0.75,0.75,0.75) sfactor = GL_SRC_ALPHA; //(Dr,Dg,Db,Da)=(1-As,1-As,1-As,1-As)= (0.25,0.25,0.25,0.25) dfactor = GL_ONE_MINUS_SRC_ALPHA; /* Initialize alpha blending function. */ static void init(void) { glenable (GL_BLEND); glblendfunc (sfactor, dfactor); glshademodel (GL_FLAT); glclearcolor (1, 1, 1, 1); // white and opaque background 2004, Tom Duff and George Ledin Jr 13

Test Case A Let sfactor = GL_SRC_ALPHA, dfactor = GL_ONE_MINUS_SRC_ALPHA Draw blue circle first, yellow second. void display() { drawleftcircle(); // yellow circle drawrightcircle(); // blue circle Predicted final blending value based on formula: (Rf,Gf,Bf,Af) = (RsSr+RdDr, GsSg+GdDg, BsSb+BdDb, AsSa+AdDa) (Rs,Gs,Bs,As) = (1,1,0,0.75) //yellow, semi-transparent (Rd,Gd,Bd,Ad) = (0,0,1,0.75) //blue, semi-transparent sfactor = GL_SRC_ALPHA (Sr,Sg,Sb,Sa) = (0.75,0.75,0.75,0.75) dfactor = GL_ONE_MINUS_SRC_ALPHA (Dr,Dg,Db,Da) = (1-0.75,1-0.75,1-0.75,1-0.75) = (0.25,0.25,0.25,0.25) Final predicted blending value (Rf,Gf,Bf,Af) = (0.75,0.75,0.25,0.75) 2004, Tom Duff and George Ledin Jr 14

Use our predicted final blending value to draw an object (circle) and compare its color with the blended color of the blue and yellow circles. In the preceding slide, our predicted final blending value was (Rf,Gf,Bf,Af) = (0.75,0.75,0.25,0.75) Function to draw a circle centered at (0,3,0) with radius of 1. static void drawtestcircle(void) { glcolor4f(0.75, 0.75, 0.25, 0.75); circle(0,3,1); Display the test circle together with two intersecting circles void display() { drawtestcircle(); // draw test circle with predicted color drawrightcircle(); // Destination: blue circle drawleftcircle(); // Source: yellow circle Result: The predicted color shown in the test circle matches the final blended color of the yellow and blue circles. 2004, Tom Duff and George Ledin Jr 15

Test Case BLet sfactor = GL_SRC_ALPHA, dfactor = GL_ONE_MINUS_SRC_ALPHA Draw yellow circle first, blue circle second. void display() { drawleftcircle(); // yellow circle drawrightcircle(); // blue circle Predicted final blending value based on formula: (Rf,Gf,Bf,Af) = (RsSr+RdDr, GsSg+GdDg, BsSb+BdDb, AsSa+AdDa) (Rs,Gs,Bs,As) = (0,0,1,0.75) //blue, semi-transparent (Rd,Gd,Bd,Ad) = (1,1,0,0.75) //yellow, semi-transparent sfactor = GL_SRC_ALPHA (Sr,Sg,Sb,Sa) = (0.75,0.75,0.75,0.75) dfactor = GL_ONE_MINUS_SRC_ALPHA (Dr,Dg,Db,Da) = (1-0.75,1-0.75,1-0.75,1-0.75) = (0.25,0.25,0.25,0.25) Final predicted blending value (Rf,Gf,Bf,Af) = (0.25,0.25,0.75,0.75) 2004, Tom Duff and George Ledin Jr 16

Use our predicted final blending value to draw an object (circle) and compare the color with the blending color of the yellow and blue circles. In the preceding slide, our predicted final blending value was (Rf,Gf,Bf,Af) = (0.25,0.25,0.75,0.75) Function to draw a circle centered at (0,3,0) with radius of 1. static void drawtestcircle(void) { glcolor4f(0.25, 0.25, 0.75, 0.75); circle(0,3,1); Display the test circle together with two intersecting circles void display() { drawtestcircle(); // draw test circle with predicted color drawleftcircle(); // Source: yellow circle drawrightcircle(); // Destination: blue circle Result: The predicted color shown in the test circle matches the final blending color of the yellow and blue circle. 2004, Tom Duff and George Ledin Jr 17

Conclusion drawn from Test A and Test B In both Test A and B, we use the same blending function: glblendfunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); The differences are: In Test A, we draw first the blue, then the yellow circle. In Test B, we draw first the yellow, then blue circle. Tests A and B display different blending effects. This shows that the order in which objects are defined makes a difference in the final color blending result. Test a Blending Model Test b 2004, Tom Duff and George Ledin Jr 18

Case 3 Blending is dangerous and has many artifacts Study Case: A cube emitting blue light. One cube emitting blue light. GLfloat mat_emission[] = { 0.0, 0, 1, 1 ; // blue glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue One of the artfacts When light shines on the cube, the material reflects red diffuse color of alpha value 0.6. GLfloat mat_transparent1[] = { 1, 0, 0, 0.8; // red 0.8 diffuse glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent1);//red light One light shining in the Z direction static void init(void) { GLfloat position[] = { 0, 0, 1.0, 0 ; //light shining headon gllightfv(gl_light0, GL_POSITION, position); glenable(gl_lighting); glenable(gl_light0); glenable(gl_depth_test); Goal:: We will change the backgournd, the blending function to see what kind of artfact we can get. We will not be going through details as to what these artifacts are. The purpose of this study case is to show how dangerous blending can be 2004, Tom Duff and George Ledin Jr 19

Case 3, A Object is visible with black background but disappears with white background void display(void) { GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 ; //black GLfloat mat_transparent[] = { 1, 0, 0, 0.6; //red 0.6 diffuse GLfloat mat_emission[] = { 0.0, 0, 1, 1 ; // blue White background SetBackground(1,1,1,1); SetBackground(1,1,1,1); glrotatef (15.0, 1.0, 1.0, 0.0); glrotatef (30.0, 0.0, 1.0, 0.0); glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent);//red 0.6 diffuse glenable (GL_BLEND); gldepthmask (GL_FALSE);// make the depth buffer read only // while drawing the translucent objects glblendfunc (GL_SRC_ALPHA, GL_ONE); glcalllist (cubelist); gldepthmask (GL_TRUE); gldisable (GL_BLEND); glflush(); Black background SetBackground(1,1,1,1); Note: This artifact can be noticed when using glblendfunc (GL_SRC_ALPHA, GL_ONE); However, when using glblendfunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); it does not matter whether the background is black and white; the cube is always viewable. 2004, Tom Duff and George Ledin Jr 20

Case 3, B Blending with depth buffer read-only vs depth buffer writable. void display(void) { GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 ; //black GLfloat mat_transparent[] = { 1, 0, 0, 0.6; //red 0.6 diffuse GLfloat mat_emission[] = { 0.0, 0, 1, 1 ; // blue SetBackground(0,0,0,1); glrotatef (15.0, 1.0, 1.0, 0.0); glrotatef (30.0, 0.0, 1.0, 0.0); glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent);//red 0.6 diffuse glenable (GL_BLEND); gldepthmask (GL_FALSE);// make the depth buffer read only // while drawing the translucent objects glblendfunc (GL_SRC_ALPHA, GL_ONE); glcalllist (cubelist); gldepthmask (GL_TRUE); gldisable (GL_BLEND); With depth buffer read-only gldepthmask (GL_FALSE); With depth buffer writable gldepthmask (GL_TRUE); glflush(); Note: This artifact is noticed when using glblendfunc (GL_SRC_ALPHA, GL_ONE); glblendfunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); However, combinations of all possible values for glblendfunc were not attempted. 2004, Tom Duff and George Ledin Jr 21

Lesson from Case 3 Blending is dangerous with mixed with light whose alpha value is < 1 (transparent). In order to avoid artifacts, you need to know exactly what you are doing. 2004, Tom Duff and George Ledin Jr 22

Case 4 How does the alpha value affect the object s color? Study Case: There are three cubes emitting blue color. If light is shine on the left, it will present red diffuse color of alpha value, such as 0.6 transparency. If light is shined on the middle, it will present red diffuse color of alpha value, such as 0.8 (transparency). If light is shine on the right, it will present red diffuse color of alpha = 1 (solid). Question: How does the alpha value of light affect the color of the cube? 2004, Tom Duff and George Ledin Jr 23

Case Study 4, continued Setup common traits for the three cubes to be rendered Set up light position (towards Z direction) Set up material s specular property (dark grey), and shininess (very shiny) Since our goal is not to study material s specular property, therefore, we minimize the specular color by setting it to dark grey (very negligible, and we set specular spot on the object by setting the material shininess to 127, very shiny. static void init(void) { //dark grey specular light GLfloat mat_specular[] = { 0.1, 0.1, 0.1, 1 // concentrated specular spot GLfloat mat_shininess[] = { 127 ; GLfloat position[] = { 0, 0, 1.0, 0 ; glmaterialfv(gl_front, GL_SPECULAR, mat_specular); glmaterialfv(gl_front, GL_SHININESS, mat_shininess); //light shining head on gllightfv(gl_light0, GL_POSITION, position); Create display list of the cube This step is not required. However, since we will render the same cube three times to display three cubes, using display list will make the rendering faster. glenable(gl_lighting); glenable(gl_light0); //one light source glenable(gl_depth_test); // create display list for a cube to be used later cubelist = glgenlists(1); glnewlist(cubelist, GL_COMPILE); glutsolidcube (0.6); glendlist(); 2004, Tom Duff and George Ledin Jr 24

Case Study 4, continued Render three cubes with same emission color (solid blue) but different alpha value of red diffuse color void display(void) { GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 ; //black GLfloat mat_transparent1[] = { 1, 0, 0, 0.6; // red 0.6 diffuse GLfloat mat_transparent2[] = { 1, 0, 0, 0.8; // red 0.8 diffuse GLfloat mat_transparent3[] = { 1, 0, 0, 1; // red 1 diffuse GLfloat mat_emission[] = { 0.0, 0, 1, 1 ; // blue SetBackground(0,0,0,1); //black // Cube on the left: diffuse = red, alpha = 0.6 glpushmatrix (); gltranslatef (-1, 0, 0);// glrotatef (15.0, 1.0, 1.0, 0.0); glrotatef (30.0, 0.0, 1.0, 0.0); glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent1);//red glenable (GL_BLEND); glblendfunc (GL_SRC_ALPHA, GL_ONE); glcalllist (cubelist); gldisable (GL_BLEND); glpopmatrix (); // Cube in the middle: diffuse = red, alpha = 0.8 glpushmatrix (); gltranslatef (0, 0, 0); glrotatef (15.0, 1.0, 1.0, 0.0); glrotatef (30.0, 0.0, 1.0, 0.0); glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent2);//red glenable (GL_BLEND); glblendfunc (GL_SRC_ALPHA, GL_ONE); glcalllist (cubelist); gldepthmask (GL_TRUE); gldisable (GL_BLEND); glpopmatrix (); //Cube on the right: diffuse=red, alpha = 1 glpushmatrix (); gltranslatef (1, 0, 0); glrotatef (15.0, 1.0, 1.0, 0.0); glrotatef (30.0, 0.0, 1.0, 0.0); glmaterialfv(gl_front, GL_EMISSION, mat_emission);//blue glmaterialfv(gl_front, GL_DIFFUSE, mat_transparent3);//red glenable (GL_BLEND); glblendfunc (GL_SRC_ALPHA, GL_ONE); glcalllist (cubelist); gldisable (GL_BLEND); glpopmatrix (); glflush(); Conclusion: The alpha value of material affects the final object color. Between [0,1], the larger the alpha value, the Deeper the color. In this case, the cube on the right has alpha value 1 for its diffuse color, therefore it diffuses more red. Since red (diffuse) + blue (emissive color) = purple, therefore, the cube on the right displays deeper purple. 2004, Tom Duff and George Ledin Jr 25

Case Study 5 Blending with depth buffer Scenario: In the scene, there is a translucent cyan cube in front of a solid yellowish sphere. The cube is centered at (0.1, 0.1, 8) The sphere is centered at (-0.1, -0.1, 8) Each time user click a on the keyboard, the cube will move backwards a bit. When user click r on the keyboard, the cube will be reset to its original position. Goal: The part of translucent cyan cube that is in front of the solid sphere blended with sphere. The part of translucent cyan cube that is behind the sphere will not be displayed. 2004, Tom Duff and George Ledin Jr 26

The code to move the translucent cyan cube backward when user click a, and reset the position of cube, when user click r #define ZINC 1 #define MAXZ 8.0 #define MINZ -8.0 static float solidz = MINZ; static float transparentz = MAXZ; void ChangeLocation(void) { transparentz -= ZINC; glutpostredisplay(); void keyboard(unsigned char key, int x, int y) { switch (key) { case 'a': case 'A': ChangeLocation(); glutpostredisplay(); break; case 'r': case 'R': transparentz = MAXZ; glutpostredisplay(); break; case 27: exit(0); In the code left, when user click a or A, the global valuable which keeps track of the location of the cyan cube is incremented. The result is that the cube is moved backward. As the transparent cube moves forward, what we expect to see is that it blends with the opaque sphere. 2004, Tom Duff and George Ledin Jr 27

Our trouble, when the translucent cyan cube is in the front of solid sphere, the color of cube does not blend with the color of the sphere. void display(void) { //glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); SetBackground(1,1,1,1); // Draw the translucent cube farther from camera glpushmatrix (); gltranslatef (0.15, 0.15, transparentz); glenable (GL_BLEND); glblendfunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glcalllist (cubelist); gldisable (GL_BLEND); glpopmatrix (); // Draw the solid sphere closer to camera glpushmatrix (); gltranslatef (-0.15, -0.15, solidz); glcalllist (spherelist); // draw the solid sphere glpopmatrix (); glutswapbuffers(); 2004, Tom Duff and George Ledin Jr 28

How to fix the problem occurred when you draw both translucent and solid objects in one scene? It might be a bug in OpenGL that when you draw translucent objects mixed with solid objects in one scene, if draw the translucent object first, OpenGL treats it as if it were solid, blending will not occur. Fix: If you want translucent objects (in front) blending with solid objects, and solid objects (in front) obscuring translucent objects, you need to exercise care if you draw the translucent and solid objects in one scene. The way to draw the objects are as follows: (1) Enable the depth buffer. (2) Draw all opaque objects. (3) Enable blend, then draw all translucent objects. 2004, Tom Duff and George Ledin Jr 29

New Code Drawing the solid sphere before drawing the transparent cube. void display(void){ //glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); SetBackground(1,1,1,1); // Draw the solid sphere closer to camera glpushmatrix (); gltranslatef (-0.15, -0.15, solidz); glcalllist (spherelist); // draw the solid sphere glpopmatrix (); // Draw the translucent cube farther from camera glpushmatrix (); gltranslatef (0.15, 0.15, transparentz); glenable (GL_BLEND); glblendfunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glcalllist (cubelist); gldisable (GL_BLEND); glpopmatrix (); glutswapbuffers(); 2004, Tom Duff and George Ledin Jr 30