1

I've been looking for hours now to find a good way to draw a textured sphere in JOGL. All I need is a point in the right direction or some code that works for someone. So far all I have been able to find is helloTexture (while it works, it is obviously not a sphere), would there be a way to convert this into a sphere or should i try my luck somewhere else?

elect
  • 6,765
  • 10
  • 53
  • 119
  • 1
    You can build on that. Just add more vertices to form a sphere and draw the triangles between those vertices. If you don't know how I'd suggest you look for a tutorial on drawing triangles first and try to understand how that works. Going to a sphere or any other shape from there is just a matter of drawing more triangles. – Thomas Feb 16 '16 at 15:41
  • Take a look at this answer. Should be the same principle,. just coded in c++: http://stackoverflow.com/questions/7687148/drawing-sphere-in-opengl-without-using-glusphere – flakes Feb 16 '16 at 15:48
  • 1
    better not @flkes, that uses deprecated `GL_QUADS` – elect Feb 16 '16 at 16:51

1 Answers1

1

I wrote this for you, it's untested since I modified it to work with GL_TRIANGLES, try and let me know

radius is the sphere radius, rings are the horizontal slices, sectors the vertical ones

private void createGeometry(float radius, short rings, short sectors)   {

    float R = 1f / (float)(rings - 1);
    float S = 1f / (float)(sectors - 1);
    short r, s;
    float x, y, z;

    points = new float[rings * sectors * 3];
    normals = new float[rings * sectors * 3];
    texcoords = new float[rings * sectors * 2];

    int t = 0, v = 0, n = 0;
    for(r = 0; r < rings; r++) {
        for(s = 0; s < sectors; s++) {
            x = (float) (Math.cos(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R ));
            y = (float) Math.sin(-Math.PI / 2 + Math.PI * r * R );
            z = (float) (Math.sin(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R ));

            texcoords[t++] = s * S;
            texcoords[t++] = r * R;

            points[v++] = x * radius;
            points[v++] = y * radius;
            points[v++] = z * radius;

            normals[n++] = x;
            normals[n++] = y;
            normals[n++] = z;
        }


    }
    int counter = 0;
    indices = new short[rings * sectors * 6];
    for(r = 0; r < rings - 1; r++){
        for(s = 0; s < sectors-1; s++) {
            indices[counter++] = (short) (r * sectors + s);      
            indices[counter++] = (short) (r * sectors + (s + 1));   
            indices[counter++] = (short) ((r + 1) * sectors + (s + 1));  
            indices[counter++] = (short) ((r + 1) * sectors + (s + 1)); 
            indices[counter++] = (short) (r * sectors + (s + 1));    
            indices[counter++] = (short) ((r + 1) * sectors + s);    
        }
    }
}
elect
  • 6,765
  • 10
  • 53
  • 119
  • How would i display something like this? I got it to work after putting a float[] in before points, normals, and textcoords. Thank you for this though. – some lizard Feb 16 '16 at 17:35
  • Yes, basically in my sample you can put together `points` and `texcoords` inside `vertexData`. `indices` inside `indexData`. Skip `normals` for the moment, you don't need those unless you perform also lighting. Modify the vertex attribute parameters accordingly, since now you position is size 3 instead 2 ;) – elect Feb 16 '16 at 17:49
  • I'm still not understanding. Sorry I suck at this... How do I put in the points and texcoors? I tried putting them in like this: `private float[] vertexData = new float[]{ texcoords, points }; private short[] indexData = new short[]{ indicies };` , but that just gives me a blue screen. Sorry if i'm missing something very obvious... – some lizard Feb 16 '16 at 18:12
  • :D, sorry it's 10pm here, open an issue on github on my project [here](https://github.com/elect86/helloTriangle/issues), let's continue over there and I'll help you ;) – elect Feb 16 '16 at 21:05