2019独角兽企业重金招聘Python工程师标准>>>

这里简单运用之前所学的知识来实现一个对应的立方体:

public class MainActivity extends AppCompatActivity {RecyclerView mRecyclerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intGL();}private GLSurfaceView mSurfaceView;@Overrideprotected void onPause() {super.onPause();if (mSurfaceView != null) {mSurfaceView.onPause();}}@Overrideprotected void onResume() {super.onResume();if (mSurfaceView != null) {mSurfaceView.onResume();}}private void intGL() {mSurfaceView = findViewById(R.id.gl);boolean isSupport=detectOpenGLES30();mSurfaceView.setEGLContextClientVersion(3);mSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);mSurfaceView.setRenderer(new MyRenderer());//RenderMode 有两种,RENDERMODE_WHEN_DIRTY 和 RENDERMODE_CONTINUOUSLY,// 前者是懒惰渲染,需要手动调用 glSurfaceView.requestRender() 才会进行更新,而后者则是不停渲染。mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);}private boolean detectOpenGLES30() {ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);ConfigurationInfo info = am.getDeviceConfigurationInfo();String resulr=Integer.toString(info.reqGlEsVersion, 16);return (info.reqGlEsVersion >= 0x30000);}private static class MyRenderer implements GLSurfaceView.Renderer {private Triangle mSquare;@Overridepublic void onSurfaceCreated(GL10 unused, EGLConfig config) {mSquare = new Triangle();}@Overridepublic void onSurfaceChanged(GL10 unused, int width, int height) {//设置 Screen space 的大小mSquare.onSurfaceChange(width,height);}//绘制的过程其实就是为 shader 代码变量赋值,并调用绘制命令的过程@Overridepublic void onDrawFrame(GL10 unused) {GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);GLES30.glEnable(GLES20.GL_DEPTH_TEST);mSquare.onDraw();}}}

Triangle方法:

public class Triangle {private FloatBuffer vertexBuffer;private FloatBuffer colorBuffer;private ByteBuffer indicateBuffer;private final float[] projectionMatrix = new float[16];private final String vertexShaderCode ="#version 300 es  \n" +"layout(location = 0) in vec4 vPosition;\n" +"layout(location = 1) in vec4 aColor;\n" +"uniform mat4 aMatrix;\n" +"out vec4 vColor;" +"void main() {\n" +"gl_Position = aMatrix*vPosition;\n" +"vColor=aColor;\n" +"}";private final String fragmentShaderCode ="#version 300 es  \n" +"precision mediump float;\n" +"out vec4 fragColor;\n" +"in vec4 vColor;" +"void main() {\n" +"fragColor = vColor;\n" +"}";// number of coordinates per vertex in this arraystatic final int COORDS_PER_VERTEX = 3;static float triangleCoords[] = {0.5f, 0.5f, 0.5f,0.5f, 0.5f, -0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, -0.5f,0.5f, -0.5f, 0.5f,0.5f, -0.5f, -0.5f,-0.5f, -0.5f, 0.5f,-0.5f, -0.5f, -0.5f,};private final int mProgram;// Set color with red, green, blue and alpha (opacity) valuesfloat color[] = {0.0f, 1.0f, 0.0f,0.0f, 1.0f, 0.0f,1.0f, 0.5f, 0.0f,1.0f, 0.5f, 0.0f,1.0f, 0.0f, 0.0f,1.0f, 0.0f, 0.0f,0.0f, 0.0f, 1.0f,1.0f, 0.0f, 1.0f};byte indicates[] = {//上0, 1, 2,1, 2, 3,//下4, 5, 6,5, 6, 7,//左2, 3, 6,3, 6, 7,//右0, 1, 4,1, 4, 5,//前0, 2, 4,2, 4, 6,//后1, 3, 7,1, 7, 5};public static int loadShader(int type, String shaderCode) {// create a vertex shader type (GLES30.GL_VERTEX_SHADER)// or a fragment shader type (GLES30.GL_FRAGMENT_SHADER)int shader = GLES30.glCreateShader(type);// add the source code to the shader and compile itGLES30.glShaderSource(shader, shaderCode);GLES30.glCompileShader(shader);return shader;}public Triangle() {indicateBuffer = ByteBuffer.allocate(indicates.length);indicateBuffer.put(indicates);indicateBuffer.position(0);// initialize vertex byte buffer for shape coordinatesByteBuffer bb = ByteBuffer.allocateDirect(// (number of coordinate values * 4 bytes per float)triangleCoords.length * 4);// use the device hardware's native byte orderbb.order(ByteOrder.nativeOrder());// create a floating point buffer from the ByteBuffervertexBuffer = bb.asFloatBuffer();// add the coordinates to the FloatBuffervertexBuffer.put(triangleCoords);// set the buffer to read the first coordinatevertexBuffer.position(0);ByteBuffer bb1 = ByteBuffer.allocateDirect(// (number of coordinate values * 4 bytes per float)color.length * 4);// use the device hardware's native byte orderbb1.order(ByteOrder.nativeOrder());// create a floating point buffer from the ByteBuffercolorBuffer = bb1.asFloatBuffer();// add the coordinates to the FloatBuffercolorBuffer.put(color);// set the buffer to read the first coordinatecolorBuffer.position(0);int vertexShader = loadShader(GLES30.GL_VERTEX_SHADER,vertexShaderCode);int fragmentShader = loadShader(GLES30.GL_FRAGMENT_SHADER,fragmentShaderCode);// create empty OpenGL ES ProgrammProgram = GLES30.glCreateProgram();// add the vertex shader to programGLES30.glAttachShader(mProgram, vertexShader);// add the fragment shader to programGLES30.glAttachShader(mProgram, fragmentShader);// creates OpenGL ES program executablesGLES30.glLinkProgram(mProgram);}private int mPositionHandle;private int mColorHandle;private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertexprivate final int colorStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertexpublic void onDraw() {// Add program to OpenGL ES environment//        Matrix.translateM(projectionMatrix,0,-0.5f,0f,0f);float[] rotateF = new float[16];Matrix.setIdentityM(rotateF, 0);Matrix.setRotateM(rotateF,0,rotateAgree,0.5f,0.5f,0f);Matrix.multiplyMM(projectionMatrix,0,orthMatrix,0,rotateF,0);rotateAgree+=1;if (rotateAgree >=360) {rotateAgree=0;}GLES30.glUseProgram(mProgram);// get handle to vertex shader's vPosition membermPositionHandle = GLES30.glGetAttribLocation(mProgram, "vPosition");
//        createVertextBuffer();// Enable a handle to th、e triangle verticesGLES30.glEnableVertexAttribArray(mPositionHandle);// Prepare the triangle coordinate dataGLES30.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,GLES30.GL_FLOAT, false,0, vertexBuffer);// get handle to fragment shader's vColor membermColorHandle = GLES30.glGetAttribLocation(mProgram, "aColor");int aMatrixPos = GLES30.glGetUniformLocation(mProgram, "aMatrix");GLES30.glUniformMatrix4fv(aMatrixPos,1,false,projectionMatrix,0);// Set color for drawing the triangle
//        GLES30.glVertexAttrib4fv(mColorHandle,  color, 0);GLES30.glEnableVertexAttribArray(mColorHandle);GLES30.glVertexAttribPointer(mColorHandle, 3,GLES30.GL_FLOAT, false,0, colorBuffer);// Draw the triangleGLES30.glDrawElements(GLES30.GL_TRIANGLES, indicates.length,GLES30.GL_UNSIGNED_BYTE, indicateBuffer);// Disable vertex arrayGLES30.glDisableVertexAttribArray(mPositionHandle);}int rotateAgree=0;float[] orthMatrix = new float[16];public void onSurfaceChange(int width, int height) {GLES30.glViewport(0, 0, width, height);final float aspectRatio = width > height ?((float) width / (float) height): ((float) height / (float) width);//正交投影Matrix.setIdentityM(orthMatrix, 0);if (width > height) {Matrix.orthoM(orthMatrix,0,-aspectRatio,aspectRatio,-1f,1f,-1f,1f);}else {Matrix.orthoM(orthMatrix, 0, -1f, 1f,-aspectRatio, aspectRatio, -1f, 1f);}}private void createVertextBuffer() {int[] value = new int[1];GLES30.glGenBuffers(1, value, 0);GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, value[0]);GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, vertexBuffer.capacity() * 4, vertexBuffer, GLES30.GL_STATIC_DRAW);}
}

代码效果如下所示:

gif图效果不好,手机上看还是很酷炫的,哈哈哈

代码地址:

https://github.com/JerryChan123/LearnOEL/tree/gl30 (opengl for cube v2为提交信息)

转载于:https://my.oschina.net/u/3863980/blog/1838532

opengl es3.0学习篇七:使用opengl绘制一个立方体相关推荐

  1. opengl es3.0学习篇八:纹理

    2019独角兽企业重金招聘Python工程师标准>>> 学习内容来源and参考 opengl es 3.0编程指南 https://www.jianshu.com/p/4d8d352 ...

  2. OpenGL ES3 0实现简单粒子火焰效果

    通过粒子系统来实现火焰效果,基本思想是把一团火焰看成是由一颗颗有其生命期的粒子组成,粒子在不停的产生直至消亡从而产生升腾的火焰效果.通过生成每个粒子的坐标,每颗火焰粒子是一个矩形,而这个矩形由两个三角 ...

  3. Android使用NDK OpenGL ES3.0绘制一个三角形

    Android使用NDK  OpenGL ES3.0绘制一个三角形 [尊重原创,转载请注明出处]https://blog.csdn.net/guyuealian/article/details/820 ...

  4. NDK OpenGL ES 3.0 开发(一):绘制一个三角形

    该原创文章首发于微信公众号:字节流动 什么是 OpenGLES OpenGLES 全称 OpenGL for Embedded Systems ,是三维图形应用程序接口 OpenGL 的子集,本质上是 ...

  5. OpenGL ES 2.0学习之路---2.Hello Triangel:一个OpenGL ES 2.0例子

    该例子主要包括以下内容: 使用EGL创造一个显示渲染窗口平面 装载顶点和片段着色器 创造一个项目,联系顶点和片段着色器,链接项目 设置视窗 清除颜色缓冲区 最基本的渲染 在EGL窗口显示颜色缓冲区的内 ...

  6. Java学习篇七——循环结构之 for 语句

    写在前面:本人是借助两本参考书自学的,其中部分例子和语句均是来自参考书.第一本:<Java 编程指南>,[美] Budi Kurniawan 著,闫斌 贺莲 译.第二本:<第一行代码 ...

  7. MaterialDesign学习篇(七),CardView卡片式布局的使用

    什么是CardView CardView顾名思义就是一个卡片型的View,它是在Android5.0引入的一个控件,作为一个容器使用,它本身继承于FrameLayout,可以说它的使用和FrameLa ...

  8. Flexe2.0 学习笔记二(利用PopUpManager实现一个组件登录窗体)

    通过对代码的学习实现了一个登录窗体的显示,还未涉及到参数的传递,这次的问题是窗体位置的控制需要如何解决?努力学习ing..... 代码下载 问题已解决 只要在 helpWindow = TitleWi ...

  9. OpenGl 绘制一个立方体

    https://www.cnblogs.com/icmzn/p/5049768.html https://blog.csdn.net/auccy/article/details/82392921

最新文章

  1. 禅道8.2.4 腾讯云迁移至VM
  2. 近世代数--子环--怎么判断是不是子环?
  3. 智能卡门禁管理系统_综合门禁管理信息系统相关技术及未来准备
  4. googleearthpro打开没有地球_嫦娥五号成功着陆地球!为何嫦娥五号返回时会燃烧,升空却不会?...
  5. 1月25日再次开抢!三星Galaxy S21系列标准版已多次开售即罄
  6. cmd命令不识别exp_GRAT2:一款功能强大的命令amp;控制(C2)工具
  7. BZOJ4017 小Q的无敌异或 好题
  8. 灵派编码器HTTP API接口说明
  9. 基于星环大数据云平台 TDC 的一站式数据湖解决方案
  10. 强化学习算法三个基线策略
  11. wnmp mysql 密码_WNMP(Windows + Nginx + PHP + MySQL) 安装
  12. Hexo + yilia 主题实现文章目录
  13. 【SAP Basis】SAP用户权限管理
  14. SpringBoot2基础篇
  15. 用汇编实现256以内的三个数的加减乘除运算
  16. CSAPP:Attack Lab —— 缓冲区溢出攻击实验
  17. Fast Planner——ESDF地图中距离计算(欧几里得距离转换EDT)
  18. 高物实验报告计算机模拟高分子,高分子物理课程实验报告(.doc
  19. 百度、字节跳动们,能否撬动在线办公市场?
  20. python excel转xml 用例_测试用例Excel转XML格式教程

热门文章

  1. unityhub是干什么的呀?unityhub的作用!
  2. 汇金操盘手简易去广告方法
  3. 什么是spring?spring组成模块、spring优缺点、应用场景、bean的生命周期、线程并发问题
  4. 乐山计算机学校小姐,这个学校,来了一批远方的兄弟姐妹 ——四川乐山市计算机学校金口河籍学生来访交流...
  5. 计算机windows7显卡怎么检测,安装windows7之后的显卡问题
  6. “高调做事,高调做人”?----关于排名和排序
  7. 在CentOS7中安装思维脑图软件XMind
  8. 物流行业中的常见术语(zt)
  9. 在word2010中启用文本朗读功能
  10. Pixy图像处理与识别