I am trying to generate the surface of Superellipsoid in OpenGL. Since this surface can be represented using the parametric equation:
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... :
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?
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