OpenGL texture on sphere
Posted
by
Cilenco
on Game Development
See other posts from Game Development
or by Cilenco
Published on 2013-10-13T18:09:20Z
Indexed on
2013/11/13
4:16 UTC
Read the original article
Hit count: 496
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?
© Game Development or respective owner