由于累积缓冲区会带来大内存的开销,所以在实时应用程序中比较少用。但在非实时的应用程序中,可以产生实时应用程序无法做到的效果。例如,你可以多次渲染场景,并在每次渲染时进行抖动零点几个像素,这样就可以产生整个场景的反走样的效果,比多重采样的效果还要好。还可以模糊前景或背景,然后清晰的渲染一个物体来模拟,照相机景深的效果
void DrawGemometry()
{
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawGround();
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0.3f, -3.5f);
glRotatef(-yRot*2.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(1.0f, 0.0f, 0.0f);
glutSolidSphere(0.1f, 17, 13);
glPopMatrix();
}
void RenderScene()
{
yRot = 35.0f;
pass = 10.0f;
for ( i = 0; i < 2; ++i)
{
yRot += 0.75f;
DrawGemometry();
//复制到累积缓冲区
if (i == 0)
{
glAccum(GL_LOAD, 0.5f);// 1
}
else
{
//累加到累积缓冲区
glAccum(GL_ACCUM, (0.5f * 1 / pass));//2
}
}
glAccum(GL_RETURN, 1.0f);
glutSwapBuffers();
}
在for循环10中,DrawGemometry将图元绘制到颜色缓冲区中,
glAccum(GL_LOAD, 0.5f);将颜色缓冲区的内容替换到累积缓冲区,
glAccum(GL_ACCUM, (0.5f * 1 / pass)) 将颜色缓冲区的内容添加到累积缓冲区,这样10次内容就都保存到了累积缓冲区。
glAccum(GL_RETURN, 1.0f);将累积缓冲区的内容拷贝到颜色缓冲区然后再进行翻页显示到屏幕上
void glAccum (GLenum op, GLfloat value);
op:GL_ACCUM,读取当前缓冲区中每个像素,把R,G,B,A值与value值相乘,然后把结果添加到累积缓冲区
GL_LOAD:于ACCUM功能基本相同,不同的是ACCUM是把结果添加到累积缓冲区,GL_LOAD是把结果替换到累积缓冲区
GL_RETURN从累积缓冲区接收值,把他们与value值相乘,然后把结果放入颜色缓冲区中
GL_ADD,把每个像素值与与value值相加,超出范围不会截取
GL_MULIT:把每个像素值与与value值相乘,范围控制在【-1,+1】
颜色通道屏蔽
void glColorMask (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
例如,屏蔽R通道 glColorMask(GL_FALSE,GL_TRUE,GL_TRUE,GL_TRUE);
剔除操作
glEnalbe(GL_CULL_FACE) 开启剔除操作效果
glDisable(GL_CULL_FACE) 关闭剔除操作效果
glCullFace()参数包括GL_FRONT和GL_BACK。表示禁用多边形正面或者背面上的光照、阴影和颜色计算及操作,消除不必要的渲染计算。
例如某对象无论如何位置变化,我们都只能看到构成其组成的多边形的某一面时,可使用该函数。
抖动
抖动允许只有少量离散颜色的显示系统来模拟更宽范围的颜色。例如,灰色可以通过白点和黑点的混合来模拟。白点多于黑点呈现浅灰色,黑点多于白点呈现深灰色。这种技巧对于只支持8位和16位的显示系统非常有用。抖动的效果可以大幅度地改善低端颜色系统的图像质量。在默认情况下,抖动是打开的。可以通过glEnable(GL_DITHER)/glDisable(GL_DITHER)来打开或关闭它。在高颜色分辨率的显示系统中,OpenGL的实现可能不需要抖动,会禁用抖动来避免性能的开销。
#include "header.h"
GLfloat fLightPos[4] = { -100.0f, 100.0f, 50.0f, 1.0f };
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
static GLfloat yRot;
void DrawGround()
{
GLfloat fExtent = 20.0f;
GLfloat y = -0.0f;
GLfloat step = 0.5f;
GLfloat x, z;
int iColor = 0;
glShadeModel(GL_FLAT);
for (x = -fExtent; x <= fExtent; x += step)
{
glBegin(GL_TRIANGLE_STRIP);
for (z = fExtent; z >= -fExtent; z -= step)
{
if ((iColor % 2) == 0)
{
glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
}
else
{
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
}
glVertex3f(x, y, z);
glVertex3f(x + step, y, z);
iColor++;
}
glEnd();
}
glShadeModel(GL_SMOOTH);
}
void DrawGemometry()
{
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawGround();
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0.3f, -3.5f);
glRotatef(-yRot*2.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(1.0f, 0.0f, 0.0f);
glutSolidSphere(0.1f, 17, 13);
glPopMatrix();
}
GLfloat pass = 10.0f;
int i;
void RenderScene()
{
yRot = 35.0f;
pass = 10.0f;
for ( i = 0; i < 20; ++i)
{
yRot += 0.75f;
DrawGemometry();
//复制到累积缓冲区
if (i == 0)
{
glAccum(GL_LOAD, 0.5f);
}
else
{
//累加到累积缓冲区
glAccum(GL_ACCUM, (0.5f * 1 / pass));
}
}
glAccum(GL_RETURN, 1.0f);
glColorMask(GL_FALSE,GL_TRUE,GL_TRUE,GL_TRUE);
glutSwapBuffers();
}
GLfloat faspect;
void ChangeSize(GLsizei w, GLsizei h)
{
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
faspect = (GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(35.0f, faspect, 1.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, -0.4f, 0.0f);
glutPostRedisplay();
}
void SetupRC()
{
glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
glClearAccum(0,0,0,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
//设置光照
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//开启颜色追踪
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
int main(int args, char **argv)
{
glutInit(&args, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_ACCUM);
glutInitWindowSize(800, 600);
glutCreateWindow("累积缓冲区");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}