libgdx系列之-加载obj(3D文件)

原文链接:http://blog.csdn.net/cng1991/article/details/7293946

今天看了下libgdx,觉得加载3d模型的功能很炫。所以分享下代码。

首先用blender(开源的3D模型工具,百度即可搜到)新建一个工程,然后导出为cube.obj。最主要的是获取obj文件。将.obj文件放在android工程的assets/data目录下。然后搭建android下的libgdx环境,即导入架包。

然后编写代码:

[java]  view plain copy
  1. package com.cng;  
  2.   
  3.   
  4.   
  5.   
  6. import android.os.Bundle;  
  7.   
  8. import com.badlogic.gdx.ApplicationListener;   
  9. import com.badlogic.gdx.Gdx;   
  10. import com.badlogic.gdx.backends.android.AndroidApplication;  
  11. import com.badlogic.gdx.graphics.Color;   
  12. import com.badlogic.gdx.graphics.GL10;   
  13. import com.badlogic.gdx.graphics.Mesh;  
  14. import com.badlogic.gdx.graphics.PerspectiveCamera;  
  15. import com.badlogic.gdx.graphics.Texture;   
  16. import com.badlogic.gdx.graphics.Texture.TextureFilter;  
  17. import com.badlogic.gdx.graphics.g2d.BitmapFont;   
  18. import com.badlogic.gdx.graphics.g2d.SpriteBatch;   
  19. import com.badlogic.gdx.graphics.g2d.TextureRegion;   
  20. import com.badlogic.gdx.graphics.g3d.loaders.ModelLoaderOld;  
  21. import com.badlogic.gdx.utils.ScreenUtils;  
  22.   
  23.   
  24.   
  25.   
  26. public class MyGameActivity extends AndroidApplication {  
  27.        
  28.     class MyGameListen  implements ApplicationListener  
  29.     {  
  30.         SpriteBatch batch;  
  31.         Texture texture;  
  32.         TextureRegion fbteRegion;  
  33.         BitmapFont font;  
  34.         PerspectiveCamera camera;  
  35.         Mesh mesh;  
  36.         Color clearColor=new Color(0.2f, 0.2f, 0.2f, 1);  
  37.         float angle;  
  38.         @Override  
  39.         public void create()  
  40.         {  
  41.             mesh=ModelLoaderOld.loadObj(Gdx.files.internal("data/cube.obj").read());  
  42.             texture=new Texture(Gdx.files.internal("data/badlogic.jpg"),true);  
  43.             texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);  
  44.               
  45.             batch=new SpriteBatch();  
  46.             font=new BitmapFont();  
  47.               
  48.             camera=new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
  49.             camera.position.set(333);  
  50.             camera.direction.set(-1, -1, -1);  
  51.         }  
  52.         @Override  
  53.         public void dispose()  
  54.         {  
  55.               
  56.         }  
  57.   
  58.         @Override  
  59.         public void pause()  
  60.         {  
  61.               
  62.         }  
  63.   
  64.         @Override  
  65.         public void render()  
  66.         {  
  67.             GL10 gl=Gdx.graphics.getGL10();  
  68.             gl.glViewport(00, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
  69.             gl.glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);  
  70.             gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);  
  71.             gl.glEnable(GL10.GL_DEPTH_TEST);  
  72.             gl.glEnable(GL10.GL_TEXTURE_2D);  
  73.               
  74.             camera.update();  
  75.             camera.apply(gl);  
  76.             angle+=45*Gdx.graphics.getDeltaTime();  
  77.             gl.glPushMatrix();  
  78.             gl.glRotatef(angle, 100);  
  79.             texture.bind();  
  80.             mesh.render(GL10.GL_TRIANGLES);  
  81.             gl.glPopMatrix();  
  82.             if(fbteRegion==null||Gdx.input.justTouched())  
  83.             {  
  84.                 if(fbteRegion!=null) fbteRegion.getTexture().dispose();  
  85.                 fbteRegion=ScreenUtils.getFrameBufferTexture();  
  86.             }  
  87.             batch.begin();  
  88.             if(fbteRegion!=null)  
  89.             {  
  90.                 batch.draw(fbteRegion, 00100100);  
  91.             }  
  92.             batch.end();  
  93.         }  
  94.   
  95.         @Override  
  96.         public void resize(int arg0, int arg1)  
  97.         {  
  98.               
  99.         }  
  100.   
  101.         @Override  
  102.         public void resume()  
  103.         {  
  104.         }  
  105.   
  106.   
  107.           
  108.   
  109.     }  
  110.       
  111.       
  112.     @Override  
  113.     protected void onCreate(Bundle savedInstanceState)  
  114.     {  
  115.         super.onCreate(savedInstanceState);  
  116.         initialize(new MyGameListen(), false);  
  117.     }  
  118.       
  119. }  
因为我是libgdx的初学者,里面有些代码不能解释,所以就先不解释了,只是提供一个例子。

 

 

你可能感兴趣的:(libgdx)