简体   繁体   English

Android-OpenGL ES:如何对三角形进行纹理处理

[英]Android - OpenGL ES: how to texture a triangle

I'm learning OpenGL ES. 我正在学习OpenGL ES。 When I'm using texture for triangle, I meet error. 当我为三角形使用纹理时,遇到错误。 Here is my code: 这是我的代码:

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

        }

    }

}

I think I have met error in function onDrawFrame and in block code try-catch . 我认为我在函数onDrawFrame和块代码try-catch遇到了错误。 Who can verify it for me and teach me how to correct it, please. 谁能为我验证它并教我如何更正它。

thanks :) 谢谢 :)

I'm not a great expert in OpenGL but as far as I can see here are some mistakes in yor code. 我不是OpenGL方面的专家,但据我所知,这里是您的代码中的一些错误。 At first it's a wrong size for byteBuffer 最初,byteBuffer的大小错误

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

For your vertices array your need 18 * 4 byte size. 对于您的vertices数组,您需要18 * 4字节大小。 18 is the number of floats in the vertices and 4 is number of bytes for every float in tha array. 18是vertices的浮点数,4是tha数组中每个浮点的字节数。

In the second why do you think your error in try-catch? 在第二篇文章中,您为什么认为尝试捕获有错误? Look for the error line in the log in IDE. 在IDE中的日志中查找错误行。 Post it here and we can see the problem. 张贴在这里,我们可以看到问题。 bobrgb888.png file must be present in assets folder. 资产文件夹中必须存在bobrgb888.png文件。

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

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