CH7 Buffers and Blending. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.

Slides:



Advertisements
Similar presentations
1 Buffers and Processing Fragments 2011 Autumn Animação e Visualização Tridimensional 2011/2012.
Advertisements

University of Sulaimani - School of Science - Computer Dept.
Compositing and Blending - Chapter 8 modified by Ray Wisman Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this.
CS 248 OpenGL Help Session CS248 Presented by Ian Buck Stanford University Nov. 10, 2002.
Draw a Simple Object. Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc,
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
CH7 Buffers and Blending. Blending Example 1/6 #include #include "glut.h" GLfloat alpha = 0.0; GLfloat pos[4] = {0, 10, 10, 0}; GLfloat dif_l[4] = {1.0,
1 CSC461 Lecture 7: 3D Programming in OpenGL Objectives: Develop 2D and 3D examples -- Sierpinski gasket: a fractal Develop 2D and 3D examples -- Sierpinski.
CS 248 OpenGL Help Session CS248 Presented by Sean Walker
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
Sierpinski Gasket Program
Computer Graphics (Fall 2004) COMS 4160, Lecture 11: OpenGL 2
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Write a Simple Program with OpenGL & GLUT. Books and Web Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible.
Jittering, Introduction to Stenciling Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday, February 6, 2004.
Texture Mapping. To add surface details… World of Warcraft, Blizzard Inc. More polygons (slow and hard to handle small details) Less polygons but with.
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
Write a Simple Program with OpenGL & GLUT. Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
1 Chapter 6 Blending, Antialiasing, Fog, and Polygon Offset Guihua Yang Jan 14, 2004.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
Texture Mapping in OpenGL. Texture Mapping Imaging we are “pasting” a picture onto a model  Which part of the picture will be pasted onto which part.
Modeling with OpenGL Practice with OpenGL transformations.
Introduction to OpenGL (part 2)
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
Computer Graphics I, Fall 2010 Programming with OpenGL Part 3: Three Dimensions.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.
CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
实验 2 设计一个真实感图形显示系统. 选题  60 分:有灯光、材质、纹理 (p203,p206) 的场景  80 分:+交互控制场景( figure11.1.c )  100 分:+读入多边形物体+ Bezier 曲面.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
Color spaces. Draw Buffers Color models. Mathmatical Protocols for defining colors with numbers  RGB Red, Green, Blue  HSV Hue, Saturation, Value(Brightness)‏
OpenGL Special Effects Jian-Liang Lin 2002 Blending: Alpha Channel Alpha value can be specify by glColor* When blending is enabled, the alpha value is.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Chap 3 Viewing and Transformation
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Accumulation-Based Effects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, February 4, 2004.
CH6 Texture.
Chap 2 Write a Simple OpenGL Program. Preparing 1/2 environment : Microsoft Visual C 、 Microsoft Visual C++.Net Also need : GLUT
CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to load other format, you can try the FreeImage.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
The Framebuffer Hyun-Chul Cho. HyunChul Cho - KUCG -2 Buffer Goal of a graphics program Draw pictures on the screen Screen Rectangular array.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU Pixel Operations and Buffers.
Introduction to Graphics Programming. Graphics API.
Implement of transformation,projection, viewing Hanyang University Jungsik Park.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
CSC Graphics Programming
Stenciling Effects Glenn G. Chappell
Draw a Simple Object.
Advanced Graphics Algorithms Ying Zhu Georgia State University
I = a I + ( ) 1 – a I BLENDING, ANTIALIASING, AND FOG l l 1 l 2 Earth
OpenGL Buffers and Tests
Lecture 16 and 17 Rasterization and Per-Fragment Operations
CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Fog and Transparency.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Lighting and Shading Lab 8:.
Lighting and Shading Lab 8:.
Ref: OpenGL Programming Guide (The Red Book)
Introduction : What is OpenGL
Presentation transcript:

CH7 Buffers and Blending

Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment operations Buffer operations Rasterizer setup Framebuffer We are here!

Topics Blending Fog Accumulation Buffer Stencil Buffer

Blending Example 1/6 #include #include "glut.h" GLfloat alpha = 0.0; GLfloat pos[4] = {0, 10, 10, 0}; GLfloat dif_l[4] = {1.0, 1.0, 1.0, 1.0}; GLfloat dif_t[4] = {1.0, 0.5, 0.8, 1.0}; GLfloat dif_m[4] = {0.5, 0.8, 0.8, cos(alpha)}; int time1,time2; void display(); void reshape(GLsizei, GLsizei); void idle();

Blending Example 2/6 void main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize (500, 500); glutCreateWindow("Blending"); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); glutMainLoop(); return; }

Blending Example 3/6 void reshape(GLsizei w, GLsizei h){ glViewport(0, 0, 500, 500); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, 1 / 1, 0.1, 1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(-10, 10, 30, 0, 0, 0, 0, 1, 0); } void idle(){ time2 = clock(); alpha += 3.14 *(time2-time1)/CLK_TCK; dif_m[3] = (cos(alpha) + 1) / 2; time1 = time2; glutPostRedisplay(); }

Blending Example 4/6 void display(){ glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, pos); glLightfv(GL_LIGHT0, GL_DIFFUSE, dif_l); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif_t); glutSolidTeapot(5);

Blending Example 5/6 glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif_m); glBegin(GL_POLYGON); glNormal3f(0, 0, 1); glVertex3f(-5, -5, 10); glVertex3f(5, -5, 10); glVertex3f(5, 5, 10); glVertex3f(-5, 5, 10); glEnd(); glutSwapBuffers(); }

Blending Example 6/6

Blend Function 1/2 glBlendFunc(GLenum sfactor, GLenum dfactor); Color = source_color * sfactor + destination_color * dfactor

Blend Function 2/2 Parameter(f(R), f(G), f(B), f(A)) GL_ZERO(0,0,0,0) GL_ONE(1,1,1,1) GL_SRC_COLOR (RS/kR,GS/kG,BS/kB,AS/kA) GL_ONE_MINUS_SRC_COLOR(1,1,1,1)-(RS/kR,GS/kG,BS/kB,AS/kA) GL_DST_COLOR(Rd/kR,Gd/kG,Bd/kB,Ad/kA) GL_ONE_MINUS_DST_COLOR(1,1,1,1)-(Rd/kR,Gd/kG,Bd/kB,Ad/kA) GL_SRC_ALPHAAS/kA,AS/kA,AS/kA,AS/kA GL_ONE_MINUS_SRC_ALPHA(1,1,1,1) -(AS/kA,AS/kA,AS/kA,AS/kA) GL_DST_ALPHA(AD/kA,AD/kA,AD/kA,AD/kA) GL_ONE_MINUS_DST_ALPHA(1,1,1,1) -(AD/kA,AD/kA,AD/kA,AD/kA)

How to Use Blending? Remember to enable glEnable(GL_BLEND); Set up blend function glBlendFunc( …, … ) Draw object

Fog Example 1/6 #include #include "glut.h" GLfloat fcolor[4]={1.0, 1.0, 1.0, 1.0}; AUX_RGBImageRec * img; GLuint texObject[1]; void LoadTexture(char* filename){ img = auxDIBImageLoad(filename); glGenTextures(1, texObject); glBindTexture(GL_TEXTURE_2D, texObject[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->sizeX, img->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); }

Fog Example 2/6 void display(){ glEnable(GL_FOG); glFogf(GL_FOG_MODE, GL_EXP); glFogf(GL_FOG_DENSITY, 0.5); glFogfv(GL_FOG_COLOR, fcolor); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texObject[0]);

Fog Example 3/6 glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, 0.0, 0.0); glTexCoord2f(0.0, 100.0); glVertex3f(-50.0, 0.0, 100.0); glTexCoord2f(100.0, 100.0); glVertex3f(50.0, 0.0, 100.0); glTexCoord2f(100.0, 0.0); glVertex3f(50.0, 0.0, 0.0); glEnd(); glFlush(); glutSwapBuffers(); }

Fog Example 4/6 void reshape(GLsizei w, GLsizei h){ glViewport(0, 0, 500, 500); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, 1 / 1, 0.1, 1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.5, 0.0, 50.0, 0.0, 50.0, 0.0, 1.0, 0.0); }

Fog Example 5/6 void main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize (500, 500); glutCreateWindow("Blending"); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); LoadTexture("check.bmp"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return; }

Fog Example 6/6

Fog Function 1/2 glFog{f,i}[v]{GLenum pname, param} Pname param GL_FOG_MODE GL_LINEAR, GL_EXP, GL_EXP2 GL_FOG_DENSITY default : 1.0 GL_FOG_STARTdefault : 0.0 GL_FOG_ENDdefault : 1.0 GL_FOG_INDEX default : 0.0 GL_FOG_COLORdefault : (0,0,0,0)

Fog Function 2/2 GL_LINEAR GL_EXP GL_EXP2 COLOR:

How to Use Fog? Remember to enable glEnable(GL_FOG); Set up fog function glFogf( …, … ) Draw object

Buffers in OpenGL Color Buffer Store the color value to be seen on screen. Depth Buffer Store the depth values for each pixel. Stencil Buffer Accumulation Buffer

ScreenAccumulation Buffer += return

Accumulation function void glAccum( GLenum op, GLfloat value ); operates on the accumulation buffer. op: The accumulation buffer operation. See next page. value: also see next page.

Accumulation function opMeaning GL_ACCUMread each pixel from the color buffer, multiplies the R, G, B, and alpha values by value, then add the result to accumulation buffer. GL_LOADthe same as GL_ACCUM, but replace the value in accumulation rather than add. GL_RETURNread values from the accumulation buffer, multiplies them by value, and places the result in the color buffers. GL_ADD / GL_MULTsimply add / multiply the values of each pixel in the accumulation buffer by value, and return to the accumulation buffer. (GL_MULT will clamp value from –1.0 to 1.0 while GL_ADD will not.)

Accumulation example 1/3 #include void GL_display() { // clear the buffer glClearColor(0.0, 0.0, 0.0, 0.0); glClearAccum(0.0, 0.0, 0.0, 0.0); glClear(GL_ACCUM_BUFFER_BIT); for(int i = 0; i < 360; i++){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1 - (float)i / 180, 1 - (float)i / 360); glPushMatrix(); glTranslatef(0, -6, -10); glRotatef(i,0, 1, 0); glutWireTeapot(5); glPopMatrix(); glAccum(GL_ACCUM, 0.01); } glAccum(GL_RETURN, 1.0) ; glutSwapBuffers(); }

Accumulation example 2/3 void GL_reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 3.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ACCUM); glutCreateWindow("Accumulation"); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutMainLoop(); }

Accumulation example 3/3 Motion blur

Accumulation example 2 1/3 #include void GL_display() { glClearColor(0.0, 0.0, 0.0, 0.0); glClearAccum(0.0, 0.0, 0.0, 0.0); glClear(GL_ACCUM_BUFFER_BIT); for(int i = 1; i <5; i++){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(i * 0.4, 0.0, 8.0, i * 0.4, 0.0, 0.0, 0.0, 1.0, 0.0); glColor3f(1, 0, 0); glutWireTeapot(2); glAccum(GL_ACCUM, (float)i); } glAccum(GL_RETURN, 0.1) ; glutSwapBuffers(); }

Accumulation example 2 2/3 void GL_reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ACCUM); glutCreateWindow("Accumulation"); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutMainLoop(); }

Accumulation example 2 3/3

How to use accumulation buffer? glutInitDisplayMode(GLUT_ACCUM); glClear(GL_ACCUM_BUFFER_BIT);

Stencil Buffer + Z Buffer

Z Buffer Test Stencil Buffer Test Pixel Pass Fail Screen Pass Fail Pass

Stencil Testing 1/2 void glStencilFunc( GLenum func, GLint ref, GLuint mask ); sets the function and reference value for stencil testing. func: test function, see next page. ref: reference value mask:A mask that is ANDed with both the reference value and the stored stencil value when the test is done.

Stencil Testing 2/2 paramMeaning GL_NEVER Always fails. GL_LESS Passes if ( ref & mask) < ( stencil & mask). GL_LEQUAL Passes if ( ref & mask) ≤ ( stencil & mask). GL_GEQUAL Passes if ( ref & mask) ≥ ( stencil & mask). GL_NOTEQUAL Passes if ( ref & mask) ( stencil & mask). GL_ALWAYS Always passes.

Modify stencil buffer 1/2 void glStencilOp( GLenum fail, GLenum zfail, GLenum zpass ); sets the stencil test actions. fail: The action to take when the stencil test fails zfail: Stencil action when the stencil test passes, but the depth test fails. zpass: both the stencil test and the depth test pass

Modify stencil buffer 2/2 paramMeaning GL_KEEPkeep the current value GL_ZEROset the value in stencil buffer to zero GL_REPLACEset the value in stencil buffer to ref in glStencilFunc() GL_INCRincrease the current value in stencil buffer GL_DECRdecrease the current value in stencil buffer GL_INVERTbitwise inverse the current value in stencil buffer

How to use stencil buffer? 1. glutInitDisplayMode(GLUT_STENCIL); 2. glEnable(GL_STENCIL_TEST); 3. glClearStencil(0); 4. glClear(GL_STENCIL_BUFFER_BIT);

Stencil buffer example 1/3 #include void GL_display(){ glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_ALWAYS, 1, 1); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glColor3f(0.0, 1.0, 1.0); glutSolidCube(16.0); glClear(GL_DEPTH_BUFFER_BIT); glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); glStencilFunc(GL_EQUAL, 1, 1); glColor3f(1.0, 1.0, 1.0); glutSolidTeapot(8); glFlush(); }

Stencil buffer example 2/3 void GL_reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(400, 400); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutCreateWindow("Stencil Buffer"); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutMainLoop(); }

Stencil buffer example 3/3

Other example … 1/2 void GL_display(){ glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_ALWAYS, 1, 1); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glColor3f(0.0, 1.0, 1.0); glutSolidCube(16.0); glClear(GL_DEPTH_BUFFER_BIT); glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); glStencilFunc(GL_EQUAL, 1, 1); glColor3f(1.0, 1.0, 1.0); glutSolidTeapot(8); glStencilFunc(GL_NOTEQUAL, 1, 1); glColor3f(1.0, 1.0, 0.0); glPushMatrix(); glTranslatef(10, 0, 0); glutSolidSphere(12, 10, 6); glPopMatrix(); glFlush(); }

Other example … 2/2

Cooler example …