andriod 3d 初探

[color=red][/color]看了sdk中的3d的例子,自已写了一个棱形的例子.

[b[color=red]]Prism.java[/b][/color]

package cn.ynw520;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

/**
* A vertex shaded prism.
*/
class Prism
{
public Prism()
{
int one = 0x10000;

//这里是四个顶点(按顺时针方向来存储)
int vertices[] = {
0, 0, one*2,
0, one, 0,
-one, -one,0,
one, -one,0,
};

//每个顶点的颜色
int colors[] = {
0, 0, 0, one,
one, 0, 0, one,
one, one, 0, one,
0, one, 0, one,
};

//按顺时针,由点组成的4个面,一行代表一个面
byte indices[] = {
0, 1, 2,
0, 2, 3,
0, 3, 1,
1, 2, 3,
};


ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);

ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asIntBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);

mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}

public void draw(GL10 gl)
{
gl.glFrontFace(gl.GL_CW); //说明按顺时针方向
gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer); //这个是画点,3代表维数,第二个参数代表值的类型,第三个好像都为0
gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer); //画点的颜色
gl.glDrawElements(gl.GL_TRIANGLES, 12, gl.GL_UNSIGNED_BYTE, mIndexBuffer); //开始画了,第一个代表用什么形状画(三角形),第二个参数代表组成面的点数,第三个是点数据的类型
}

private IntBuffer mVertexBuffer;
private IntBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
}


[b]PrismRender.java[/b]

package cn.ynw520;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

public class PrismRender implements Renderer {

public PrismRender(boolean useTranslucentBackground) {
mTranslucentBackground = useTranslucentBackground;
mPrism = new Prism();
}

public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //清除

gl.glMatrixMode(GL10.GL_MODELVIEW); //mode
gl.glLoadIdentity(); // 重置模型观察矩阵
gl.glTranslatef(0, 0, -3.0f); //相当于原点移动
gl.glRotatef(mAngle, 0, 1, 0); //绕y轴转动,mAngel代表角度
gl.glRotatef(mAngle*0.25f, 1, 0, 0); //绕x轴转动

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //说明数据可以画
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

mPrism.draw(gl);
mAngle += 1.2f;
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);

float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);

/*
* Some one-time OpenGL initialization can be made here
* probably based on features of this particular context
*/
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);

if (mTranslucentBackground) {
gl.glClearColor(0,0,0,0);
} else {
gl.glClearColor(1,1,1,1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}

private boolean mTranslucentBackground;
private float mAngle;
private Prism mPrism;
}


[color=red][b]prismActivity.java[/b][/color]


package cn.ynw520;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class PrismActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(new PrismRender(false));
setContentView(mGLSurfaceView);
}

@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}

private GLSurfaceView mGLSurfaceView;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值