/*******************************************************************************
* Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
*******************************************************************************/
package org.simantics.proconf.g3d.shapes;
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;
public class Quad extends TriMesh {
private static final long serialVersionUID = 1L;
public Quad() {
}
/**
* Constructor creates a new Quad
object. That data for the
* Quad
is not set until a call to initialize
* is made.
*
* @param name
* the name of this Quad
.
*/
public Quad(String name) {
super(name);
}
/**
* Constructor creates a new Quade
object with the provided
* width and height.
*
* @param name
* the name of the Quad
.
* @param width
* the width of the Quad
.
* @param height
* the height of the Quad
.
*/
public Quad(String name, float width, float height) {
super(name);
initialize(width, height);
}
/**
* resize
changes the width and height of the given quad by
* altering its vertices.
*
* @param width
* the new width of the Quad
.
* @param height
* the new height of the Quad
.
*/
public void resize(float width, float height) {
TriangleBatch batch = getBatch(0);
batch.getVertexBuffer().clear();
batch.getVertexBuffer().put(-width / 2f).put(height / 2f).put(0);
batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
}
/**
*
* initialize
builds the data for the Quad
* object.
*
*
* @param width
* the width of the Quad
.
* @param height
* the height of the Quad
.
*/
public void initialize(float width, float height) {
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(height / 2f).put(0);
batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
batch.getNormalBuffer().put(0).put(0).put(1);
batch.getNormalBuffer().put(0).put(0).put(1);
batch.getNormalBuffer().put(0).put(0).put(1);
batch.getNormalBuffer().put(0).put(0).put(1);
tbuf.put(0).put(0);
tbuf.put(0).put(1);
tbuf.put(1).put(1);
tbuf.put(1).put(0);
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);
}
/**
* getCenter
returns the center of the Quad
.
*
* @return Vector3f the center of the Quad
.
*/
public Vector3f getCenter() {
return worldTranslation;
}
}