Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 CH7 Buffers and Blending

2 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();

3 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; }

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

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

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

7 Blending Example 6/6

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

9 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)

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

11 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); }

12 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]);

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

14 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); }

15 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; }

16 Fog Example 6/6

17 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)

18 Fog Function 2/2 GL_LINEAR GL_EXP GL_EXP2 COLOR:

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

20 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

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

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

23 Accumulation example 3/3 Motion blur

24 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 = 0; i < 30; i++){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(i * 0.01, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glTranslatef(0, 4, -10); glColor3f(1, 0, 0); glutWireTeapot(2); glPopMatrix(); glAccum(GL_ACCUM, 0.03333); } glAccum(GL_RETURN, 1.0) ; glPushMatrix(); glColor3f(1, 1, 1); glutWireTeapot(1); glPopMatrix(); glutSwapBuffers(); }

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

26 Accumulation example 2 3/3

27 Cooler example

28 How to use accumulation buffer? glutInitDisplayMode(GLUT_ACCUM); glClear(GL_ACCUM_BUFFER_BIT); That ’ s all … ~(  ̄▽ ̄ )~( _△_ )~(  ̄▽ ̄ )~

29 Accumulation function void glAccum( GLenum op, GLfloat value ); 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.

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

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

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

33 Stencil buffer example 3/3

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

35 Other example … 2/2

36 Cooler example …

37 Stencil buffer + Z buffer

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

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

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

41 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

42 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


Download ppt "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,"

Similar presentations


Ads by Google