android OpenGl如何绘制矩形

android OpenGl如何绘制矩形,第1张

概述我使用opengl绘制一个矩形.packagejnidemo.example.com.openglsquare;importandroid.content.Context;importandroid.opengl.GLSurfaceView;importandroid.support.v7.app.ActionBarActivity;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.

我使用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);    }}

但是屏幕上没有任何显示.
我不想使用任何颜色,我只想绘制一个简单的正方形或矩形,这样我就可以对简单的gles绘制机制有所了解.

我不知道是什么问题.

解决方法:

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

您可以像这样调用此方法

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

您将看到一个很酷的图像,如下所示.

总结

以上是内存溢出为你收集整理的android OpenGl如何绘制矩形全部内容,希望文章能够帮你解决android OpenGl如何绘制矩形所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1079024.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存