# Rendering superellipsoids

Hi. I want to render superellipsoids with jme3. http://paulbourke.net/geometry/superellipse/

I use the following code for calculating one vertex:

[java]public Vector3f EvalSuperEllipse(float t1,float t2,float p1,float p2)

{

float tmp;

float ct1,ct2,st1,st2;

ct1 = (float) Math.cos(t1);

ct2 = (float) Math.cos(t2);

st1 = (float) Math.sin(t1);

st2 = (float) Math.sin(t2);

tmp = (float) Math.pow(fabs(ct1),p1)sign(ct1);

Vector3f pp = new Vector3f(sign(ct2)15(float) (tmp
Math.pow(fabs(ct2),p2)),

sign(st1)15(float) Math.pow(fabs(st1),p1),

sign(st2)15(float) (tmp * Math.pow(fabs(st2),p2))

);

return pp;

}[/java]

The following code calculates all the vertices and sets the arrays for them, the indexes array and the textures array:

[java]for (int j=0;j<n/2;j++) {

theta1 = j * TWOPI / n - PID2;

theta2 = (j + 1) * TWOPI / n - PID2;

for (int i=0;i<=n;i++) {

if (i == 0 || i == n)

theta3 = 0;

else

theta3 = i * TWOPI / n;

p = EvalSuperEllipse(theta2,theta3,e1,e2);

p1 = EvalSuperEllipse(theta1+delta,theta3,e1,e2);

p2 = EvalSuperEllipse(theta1,theta3+delta,e1,e2);

en = calcNormal(p1,p,p2);

tex = new Vector2f(i/(float)n,2j/(float)n);

p = EvalSuperEllipse(theta1,theta3,e1,e2);

p1 = EvalSuperEllipse(theta2+delta,theta3,e1,e2);

p2 = EvalSuperEllipse(theta2,theta3+delta,e1,e2);

en = calcNormal(p1,p,p2);

tex = new Vector2f(i/(float)n,2
(j+1)/(float)n);

}

}

vertices = new Vector3f[vertexArray.size()];

vertices = vertexArray.toArray(vertices);

texCoord = new Vector2f[textureArray.size()];

texCoord = textureArray.toArray(texCoord);

int k = 0;

while(k < vertices.length) {

int d = k;

for(int j=d; j < d+3; j++) {

if(j < vertices.length) {

k++;

}

}

}

indexes = new int[indexList.size()];

for(int i=0; i<indexes.length; i++) {

indexes = indexList.get(i);

}

setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));

setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));

updateBound();

}[/java]

I used these codes from the link given above. In the end i render the mesh a standard way:

[java]SuperEllipsoid se = new SuperEllipsoid(1.0f, 1.0f, 100);

Geometry seGeo = new Geometry(â€śSuperEllipsoidâ€ť, se);

Material seMat = new Material(assetManager, â€śCommon/MatDefs/Misc/Unshaded.j3mdâ€ť);

seMat.setColor(â€śColorâ€ť, ColorRGBA.Blue);

seGeo.setMaterial(seMat);

rootNode.attachChild(seGeo);[/java]

Somehow i get this:

http://i.imgur.com/Fq7li.png

What is the problem? Iâ€™ve read the Custom Mesh tutorial. I think the problem is the indexes, but i dont really know how to solve itâ€¦

That should be a sphere, for the parameters 1.0, 1.0

When each triangle is projected onto screenspace, the vertices should be in counterclockwise order (on screen), otherwise they are culled. You may need to orient your indices to generate triangles in that order.

1 Like

The main problem is that these superellipsoids can be very complicated shapes, so its a little hard to imagine an order of the vertices.

Well, you could cheat and turn off back face culling but later when you go to have normals and stuff, youâ€™ll wish you hadnâ€™t.

Edit: you â€śmightâ€ť wish you hadnâ€™t. Could be that it works ok for you and your use-case.

1 Like

Without backface culling it looks good, but as you said: when i switch to Lightning.j3m material, its a mess again. I am atill pretty sure, that my indexes are wrong, but I just couldnt find out how to arrange them in the correct order. I am attaching a txt file containing the coordinates of the sphere, if someone could just take a look at it, maybe somebody else can figure it out, i would appriciate that Currently the indexes look like this: 0,1,2, 1,2,3, 2,3,4, 3,4,5, 4,5,6, etc, you see the logic.

Btw dont open it in notepad, because you wont see sh*t, wordpad works fine.

http://www.fileswap.com/dl/vPiZmEOZeq/

1----2

| |

| |

0----3

Assuming you have indexed your angles this way the correct order is:

0,3,2

0,2,1

1 Like

Just to see how does it look like now (with backface culling ON, and the indexes mentioned in my previous post):

Seems heâ€™s making a triangle strip mesh, why not do that as well? Then you dont need to specify the index order. Otherwise why not look at the indexing of the jME sphere? Maybe even start from that class and just change the equations, and maybe do some extra alternatives for degenerate shapes etc.

1 Like

I set it to TriangleStrip, and removed the indexes. Its better now thx for the tip, but still something is wrong. Maybe not the indexes are the problem? The objects are look like this now, all have these stripes:

Just got something in my mindâ€¦maybe something wrong about the normalsâ€¦Iâ€™ll check it.

YES! It was the normals. Problem ALMOST solved Now only one issue left, every object has a â€śholeâ€ť (like a butthole :D) on the top and bottom side, here how it looks like on the sphere:

Thx for the help everyone btw, I try to solve this last little problem (i hope its a little problem), but any ideas are welcomed

I dunno if anyone reading this or iâ€™m talking with myself, but heres a hint, if someone is into solving this last problem: it looks like this isnt a hole (which is btw much bigger at objects like a cylinder for example), but it looks like the top and bottom of the object are mixed up. This black circle u see on the picture above is gray on the bottom. TLDR: the top is on the bottom, and the bottom is on the top.

PROBLEM SOLVED! It was the normals again. I wrote my own method to normalize the vectors and something was wrong in it, so i just picked the built in normalize() method and it works like a charm Thanks everyone again!