import android.app.Activity;
import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
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 Test2 extends Activity {
GLSurfaceView glSurfaceView;
//定点着色器
public static String VL = "attribute vec4 vPosition;\n" +
"uniform mat4 u_Matrix;"
+ "void main() {\n"
+ " gl_Position = u_Matrix*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[120*3*3];
float[] projectMatrix = new float[16];
private int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMatricHandle;
public RenderListener() {
float x = 0;
float y= 0;
float z = 0;
float r = 1;
int index=-1;
for(int i=3;i<=360;i=i+3){
double d1 = i*Math.PI/180;
verticals[++index] = 0;
verticals[++index] = 0;
verticals[++index] = 0;
verticals[++index] = (float) (x+ r*Math.cos(d1-3*Math.PI/180));
verticals[++index] = (float) (y+ r*Math.sin(d1-3*Math.PI/180));
verticals[++index] = 0;
verticals[++index] = (float) (x+ r*Math.cos(d1));
verticals[++index] = (float) (y+ r*Math.sin(d1));
verticals[++index] = 0;
}
verticalsBuffer = ByteBuffer.allocateDirect(verticals.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(verticals);
verticalsBuffer.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");
mMatricHandle = GLES20.glGetUniformLocation(mProgram,"u_Matrix");
//处理变形
final float aspectRatio = width>height?width*1f/height:height*1f/width;
if(width>height){
Matrix.orthoM(projectMatrix,0,-aspectRatio,aspectRatio,-1f,1f,-1f,1f);
}else{
Matrix.orthoM(projectMatrix,0,-1f,1f,-aspectRatio,aspectRatio,-1f,1f);
}
}
@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.glUniformMatrix4fv(mMatricHandle,1,false,projectMatrix,0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 120*3);
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
opengl es2.0绘制圆形
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 本文是关于OpenGL ES的系统性学习过程,记录了自己在学习OpenGL ES时的收获。这篇文章的目标是学...
- 先上效果图 我是android opengl es的初学者,有很多东西还不懂,仍在学习;这里实现全景图浏览的一个思...
- Android使用OpenGL ES2.0绘制3D图像或者加载3D模型时,为了达到立体效果往往需要设置视见转换矩阵...
- 对最近一段时间学习OpenGL ES一个阶段性的成果展示,对OpenGL ES从茫茫然,漫无目的的看资料到找到头绪...