floorquad.java

来自「一个JAVA程序员的游戏」· Java 代码 · 共 146 行

JAVA
146
字号
/*
 * FloorQuad.java
 *
 * Created on 18. Februar 2007, 15:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.graphics.shape;

import java.nio.FloatBuffer;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.TriMesh;
import com.jme.scene.batch.TriangleBatch;
import com.jme.util.geom.BufferUtils;

/**
 * <code>Quad</code> defines a four sided, two dimensional shape. The local
 * height of the <code>Quad</code> defines it's size about the y-axis, while
 * the width defines the x-axis. The z-axis will always be 0.
 * 
 * <code>FloorQuad</code> is the same as <code>Quad</code>, but in the x-z plane,
 * and with the possibility to flip normals. This class is needed because
 * GeometryBatch currently doesn't support rotation of <code>TriMesh</code>s.
 *
 * @author Mark Powell
 * @author Daniel Gronau
 */
public class FloorQuad extends TriMesh {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * Constant for normals pointing up (e.g. for a floor)
     */
    public static final int UP = 1;
    /**
     * Constant for normals pointing down (e.g. for a ceiling)
     */
    public static final int DOWN = -1;
    
    /**
     * For internal use only
     */
    public FloorQuad() {
        
    }
    
    /**
     * Constructor creates a new <code>FloorQuad</code> object. That data for the
     * <code>FloorQuad</code> is not set until a call to <code>initialize</code>
     * is made.
     * @param name the name of this <code>FloorQuad</code>.
     */
    public FloorQuad(String name) {
        super(name);
    }
    
    /**
     * Constructor creates a new <code>Quade</code> object with the provided
     * width and height.
     *
     * @param name the name of the <code>Quad</code>.
     * @param width the width of the <code>Quad</code>.
     * @param height the height of the <code>Quad</code>.
     * @param norms the direction of the normals
     */
    public FloorQuad(String name, float width, float height, int norms) {
        super(name);
        initialize(width, height, norms);
    }
    
    /**
     * <code>resize</code> changes the width and height of the given quad by
     * altering its vertices.
     *
     * @param width the new width of the <code>Quad</code>.
     * @param height the new height of the <code>Quad</code>.
     */
    public void resize(float width, float height) {
        TriangleBatch batch = getBatch(0);
        batch.getVertexBuffer().clear();
        batch.getVertexBuffer().put(width / 2f).put(0).put(height / 2f);
        batch.getVertexBuffer().put(width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(height / 2f);
    }
    
    /**
     * 
     * <code>initialize</code> builds the data for the <code>FloorQuad</code>
     * object.
     * @param width the width of the <code>Quad</code>.
     * @param height the height of the <code>Quad</code>.
     * @param norms the direction of the normals
     * @see FloorQuad.UP
     * @see FloorQuad.DOWN
     */
    public void initialize(float width, float height, int norms) {
        TriangleBatch batch = getBatch(0);
        batch.setVertexCount(4);
        batch.setVertexBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        batch.setNormalBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        FloatBuffer tbuf = BufferUtils.createVector2Buffer(batch.getVertexCount());
        setTextureBuffer(0,tbuf);
        batch.setTriangleQuantity(2);
        batch.setIndexBuffer(BufferUtils.createIntBuffer(batch.getTriangleCount() * 3));
        
        batch.getVertexBuffer().put(width / 2f).put(0).put(height / 2f);
        batch.getVertexBuffer().put(width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(height / 2f);
        
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        
        
        tbuf.put(0).put(1);
        tbuf.put(0).put(0);
        tbuf.put(1).put(0);
        tbuf.put(1).put(1);
        
        setDefaultColor(ColorRGBA.white);
        
        batch.getIndexBuffer().put(0);
        batch.getIndexBuffer().put(1);
        batch.getIndexBuffer().put(2);
        batch.getIndexBuffer().put(0);
        batch.getIndexBuffer().put(2);
        batch.getIndexBuffer().put(3);
    }
    
    /**
     * <code>getCenter</code> returns the center of the <code>FloorQuad</code>.
     *
     * @return Vector3f the center of the <code>FloorQuad</code>.
     */
    public Vector3f getCenter() {
        return worldTranslation;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?