简体   繁体   中英

Android test with OpenGL ES Shaders

I have written a test class that should only paint the application icon into the screen. So far no luck. What I am doing wrong?

public class GLTester
{


void test(final Context context)
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon, options);

    setupGLES();
    createProgram();
    setupTexture();
    draw();
}


void draw()
{
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    glUseProgram(glProgramHandle);

}
}

A couple of things.

I presume your squareVertices buffer is supposed to contain 4 vec3s. But your shader is setup for vec4s. Maybe this is ok, but it seems odd to me.

You also are not setting up any sort of perspective matrix with glFrustum or glOrtho, and are also not setting up any sort of viewing matrix with something like Matrix.setLookAtM. You should always try to keep the vertex pipeline in mind as well. Look at slide 2 from this lecture https://wiki.engr.illinois.edu/download/attachments/195761441/3-D+Transformational+Geometry.pptx?version=2&modificationDate=1328223370000

I think what is happening is your squareVertices are going through this pipeline and coming out on the other side as pixel coordinates. So your image is probably a very tiny spec in the corner of your screen, since you are using vertices from -1.0 to 1.0.

As a shameless sidenote, I posted some code on SourceForge that makes it possible to work on and load shaders from a file in your assets folder and not have to do it inside your .java files as strings. https://sourceforge.net/projects/androidopengles/ Theres an example project in the files section that uses this shader helper.

I hope some part of this rambling was helpful. :)

It looks pretty good, but I see that for one thing you are calling glUniform inside setupTexture, while the shader is not currenty bound. You should only call glUniform after calling glUseProgram.

I don't know if this is the problem, cause I would guess that it would probably default to 0 anyway, but I don't know for sure.

Other than that, you should get familiar calling glGetError to check if there are any error conditions pending.

Also, when creating shaders, its good habit to check their success status with glGetShader(GL_COMPILE_STATUS), also glGetShaderInfoLog if the compile fails, and similar for programs with glGetProgram/glGetProgramInfoLog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM