Android 系列的光照效果

來源:互聯網
上載者:User
Android 系列的光照效果(一)

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=79942&fromuid=642886Android 系列的光照效果(二)http://www.eoeandroid.com/forum.php?mod=viewthread&tid=79946&fromuid=642886

我們這個執行個體主要是代碼比較多,一些重要的代碼都會有注釋,我們這個代碼啟動並執行效果就是,當運行時,就像是沒有燈光來照射一樣,啟動並執行物體一下子就會變暗。不多說了,馬上就來看看代碼吧:

java代碼:
package eoe.demo;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;

public class My3d extends Activity {
/** Called when the activity is first created. */

private MyRenderer renderer = new MyRenderer();
private GLSurfaceView glSurfaceView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BitGL.init(getResources());
glSurfaceView = new GLSurfaceView(this);

glSurfaceView.setRenderer(renderer);
setContentView(glSurfaceView);
}

// 處理事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
renderer.onKeyDown(keyCode, event);
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
renderer.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
}

class BitGL {
public static Bitmap bitmap;

public static void init(Resources resources) {
bitmap = BitmapFactory.decodeResource(resources, R.drawable.img);
}
}
來看看主要的java代碼:

java代碼:

package eoe.demo;

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

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.view.KeyEvent;

public class MyRenderer implements Renderer {
float step = 0.4f;
boolean key;
boolean light = true;

float xrot, yrot; //x,y軸旋轉
float xspeed, yspeed;//旋轉的速度
private int one = 0x10000;
private int[] textures = new int[1];
/**
* @param context
* 頂點及紋理
*/
// verticesBuffer
private IntBuffer verticesBuffer;
// texcoordBuffer
private IntBuffer texcoordBuffer;
// 頂點數組vertices
private int[] vertices;
// 紋理數組texcoord
private int[] texcoord;

/**
* @param context
* 光線
*/
// lightAmbientBuffer
private FloatBuffer lightAmbientBuffer;
// 環境光線lightAmbient
private float[] lightAmbient;
// lightDiffuseBuffer
private FloatBuffer lightDiffuseBuffer;
// 漫射光lightDiffuse
private float[] lightDiffuse;
// lightPositionBuffer
private FloatBuffer lightPositionBuffer;
// 光源的位置
private float[] lightPosition;

// 構造方法
public MyRenderer() {
// 在構造方法中初始化
initData();
initBuffer();
}

// 初始化各類值
public void initData() {
vertices = new int[] { -one, -one, one, one, -one, one, -one, one, one,
one, one, one, one, -one, one, one, -one, -one, one, one, one,
one, one, -one, one, -one, -one, -one, -one, -one, one, one,
-one, -one, one, -one, -one, -one, -one, -one, -one, one, -one,
one, -one, -one, one, one, -one, one, -one, one, one, -one,
-one, one, one, one, one, one, -one, -one, -one, -one, -one,
one, one, -one, -one, one, -one, one };

texcoord = new int[] { 0, 0, one, 0, 0, one, one, one };

lightAmbient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };

lightDiffuse = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };

lightPosition = new float[] { 0.0f, 0.0f, 2.0f, 1.0f };
}

public void initBuffer() {
// verticesbyteBuffer
ByteBuffer verticesbyteBuffer = ByteBuffer
.allocateDirect(vertices.length * 4);
verticesbyteBuffer.order(ByteOrder.nativeOrder());
verticesBuffer = verticesbyteBuffer.asIntBuffer();
verticesBuffer.put(vertices);
verticesBuffer.position(0);

// texcoordBuffer
ByteBuffer texcoordbyteBuffer = ByteBuffer
.allocateDirect(texcoord.length * 4 * 6);
texcoordbyteBuffer.order(ByteOrder.nativeOrder());
texcoordBuffer = texcoordbyteBuffer.asIntBuffer();
for (int i = 0; i < 6; i++) {
texcoordBuffer.put(texcoord);
}
texcoordBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightAmbientbyteBuffer = ByteBuffer.allocateDirect(lightAmbient.length * 4 * 6);
lightAmbientbyteBuffer.order(ByteOrder.nativeOrder());
lightAmbientBuffer = lightAmbientbyteBuffer.asFloatBuffer();
lightAmbientBuffer.put(lightAmbient);
lightAmbientBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightPositionbyteBuffer = ByteBuffer.allocateDirect(lightPosition.length * 4 * 6);
lightPositionbyteBuffer.order(ByteOrder.nativeOrder());
lightPositionBuffer = lightPositionbyteBuffer.asFloatBuffer();
lightPositionBuffer.put(lightPosition);
lightPositionBuffer.position(0);

// lightAmbientBuffer
ByteBuffer lightDiffusebyteBuffer = ByteBuffer.allocateDirect(lightDiffuse.length * 4 * 6);
lightDiffusebyteBuffer.order(ByteOrder.nativeOrder());
lightDiffuseBuffer = lightDiffusebyteBuffer.asFloatBuffer();
lightDiffuseBuffer.put(lightDiffuse);
lightDiffuseBuffer.position(0);
}

@Override
public void onDrawFrame(GL10 gl) {

// 清除深度緩衝和顏色
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
// 重設觀察模型
gl.glLoadIdentity();

//啟用光源
gl.glEnable(GL10.GL_LIGHTING);
// 啟用vertex及texcoord
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

// 向左移動1.5f並向裡移動6.0f
gl.glTranslatef(0.0f, 0.0f, -6.0f);

//設定旋轉
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

// 指定頂點映射
gl.glVertexPointer(3, GL10.GL_FIXED, 0, verticesBuffer);
// 指定紋理映射(每次都寫錯,煩)
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texcoordBuffer);

for (int i = 0; i < 6; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}

// 取消頂點及紋理映射
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

//旋轉角度
if ( key )
{
xrot+=xspeed;
yrot+=yspeed;
}

if (!light) // 判斷是否開始光源
{
gl.glDisable(GL10.GL_LIGHT1); // 禁用一號光源
}
else // 否則
{
gl.glEnable(GL10.GL_LIGHT1); // 啟用一號光源
}
}

@Override
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);
// 設定觀察模型
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 告訴系統對透視進行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 設定背景色為黑色
gl.glClearColor(0, 0, 0, 0);
// 啟用陰影平滑
gl.glShadeModel(GL10.GL_SMOOTH);

// 設定深度緩衝
gl.glClearDepthf(one);
// 啟用深度測試
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所做深度測試的類型
gl.glDepthFunc(GL10.GL_LEQUAL);

// 啟用紋理
gl.glEnable(GL10.GL_TEXTURE_2D); // 不啟用沒有效果的
// 建立紋理
gl.glGenTextures(1, textures, 0);
// 綁定紋理
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// 產生紋理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmap, 0);

// 線性濾波
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);

//設定環境光線
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbientBuffer);

//設定漫射光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuseBuffer);

//設定光源的位置
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPositionBuffer);

//啟用一號光源
gl.glEnable(GL10.GL_LIGHT1);
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch ( keyCode )
{
case KeyEvent.KEYCODE_DPAD_UP:
key = true;
xspeed=-step;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
key = true;
xspeed=step;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
key = true;
yspeed=-step;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
key = true;
yspeed=step;
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
light = !light;
break;
}
return false;
}

public boolean onKeyUp(int keyCode, KeyEvent event)
{
key = false;
return false;
}

}

運行:

        第二張圖片就是我們這篇執行個體所要講述的,說實話這個可以再遊戲的裡當黑天來使用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.