Android 3D game tutorial – Part II

转载 2011年01月13日 22:16:00

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part .

The second part of this series will show you how to add a triangle and how to rotate it a bit.

The first thing we have to do is to initialize the triangle we want to display. We have to create a function named initTriangle() in our VortexRenderer class.

Lets start with the new object variables. The _vertexBuffer will be filled with the coordinates for our triangle. The _indexBuffer stores the indices. The variable _nrOfVertices defines how many vertices are required. For a triangle we have three vertices.
The method itself allocate the needed memory for both buffer (line 14 – 22). Than we define some coordinates (line 24 – 28) and the comments behind each row explains you, how you can read the coordinates.
In line 30 we fill the _vertexBuffer with the coordinates stored on the coords array. The same with the indices array and the _indexBuffer on line 31. Finally we set both buffer to position 0.

To prevent the initialization of the triangle for every frame, we just do it once in a function that will be called before onDrawFrame(). On choice could be the method onSurfaceCreated().

glEnableClientState() set OpenGL to use vertex arrays to draw. Thats important to enable because otherwise OpenGL don’t know how to handle our data. Than we will initialize our triangle.

Why do we have to work with different buffers? Lets take a look at the new onDrawFrame() method where we have to add some new OpenGL calls.

Ok now step for step:
glClearColor() and glClear() should be known by the first part of the series.
On line 10 we set the color for our triangle to a darker red using glColor4f(red, green, blue, alpha) .
On line 13 we initialize the vertex pointer using glVertexPointer() . The first parameter is for the size, also known as dimension of our vertices. Are we using just x and y or also z? We use all three dimensions. The second parameter, GL_FLOAT, defines the data type used in the buffer. The third parameter is 0 because our coordinates are tightly packed in the array, no offset used. And finally the fourth parameter is the buffer in which we have our vertices stored.
The last call, glDrawElements() , will draw the elements. First parameter defines what kind of primitives have to rendered. The second element defines the number of elements and the third parameter the type of the values used for the indices. The last one is the index buffer which will be used to render the vertices.

When you finally test the application, you will see a static triangle in the middle of your screen. The change of the color of your background should still work if you touch the screen.

Lets add some rotation to the triangle. The following code must be implemented in our VortexRenderer class.

The glRotatef() method will be called in our onDrawFrame() right above glColor4f().

We rotate at the moment just around the y-axis. If you want to change this, simply change the 0f of the glRotatef() method call. The value of this parameter are for a vector which represent the axis on which the triangle will rotate.

To make this work, we have to add a call to the onTouchEvent() method in our VortexView class.

The division through 10 is to reduce the speed of angle changing.

Now compile and run the application. If you touch the screen at the most left side, you should see the triangle rotate slightly. If you move your finger to the right, the speed of the rotation should increase dramatically.

Source as Eclipse project: Vortex Part II

3d-part-two-triangle 3d-part-two-triangle1 3d-part-two-triangle2

Go to Android 3D game tutorial – Part III

国外intel总结的比较全的Android 3D游戏引擎介绍

Game Engines for Android* Submitted by Nathan Totura (Intel) on Wed, Sep 18, 2013 Share Tw...
  • antoineyuan
  • antoineyuan
  • 2014年11月30日 23:19
  • 587

DirectX12笔记 初始化

  • weixin_39662196
  • weixin_39662196
  • 2017年08月04日 12:42
  • 92

Unity3D 射线指定层获取GameObject 注意

这一篇是纯技术讨论,看过我前面文章的童鞋应该清楚,我的奔跑是靠鼠标响应的,鼠标点到哪就跑到哪,后来又有了界面,麻烦就来了,我的界面竟然能点 穿,我不希望点界面的时候还能点到界面后面的地面上,角色傻不拉...
  • yaokang522
  • yaokang522
  • 2015年07月03日 12:40
  • 647

SDL游戏之路(八)--2D游戏添加3D元素

3D游戏如何制作呢? 2D游戏场景中加入3D元素怎么实现呢? 我的问题是,一个SDL写的2D游戏,如何在适当的地方添加3D元素? 经过不断的尝试和google,这个问题也解决了。 第一步:创建...
  • ycg01
  • ycg01
  • 2014年01月13日 21:40
  • 3432

3D建模系统blender快捷键

3D建模系统Blender 常用快捷键功能Ctrl+LMB编辑模式:挤压选中的点、线或面到点击位置A 选择所有物体或取消选择Ctrl+A 应用旋转和缩放,之前的旋转和缩放操作将不能被复位C 以3D C...
  • caimouse
  • caimouse
  • 2016年12月18日 20:20
  • 1467

Android开发笔记(八十五)手机数据库Realm

原文:http://blog.csdn.net/aqi00/article/details/51145001 Realm应用背景 Android自带的SQLite数据库,在多数场合能够满足我们...
  • lxh_gdmu
  • lxh_gdmu
  • 2016年06月16日 00:38
  • 2714

【算法】【Greedy】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • wurlin
  • wurlin
  • 2017年12月28日 12:31
  • 18

AutoFac使用方法总结:Part II

事件 AutoFac支持三种事件:OnActivating,OnActivated,OnRelease。OnActivating在注册组件使用之前会被调用,此时可以替换实现类或者进行一些其他的初...
  • dhx20022889
  • dhx20022889
  • 2013年07月15日 21:52
  • 2126

经典算法——Jump Game(II)

Given an array of non-negative integers, you are initially positioned at the first index of the arra...
  • geekmanong
  • geekmanong
  • 2016年04月08日 15:14
  • 756

AutoFac使用方法总结:Part II

事件 AutoFac支持三种事件:OnActivating,OnActivated,OnRelease。OnActivating在注册组件使用之 前会被调用,此时可以替换实现类或者进行一些其他的初始...
  • xingxing513234072
  • xingxing513234072
  • 2013年08月20日 17:35
  • 1380
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android 3D game tutorial – Part II
举报原因:
原因补充:

(最多只允许输入30个字)