2

I am trying to generate the surface of Superellipsoid in OpenGL. Since this surface can be represented using the parametric equation:

enter image description here

I generate certain amount of points on this surface varying the parameters u v and put them in array using function that looks something like this :

float du = 1.0/(float)(u_max-1);
float dv = 1.0/(float)(v_max-1);
int u, v;

vertices.reserve(u_max*v_max);
normals.reserve(u_max*v_max);
texcoords.reserve(u_max*v_max);

for(u = 0; u < u_max; u++)
for(v = 0; v < v_max; v++)
{
    texcoords.emplace(texcoords.end(),vec2(u*du,v*dv));

    float cos_v_dv=cos(2*M_PI*v*dv);
    float sin_v_dv=sin(2*M_PI*v*dv);
    float sin_u_du=sin(M_PI*u*du);
    float cos_u_du=cos(M_PI*u*du);

    // Parametric equation of the surface
    float x=A*sgn(cos_v_dv)*sgn(sin_u_du)*pow(std::abs(cos_v_dv),n)*pow(std::abs(sin_u_du),m);
    float y=B*sgn(sin_v_dv)*sgn(sin_u_du)*pow(std::abs(sin_v_dv),n)*pow(std::abs(sin_u_du),m);
    float z=C*sgn(cos_u_du)*pow(std::abs(cos_u_du),m);
    vertices.emplace(vertices.end(),x,y,z);

    // Derivative with respect to u
    float dx_du=A*sgn(cos_v_dv)*cos_u_du*pow(std::abs(cos_v_dv),n)*pow(std::abs(sin_u_du),m-1);
    float dy_du=B*sgn(sin_v_dv)*cos_u_du*pow(std::abs(sin_v_dv),n)*pow(std::abs(sin_u_du),m-1);
    float dz_du=-C*sin_u_du*pow(std::abs(cos_u_du),m-1);

    // Derivative with respect to v
    float dx_dv=-A*sgn(sin_u_du)*pow(std::abs(sin_u_du),m)*sin_v_dv*pow(std::abs(cos_v_dv),n-1);
    float dy_dv=B*sgn(sin_u_du)*pow(std::abs(sin_u_du),m)*cos_v_dv*pow(std::abs(sin_v_dv),n-1);
    // derivative of z with respect to v is 0

    //Crossing the tangent vectors to get the normal
    vec3 normal(-dz_du*dy_dv,dx_dv*dz_du,dx_du*dy_dv-dx_dv*dy_du);
    normal.normalize();
    normals.push_back(normal);
}

Then I generate the indices for Quads connecting neighboring points like :

indices.reserve(u_max * v_max * 4);
for(u = 0; u < u_max-1; u++) 
    for(v = 0; v < v_max-1; v++) 
    {
        indicies.push_back(u* v_max + v);
        indicies.push_back(u* v_max + (v+1));
        indicies.push_back((u+1)* v_max + (v+1));
        indicies.push_back((u+1)* v_max + v);
    }

The results however are quite bad... : enter image description here

Most of the time the tessellation is very poor on certain spots and too much on other. There are also some weird looking black spots that are probably caused by bad normals.At first I thought that the method with which I was generating normals is flawed.I used to calculate the normals by crossing two tangent vectors which I get by differentiating the vertices as shown in the code above eg:

So then I decided to compute the normals by crossing tangents which I get by subtracting the coordinates of two neighboring vertices. The result was exactly the same the black spots remained there and in some sense the lightning didn't look as good.

Obviously there is a very good way to do this since the wiki article shows very nice images of this surface. I am probably doing something very stupid.

So I guess my question is : Is there a good way to generate the surface of this object? How can I avoid the problems I am getting now?

3
  • 1
    My bet is that on the black spots the normal is negative (directing inwards) due to some u,v non linearity so try negate all normals to see if the black spots will be OK and the rest will be black ... if yes then adjust your OpenGL material/lighting to be double sided. if not then visualize the normals like this: stackoverflow.com/a/28261038/2521214
    – Spektre
    Feb 3, 2015 at 20:35
  • Yes you are right that inverting the normals makes the black spots white but only on one of the sides. When I actually printed out the normal components along with the u v I discovered that what I am getting on one of the sides is a bunch of NaN-s :( . The reason for this is that when u=0, my vertex is always the same independently of v. So when i subtract F(u,v+1)-F(u,v) it is 0. When I cross it with F(u+1,v)-F(u,v) the result is 0 and then the normalization causes 0/0 which is NaN. Feb 3, 2015 at 21:44
  • @Abstarction then for poles do not compute normal just use direction from center of object to the vertex instead. it is not precise but should be enough
    – Spektre
    Feb 4, 2015 at 8:40

2 Answers 2

3

Your problems are in my opinion most likely due to your sampling and due to the singularities of the parametric surface itself.

You sample the surface regularly in parameter-space (your u's and v's), giving a non-uniform sampling in geometry space (the images you show).

The singularities at the poles probably give you the shading artefacts you mention. Both in parameter space and in geometry space your calculation of the normal becomes ill conditioned, leading to "extreme" errors in your normals.

The images on wikipedia may likely be created using ray-tracing and the fully implicit form of the surface. You can attempt to do the same thing to get a better normal for your surface: Instead of differentiating the explicit parametric equation at the parameter value (u,v), differentiate the implicit equation at the position (x, y, z).

On the other hand, you may also simply try the explicitly given normals on http://www.gamedev.net/page/resources/_/technical/opengl/superquadric-ellipsoids-and-toroids-opengl-lig-r1172

2

You get a similar problem when drawing a sphere, any parametrisation of a sphere will have at least one place which is singular, parameterising by latitude and longitude break down at the north pole.

One way round this is to split the surface into two or more patches which meet along curves. There are several ways of doing this you could have two hemispheres, meeting along the equator. Quite a nice way to do it is to take a unit cube and project that along the radius vectors to the unit sphere. Once we have a point of the sphere we can get the polar coordinates and use these to find the points on the super-ellipsoid.

If u, v are the two parameters of the surface, 0<=u<=1, 0<=v<=1 one face of the cube will be given by x=2u-1, y=2v-1, z=1. We can find points on the unit sphere by dividing by the length l=sqrt(x^2+y^2+z^2), x1=x/l, y1=y/l, z1=z/l. Now find the polar coordinates th=atan2(y1, x1), phi=asin(z1) and use these to find the coordinates on the super ellipse.

I've implemented this as a fiddle in JavaScript. http://jsfiddle.net/SalixAlba/n1hjm35n/ It should be fairly straightforward to implement using OpenGL.

Super ellipsoid using cubic mesh

// Auxiliary cos function  
function auxC(w,m) {
    var c = Math.cos(w);
    return sign(c) * Math.pow(Math.abs(c),m);
}

// Auxiliary sin function  
function auxS(w,m) {
    var s = Math.sin(w);
    return sign(s) * Math.pow(Math.abs(s),m);
}

// Given a point on a sphere find the corresponding point on the super-ellipsoid
function SEvec(x,y,z) {
    var th = Math.atan2(y,x);
    var phi = Math.asin(z);
    var xx = A * auxC(phi, 2/ t) * auxC(th, 2/r);
    var yy = B * auxC(phi, 2/ t) * auxS(th, 2/r);
    var zz = C * auxS(phi, 2/ t);
    console.log(x,y,z,xx,yy,zz);
    return new THREE.Vector3(xx*100,yy*100,zz*100);
}

// Generate points for the first face
function sf1(u,v) {
    var x = 2*u-1;
    var y=  2*v-1;
    var z= 1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}

// second face
function sf2(u,v) {
    var x = 2*u-1;
    var y=  2*v-1;
    var z= -1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}
function sf3(u,v) {
    var x = 2*u-1;
    var z=  2*v-1;
    var y= 1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}
function sf4(u,v) {
    var x = 2*u-1;
    var z=  2*v-1;
    var y= -1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}
function sf5(u,v) {
    var z = 2*u-1;
    var y=  2*v-1;
    var x= 1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}
function sf6(u,v) {
    var z = 2*u-1;
    var y=  2*v-1;
    var x= -1;
    var l = Math.sqrt(x*x+y*y+z*z);
    return SEvec( x/l,y/l,z/l);
}

I've not calculated normals but you code should work. The only problem happens when you are at the north or south pole. Here you can just use the vectors (0,0,1), (0,0, -1) or you can choose odd values for the number of steps, so the poles are never included in the mesh.

Using a cube as a base should make it quite easy to match up the corresponding patches along the edges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.