libgdx 3D

来源:互联网 发布:java工程师 编辑:程序博客网 时间:2024/05/16 18:39

测试代码:

内容参考:https://github.com/xoppa/blog/edit/master/tutorials/src/com/xoppa/blog/libgdx/g3d/basic3d/step1/Basic3DTest.java#

/******************************************************************************* * Copyright 2011 See AUTHORS file. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *   http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/package net.peng.game.game1;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.graphics.GL20;import com.badlogic.gdx.graphics.PerspectiveCamera;import com.badlogic.gdx.graphics.VertexAttributes.Usage;import com.badlogic.gdx.graphics.g3d.Environment;import com.badlogic.gdx.graphics.g3d.Model;import com.badlogic.gdx.graphics.g3d.ModelBatch;import com.badlogic.gdx.graphics.g3d.ModelInstance;import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;import com.badlogic.gdx.graphics.g3d.Material;import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;/** * See: http://blog.xoppa.com/basic-3d-using-libgdx-2/ * @author Xoppa */public class MyGame implements ApplicationListener {public Environment environment;public PerspectiveCamera cam;public CameraInputController camController;public ModelBatch modelBatch;public Model model;public ModelInstance instance;@Overridepublic void create() {environment = new Environment();environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));modelBatch = new ModelBatch();cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());cam.position.set(10f, 10f, 10f);cam.lookAt(0,0,0);cam.near = 1f;cam.far = 300f;cam.update();        ModelBuilder modelBuilder = new ModelBuilder();        model = modelBuilder.createBox(5f, 5f, 5f,             new Material(ColorAttribute.createDiffuse(Color.GREEN)),            Usage.Position | Usage.Normal);        instance = new ModelInstance(model);                camController = new CameraInputController(cam);        Gdx.input.setInputProcessor(camController);}@Overridepublic void render() {camController.update();        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);         modelBatch.begin(cam);        modelBatch.render(instance,environment);        modelBatch.end();}@Overridepublic void dispose() {modelBatch.dispose();model.dispose();}@Overridepublic void resize(int width, int height) {}@Overridepublic void pause() {}@Overridepublic void resume() {}}


0 0
原创粉丝点击