• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.graphics.g3d.utils.shapebuilders;
18 
19 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
20 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder.VertexInfo;
21 import com.badlogic.gdx.math.MathUtils;
22 
23 /** Helper class with static methods to build cylinders shapes using {@link MeshPartBuilder}.
24  * @author xoppa */
25 public class CylinderShapeBuilder extends BaseShapeBuilder {
26 	/** Build a cylinder */
build(MeshPartBuilder builder, float width, float height, float depth, int divisions)27 	public static void build (MeshPartBuilder builder, float width, float height, float depth, int divisions) {
28 		build(builder, width, height, depth, divisions, 0, 360);
29 	}
30 
31 	/** Build a cylinder */
build(MeshPartBuilder builder, float width, float height, float depth, int divisions, float angleFrom, float angleTo)32 	public static void build (MeshPartBuilder builder, float width, float height, float depth, int divisions, float angleFrom, float angleTo) {
33 		build(builder, width, height, depth, divisions, angleFrom, angleTo, true);
34 	}
35 
36 	/** Build a cylinder */
build(MeshPartBuilder builder, float width, float height, float depth, int divisions, float angleFrom, float angleTo, boolean close)37 	public static void build (MeshPartBuilder builder, float width, float height, float depth, int divisions, float angleFrom,
38 		float angleTo, boolean close) {
39 		// FIXME create better cylinder method (- axis on which to create the cylinder (matrix?))
40 		final float hw = width * 0.5f;
41 		final float hh = height * 0.5f;
42 		final float hd = depth * 0.5f;
43 		final float ao = MathUtils.degreesToRadians * angleFrom;
44 		final float step = (MathUtils.degreesToRadians * (angleTo - angleFrom)) / divisions;
45 		final float us = 1f / divisions;
46 		float u = 0f;
47 		float angle = 0f;
48 		VertexInfo curr1 = vertTmp3.set(null, null, null, null);
49 		curr1.hasUV = curr1.hasPosition = curr1.hasNormal = true;
50 		VertexInfo curr2 = vertTmp4.set(null, null, null, null);
51 		curr2.hasUV = curr2.hasPosition = curr2.hasNormal = true;
52 		short i1, i2, i3 = 0, i4 = 0;
53 
54 		builder.ensureVertices(2 * (divisions + 1));
55 		builder.ensureRectangleIndices(divisions);
56 		for (int i = 0; i <= divisions; i++) {
57 			angle = ao + step * i;
58 			u = 1f - us * i;
59 			curr1.position.set(MathUtils.cos(angle) * hw, 0f, MathUtils.sin(angle) * hd);
60 			curr1.normal.set(curr1.position).nor();
61 			curr1.position.y = -hh;
62 			curr1.uv.set(u, 1);
63 			curr2.position.set(curr1.position);
64 			curr2.normal.set(curr1.normal);
65 			curr2.position.y = hh;
66 			curr2.uv.set(u, 0);
67 			i2 = builder.vertex(curr1);
68 			i1 = builder.vertex(curr2);
69 			if (i != 0) builder.rect(i3, i1, i2, i4); // FIXME don't duplicate lines and points
70 			i4 = i2;
71 			i3 = i1;
72 		}
73 		if (close) {
74 			EllipseShapeBuilder.build(builder, width, depth, 0, 0, divisions, 0, hh, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, angleFrom,
75 				angleTo);
76 			EllipseShapeBuilder.build(builder, width, depth, 0, 0, divisions, 0, -hh, 0, 0, -1, 0, -1, 0, 0, 0, 0, 1,
77 				180f - angleTo, 180f - angleFrom);
78 		}
79 	}
80 }
81