]> gerrit.simantics Code Review - simantics/3d.git/blob - dev/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/shapes/Quad.java
cab5e37ebab6cb12c86b4be367fe0fc4c322c889
[simantics/3d.git] / dev / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / shapes / Quad.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.shapes;\r
12 \r
13 import java.nio.FloatBuffer;\r
14 \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
20 \r
21 public class Quad extends TriMesh {\r
22 \r
23         private static final long serialVersionUID = 1L;\r
24 \r
25     public Quad() {\r
26         \r
27     }\r
28     \r
29         /**\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
32          * is made.\r
33          * \r
34          * @param name\r
35          *            the name of this <code>Quad</code>.\r
36          */\r
37         public Quad(String name) {\r
38                 super(name);\r
39         }\r
40 \r
41         /**\r
42          * Constructor creates a new <code>Quade</code> object with the provided\r
43          * width and height.\r
44          * \r
45          * @param name\r
46          *            the name of the <code>Quad</code>.\r
47          * @param width\r
48          *            the width of the <code>Quad</code>.\r
49          * @param height\r
50          *            the height of the <code>Quad</code>.\r
51          */\r
52         public Quad(String name, float width, float height) {\r
53                 super(name);\r
54                 initialize(width, height);\r
55         }\r
56 \r
57         /**\r
58          * <code>resize</code> changes the width and height of the given quad by\r
59          * altering its vertices.\r
60          * \r
61          * @param width\r
62          *            the new width of the <code>Quad</code>.\r
63          * @param height\r
64          *            the new height of the <code>Quad</code>.\r
65          */\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
73         }\r
74 \r
75         /**\r
76          * \r
77          * <code>initialize</code> builds the data for the <code>Quad</code>\r
78          * object.\r
79          * \r
80          * \r
81          * @param width\r
82          *            the width of the <code>Quad</code>.\r
83          * @param height\r
84          *            the height of the <code>Quad</code>.\r
85          */\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
95 \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
100 \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
105 \r
106         \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
111 \r
112             setDefaultColor(ColorRGBA.white);\r
113 \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
120         }\r
121 \r
122         /**\r
123          * <code>getCenter</code> returns the center of the <code>Quad</code>.\r
124          * \r
125          * @return Vector3f the center of the <code>Quad</code>.\r
126          */\r
127         public Vector3f getCenter() {\r
128                 return worldTranslation;\r
129         }\r
130 }\r