尝试在设备上绘制纹理三角形失败,但模拟器工作。 为什么?

我有一系列OpenGL-ES调用,可以在模拟器(2.0.1)上正确渲染三角形并使用alpha混合对其进行纹理处理。 当我在实际设备上启动相同的代码时(Droid 2.0.1),我所得到的只是白色方块。

这表明纹理没有加载,但我无法弄清楚它们为什么不加载。 我的所有纹理都是带有alpha通道的32位PNG,在res / raw下,因此它们并未根据sdk文档进行优化。

以下是我如何加载我的纹理:

private void loadGLTexture(GL10 gl, Context context, int reasource_id, int texture_id)
{
    //Get the texture from the Android resource directory
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions);

    //Generate one texture pointer...
    gl.glGenTextures(1, textures, texture_id);
    //...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[texture_id]);

    //Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    //Clean up
    bitmap.recycle();
}

以下是我如何渲染纹理:

        //Clear
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    //Enable vertex buffer
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
        //Push transformation matrix
        gl.glPushMatrix();
        //Transformation matrices
        gl.glTranslatef(x, y, 0.0f);
        gl.glScalef(scalefactor, scalefactor, 0.0f);
        gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
        //Bind the texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[textureid]);
        //Draw the vertices as triangles
        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
        //Pop the matrix back to where we left it
        gl.glPopMatrix();
            //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

以下是我启用的选项:

        gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
        gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);

编辑:我只是试图提供一个BitmapOptions到BitmapFactory.decodeResource()调用,但这似乎并没有解决问题,尽管手动设置相同的首选配置,密度和targetdensity。

Edit2:按照要求,这里是模拟器工作的屏幕截图。 显示下面的三角形时会显示一个圆形纹理,透明度正在工作,因为您可以看到黑色背景。

删除了死的ImageShack链接

这是一个关于机器人使用完全相同的代码的镜头:

删除了死的ImageShack链接

Edit3:这是我的BitmapOptions,更新了上面的调用,现在我正在调用BitmapFactory,结果仍然如下:sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;

sBitmapOptions.inDensity = 160;
sBitmapOptions.inTargetDensity = 160;
sBitmapOptions.inScreenDensity = 160;
sBitmapOptions.inDither = false;
sBitmapOptions.inSampleSize = 1;
sBitmapOptions.inScaled = false;

这里是我的顶点,纹理坐标和索引:

    /** The initial vertex definition */
    private static final float vertices[] = { 
                                        -1.0f, -1.0f, 0.0f, 
                                        1.0f, -1.0f, 0.0f,
                                        -1.0f, 1.0f, 0.0f,
                                        1.0f, 1.0f, 0.0f
                                                        };
    /** The initial texture coordinates (u, v) */   
private static final float texture[] = {            
                                    //Mapping coordinates for the vertices
                                    0.0f, 1.0f,
                                    1.0f, 1.0f,
                                    0.0f, 0.0f,
                                    1.0f, 0.0f
                                    };
/** The initial indices definition */   
private static final byte indices[] = {
                                        //Faces definition
                                        0,1,3, 0,3,2
                                        };

无论如何,一旦将纹理加载到OpenGL ES中,就可以转储纹理的内容? 也许我可以将模拟器的加载纹理与实际设备的加载纹理进行比较?

我曾尝试使用不同的纹理(默认的Android图标),同样,它对模拟器工作正常,但无法在实际手机上呈现。

编辑4:尝试切换,当我做纹理加载。 没有运气。 尝试使用0到glGenTextures的常量偏移量,没有改变。

有什么我使用的模拟器支持实际的手机不?

Edit5:下面的每个Ryan,我将我的纹理从200x200调整到256x256,并且问题没有解决。

编辑:按照要求,将上述呼叫添加到glVertexPointer和glTexCoordPointer。 另外,这里是vertexBuffer,textureBuffer和indexBuffer的初始化:

ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
loadGLTextures(gl, this.context);

我也是Android OpenGL ES开发新手。 我拥有里程碑(Droid欧元版)。

从我目前看到的情况来看,在我的里程碑上从不同的书籍/网站上发布了几个教程应用程序,似乎这款手机的处理方式与迄今为止发布的所有其他Android手机不同。

我认为它与设备支持的OpenGL扩展有关。

检查这个链接的支持的扩展名列表,我敢肯定,代码大师会发现为什么这个droid正在以这种奇怪的方式处理opengl。

http://www.rbgrn.net/content/345-hands-on-motorola-droid-opengl-es-specs

希望这个对你有帮助


编辑:

我调整了manifest.xml,并在使用时注意到了

<uses-sdk android:minSdkVersion="6"/>

资源根本不加载。

使用时

<uses-sdk android:minSdkVersion="3"/>

资源加载正常。

所以我切换回

<uses-sdk android:minSdkVersion="6"/>

并改变了位图加载的方式。

尝试替换:

  //Get the texture from the Android resource directory 
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions); 

与:

InputStream is = context.getResources().openRawResource(your.resource.there);
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(is);

    } finally {
        //Always clear and close
        try {
            is.close();
            is = null;
        } catch (IOException e) {
        }
    }

使用该代码纹理在我的里程碑上加载正常,希望为您的工作提供帮助!


当我从SDK 3移动到4时,我遇到了这个问题。我的纹理将显示在模拟器上,但不会显示在真实设备上(Motorola Milestone / Droid)。

你所有的纹理必须是二进制的幂。 我的是256x256。 然而纹理不显示的原因是操作系统将它们从二进制功率大小缩放到Milestone的高密度屏幕上显示。

解决方案很简单 - 只需将纹理移动到drawable-nodpi目录,操作系统在加载它们时不会缩放它们。


请确保使用适当的BitmapConfig加载位图,而不是在后台缩放图像,因为会发生这种情况。

另外,是否有一个原因,你存储你的PNG在/原/而不是/ drawable /?

链接地址: http://www.djcxy.com/p/33893.html

上一篇: Trying to draw textured triangles on device fails, but the emulator works. Why?

下一篇: Advice on speeding up OpenGL ES 1.1 on the iPhone