android OpenGl How to draw a rectangle

最后都变了- 提交于 2020-07-17 17:33:25

问题


I wnat to draw a rectangle using 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);
//        setContentView(R.layout.activity_main);

        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);


    }



}

But nothing appears on teh screen. I dont want to use any color I just want to draw a simple square or rectangle so that I can get some idea of simple gles drawing mechanism.

I don't know what is the problem.


回答1:


 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[] = //3
                    {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);

        }

And you can call this method like this---

 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);

You will see a cool image like below....

enter image description here




回答2:


you can use below code... I have used it many times for reference....

 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);


    }



}

I hope it will help you....



来源:https://stackoverflow.com/questions/28711850/android-opengl-how-to-draw-a-rectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!