CH7 Buffers and Blending. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting...

45
CH7 Buffers and Blending

Transcript of CH7 Buffers and Blending. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting...

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

CH7 Buffers and Blending

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

Pixel pipeline

Vertex pipeline

Course Map

Transformation&

Lighting

Primitiveassembly

Viewport culling&

clipping

Textureblending

Per Fragmentoperations

Bufferoperations

Rasterizersetup

Framebuffer

We are here!

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

Topics

Blending Fog Accumulation Buffer Stencil Buffer

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

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

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

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;

}

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

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

}

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

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

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

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

}

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

Blending Example 6/6

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

Blend Function 1/2

glBlendFunc(GLenum sfactor, GLenum dfactor); Color = source_color * sfactor +

destination_color * dfactor

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

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_ALPHA AS/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)

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

How to Use Blending?

Remember to enable glEnable(GL_BLEND);

Set up blend function glBlendFunc(…,…)

Draw object

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

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

}

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

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

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

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

}

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

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

}

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

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;

}

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

Fog Example 6/6

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

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)

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

Fog Function 2/2

GL_LINEAR

GL_EXP

GL_EXP2

COLOR:

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

How to Use Fog?

Remember to enable glEnable(GL_FOG);

Set up fog function glFogf(…,…)

Draw object

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

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

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

Accumulation Buffer

Screen AccumulationBuffer

+=

return

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

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.

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

Accumulation functionop 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.)

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

Accumulation example 1/3

#include <GL/glut.h>void GL_display(){

// clear the bufferglClearColor(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();

}

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

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

}

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

Accumulation example 3/3

Motion blur

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

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

}

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

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

}

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

Accumulation example 2 3/3

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

How to use accumulation buffer?

glutInitDisplayMode(GLUT_ACCUM);

glClear(GL_ACCUM_BUFFER_BIT);

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

Stencil Buffer + Z Buffer

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

Stencil Buffer + Z Buffer

Z BufferTest

StencilBufferTest

Pixel

Pass

Fail

Screen

Pass Fail

Pass

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

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.

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

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.

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

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

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

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

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

How to use stencil buffer?

1. glutInitDisplayMode(GLUT_STENCIL);

2. glEnable(GL_STENCIL_TEST);

3. glClearStencil(0);4. glClear(GL_STENCIL_BUFFER_BIT);

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

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

}

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

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

}

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

Stencil buffer example 3/3

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

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

}

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

Other example… 2/2

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

Cooler example…