关于运动模糊效果的制作
运动模糊效果其实是一种特效,它有助于显示出一个场景当中哪些对象是运动的,以及它们运动的有多快,在电影中和现实生活中都有这样的例子,当一个物体以一个相对于单帧快门速度来说的高速经过照相机的时候,这一帧和相邻帧的图片上就会沿着运动的方向出现一片模糊的痕迹,就比如说在高速公路上行驶当中的汽车中的一位乘客从汽车中向侧面拍摄照片的场景
OpenGL中有很多方法可以去作出这样的效果,一种就是在应用程序当中可以在一个缓冲区当中进行多次渲染,将快速移动的对象稍稍进行偏移并且将得到的效果混合到一起,另一个选择就是为一个物体的图像沿着运动方向对纹理单元像素数据进行多次采样,然后将采样的结果混合在一起,当然我们还有种简单的方式,就是可以将前面帧的结果进行存储并且与当前帧混合到一起,可以把这些帧的结果数据先复制到CPU中再复制出来,也可以去使用更快的PBO的方式
程序分析如下所示
首先先看看需要用到的属性
//绿色
static GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };
//白色
static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//光照位置
static GLfloat vLightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f };
//屏幕宽度
GLsizei screenWidth; // Desired window or desktop width
//屏幕高度
GLsizei screenHeight; // Desired window or desktop height
//着色器manager
GLShaderManager shaderManager; // Shader Manager
//模型视图矩阵
GLMatrixStack modelViewMatrix; // Modelview Matrix
//投影矩阵
GLMatrixStack projectionMatrix; // Projection Matrix
//正投影矩阵
M3DMatrix44f orthoMatrix;
//透视投影矩阵
GLFrustum viewFrustum; // View Frustum
//变换管道类
GLGeometryTransform transformPipeline; // Geometry Transform Pipeline
//角色帧
GLFrame cameraFrame; // Camera frame
//三角形批次类
GLTriangleBatch torusBatch;
//地板批次类
GLBatch floorBatch;
//对屏幕对齐的批次类
GLBatch screenQuad;
//地板纹理
GLuint textures[1];
//模糊纹理
GLuint blurTextures[6];
//像素缓冲区
GLuint pixBuffObjs[1];
//纹理的单元
GLuint curBlurTarget;
//是否使用PBO
bool bUsePBOPath;
//速度比例
GLfloat speedFactor;
//着色器程序
GLuint blurProg;
//指向像素数据
void *pixelData;
//像素数据的大小
GLuint pixelDataSize;
接下来是两个方法
//这个方法会在RenderScene方法中被调用,要为每一帧去设置激活一个纹理做准备
void AdvanceBlurTaget() {
curBlurTarget = ((curBlurTarget+ 1) %6);
}
//下面的这些方法会在SetupBlurProg方法中被调用,拿来传入纹理采样器的值
GLuint GetBlurTarget0(){ return (1 + ((curBlurTarget + 5) %6)); }
GLuint GetBlurTarget1(){ return (1 + ((curBlurTarget + 4) %6)); }
GLuint GetBlurTarget2(){ return (1 + ((curBlurTarget + 3) %6)); }
GLuint GetBlurTarget3(){ return (1 + ((curBlurTarget + 2) %6)); }
GLuint GetBlurTarget4(){ return (1 + ((curBlurTarget + 1) %6)); }
GLuint GetBlurTarget5(){ return (1 + ((curBlurTarget) %6)); }
再之后就是根据帧数量也就是RenderScene函数更新的频率去计算fps
void UpdateFrameCount()
{
static int iFrames = 0; // Frame count
static CStopWatch frameTimer; // Render time
// Reset the stopwatch on first time
if(iFrames == 0)
{
frameTimer.Reset();
iFrames++;
}
// Increment the frame count
iFrames++;
// Do periodic frame rate calculation
//fps = frameNum / elapsedTime; 每隔固定的帧数,计算帧数使用的时间,也可求出帧率
if (iFrames == 101)
{
float fps;
fps = 100.0f / frameTimer.GetElapsedSeconds();
//判断是否使用了PBO
if (bUsePBOPath)
printf("Pix_buffs - Using PBOs %.1f fps\n", fps);
else
printf("Pix_buffs - Using Client mem copies %.1f fps\n", fps);
frameTimer.Reset();
iFrames = 1;
}
}
之后就是加载bmp图像
bool LoadBMPTexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)
{
GLbyte *pBits;
GLint iWidth, iHeight;
//读取BMP图像
pBits = gltReadBMPBits(szFileName, &iWidth, &iHeight);
//如果pBits == NULL的话,就返回false
if(pBits == NULL)
return false;
// Set Wrap modes
//设置纹理参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
// Do I need to generate mipmaps?
//判断是否使用的是mip贴图使用的纹理参数
if(minFilter == GL_LINEAR_MIPMAP_LINEAR || minFilter == GL_LINEAR_MIPMAP_NEAREST || minFilter == GL_NEAREST_MIPMAP_LINEAR || minFilter == GL_NEAREST_MIPMAP_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
//设置过滤参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
//加载纹理数据
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pBits);
return true;
}
设置OpenGL的渲染前的配置
void SetupRC(void)
{
//初始化glew库
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
// Initialze Shader Manager 初始化固定着色器
shaderManager.InitializeStockShaders();
//开启深度测试
glEnable(GL_DEPTH_TEST);
// Black 设置清屏的颜色
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//制作甜甜圈
gltMakeTorus(torusBatch, 0.4f, 0.15f, 35, 35);
//设置透明度
GLfloat alpha = 0.25f;
//设置开始绘制
floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1);
//颜色
floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
//纹理坐标
floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
//法线数据
floorBatch.Normal3f(0.0, 1.0f, 0.0f);
//顶点数据
floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f);
floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
floorBatch.MultiTexCoord2f(0, 10.0f, 0.0f);
floorBatch.Normal3f(0.0, 1.0f, 0.0f);
floorBatch.Vertex3f(20.0f, -0.41f, 20.0f);
floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
floorBatch.MultiTexCoord2f(0, 10.0f, 10.0f);
floorBatch.Normal3f(0.0, 1.0f, 0.0f);
floorBatch.Vertex3f(20.0f, -0.41f, -20.0f);
floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
floorBatch.MultiTexCoord2f(0, 0.0f, 10.0f);
floorBatch.Normal3f(0.0, 1.0f, 0.0f);
floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f);
floorBatch.End();
//生成纹理
glGenTextures(1, textures);
//绑定纹理
glBindTexture(GL_TEXTURE_2D, textures[0]);
//加载图像
LoadBMPTexture("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);
// Create blur program 生成模糊的着色器程序
blurProg = gltLoadShaderPairWithAttributes("blur.vsh", "blur.fsh", 2,
GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "texCoord0");
// Create blur textures 生成6个纹理
glGenTextures(6, blurTextures);
// XXX I don't think this is necessary. Should set texture data to NULL
// Allocate a pixel buffer to initialize textures and PBOs
//计算像素数据的大小
pixelDataSize = screenWidth*screenHeight*3*sizeof(unsigned int); // XXX This should be unsigned byte
//申请空间
void* data = (void*)malloc(pixelDataSize);
//将data中的pixelDataSize字节全部设置为0x00
memset(data, 0x00, pixelDataSize);
// Setup 6 texture units for blur effect
// Initialize texture data
for (int i=0; i<6;i++)
{
//激活纹理单元
glActiveTexture(GL_TEXTURE1+i);
//绑定纹理
glBindTexture(GL_TEXTURE_2D, blurTextures[i]);
//设置纹理的参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//传入纹理数据,这里传入的数据为空,到了RenderScene函数里面的时候才会进行传入真实数据
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
// Alloc space for copying pixels so we dont call malloc on every draw
//生成像素缓冲区
glGenBuffers(1, pixBuffObjs);
//绑定一个像素缓冲区
glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]);
//插入数据
glBufferData(GL_PIXEL_PACK_BUFFER, pixelDataSize, pixelData, GL_DYNAMIC_COPY);
//解绑像素缓冲区
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
// Create geometry and a matrix for screen aligned drawing
//根据一个基于宽度和高度的正模型视图投影矩阵进行设置一个批次类
gltGenerateOrtho2DMat(screenWidth, screenHeight, orthoMatrix, screenQuad);
// Make sure all went well 确认运行无误
gltCheckErrors();
}
清理纹理和缓冲对象
void ShutdownRC(void)
{
// Make sure default FBO is bound
//默认的frameBuffer的name为0
//glBindFramebuffer(GL_FRAMEBUFFER, 0);/将framebuffer绑定到默认的FBO处,一般用于打破之前的FBO绑定关系,使OpenGL的FBO绑定状态恢复到默认状态。
/*
这里的target一般可以填写GL_FRAMEBUFFER,这个缓冲区将会用来进行读和写操作;如果需要绑定到读操作的缓冲区使用GL_READ_FRAMEBUFFER,支持 glReadPixels这类读操作;如果需要绑定到写操作的缓冲区使用GL_DRAW_FRAMEBUFFER,支持渲染、清除等操作。
// 解除绑定,再次绑定到默认FBO
// 一旦默认FBO被绑定,那么读取和写入就都再次绑定到了窗口的帧缓冲区
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
// Cleanup textures
for (int i=0; i<7;i++)
{
glActiveTexture(GL_TEXTURE0+i);
////将2D纹理绑定到默认的纹理,一般用于打破之前的纹理绑定关系,使OpenGL的纹理绑定状态恢复到默认状态。
glBindTexture(GL_TEXTURE_2D, 0);
}
// Now delete detached textures
//删除纹理
glDeleteTextures(1, textures);
//删除纹理
glDeleteTextures(6, blurTextures);
// delete PBO 删除pbo
glDeleteBuffers(1, pixBuffObjs);
}
改变了视口的方法
void ChangeSize(int nWidth, int nHeight)
{
//设置视口
glViewport(0, 0, nWidth, nHeight);
//设置模型视图矩阵和投影矩阵给管道类进去管理
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
//设置投影
viewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);
//加载投影矩阵
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
//模型视图矩阵加载矩阵
modelViewMatrix.LoadIdentity();
// update screen sizes
screenWidth = nWidth;
screenHeight = nHeight;
// reset screen aligned quad 重新去根据宽度和高度设置一个基于窗口宽度和高度正模型视图投影矩阵进行设置
gltGenerateOrtho2DMat(screenWidth, screenHeight, orthoMatrix, screenQuad);
//释放像素数据
free(pixelData);
//设置宽度和高度乘像素点所占据的字节
pixelDataSize = screenWidth*screenHeight*3*sizeof(unsigned int);
//分配空间
pixelData = (void*)malloc(pixelDataSize);
// Resize PBOs
//绑定完像素缓冲区
glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]);
//绑定数据可以先分配为空
glBufferData(GL_PIXEL_PACK_BUFFER, pixelDataSize, pixelData, GL_DYNAMIC_COPY);
//解绑缓冲区,调用以0为缓冲区
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
gltCheckErrors();
}
按住键盘改变的是否需要去使用PBO
void ProccessKeys(unsigned char key, int x, int y)
{
static CStopWatch cameraTimer;
//获取时间间隔
float fTime = cameraTimer.GetElapsedSeconds();
float linear = fTime * 12.0f;
//照相机的时间重置
cameraTimer.Reset();
// Alternate between PBOs and local memory when 'P' is pressed
//判断是否使用PBO
if(key == 'P' || key == 'p')
bUsePBOPath = (bUsePBOPath)? GL_FALSE : GL_TRUE;
// Speed up movement
if(key == '+')
{
speedFactor += linear/2;
//速度如果大于6就设置为6
if(speedFactor > 6)
//设置速度的大小
speedFactor = 6;
}
// Slow down moement
if(key == '-')
{
speedFactor -= linear/2;
//如果速度小于0.5,就设置为0.5
if(speedFactor < 0.5)
//设置速度
speedFactor = 0.5;
}
}
设置着色器程序
void SetupBlurProg(void)
{
// Set the blur program as the current one
//使用着色器程序
glUseProgram(blurProg);
// Set MVP matrix
//传入MVP矩阵
glUniformMatrix4fv(glGetUniformLocation(blurProg, "mvpMatrix"), 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix());
// Setup the textue units for the blur targets, these rotate every frame
//为模糊目标设置textue单元,也就是说会去设置纹理采样单元
glUniform1i(glGetUniformLocation(blurProg, "textureUnit0"), GetBlurTarget0());
glUniform1i(glGetUniformLocation(blurProg, "textureUnit1"), GetBlurTarget1());
glUniform1i(glGetUniformLocation(blurProg, "textureUnit2"), GetBlurTarget2());
glUniform1i(glGetUniformLocation(blurProg, "textureUnit3"), GetBlurTarget3());
glUniform1i(glGetUniformLocation(blurProg, "textureUnit4"), GetBlurTarget4());
glUniform1i(glGetUniformLocation(blurProg, "textureUnit5"), GetBlurTarget5());
}
绘制甜甜圈
void DrawWorld(GLfloat yRot, GLfloat xPos)
{
//照相机矩阵
M3DMatrix44f mCamera;
//模型视图矩阵获取照相机矩阵
modelViewMatrix.GetMatrix(mCamera);
// Need light position relative to the Camera
//需要相对于相机的光源位置
M3DVector4f vLightTransformed;
//将光源位置进行转置,因为整个世界是基于照相机进行移动的
m3dTransformVector4(vLightTransformed, vLightPos, mCamera);
// Draw stuff relative to the camera
//先push矩阵
modelViewMatrix.PushMatrix();
//设置y轴和z轴的平移矩阵
modelViewMatrix.Translate(0.0f, 0.2f, -2.5f);
//设置x轴的平移矩阵,之后就会通过这个效果来制作
modelViewMatrix.Translate(xPos, 0.0f, 0.0f);
//设置旋转矩阵,初始值设置的都是0
modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
/*使用着色器程序点光源着色器
与默认光源着色器类似,但光源位置可能是特定的,这种着色器接受4Uniform值,即模型视图矩阵、投影矩阵、视点坐标系中的光源位置和对象的基本漫反射颜色。所需属性有GLT_ATTRIBUTE_VERTEX和GLT_ATTRIBUTE_NORMAL。*/
//使用着色器程序
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,
modelViewMatrix.GetMatrix(),
transformPipeline.GetProjectionMatrix(),
vLightTransformed, vGreen, 0);
//贴贴圈进行绘制
torusBatch.Draw();
//弹出矩阵
modelViewMatrix.PopMatrix();
}
RenderScene方法
void RenderScene(void)
{
static CStopWatch animationTimer;
//走的总路径,也就是一来一回
static float totalTime = 6; // To go back and forth
//走的距离
static float halfTotalTime = totalTime/2;
//获取时间的间隔乘速度
float seconds = animationTimer.GetElapsedSeconds() * speedFactor;
float xPos = 0;
//计算移动对象的下一个位置
// Calculate the next postion of the moving object
// First perform a mod-like operation on the time as a float
while(seconds > totalTime)
seconds -= totalTime;
// Move object position, if it's gone half way across
// start bringing it back
//seconds的取值为0到6不包括0,在0到3的时候会走下面
if(seconds < halfTotalTime)
{
//从左到右从-1.5到1.5
xPos = seconds -halfTotalTime*0.5f;
}
//3到6的时候
else
{
//比如说totalTime为6,seconds为6,halfTotalTime为3,最小值为-1.5
//从右往左从1.5到-1.5
xPos = totalTime - seconds -halfTotalTime*0.5f;
}
// First draw world to screen
modelViewMatrix.PushMatrix();
M3DMatrix44f mCamera;
//获取照相机矩阵
cameraFrame.GetCameraMatrix(mCamera);
//模型视图矩阵堆栈和照相机矩阵相乘
modelViewMatrix.MultMatrix(mCamera);
//激活纹理单元
glActiveTexture(GL_TEXTURE0);
//绑定纹理
glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble
//情况颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*
纹理调整着色器
这种着色器将一个基本色乘以一个取自纹理单元你TextureUnit的纹理。
所需的属性有GLT_ATTRIBUTE_VERTEX和GLT_ATTRIBUTE_NORMAL
GLShaderManager::UseStockShader(GLT_SHADER_TEXTURE_MODULATE, GLfloat mvpMatrix[16], GLfloat vColor, GLint nTextureUnit);
*/
shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vWhite, 0);
//地板进行绘制
floorBatch.Draw();
//绘制甜甜圈
DrawWorld(0.0f, xPos);
modelViewMatrix.PopMatrix();
if(bUsePBOPath)
{
// First bind the PBO as the pack buffer, then read the pixels directly to the PBO
//绑定纹理
glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]);
//从帧缓冲区读取像素数据返回到GPU当中的PBO对象中
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, NULL);
//解绑缓冲区
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
// Next bind the PBO as the unpack buffer, then push the pixels straight into the texture
//绑定为解包缓冲区,直接将像素读取存放到纹理当中
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixBuffObjs[0]);
// Setup texture unit for new blur, this gets imcremented every frame
//设置纹理单元为新的模糊,这将使每一个帧都受到影响。
glActiveTexture(GL_TEXTURE0+GetBlurTarget0() );
//如果当一个像素缓冲区绑作为解包缓冲区的时候,就会让glTexture2D这个函数原本是从CPU内存读取到帧缓冲区的改为从GPU内存读取到帧缓冲区
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
//解绑对象
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
else
{
//如果不是用PBO的话性能会下降,因为PBO在处理需要经常访问、修改或者是更新像素数据有着巨大的优势
// Grab the screen pixels and copy into local memory
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
// Push pixels from client memory into texture
// Setup texture unit for new blur, this gets imcremented every frame
//激活纹理对象
glActiveTexture(GL_TEXTURE0+GetBlurTarget0() );
//读取数据
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData);
}
// Draw full screen quad with blur shader and all blur textures
//推入投影矩阵
projectionMatrix.PushMatrix();
//加载单位矩阵
projectionMatrix.LoadIdentity();
//加载正投影矩阵
projectionMatrix.LoadMatrix(orthoMatrix);
//push矩阵
modelViewMatrix.PushMatrix();
//加载单位矩阵
modelViewMatrix.LoadIdentity();
//关闭深度测试
glDisable(GL_DEPTH_TEST);
//使用着色器程序
SetupBlurProg();
//进行绘制那个屏幕四方形
screenQuad.Draw();
//开启深度测试
glEnable(GL_DEPTH_TEST);
modelViewMatrix.PopMatrix();
projectionMatrix.PopMatrix();
// Move to the next blur texture for the next frame
//移动到下一帧的下一个模糊纹理
AdvanceBlurTaget();
// Do the buffer Swap
//交换缓冲区
glutSwapBuffers();
// Do it again
//PostRedisplay重新
glutPostRedisplay();
//计算帧率的函数
UpdateFrameCount();
}
在不使用PBO的情况下是通过调用glReadPixels来获取像素数据的,然后通过调用glTexImage2D将像素数据复制到一个纹理对象上完成,也就是说数据的纹理目标将在6个模糊纹理之间进行轮换,如果上一次使用的是纹理3,那么接下来用的就是纹理4,这也就是说纹理4包含这一帧的数据,纹理3包含的是上一帧的数据,当使用完最后一个纹理之后,当前帧的目标将再次进行循环
在使用PBO的情况下,我们不是将数据复制回CPU,而是将PBO绑定到GL_PIXEL_PACK_BUFFER,在调用glReadPixels的时候,这些像素就可以被重定向到PBO当中,而不是再次回到CPU,之后如果同一个缓冲区从GL_PIXEL_PACK_BUFFER绑定点当中解除绑定,然后再去绑定到GL_PIXEL_UNPACK_BUFFER。那么在接下来取调用glTexImage2D的时候,缓冲区中的像素数据将被加载到纹理上
最后看main方法
int main(int argc, char* argv[])
{
//宽度和高度
screenWidth = 800;
screenHeight = 600;
//是否使用PBO
bUsePBOPath = false;
//着色器程序
blurProg = 0;
//速度
speedFactor = 1.0f;
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenWidth,screenHeight);
//创建window
glutCreateWindow("Pix Buffs");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutKeyboardFunc(ProccessKeys);
//初始化环境
SetupRC();
glutMainLoop();
ShutdownRC();
return 0;
}
顶点着色器程序
uniform mat4 mvpMatrix;
attribute vec3 vVertex;
attribute vec2 texCoord0;
varying vec2 vTexCoord;
void main()
{
vTexCoord = texCoord0;
gl_Position = mvpMatrix * vec4(vVertex, 1.0);
}
片元着色器
//将6个纹理进行采样并且对结果进行平均
varying vec2 vTexCoord;
uniform sampler2D textureUnit0;
uniform sampler2D textureUnit1;
uniform sampler2D textureUnit2;
uniform sampler2D textureUnit3;
uniform sampler2D textureUnit4;
uniform sampler2D textureUnit5;
void main()
{
// 0 is the newest image and 5 is the oldest
vec4 blur0 = texture2D(textureUnit0, vTexCoord);
vec4 blur1 = texture2D(textureUnit1, vTexCoord);
vec4 blur2 = texture2D(textureUnit2, vTexCoord);
vec4 blur3 = texture2D(textureUnit3, vTexCoord);
vec4 blur4 = texture2D(textureUnit4, vTexCoord);
vec4 blur5 = texture2D(textureUnit5, vTexCoord);
vec4 summedBlur = blur0 + blur1 + blur2 +blur3 + blur4 + blur5;
gl_FragColor = summedBlur/6.0;
}
效果图