常量
|
RGB混合因子
|
alpha混合因子
|
GL_ZERO
|
(0,0,0)
|
0
|
GL_ONE
|
(1,1,1)
|
1
|
GL_SRC_COLOR
|
(Rs,Gs,Bs)
|
As
|
GL_ONE_MINUS_SRC_COLOR
|
(1,1,1)-(Rs,Gs,Bs)
|
1-As
|
GL_DST_COLOR
|
(Rd,Gd,Bd)
|
Ad
|
GL_ONE_MINUS_DST_COLOR
|
(1,1,1)-(Rd,Gd,Bd)
|
1-Ad
|
GL_SRC_ALPHA
|
(As,As,As) |
As
|
GL_ONE_MINUS_SRC_ALPHA
|
(1,1,1)-(As,As,As)
|
1-As
|
GL_DST_ALPHA
|
(Ad,Ad,AD)
|
Ad
|
GL_ONE_MINUS_DST_ALPHA
|
(1,1,1)-(Ad,Ad,Ad)
|
1-Ad
|
GL_CONSTANT_COLOR
|
(Rc,Gc,Bc)
|
Ac
|
GL_ONE_MINUS_CONSTANT_COLOR
|
(1,1,1)-(Rc,Gc,Bc)
|
1-Ac
|
GL_CONSTANT_ALPHA
|
(Ac,Ac,Ac)
|
Ac
|
GL_ONE_MINUS_CONSTANT_ALPHA
|
(1,1,1)-(Ac,Ac,Ac)
|
Ac
|
GL_SRC_ALPHA_SATURATE
|
(f,f,f)=min(As,1-Ad)
|
1
|
|
|
|
|
|
|
混合模式参数
|
数学操作
|
|
GL_FUNC_ADD(默认)
|
Cs*S+Cd*D
|
GL_FUNC_SUBTRACT
|
Cs*S-Cd*D
|
GL_FUNC_REVERSE_SUBTRACT
|
Cd*D-Cs*S
|
GL_MIN
|
min(Cs*S,Cd*D)
|
GL_MAX
|
max(Cs*S,Cd*D)
|
GL_LOGIC_OP
|
Cs op Cd
|
#include "alpha.h" #include "CLApp.h" static int leftFirst = GL_TRUE; void alphaInit() { glEnable( GL_BLEND ); glDisable( GL_DEPTH ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glShadeModel( GL_FLAT ); glClearColor( 0.0, 0.0, 0.0, 0.0 ); } void alphaDrawLeftTriangle() { //黄色三角形 glBegin( GL_TRIANGLES ); glColor4f(1.0, 1.0, 0.0, 0.75); glVertex3f( 1, 9, 0.0 ); glVertex3f( 1, 1, 0.0 ); glVertex3f( 7, 5, 0.0 ); glEnd(); } void alphaDrawRightTriangle() { // glBegin( GL_TRIANGLES ); glColor4f( 0.0, 1.0, 1.0, 0.25 ); glVertex3f(9, 9,0.0); glVertex3f(3, 5, 0.0); glVertex3f(9, 1,0.0); glEnd(); } void alphaDisplay() { glClear( GL_COLOR_BUFFER_BIT ); if (leftFirst) { alphaDrawLeftTriangle(); alphaDrawRightTriangle(); } else { alphaDrawRightTriangle(); alphaDrawLeftTriangle(); } } void alphaUpdate(float dt) { CLInput* pInput = theAppInstance->getInputInstance(); if (pInput->IsKeyUp(DIK_T)) { leftFirst = !leftFirst; } }
#include "alpha3D.h" #define MAXZ 8.0 #define MINZ -8.0 #define ZINC 0.4 static float solidz = MAXZ; static float transparentz = MINZ; static GLuint sphereList, cubeList; void alpha3DInit() { GLfloat mat_specular[] = {1.0, 1.0, 1.0, 0.15 }; GLfloat mat_shininess[] = {100.0}; GLfloat position[] = {5, 5, 10.0, 0.0 }; glLoadIdentity(); gluLookAt(0.0, 0.0, -30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular ); glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess ); glMaterialfv( GL_LIGHT0, GL_POSITION, position ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); glEnable( GL_DEPTH_TEST ); sphereList = glGenLists(1); glNewList( sphereList, GL_COMPILE ); GLUquadricObj* qobj = gluNewQuadric(); //二次曲面对象 gluSphere(qobj, 3.0, 16, 16); gluDeleteQuadric(qobj); glEndList(); cubeList = glGenLists(1); glNewList( cubeList, GL_COMPILE ); GLUquadricObj* qobj1 = gluNewQuadric(); //二次曲面对象 //gluDisk(qobj1, 2.0, 5.0, 15.0, 10.0); gluQuadricDrawStyle(qobj1, GLU_FILL); gluCylinder(qobj1, 1, 4, 3, 15, 10); gluDeleteQuadric(qobj1); glEndList(); } void alpha3DDisplay() { GLfloat mat_solid[] = {0.75, 0.75, 0.0, 1.0 }; GLfloat mat_zero[] = {0.0, 0.0, 0.0, 1.0}; GLfloat mat_transparent[] = {0.0, 0.8, 0.8, 0.6 }; GLfloat mat_emission[] = {0.0, 0.3, 0.3, 0.6 }; glClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT ); glPushMatrix(); glTranslatef( 5, 0, solidz ); glMaterialfv( GL_FRONT, GL_EMISSION, mat_zero ); glCallList( sphereList ); glPopMatrix(); glPushMatrix(); glTranslatef( 5, 0, transparentz ); 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 ); glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_transparent ); glEnable( GL_BLEND ); glDepthMask( GL_FALSE ); glBlendFunc( GL_SRC_ALPHA, GL_ONE ); glCallList( cubeList ); glDepthMask(GL_TRUE ); glDisable( GL_BLEND ); glPopMatrix(); } void alpha3DUpdate(float delta) { static bool ani = false; static int tTime = 0; if (solidz <= MINZ || transparentz >= MAXZ) { ani = false; } if ( ani ) { tTime += delta; if (tTime > 100) { solidz -= ZINC; transparentz += ZINC; tTime = 0; } } CLInput* pInput = theAppInstance->getInputInstance(); if (pInput->IsKeyUp(DIK_A)) { ani = true; } else if (pInput->IsKeyUp(DIK_R)) { solidz = MAXZ; transparentz = MINZ; } }