【问题标题】:Android - OpenGL ES: how to texture a triangleAndroid - OpenGL ES:如何纹理三角形
【发布时间】:2012-02-18 16:36:51
【问题描述】:

我正在学习 OpenGL ES。当我将纹理用于三角形时,我遇到了错误。这是我的代码:

package com.test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.os.Bundle;

public class TexttureTriangleTest extends Activity{

    GLSurfaceView glView;
    ByteBuffer byteBuffer;
    FloatBuffer vertices;
    AssetManager assetManager;

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        assetManager = getAssets();

        int VERTEX_SIZE = (2+2)*4;
        byteBuffer = ByteBuffer.allocateDirect(3*VERTEX_SIZE);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] {   0.0f, 0.0f, 1, 0, 0, 1,
                                     319.0f, 0.0f, 0, 1, 0, 1,
                                     160.0f, 479.0f, 0, 0, 1, 1});
        vertices.flip();

        glView =  new GLSurfaceView(this);
        glView.setRenderer(new Render());
        setContentView(glView);
    }

    class Render implements Renderer{

        @Override
        public void onDrawFrame(GL10 gl) {

            try { //I think error in this block of code

                Bitmap bitmap = BitmapFactory.decodeStream(assetManager.open("bobrgb888.png"));
                int textureIds[] = new int[1];
                gl.glGenTextures(1, textureIds, 0);
                int textureId = textureIds[0];
                gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
                GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
                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_NEAREST);
                gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
                bitmap.recycle();

            } catch (IOException e) {
                throw new RuntimeException("couldn't load asset!");
            }

            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(340, 0, 420, 0, 0, 0);

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

            int VERTEX_SIZE = (2+2)*4;
            vertices.position(0);
            gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
            vertices.position(2);
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);

            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);




        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            // TODO Auto-generated method stub

        }

    }

}

我想我在函数onDrawFrame 和块代码try-catch 中遇到了错误。谁能帮我验证一下,教我改正方法。

谢谢:)

【问题讨论】:

    标签: java android opengl-es rendering


    【解决方案1】:

    我不是 OpenGL 方面的专家,但据我所知,这里的代码中有一些错误。 起初它的字节缓冲区大小错误

    int VERTEX_SIZE = (2+2)*4;
    byteBuffer = ByteBuffer.allocateDirect(3*VERTEX_SIZE);
    

    对于您的 vertices 数组,您需要 18 * 4 字节大小。 18 是 vertices 中的浮点数,4 是 tha 数组中每个浮点数的字节数。

    第二个为什么你认为你在 try-catch 中的错误? 在 IDE 的日志中查找错误行。把它贴在这里,我们可以看到问题。 bobrgb888.png 文件必须存在于 assets 文件夹中。

    【讨论】:

    • 你能更详细地解释为什么18。我认为2+2 因为一个是普通坐标,一个是纹理坐标。
    • 18 只是vertices 的大小,但它对您的程序没有帮助。您需要阅读有关 OpenGL 中的顶点、纹理顶点和面的任何教程或示例。像这样 [link]obviam.net/index.php/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2014-07-08
    • 2013-08-14
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多