package ro.brite.android.nehe16;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import ro.brite.android.opengl.R;
import ro.brite.android.opengl.common.Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class GlRenderer implements Renderer {
private Context context;
public GlRenderer(Context context)
{
this.context = context;
}
private final static float[][] cubeVertexCoords = new float[][] {
new float[] { // top
1, 1,-1,
-1, 1,-1,
-1, 1, 1,
1, 1, 1
},
new float[] { // bottom
1,-1, 1,
-1,-1, 1,
-1,-1,-1,
1,-1,-1
},
new float[] { // front
1, 1, 1,
-1, 1, 1,
-1,-1, 1,
1,-1, 1
},
new float[] { // back
1,-1,-1,
-1,-1,-1,
-1, 1,-1,
1, 1,-1
},
new float[] { // left
-1, 1, 1,
-1, 1,-1,
-1,-1,-1,
-1,-1, 1
},
new float[] { // right
1, 1,-1,
1, 1, 1,
1,-1, 1,
1,-1,-1
},
};
private final static float[][] cubeNormalCoords = new float[][] {
new float[] { // top
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0
},
new float[] { // bottom
0,-1, 0,
0,-1, 0,
0,-1, 0,
0,-1, 0
},
new float[] { // front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1
},
new float[] { // back
0, 0,-1,
0, 0,-1,
0, 0,-1,
0, 0,-1
},
new float[] { // left
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0
},
new float[] { // right
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0
},
};
private final static float[][] cubeTextureCoords = new float[][] {
new float[] { // top
1, 0,
1, 1,
0, 1,
0, 0
},
new float[] { // bottom
0, 0,
1, 0,
1, 1,
0, 1
},
new float[] { // front
1, 1,
0, 1,
0, 0,
1, 0
},
new float[] { // back
0, 1,
0, 0,
1, 0,
1, 1
},
new float[] { // left
1, 1,
0, 1,
0, 0,
1, 0
},
new float[] { // right
0, 1,
0, 0,
1, 0,
1, 1
},
};
private final static float lightAmb[]= { 0.5f, 0.5f, 0.5f, 1.0f };
private final static float lightDif[]= { 1.0f, 1.0f, 1.0f, 1.0f };
private final static float lightPos[]= { 0.0f, 0.0f, 2.0f, 1.0f };
private final static float fogColor[] = { 0.5f, 0.5f, 0.5f, 1.0f };
private final static int fogMode[]= { GL10.GL_EXP, GL10.GL_EXP2, GL10.GL_LINEAR };
private final static FloatBuffer[] cubeVertexBfr;
private final static FloatBuffer[] cubeNormalBfr;
private final static FloatBuffer[] cubeTextureBfr;
private final static FloatBuffer lightAmbBfr;
private final static FloatBuffer lightDifBfr;
private final static FloatBuffer lightPosBfr;
private final static FloatBuffer fogColorBfr;
private IntBuffer texturesBuffer;
static final SceneState sceneState;
private long lastMillis;
static
{
cubeVertexBfr = new FloatBuffer[6];
cubeNormalBfr = new FloatBuffer[6];
cubeTextureBfr = new FloatBuffer[6];
for (int i = 0; i < 6; i++)
{
cubeVertexBfr[i] = FloatBuffer.wrap(cubeVertexCoords[i]);
cubeNormalBfr[i] = FloatBuffer.wrap(cubeNormalCoords[i]);
cubeTextureBfr[i] = FloatBuffer.wrap(cubeTextureCoords[i]);
}
lightAmbBfr = FloatBuffer.wrap(lightAmb);
lightDifBfr = FloatBuffer.wrap(lightDif);
lightPosBfr = FloatBuffer.wrap(lightPos);
fogColorBfr = FloatBuffer.wrap(fogColor);
sceneState = new SceneState();
}
private void LoadTextures(GL10 gl) {
// create textures
gl.glEnable(GL10.GL_TEXTURE_2D);
texturesBuffer = IntBuffer.allocate(3);
gl.glGenTextures(3, texturesBuffer);
// load bitmap
Bitmap texture = Utils.getTextureFromBitmapResource(context, R.drawable.nehe_texture_crate);
// setup texture 0
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(0));
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
// setup texture 1
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(1));
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
// setup texture 2
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(2));
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
Utils.generateMipmapsForBoundTexture(texture);
// free bitmap
texture.recycle();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
// lighting
gl.glEnable(GL10.GL_LIGHT0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbBfr);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDifBfr);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosBfr);
// fog
gl.glFogfv(GL10.GL_FOG_COLOR, fogColorBfr);
gl.glFogf(GL10.GL_FOG_DENSITY, 0.35f);
gl.glHint(GL10.GL_FOG_HINT, GL10.GL_DONT_CARE);
gl.glFogf(GL10.GL_FOG_START, 2.0f);
gl.glFogf(GL10.GL_FOG_END, 6.0f);
gl.glEnable(GL10.GL_FOG);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// update lighting
if (sceneState.lighting) {
gl.glEnable(GL10.GL_LIGHTING);
} else {
gl.glDisable(GL10.GL_LIGHTING);
}
// update fog
gl.glFogx(GL10.GL_FOG_MODE, fogMode[sceneState.fogFilter]);
// draw cube
gl.glTranslatef(0, 0, -6);
sceneState.rotateModel(gl);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(sceneState.filter));
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
for (int i = 0; i < 6; i++) // draw each face
{
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeVertexBfr[i]);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, cubeTextureBfr[i]);
gl.glNormalPointer(GL10.GL_FLOAT, 0, cubeNormalBfr[i]);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// get current millis
long currentMillis = System.currentTimeMillis();
// update rotations
if (lastMillis != 0) {
long delta = currentMillis - lastMillis;
sceneState.dx += sceneState.dxSpeed * delta;
sceneState.dy += sceneState.dySpeed * delta;
sceneState.dampenSpeed(delta);
}
// update millis
lastMillis = currentMillis;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// reload textures
LoadTextures(gl);
// avoid division by zero
if (height == 0) height = 1;
// draw on the entire screen
gl.glViewport(0, 0, width, height);
// setup projection matrix
gl.glMatrixMod
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
This is a port of NeHe's tutorials for the Android platform. The code is primarily targeted for Android 1.5, and tested on an HTC Magic. My aim is to develop in the end an app that offers (hopefully) all of NeHe's tutorials. Feel free to contact me for any comments/suggestions.
资源推荐
资源详情
资源评论
收起资源包目录
nehe-android (928个子文件)
all-wcprops 3KB
all-wcprops 2KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 621B
all-wcprops 586B
all-wcprops 576B
all-wcprops 518B
all-wcprops 498B
all-wcprops 498B
all-wcprops 498B
all-wcprops 498B
all-wcprops 498B
all-wcprops 498B
all-wcprops 493B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 468B
all-wcprops 465B
all-wcprops 464B
all-wcprops 460B
all-wcprops 460B
all-wcprops 460B
all-wcprops 460B
all-wcprops 458B
all-wcprops 388B
all-wcprops 363B
all-wcprops 363B
all-wcprops 363B
all-wcprops 348B
all-wcprops 348B
all-wcprops 348B
all-wcprops 348B
all-wcprops 348B
all-wcprops 342B
all-wcprops 342B
all-wcprops 342B
all-wcprops 342B
all-wcprops 342B
all-wcprops 294B
all-wcprops 287B
all-wcprops 287B
all-wcprops 287B
all-wcprops 285B
all-wcprops 285B
all-wcprops 285B
all-wcprops 273B
all-wcprops 236B
all-wcprops 222B
all-wcprops 196B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 186B
all-wcprops 184B
all-wcprops 184B
all-wcprops 184B
all-wcprops 182B
all-wcprops 182B
all-wcprops 182B
all-wcprops 176B
all-wcprops 164B
all-wcprops 96B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 91B
all-wcprops 89B
共 928 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- xlhu_cn12019-06-07很好,值得学习
- hhmy502012-05-17例子很好,但不能在2.2以上的模拟器上运行,1.5可以,1.5到2.2之间的版本我没试过,wrap()这个方法在2.2以上的版本中不能使用
- havefivedream2013-03-15很好的关于opengl教程
prince58
- 粉丝: 97
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功