我尝试对我的物体(大约10个物体)产生一些模糊效果。但是当我应用效果并移动物体时,它非常慢。例如,当我翻译对象时,九个对象移动并不是同步的,但是在翻译之前,最后一个延迟了一秒...
这是我尝试应用着色器的地方(着色器来自此网站:https://github.com/Jam3/glsl-fast-gaussian-blur):
gettimeofday(&t2, &tz);
deltatime = (float)(t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6);
t1 = t2;
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.f, 1.f);
glUseProgram(shaderId);
glUniformMatrix4fv(glGetUniformLocation(shaderId, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderId, "view"), 1, GL_FALSE, glm::value_ptr(view));
//draw all objects
_transform->drawAll(shaderId);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLfloat dir[] = {0.0f, 0.0f};
GLfloat sizeFbo[] = {float(width), float(height)};
for(int i = 0; i < valueSigma + 1; i++){
float timeBlur = sin(deltatime) * 0.5f + 0.5f;
float resBlur = (valueSigma - i) * timeBlur ;
glBindFramebuffer(GL_FRAMEBUFFER, fbo[1]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.f, 1.f);
glUseProgram(blurShaderId);
if(i == 0){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex[0]);
}
quadForm->draw();
auto resolution = glGetUniformLocation(blurShaderId, "resolution");
if(resolution < 0){
std::cerr << "failed resolution" << std::endl;
}
auto direction = glGetUniformLocation(blurShaderId, "direction");
if(direction < 0)
std::cerr << "failed direction" << std::endl;
if(i % 2 == 0){
dir[0] = resBlur;
dir[1] = 0.0f;
glUniform2fv(direction, 2, dir);
}
else{
dir[0] = 0.0f;
dir[1] = resBlur;
}
glUniform2fv(resolution, 2, sizeFbo);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.f, 1.f);
glUseProgram(blurShaderId);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex[1]);
quadForm->draw();
// dir[0] = dir[1] = 0.0f;
glUniform2fv(direction, 2, dir);
glUniform2fv(resolution, 2, sizeFbo);
}
答案 0 :(得分:0)
你有很多冗余的opengl调用; glUseProgram,glGetUniformLocation,glClearColor,可能还有glClear。摆脱它们并告诉你的结果。