Reputation: 95
I want to draw a sphere and texture it,i draw it by triangles and when i am trying to texture it some triangles aren't covered
i am using this function to generate the Coordinates
public void createSolidSphere()
{
float R = (float) (1./(float)(rings-1));
float S = (float) (1./(float)(sectors-1));
int r, s;
int texCoordsIndex = -1;
int verticesIndex = -1;
int normalsIndex = -1;
int indicesIndex = -1;
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
float y = (float)Math.sin( -Math.PI/2 + Math.PI * r * R );
float x = (float)Math.cos(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );
float z = (float)Math.sin(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );
texcoords[++texCoordsIndex] = s*S;
texcoords[++texCoordsIndex] = r*R;
vertices[++verticesIndex] = x * radius;
vertices[++verticesIndex] = y * radius;
vertices[++verticesIndex] = z * radius;
normals[++normalsIndex] = x;
normals[++normalsIndex] = y;
normals[++normalsIndex] = z;
}
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;
}
}
Upvotes: 1
Views: 167
Reputation: 210909
You've to draw quads rather than triangles. A quad can be formed by 2 triangles.
Each quad consist of 4 points:
0: r * sectors + (s+1)
1: (r+1) * sectors + (s+1)
2: (r+1) * sectors + s
3: r * sectors + s
This 4 points can be arranged to 2 triangles:
2 1
+ +-------+
| \ \ |
| \ \ |
| \ \ |
+------+ +
3 0
You've to add 6 indices for each quad rather than 3:
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
// triangle 1
indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;
// triangle 2
indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;
indices[++indicesIndex] = r * sectors + s+;
}
Upvotes: 1