import android.app.Activity;
import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.ViewGroup;
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;
/**
* Created by Administrator on 2016/8/31 0031.
*/
public class OPENGLTestActivity extends Activity {
GLSurfaceView glSurfaceView;
//定点着色器
public static String VL = "attribute vec4 vPosition;\n"
+ "void main() {\n"
+ " gl_Position = vPosition;\n"
+ "}";
//片段着色器
public static String FL = "precision mediump float;\n" +
"uniform vec4 u_Color;"
+ "void main() {\n"
+ " gl_FragColor = u_Color;\n"
+ "}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
setContentView(glSurfaceView);
//设置opengl es版本
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new RenderListener());
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
class RenderListener implements GLSurfaceView.Renderer {
FloatBuffer verticalsBuffer;
//多边形定点
float[] verticals = new float[]{
-1, -1, 0.0f, // top
-1f, 1, 0.0f, // bottom left
1f, 1, 0.0f, // bottom right
1,-0.5f,0.0f,
};
ShortBuffer verticalsIndexBuffer;
//每个三角形的定点编号,多边形有两个三角形组成
short[] verticalsIndex = new short[]{
0, 1, 2,
0, 2, 3
};
private int mProgram;
private int mPositionHandle;
private int mColorHandle;
public RenderListener() {
verticalsBuffer = ByteBuffer.allocateDirect(verticals.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(verticals);
verticalsBuffer.position(0);
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (对应顺序的坐标数 * 2)short是2字节
verticalsIndex.length * 2);
dlb.order(ByteOrder.nativeOrder());
verticalsIndexBuffer = dlb.asShortBuffer();
verticalsIndexBuffer.put(verticalsIndex);
verticalsIndexBuffer.position(0);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
mProgram = GLES20.glCreateProgram();
int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, VL);
GLES20.glCompileShader(vertexShader);
int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, FL);
GLES20.glCompileShader(fragmentShader);
GLES20.glAttachShader(mProgram,vertexShader);
GLES20.glAttachShader(mProgram,fragmentShader);
GLES20.glLinkProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
mColorHandle = GLES20.glGetUniformLocation(mProgram,"u_Color");
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false,
12, verticalsBuffer);
GLES20.glUniform4fv(mColorHandle,1, new float[]{0,1,1,5},0);
//GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, verticalsIndex.length, GLES20.GL_UNSIGNED_SHORT, verticalsIndexBuffer);
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
}
opengl es2.0绘制多边形
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 基础图形绘制 之前讲解过,OpenGL ES中,只提供了3种基本图形:点、线、三角形。而其他我们熟知的图形,都是基...
- 在上一篇OpenGL绘制甜甜圈、正反面剔除、深度测试问题中,文末还遗留一个问题未解决,在解决之前,先说说这种现象产...
- 刚开始接触arcgis for js 不久,画个矩形都折腾半天,现在汇总一下,方便以后使用,也算是总结一下 绘制文字