1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.shapes;
\r
13 import java.nio.FloatBuffer;
\r
15 import com.jme.math.Vector3f;
\r
16 import com.jme.renderer.ColorRGBA;
\r
17 import com.jme.scene.TriMesh;
\r
18 import com.jme.scene.batch.TriangleBatch;
\r
19 import com.jme.util.geom.BufferUtils;
\r
21 public class Quad extends TriMesh {
\r
23 private static final long serialVersionUID = 1L;
\r
30 * Constructor creates a new <code>Quad</code> object. That data for the
\r
31 * <code>Quad</code> is not set until a call to <code>initialize</code>
\r
35 * the name of this <code>Quad</code>.
\r
37 public Quad(String name) {
\r
42 * Constructor creates a new <code>Quade</code> object with the provided
\r
46 * the name of the <code>Quad</code>.
\r
48 * the width of the <code>Quad</code>.
\r
50 * the height of the <code>Quad</code>.
\r
52 public Quad(String name, float width, float height) {
\r
54 initialize(width, height);
\r
58 * <code>resize</code> changes the width and height of the given quad by
\r
59 * altering its vertices.
\r
62 * the new width of the <code>Quad</code>.
\r
64 * the new height of the <code>Quad</code>.
\r
66 public void resize(float width, float height) {
\r
67 TriangleBatch batch = getBatch(0);
\r
68 batch.getVertexBuffer().clear();
\r
69 batch.getVertexBuffer().put(-width / 2f).put(height / 2f).put(0);
\r
70 batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
\r
71 batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
\r
72 batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
\r
77 * <code>initialize</code> builds the data for the <code>Quad</code>
\r
82 * the width of the <code>Quad</code>.
\r
84 * the height of the <code>Quad</code>.
\r
86 public void initialize(float width, float height) {
\r
87 TriangleBatch batch = getBatch(0);
\r
88 batch.setVertexCount(4);
\r
89 batch.setVertexBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
\r
90 batch.setNormalBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
\r
91 FloatBuffer tbuf = BufferUtils.createVector2Buffer(batch.getVertexCount());
\r
92 setTextureBuffer(0,tbuf);
\r
93 batch.setTriangleQuantity(2);
\r
94 batch.setIndexBuffer(BufferUtils.createIntBuffer(batch.getTriangleCount() * 3));
\r
96 batch.getVertexBuffer().put(-width / 2f).put(height / 2f).put(0);
\r
97 batch.getVertexBuffer().put(-width / 2f).put(-height / 2f).put(0);
\r
98 batch.getVertexBuffer().put(width / 2f).put(-height / 2f).put(0);
\r
99 batch.getVertexBuffer().put(width / 2f).put(height / 2f).put(0);
\r
101 batch.getNormalBuffer().put(0).put(0).put(1);
\r
102 batch.getNormalBuffer().put(0).put(0).put(1);
\r
103 batch.getNormalBuffer().put(0).put(0).put(1);
\r
104 batch.getNormalBuffer().put(0).put(0).put(1);
\r
107 tbuf.put(0).put(0);
\r
108 tbuf.put(0).put(1);
\r
109 tbuf.put(1).put(1);
\r
110 tbuf.put(1).put(0);
\r
112 setDefaultColor(ColorRGBA.white);
\r
114 batch.getIndexBuffer().put(0);
\r
115 batch.getIndexBuffer().put(1);
\r
116 batch.getIndexBuffer().put(2);
\r
117 batch.getIndexBuffer().put(0);
\r
118 batch.getIndexBuffer().put(2);
\r
119 batch.getIndexBuffer().put(3);
\r
123 * <code>getCenter</code> returns the center of the <code>Quad</code>.
\r
125 * @return Vector3f the center of the <code>Quad</code>.
\r
127 public Vector3f getCenter() {
\r
128 return worldTranslation;
\r