This is source code for drawing a sphere.
but I can not draw a sphere. It was just drawing to a circular cylinder.
what’s the matter?
You know I changed ‘glTexCoord2f, glVertex3f’ into ‘glVertexPointer, glDrawArrays’.
like this…
glBegin(GL_TRIANGLE_FAN);
for (i=0; i < NumVertices; i++)
{
glTexCoord2f(Vertices[i].u, Vertices[i].v);
glVertex3f(Vertices[i].x, Vertices[i].y, Vertices[i].z);
}
glEnd();
→
glVertexPointer(3, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, NumVertices);
#define PI 3.1415926535897f
#define DTOR (PI / 180.0f)
typedef unsigned int DWORD;
typedef struct {
float x, y, z;
DWORD color;
float u, v;
} VERTEX;
static VERTEX *Vertices;
void GenerateSphere(float radius, int theta, int phi, float hTile, float vTile)
{
int dtheta = 10;
int dphi = 10;
int n = 0;
int i;
float vx, vy, vz;
float mag;
int NumVertices = (int)((360/dtheta)*(360/dphi)*4);
Vertices = malloc (NumVertices * sizeof(*Vertices));
if (Vertices == NULL)
{
puts("Memory allocation error.");
exit(1);
}
for (phi=0; phi <= 360 - dphi; phi += (int)dphi) {
for (theta=0; theta <= 360 - dtheta; theta += (int)dtheta) {
Vertices[n].x = radius * sin(phi*DTOR) * cos(DTOR*theta);
Vertices[n].y = radius * sin(phi*DTOR) * sin(DTOR*theta);
Vertices[n].z = radius * cos(phi*DTOR);
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
n++;
Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(theta*DTOR);
Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(theta*DTOR);
Vertices[n].z = radius * cos((phi+dphi)*DTOR);
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
n++;
Vertices[n].x = radius * sin(DTOR*phi) * cos(DTOR*(theta+dtheta));
Vertices[n].y = radius * sin(DTOR*phi) * sin(DTOR*(theta+dtheta));
Vertices[n].z = radius * cos(DTOR*phi);
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
n++;
if (phi >-180 && phi < 180) {
Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(DTOR*(theta+dtheta));
Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(DTOR*(theta+dtheta));
Vertices[n].z = radius * cos((phi+dphi)*DTOR);
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt((vx * vx) + (vy * vy) + (vz * vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;
n++;
}
}
}
glVertexPointer(3, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, NumVertices);
free(Vertices);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPushMatrix();
glColor3f(0.2f, 0.7f, 0.7f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texName[0]);
GenerateSphere(0.65f, 50, 50, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glFlush();
}
[ November 01, 2004: Message edited by: James Kay ]