简体   繁体   English

使用OpenGL ES着色器进行Android测试

[英]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. 我认为你的squareVertices缓冲区应该包含4个vec3s。 But your shader is setup for vec4s. 但你的着色器是为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. 您也没有使用glFrustum或glOrtho设置任何类型的透视矩阵,也没有使用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 从本讲座看幻灯片2 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. 我认为正在发生的事情是你的squareVertices正在通过这个管道并在另一侧作为像素坐标出现。 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. 因此,您的图像可能是屏幕一角的非常小的图像,因为您使用的是从-1.0到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. 作为一个无耻的旁注,我在SourceForge上发布了一些代码,可以在资源文件夹中的文件中处理和加载着色器,而不必在.java文件中作为字符串进行处理。 https://sourceforge.net/projects/androidopengles/ Theres an example project in the files section that uses this shader helper. https://sourceforge.net/projects/androidopengles/ Theres是一个使用此着色器助手的文件部分中的示例项目。

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. 它看起来很不错,但我发现有一件事你在setupTexture中调用glUniform,而着色器不是当前绑定的。 You should only call glUniform after calling glUseProgram. 你应该在调用glUseProgram后调用glUniform。

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. 我不知道这是不是问题,因为我猜它可能默认为0,但我不确定。

Other than that, you should get familiar calling glGetError to check if there are any error conditions pending. 除此之外,你应该熟悉调用glGetError来检查是否有任何错误条件未决。

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. 此外,在创建着色器时,习惯用glGetShader(GL_COMPILE_STATUS)检查其成功状态,如果编译失败则检查glGetShaderInfoLog,对于具有glGetProgram / glGetProgramInfoLog的程序也类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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