安卓OpenGl如何绘制矩形

3
我想使用OpenGL画一个矩形。
package jnidemo.example.com.openglsquare;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

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


public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MySurfaceView mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView);
    }

    class MySurfaceView extends GLSurfaceView {
        public MySurfaceView(Context context) {
            super(context);
            setRenderer(new MyRenderer());
        }
    }

    class MyRenderer implements GLSurfaceView.Renderer {
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            gl.glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);
            float aspect = (float)width / height;
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glFrustumf(-aspect, aspect, -1.0f, 1.0f, 1.0f, 10.0f);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

            Square square = new Square();
            square.draw(gl);
        }
    }
}

class Square{
    FloatBuffer vertexbuffer;
    ByteBuffer indicesBuffer;

    float vetices[] = {
        //-0.5f,0.5f,0.0f,
        //0.5f,0.5f,0.0f,
        //0.5f,-0.5f,0.0f,
        //-0.5f,0.5f,0.0f

        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f, 1.0f,
        1.0f, 1.0f
    };

    byte indices[] = {0,1,2,2,1,3};

    public Square(){

        ByteBuffer byteBuffer=ByteBuffer.allocateDirect(vetices.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexbuffer = byteBuffer.asFloatBuffer();
        vertexbuffer.put(vetices);
        vertexbuffer.position(0);

        indicesBuffer = ByteBuffer.allocateDirect(indices.length);
        //indicesBuffer.order(ByteOrder.nativeOrder());
        indicesBuffer.put(indices);
        indicesBuffer.position(0);
    }

    public void draw(GL10 gl){
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexbuffer);

        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indicesBuffer);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

但屏幕上没有显示任何内容。 我不想使用任何颜色。我只想画一个简单的正方形或矩形,以便了解简单的OpenGL ES绘图机制。 我不知道问题出在哪里。
2个回答

5
public void setVerticesAndDraw(Float value, GL10 gl, byte color) {
    FloatBuffer vertexbuffer;
    ByteBuffer indicesBuffer;
    ByteBuffer mColorBuffer;

    byte indices[] = {0, 1, 2, 0, 2, 3};

    float vetices[] = {
        -value, value, 0.0f,
        value, value, 0.0f,
        value, -value, 0.0f,
        -value, -value, 0.0f
    };

    byte colors[] = {
        color, color, 0, color,
        0, color, color, color,
        0, 0, 0, color,
        color, 0, color, maxColor
    };

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vetices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexbuffer = byteBuffer.asFloatBuffer();
    vertexbuffer.put(vetices);
    vertexbuffer.position(0);

    indicesBuffer = ByteBuffer.allocateDirect(indices.length);
    indicesBuffer.put(indices);
    indicesBuffer.position(0);

    mColorBuffer = ByteBuffer.allocateDirect(colors.length);
    mColorBuffer.put(colors);
    mColorBuffer.position(0);

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

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexbuffer);
    gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, mColorBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indicesBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

你可以这样调用该方法:

Square square = new Square();
//square.draw(gl);

square.setVerticesAndDraw(0.8f, gl, (byte) 255);

square.setVerticesAndDraw(0.7f, gl, (byte) 150);
square.setVerticesAndDraw(0.6f, gl, (byte) 100);
square.setVerticesAndDraw(0.5f, gl, (byte) 80);
square.setVerticesAndDraw(0.4f, gl, (byte) 50);

您将看到这样一个很酷的图片:

在此输入图像描述


1
什么是maxColor? - e-info128

-1

你可以使用以下代码... 我已经多次用它作为参考...

 public Square(){

        ByteBuffer byteBuffer=ByteBuffer.allocateDirect(vetices.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexbuffer=byteBuffer.asFloatBuffer();
        vertexbuffer.put(vetices);
        vertexbuffer.position(0);


        indicesBuffer=ByteBuffer.allocateDirect(indices.length);
//        indicesBuffer.order(ByteOrder.nativeOrder());
        indicesBuffer.put(indices);
        indicesBuffer.position(0);

    }


    public void draw(GL10 gl){

//        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//        gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexbuffer);
//
//        gl.glDrawElements(GL10.GL_TRIANGLES,indices.length,GL10.GL_UNSIGNED_BYTE,indicesBuffer);
//
//        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);





        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexbuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES,indices.length,GL10.GL_UNSIGNED_BYTE,indicesBuffer);
        // Draw the primitives from the vertex-array directly
//        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);


    }



}

希望它能对你有所帮助...


1
如果您想以相同的方式应用颜色,可以创建一个颜色数组,并使用glColorPointer来应用颜色......并在应用您的Gl_Color_Array之前启用客户端状态... - RajSharma
一个简单的问题...既然我们已经有了一个正方形类,我想再画一个不同顶点的正方形。这可能吗? - user4595591
你可以创建一个单独的函数,并且可以像以下代码一样指定各种顶点值和颜色值... - RajSharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接