OpenGL ES and Google Cardboard for iOS Post Processing Problems

Hello, I am a complete beginner learning to use OpenGL ES for Google Cardboard on iOS. I am trying to edit the supplied sample code (“Treasure Hunt”) to introduce a blur that can be toggled between the left and right eyes, a first step towards creating a bare bones visual therapy tool.

I’ve added a trigger event counter that calls an edited renderer based on which eye is currently being rendered (kGVRLeftEye or Right), created a texture in that new renderer, rendered the scene as usual, then used glCopyTexImage2D to save the viewport image to that texture. Added a new vertex and fragment shader to apply the blur, but my output does nothing but take the capture texture and spread it across both viewports.

I’ve searched through forums and revisited tutorials, but I know I’m still missing something that is likely glaringly obvious. I feel sufficiently stupid, and would be grateful for any advice that you may be able to give me.

Here are some sections of the code:

Vertex Shader -

"#version 300 es
"
    "
"
    "precision mediump float;
"
    "
"
    "in vec2 position;"
    "in vec2 texCoords; 
"
    "out vec2 TexCoords;
"
    "void main(void) { 
"
    "   gl_Position = vec4(position, 0.0, 1.0); 
"
    "   TexCoords = texCoords; 
"
    "}
";

Fragment Shader -

"#version 300 es
"
    "
"
    "precision mediump float;
"
    "
"
    "in vec2 TexCoords;
"
    "out vec4 FragColor;
"
    "uniform sampler2D image;
"
    "uniform bool horizontal;
"
    "const float weight[5] = float[5](0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
"
    "
"
    "
"
    "void main()
"
    "{
"
    "   vec2 tex_offset = vec2(0.00111607, 0.00096525);
"
    "   vec3 result = texture(image, TexCoords).rgb * weight[0];
"
    "   if(horizontal)
"
    "   {
"
    "       for(int i = 1; i < 5; ++i)
"
    "       {
"
    "           result += texture(image, TexCoords + vec2(tex_offset.x * float(i), 0.0)).rgb * weight[i];
"
    "           result += texture(image, TexCoords - vec2(tex_offset.x * float(i), 0.0)).rgb * weight[i];
"
    "       }
"
    "   }
"
    "   else
"
    "   {
"
    "       for(int i = 1; i < 5; ++i)
"
    "       {
"
    "           result += texture(image, TexCoords + vec2(0.0, tex_offset.y * float(i))).rgb * weight[i];
"
    "           result += texture(image, TexCoords - vec2(0.0, tex_offset.y * float(i))).rgb * weight[i];
"
    "       }
"
    "   }
"
    "FragColor = vec4(result, 1.0);
"
    "}
";

Renderer -

GLuint blurTex;                       
    glGenTextures(1, &blurTex);

(render scene as usual)

(texture capture & blur)


    glReadBuffer(GL_BACK);
    
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, _view_width, _view_height, 0);
    
    glClearColor(0.0f, 0.0f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_SCISSOR_TEST);
    
    glViewport(0, 0, _view_width, _view_height);
    glScissor(0, 0, _view_width, _view_height);
    
    glGenBuffers(1, &_blur_vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, _blur_vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, 4, textureVertices, GL_STREAM_DRAW);

    const GLuint blur_v_vertex_shader = LoadShader(GL_VERTEX_SHADER, kBlurVertexShaderString);
    const GLuint blur_fragment_shader = LoadShader(GL_FRAGMENT_SHADER, kBlurFragmentShaderString);
   
    _blur_cube_program = glCreateProgram();
    glAttachShader(_blur_cube_program, blur_v_vertex_shader);
    glAttachShader(_blur_cube_program, blur_fragment_shader);

    glLinkProgram(_blur_cube_program);
    
    glActiveTexture(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, blurTex);            // Bind The Texture
    
    glUseProgram(_blur_cube_program);
    
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    
    _blur_texcoords = glGetAttribLocation(_blur_cube_program, "texCoords");
    _blur_position = glGetAttribLocation(_blur_cube_program, "position");
    
    glVertexAttribPointer(_blur_texcoords, 2, GL_FLOAT, 0, 0, textureVertices);
    glEnableVertexAttribArray(_blur_texcoords);
    glVertexAttribPointer(_blur_position, 2, GL_FLOAT, 0, 0, squareVertices);
    glEnableVertexAttribArray(_blur_position);
    
    glDrawArrays(GL_ARRAY_BUFFER, 0, 4);
    
    glDisableVertexAttribArray(_blur_texcoords);
    glDisableVertexAttribArray(_blur_position);

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.