1 / 45

CH7 Buffers and Blending

CH7 Buffers and Blending. Course Map. We are here!. Vertex pipeline. Transformation & Lighting. Viewport culling & clipping. Primitive assembly. Rasterizer setup. Pixel pipeline. Texture blending. Per Fragment operations. Buffer operations. Framebuffer. Topics. Blending Fog

Download Presentation

CH7 Buffers and Blending

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CH7 Buffers and Blending

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

  3. Topics • Blending • Fog • Accumulation Buffer • Stencil Buffer

  4. Blending Example 1/6 #include <math.h> #include <time.h> #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();

  5. 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; }

  6. 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(); }

  7. 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);

  8. 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(); }

  9. Blending Example 6/6

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

  11. 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)

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

  13. Fog Example 1/6 #include <gl/glaux.h> #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); }

  14. 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]);

  15. 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(); }

  16. 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); }

  17. 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; }

  18. Fog Example 6/6

  19. 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_START default : 0.0 GL_FOG_END default : 1.0 GL_FOG_INDEX default : 0.0 GL_FOG_COLOR default : (0,0,0,0)

  20. Fog Function 2/2 • GL_LINEAR • GL_EXP • GL_EXP2 • COLOR:

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

  22. 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

  23. Accumulation Buffer += return Screen Accumulation Buffer

  24. Accumulation function • void glAccum( GLenumop,GLfloatvalue); • operates on the accumulation buffer. • http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc01_8o4t.asp • op: The accumulation buffer operation. See next page. • value: also see next page.

  25. op Meaning GL_ACCUM read each pixel from the color buffer, multiplies the R, G, B, and alpha values by value, then add the result to accumulation buffer. GL_LOAD the same as GL_ACCUM, but replace the value in accumulation rather than add. GL_RETURN read values from the accumulation buffer, multiplies them by value, and places the result in the color buffers. GL_ADD / GL_MULT simply 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 function

  26. Accumulation example 1/3 #include <GL/glut.h> 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(); }

  27. 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(); }

  28. Accumulation example 3/3 Motion blur

  29. Accumulation example 2 1/3 #include <GL/glut.h> 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(); }

  30. 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(); }

  31. Accumulation example 2 3/3

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

  33. Stencil Buffer + Z Buffer

  34. Stencil Buffer + Z Buffer Fail Pass Fail Pass Pass Stencil Buffer Test Pixel Z Buffer Test Screen

  35. Stencil Testing 1/2 • void glStencilFunc( GLenumfunc,GLintref,GLuintmask); • 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.

  36. Stencil Testing 2/2 param Meaning 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.

  37. Modify stencil buffer 1/2 • void glStencilOp( GLenumfail,GLenumzfail,GLenumzpass); • 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

  38. Modify stencil buffer 2/2 param Meaning GL_KEEP keep the current value GL_ZERO set the value in stencil buffer to zero GL_REPLACE set the value in stencil buffer to ref in glStencilFunc() GL_INCR increase the current value in stencil buffer GL_DECR decrease the current value in stencil buffer GL_INVERT bitwise inverse the current value in stencil buffer

  39. How to use stencil buffer? • glutInitDisplayMode(GLUT_STENCIL); • glEnable(GL_STENCIL_TEST); • glClearStencil(0); • glClear(GL_STENCIL_BUFFER_BIT);

  40. Stencil buffer example 1/3 #include<gl/glut.h> 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(); }

  41. 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(); }

  42. Stencil buffer example 3/3

  43. 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(); }

  44. Other example…2/2

  45. Cooler example…

More Related