关闭

android opengl 原理及开发(2)绘制基本图形

标签: androidfloatanimationthreadimportclass
2287人阅读 评论(0) 收藏 举报
分类:


工程名称为vertex,标识点的意思,点是3d图形空间最基本元素,三角形的绘制通过指定三个点,线由两个点连接构成,对边型至少由三个vertex构成,

Activity 名为VertexActivity


package com.android.vertex;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import java.io.File;


public class VertexActivity extends Activity {

    VertexView mView;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mView = new VertexView(getApplication());
		setContentView(mView);
    }

    @Override protected void onPause() {
        super.onPause();
        mView.onPause();
    }

    @Override protected void onResume() {
        super.onResume();
        mView.onResume();
    }
}

VertexActivity 创建时会加载
VertexView ,这里要注意activity状态改变时一定要让view状态同步,因为这会影响opengl绘制线程的状态,比如activity pause状态时,GLSurfaceView的绘制线程也回同步

到pause状态。


VertexView 继承GLSurfaceView ,MyRenderer实现GLSurface.renderer接口,负责具体绘制


VertexView.java 代码:


package com.android.vertex;


import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;


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


import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;


import android.opengl.GLES10;


/**
 * An implementation of SurfaceView that uses the dedicated surface for
 * displaying an OpenGL animation.  This allows the animation to run in a
 * separate thread, without requiring that it be driven by the update mechanism
 * of the view hierarchy.
 *
 * The application-specific rendering code is delegated to a GLView.Renderer
 * instance.
 */
class VertexView extends GLSurfaceView {
    private static String TAG = "vertex";
	private MyRenderer mRenderer;
	


    public VertexView(Context context) {
        super(context);
	//set this if you use opengl es version 2.0
        //setEGLContextClientVersion(2);
        mRenderer = new MyRenderer();
        setRenderer(mRenderer);
    }


    private static class MyRenderer implements GLSurfaceView.Renderer {

		private float red = 0.9f;
		private float green = 0.2f;
		private float blue = 0.2f;
		
        public MyRenderer() {
            
        }


        public void onDrawFrame(GL10 gl) {
        	gl.glClearColor(red, green, blue, 1.0f);
        }


        public void onSurfaceChanged(GL10 gl, int width, int height) {
           gl.glViewport(0,0,width,height);
		   gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        }


        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
           
        }

    }

}


实际绘制动作 在MyRederer实现的三个接口函数里,

如果我们想通过按键事件与绘制线程交互可以用上一篇文章提到的queueEvent方法操作绘制线程对象mRederer,

在VertexView类中添加按键事件处理

//comunicate with render thread 
	public boolean onTouchEvent(final MotionEvent event) {
		queueEvent(new Runnable() {
			public void run() {
				mRenderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
			}
		} );


		return true;
	}

MyRenderer类中添加setColor,改变颜色。

public void setColor(float r, float g, float b) {
			red = r;
			green = g;
			blue = b;
		}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1650525次
    • 积分:23353
    • 等级:
    • 排名:第284名
    • 原创:671篇
    • 转载:72篇
    • 译文:7篇
    • 评论:298条
    博客专栏
    最新评论
    database
    voip