0
votes

I want to do image processing on a bitmap I have loaded. I am afraid it will take too long to do anything in normal android on the cpu, and have read that allowing the gpu to do stuff may speed things up substantially. I feel if I could turn the image from rgb to hsv in opengl I could figure out how to do everything I need.

I want to do work in the fragment shader(since that is about pixel color), and since I don't want to transform the image at all I feel the vertex shader is useless for my needs. Do I still need the vertex shader? Read something about a passthrough vertex shader. I read some implementations don't need both. My main points of confusion definitely lie in how opengl works. I have gathered how to form a texture and a framebuffer object. I don't understand the GLUtils.texSubImage2D. what is the diff between that and texImage2d. In any case, per this site (Site 1) and this code (Site 2) they loaded the bitmap using texSubImage2D.

I get how to load a vertex shader and fragment shader. I don't understand what happens after glUseProgram(). Does it run to completion? Do you continually supply it with information to process? how to stop it?

I think you pass in a texture using a uniform sampler2D -- I mean I think this is how to use the texture in the fragment shader.I guess it gets placed into the fragment shader automatically. How all this works is a mystery. I plan on using glReadPixels to get back my bitmap from the texture. when would I call this? the callback functions discussed in android doc only deal with a surfaceview afaik

I plan on using the information from this site (Site3) minus their vertex shader to convert it. I just need to put it all together.

Sorry if this is not enough to go on. BTW I don't want to use any libraries.

I have tried reading about opengl and specifically opengl es 2.0. They all talk about either using a surfaceview ( which I don't want ) or working on many triangles. I do get that I am using opengl for a purpose that opengl was not exactly made for.

This is some code from Site3
So I think tex would be the texture passed via texSubImage2D
hue, no clue.
texture2D() gets the pixel I guess, but where vTextureCoord comes from is a mystery. If I want, say the 3x3 matrix around the pixel do I just subtract and add to vTextureCoord values and keep calling texture2D(). and how to set a pixel a certain color.what is gl_FragColor

uniform sampler2D tex;
uniform vec3 hue;

// Add the two methods here (rgb2hsv and hsv2rgb are at Site 3)

void main() {
    vec4 textureColor = texture2D(tex, vTextureCoord);
    vec3 fragRGB = textureColor.rgb;
    vec3 fragHSV = rgb2hsv(fragRGB).xyz;
    fragHSV.x += hue.x;
    fragHSV.yz *= hue.yz;
    fragHSV.xyz = mod(fragHSV.xyz, 1.0);
    fragRGB = hsv2rgb(fragHSV);
    gl_FragColor = vec4(fragRGB, textureColor.w);
} 
1

1 Answers

0
votes

Do I still need the vertex shader? Read something about a passthrough vertex shader. I read some implementations don't need both.

You have to use both as per the specification, otherwise your program will fail to link:

The following lists some of the conditions that will cause a link error. A vertex shader and a fragment shader are not both present in the program object.

GLUtils.texSubImage2D. what is the diff between that and texImage2d.

glTexSubImage2D uploads a region of data to a currently existing texture. But if you are using it, you have to allocate the texture with some other operation first (e. g. glTexImage2D or glCopyTexImage2D.

I don't understand what happens after glUseProgram(). Does it run to completion? Do you continually supply it with information to process? how to stop it?

It doesn't run anything. The vertex and fragment shaders will be executed when you render something, using glDraw* commands. glUseProgram only sets the program that will be used in some of the operations.

I think you pass in a texture using a uniform sampler2D -- I mean I think this is how to use the texture in the fragment shader.I guess it gets placed into the fragment shader automatically. How all this works is a mystery. I plan on using glReadPixels to get back my bitmap from the texture. when would I call this? the callback functions discussed in android doc only deal with a surfaceview afaik

Samplers are references to Texture Image Units. Basically yes, you pass the corresponding texture image unit number as a uniform. glReadPixels returns the data from the current framebuffer. You would need to call after all the needed rendering operations are done. Although be aware that glReadPixels stalls the pipeline, slowing your program. You could also use it with Pixel Buffer Objects (available in OpenGL ES 3.0+) or, maybe, use EGLImageKHR.

I have tried reading about opengl and specifically opengl es 2.0. They all talk about either using a surfaceview ( which I don't want ) or working on many triangles. I do get that I am using opengl for a purpose that opengl was not exactly made for.

SurfaceView is just a system-level thing you can render into. You don't have to use it, and you can instead set up an off-screen rendering surface with EGL. You'll have to work with triangles (unless can you use OpenGL ES 3.1 and Compute Shaders), though you would only need 2 of them. Image processing is a valid application for OpenGL, it's actually what many of the image processing libs/programs use.

texture2D() gets the pixel I guess, but where vTextureCoord comes from is a mystery.

You would need to pass it as a varying from the vertex shader.

and how to set a pixel a certain color.what is gl_FragColor

gl_FragColor is the output color of the fragment this fragment shader instance is currently working on.

If I want, say the 3x3 matrix around the pixel do I just subtract and add to vTextureCoord values and keep calling texture2D().

Yes, but be aware of filtering, since the nearby texels could affect the texture lookup functions. Newer versions of OpenGL ES versions let you use texelFetch function instead.