欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

libgdx之加载obj(3D文件)

程序员文章站 2022-03-26 17:50:10
...

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

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

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

然后编写代码:

package com.cng;




import android.os.Bundle;

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.graphics.g3d.loaders.ModelLoaderOld;
import com.badlogic.gdx.utils.ScreenUtils;




public class MyGameActivity extends AndroidApplication {
	 
	class MyGameListen  implements ApplicationListener
	{
		SpriteBatch batch;
		Texture texture;
		TextureRegion fbteRegion;
		BitmapFont font;
		PerspectiveCamera camera;
		Mesh mesh;
		Color clearColor=new Color(0.2f, 0.2f, 0.2f, 1);
		float angle;
		@Override
		public void create()
		{
			mesh=ModelLoaderOld.loadObj(Gdx.files.internal("data/cube.obj").read());
			texture=new Texture(Gdx.files.internal("data/badlogic.jpg"),true);
			texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
			
			batch=new SpriteBatch();
			font=new BitmapFont();
			
			camera=new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
			camera.position.set(3, 3, 3);
			camera.direction.set(-1, -1, -1);
		}
		@Override
		public void dispose()
		{
			
		}

		@Override
		public void pause()
		{
			
		}

		@Override
		public void render()
		{
			GL10 gl=Gdx.graphics.getGL10();
			gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
			gl.glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
			gl.glEnable(GL10.GL_DEPTH_TEST);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			
			camera.update();
			camera.apply(gl);
			angle+=45*Gdx.graphics.getDeltaTime();
			gl.glPushMatrix();
			gl.glRotatef(angle, 1, 0, 0);
			texture.bind();
			mesh.render(GL10.GL_TRIANGLES);
			gl.glPopMatrix();
			if(fbteRegion==null||Gdx.input.justTouched())
			{
				if(fbteRegion!=null) fbteRegion.getTexture().dispose();
				fbteRegion=ScreenUtils.getFrameBufferTexture();
			}
			batch.begin();
			if(fbteRegion!=null)
			{
				batch.draw(fbteRegion, 0, 0, 100, 100);
			}
			batch.end();
		}

		@Override
		public void resize(int arg0, int arg1)
		{
			
		}

		@Override
		public void resume()
		{
		}


		

	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initialize(new MyGameListen(), false);
	}
	
}
因为我是libgdx的初学者,里面有些代码不能解释,所以就先不解释了,只是提供一个例子。

效果图如下:

libgdx之加载obj(3D文件)

估计91助手的屏幕截图有点问题,这正方体看起来有点弯曲,但实际就是一个四四方方的正方体,并在旋转。