# 3D sphere generation

I have found a 3D sphere in a closed-source game engine that I really like the look of and would like to have something similar in my own 3D engine.

This is how the sphere looks like when it is created in the game engine, at program/game start:

At program start, a function named CreateSphere is called and the user has the option to choose a 3D position and a radius of the sphere. That's all I know about the function since the engine is closed source.

Anyone have any idea of how this sphere might be generated programmatically? I have checked other posts/sites discussing spheres but none of them has the look of the sphere in the image.

Edit: removed some unnecessary information to get to the point of what I need help with.

• Sounds to me like you are generating it twice. Nov 1, 2016 at 14:24
• This looks like what you get in Blender when you create an UV Sphere with 8 segments and 4 rings. Nov 1, 2016 at 15:52

I created sphere before. My way is, generated all longitude points according to the latitude points. Then, store these points in longitude order for drawing longitude lines, store these points in latitude order for drawing latitude lines. So actually all the points will be use two times.

This is the way i created all the points first.

    for(var latNumber = 0; latNumber <= latitudeBands; latNumber ++)
{
var theta2 = latNumber * Math.PI / latitudeBands;
var sinTheta = Math.sin(theta2);
var cosTheta = Math.cos(theta2);
for(var longNumber = 0; longNumber <= longitudeBands; longNumber ++)
{
var phi2 = longNumber * 2 * Math.PI / longitudeBands;
var sinPhi = Math.sin(phi2);
var cosPhi = Math.cos(phi2);
var x = cosPhi * sinTheta;
var y = cosTheta;
var z = sinPhi * sinTheta;

vertexPositionData.push(points);

}
}


Then i store all the latitude points and all the longitude points.

for(var latNumber = 0; latNumber < latitudeBands; latNumber ++)
{
for(var longNumber = 0; longNumber <= longitudeBands; longNumber ++)
{

pointsArray.push(vertexPositionData[(latNumber*(longitudeBands+1))+longNumber]);
colorsArray.push(sphereColor);
}
}

for(var latNumber = 0; latNumber < latitudeBands; latNumber ++)
{
for(var longNumber = 0; longNumber <= longitudeBands; longNumber ++)
{
pointsArray.push(vertexPositionData[(longNumber*(latitudeBands+1))+latNumber]);
colorsArray.push(sphereColor);
}
}


Then i connect all the latitude points into circle. same as longitude points.

   for(var i=0; i<=latitudeBands; i++)
gl.drawArrays( gl.LINE_STRIP, i*(longitudeBands+1),longitudeBands+1);

offset = latitudeBands*(longitudeBands+1);

for(var j=0; j<=longitudeBands; j++)
gl.drawArrays( gl.LINE_STRIP, j*(latitudeBands+1)+offset,latitudeBands+1);


This is not a best way to draw shpere. but it can successfully draw sphere.

This is the picture i have.

• Can you go into further detail how you calculated the exact vertex coordinates? Nov 1, 2016 at 15:57
• @JasonLiu, I really need help with this. Do you mind sharing how you created your sphere? Nov 2, 2016 at 20:50
• Sorry, these days, i'm busy with my work. This is the way i created my sphere. But i prefer to use triangle to create sphere. I pasted my code here. This part is how to generate all points. Nov 4, 2016 at 18:56
• @Naith Is my answer useful to you? Nov 7, 2016 at 19:14
• @JasonLiu, sorry for late answer. Your answer has been a great help, big thanks to you! I will mark it as the correct answer. Nov 8, 2016 at 14:38