2017-08-28 129 views
0

我嘗試對我的對象(約10個對象)產生一些模糊效果。但是,當我應用效果並移動對象時,它非常緩慢。例如,當我翻譯的對象,它不是同步的九個對象移動,但最後放緩的二分之一翻譯太前...opengl es 2.0加速着色器效果

這裏是我嘗試應用着色器(着色器來自這個網站: 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

你有很多冗餘的OpenGL調用; glUseProgram,glGetUniformLocation,glClearColor和glClear。擺脫他們,並告訴你的結果。

相關問題