1
\$\begingroup\$

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:

enter image description here

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.

\$\endgroup\$
  • \$\begingroup\$ Sounds to me like you are generating it twice. \$\endgroup\$ – jgallant Nov 1 '16 at 14:24
  • \$\begingroup\$ This looks like what you get in Blender when you create an UV Sphere with 8 segments and 4 rings. \$\endgroup\$ – Philipp Nov 1 '16 at 15:52
2
\$\begingroup\$

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;

        var points=vec4(radiusSphere * x,radiusSphere * y,radiusSphere * z,1);
        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.

enter image description here

\$\endgroup\$
  • 3
    \$\begingroup\$ Can you go into further detail how you calculated the exact vertex coordinates? \$\endgroup\$ – Philipp Nov 1 '16 at 15:57
  • \$\begingroup\$ @JasonLiu, I really need help with this. Do you mind sharing how you created your sphere? \$\endgroup\$ – Daniel_1985 Nov 2 '16 at 20:50
  • \$\begingroup\$ 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. \$\endgroup\$ – JasonLiu Nov 4 '16 at 18:56
  • \$\begingroup\$ @Naith Is my answer useful to you? \$\endgroup\$ – JasonLiu Nov 7 '16 at 19:14
  • \$\begingroup\$ @JasonLiu, sorry for late answer. Your answer has been a great help, big thanks to you! I will mark it as the correct answer. \$\endgroup\$ – Daniel_1985 Nov 8 '16 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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