pro opengl es for android 中文,OpenGL ES for Android 绘制三角形

一个

有态度

的程序员

0604745a2d6f366ac2193a910b9389a6.png

在Android中绘制三角形的顶点shader如下:

attribute vec4 vPosition;void main() {gl_Position = vPosition;}

vPosition是顶点,由应用程序传入。

片段shader代码如下:

precision mediump float;void main(){gl_FragColor = vec4(1,0,0,1);}

创建program:

private fun createProgram() {var vertexCode =AssetsUtils.readAssetsTxt(context = context,filePath = "glsl/triangle_vertex.glsl")var fragmentCode =AssetsUtils.readAssetsTxt(context = context,filePath = "glsl/triangle_fragment.glsl")mProgramHandle = GLTools.createAndLinkProgram(vertexCode, fragmentCode)}

triangle_vertex.glsl和triangle_vertex.glsl分别表示顶点shader和片段shader的文件,存放于assets/glsl目录下,readAssetsTxt为读取assets目录下文件的公用方法。

获取参数句柄:

vPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "vPosition")

初始化线的顶点数据,代码如下:

var vertexBuffer = GLTools.array2Buffer(floatArrayOf(0.0f, 0.5f, 0.0f, // top-0.5f, -0.5f, 0.0f, // bottom left0.5f, -0.5f, 0.0f // bottom right))

绘制:

override fun onDrawFrame(p0: GL10?) {GLES20.glUseProgram(mProgramHandle)vertexBuffer.position(0)GLES20.glEnableVertexAttribArray(vPositionLoc)GLES20.glVertexAttribPointer(vPositionLoc, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer)GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3)}

GL_TRIANGLES表示绘制三角形。

三角形的绘制有3种方式:

GL_TRIANGLES:3个顶点绘制一个三角形,即使三角形的顶点有重复的,也必须在顶点数组中声明。如果有6个顶点,那么1,2,3组成一个三角形,4,5,6组成一个三角形。

GL_TRIANGLE_STRIP:前一个三角形的后两个顶点,和接下来的一个顶点组成另外一个三角形,如果有6个顶点,组成三角形的顶点有(1,2,3)、(2,3,4)、(3,4,5)、(4,5,6)共4个三角形,所以有N个顶点,则绘制出的三角形有N-2个。

GL_TRIANGLE_FAN :以第一个点为中心点,其它顶点作为边缘点绘制出组成扇型的相邻三角形,如果有6个顶点,组成三角形的顶点有(1,2,3)、(1,3,4)、(1,4,5)、(1,5,6)共4个三角形

更多相关阅读:

关注我们

获得更多精彩内容

81accfe0f7900c57428aff8d61f78281.gif

戳原文,查看OpenGL ES for Android 总览

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES! OpenGL has set the standard for 3D computer graphics, and is an essential aspect of Android development. This book offers everything you need to know, from basic mathematical concepts to advanced coding techniques. You'll learn by building a fascinating 3D solar system simulator! After introducing Open GL ES, Pro OpenGL ES for Android explains the basics of 3D math and then orients you to the native Android 3D libraries you'll be using in your own 3D games and the solar system project you'll build using this book. Through the solar system example project, you'll learn how to incorporate a variety of graphic and animation techniques into your applications. You will also discover how the full spectrum of 3D development that awaits, with topics such as lighting, texture-mapping, modeling, shaders, blending modes, and several more advanced concepts. By the time you finish Pro OpenGL ES for Android, you'll have learned all the skills you'll need to build your own incredible 3D applications, based on one of the most powerful 3D libraries available. What you'll learn * The basics of 3D mathematics, and how they are applied in the OpenGL library * How to design and build your 3D worlds * To create 2D interfaces within the 3D world * To develop animation and 3D movement * How to implement 3D shading, coloring, and texturing * The differences between OpenGL and other 3D toolkits * To build a fully-functional 3D solar system simulator using OpenGL ES Who this book is for Experienced Android programmers who want to enter the 3D world of OpenGL ES programming. Table of Contents * Introduction to OpenGL ES and Our 3D Solar System Project * Generating a Basic OpenGL Program * Getting Past the 3D Math * Shading, Lighting and Colors * Materials and Textures * Animation * Creating a User Interface * Blending Modes, Buffer Objects, and Other Cool Stuff * Latest Features of OpenGL ES * Ray Tracing, Hidden Surfaces, and Other Advanced Topics Appendix A: APIs
Apress, 2012 In 1985 I brought home a new shiny Commodore Amiga 1000, about one week after they were released. Coming with a whopping 512K of memory, programmable colormaps, a Motorola 68K CPU, and a modern multitasking operating system, it had “awesome” writ all over it. Metaphorically speaking, of course. I thought it might make a good platform for an astronomy program, as I could now control the colors of those star-things instead of having to settle for a lame fixed color palette forced upon me from the likes of Hercules or the C64. So I coded up a 24-line basic routine to draw a random star field, turned out the lights, and thought, “Wow! I bet I could write a cool astronomy program for that thing!” Twenty-six years later I am still working on it and hope to get it right one of these days. Back then my dream device was something I could slip into my pocket, pull out when needed, and aim it at the sky to tell me what stars or constellations I was looking at. It’s called a smartphone. I thought of it first. As good as these things are for playing music, making calls, or slinging birdies at piggies, it really shines when you get to the 3D stuff. After all, 3D is all around us— unless you are a pirate and have taken to wearing an eye patch, in which case you’ll have very limited depth perception. Arrrggghhh. Plus 3D apps are fun to show off to people. They’ll “get it.” In fact, they’ll get it much more than, say, that mulch buyer’s guide app all the kids are talking about. (Unless they show off their mulch in 3D, but that would be a waste of a perfectly good dimension.) So, 3D apps are fun to see, fun to interact with, and fun to program. Which brings me to this book. I am by no means a guru in this field. The real gurus are the ones who can knock out a couple of NVIDIA drivers before breakfast, 4-dimensional hypercube simulators by lunch, and port Halo to a TokyoFlash watch before the evening’s Firefly marathon on SyFy. I can’t do that. But I am a decent writer, have enough of a working knowledge of the subject to make me harmless, and know how to spell “3D.” So here we are. amazon link:http://www.amazon.com/exec/obidos/ASIN/1430240024/buythisbooks-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值