# Cylinder (frustrum of a cone) normals fixed

The normals of Cylinder were always orthogonal to it’s axis, even if it’s a frustrum of a cone (radius != radius 2). Here’s a quick fix.

Before:

After:

Patch:

[patch]

# Above lines and this line are ignored by the patching process.

Index: Cylinder.java

— Cylinder.java Base (BASE)

+++ Cylinder.java Locally Modified (Based On LOCAL)

@@ -259,6 +259,25 @@

• ``````   // calculate normals<br />
``````
• ``````   Vector3f[] vNormals = null;<br />
``````
• ``````   Vector3f vNormal = Vector3f.UNIT_Z;<br />
``````

+

• ``````   if ((height != 0.0f) &amp;&amp; (radius != radius2)) {<br />
``````
• ``````       vNormals = new Vector3f[radialSamples];<br />
``````
• ``````       Vector3f vHeight = Vector3f.UNIT_Z.mult(height);<br />
``````
• ``````       Vector3f vRadial = new Vector3f();<br />
``````

+

• ``````       for (int radialCount = 0; radialCount &lt; radialSamples; radialCount++) {<br />
``````
• ``````           vRadial.set(cos[radialCount], sin[radialCount], 0.0f);<br />
``````
• ``````           Vector3f vRadius = vRadial.mult(radius);<br />
``````
• ``````           Vector3f vRadius2 = vRadial.mult(radius2);<br />
``````
• ``````           Vector3f vMantle = vHeight.subtract(vRadius2.subtract(vRadius));<br />
``````
• ``````           Vector3f vTangent = vRadial.cross(Vector3f.UNIT_Z);<br />
``````
• ``````           vNormals[radialCount] = vMantle.cross(vTangent).normalize();<br />
``````
• ``````       }<br />
``````
• ``````   }<br />
``````

+

FloatBuffer nb = getFloatBuffer(Type.Normal);

FloatBuffer pb = getFloatBuffer(Type.Position);

FloatBuffer tb = getFloatBuffer(Type.TexCoord);

@@ -286,21 +305,28 @@

axisFractionTexture = axisCount * inverseAxisLessTexture;

}

}

• ``````       float z = -halfHeight + height * axisFraction;<br />
``````

// compute center of slice
+ float z = -halfHeight + height * axisFraction;
Vector3f sliceCenter = new Vector3f(0, 0, z);

// compute slice vertices with duplication at end point
int save = i;
+
+ if (vNormals != null) {
+ vNormal = tempNormal;
+ }
+
if (topBottom == 0) {
if (!inverted)
- nb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
+ nb.put(vNormal.x).put(vNormal.y).put(vNormal.z);
else
- nb.put(-tempNormal.x).put(-tempNormal.y).put(-tempNormal.z);
+ nb.put(-vNormal.x).put(-vNormal.y).put(-vNormal.z);
} else {
nb.put(0).put(0).put(topBottom * (inverted ? -1 : 1));
}
[/patch]
3 Likes