2
\$\begingroup\$

I want to create a rolling, textured ball in OpenGL ES 1.0 for Android. With this function I can create a sphere:

public Ball(GL10 gl, float radius) 
{
    ByteBuffer bb = ByteBuffer.allocateDirect(40000);
    bb.order(ByteOrder.nativeOrder());

    sphereVertex = bb.asFloatBuffer();
    points = build();
}

private int build() 
{
    double dTheta = STEP * Math.PI / 180;
    double dPhi = dTheta;

    int points = 0;

    for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi)
    {
        for(double theta = 0.0; theta <= (Math.PI * 2); theta+=dTheta)
        {
            sphereVertex.put((float) (raduis * Math.sin(phi) * Math.cos(theta)));
            sphereVertex.put((float) (raduis * Math.sin(phi) * Math.sin(theta)));
            sphereVertex.put((float) (raduis * Math.cos(phi)));

            points++;
        }
    }

    sphereVertex.position(0);
    return points;
}

public void draw() 
{
    texture.bind();

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, sphereVertex);

    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

My problem now is that I want to use this texture for the sphere but then only a black ball is created (of course because the top right corner s black). I use this texture coordinates because I want to use the whole texture:

0|0    0|1    1|1    1|0

That's what I learned from texturing a triangle. Is that incorrect if I want to use it with a sphere? What do I have to do to use the texture correctly?

\$\endgroup\$
1
\$\begingroup\$

Use an icosahedral sphere instead of a polar-coordinates one.

The net for the icosahedron can be laid out nicely, like this:

enter image description here

In a texture editing program, it's easy to UVMap an icosahedron with good/regular texel shapes.

\$\endgroup\$
0
\$\begingroup\$

Texture Coordinates -in a very technical terms-, are part of a parametric space that is a mapping between surface positions usually in model space and those texture coordinate. Those surface position are mapped to the parametric space using some sort of function. In other words each texture coordinate is a result of mapping a surface position to a parametric space (2D in case of 2D images).

The point that I wanted to explain is that texture coordinates are used to determine how each texture pixel(also called texel) can be mapped to a surface position.

Now back to your problem, given the above definition you need to assign each vertex its 2D texture coordinates in order to correctly map your 2D image to the 3D sphere. OpenGL uses per vertex attributes and can't simply use the given four texture coordinates values and imply the rest of the texture coordinates for each vertex. You either need to generate your texture coordinates using some sort of function or use a modelling package where an artist usually assigns each vertex its texture coordinate.

There are many ways you can generate texture coordinates a sphere but since you are generating a UV sphere, what best fits you is called the naiive appraoch in this page to summerize the approach:

  • Directly map latitude-longitude onto sphere
  • use single rectangular texture with a proportion of 2x1
  • texture coordinates: u goes from 0 to 1 around the equator, v goes from 0 to 1 from pole to pole
  • pro: simple, most data sources are already in this format
  • con: wastes the majority of the texture resolution at the poles, where it is usually least needed
\$\endgroup\$
  • \$\begingroup\$ okay tahnks for that. In theorie I understnad the method but I'm not sure how I should generate the texture coordinates. Do you maybe have a piece of code for me? \$\endgroup\$ – Cilenco Oct 13 '13 at 22:30

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.