I have it. Well, I have something that works and something that I'm pleased with... considering I thought I was stuck earlier.
The code I've produced is a bit different, but is following the same principle. Firstly, I calculate all the Vertices I will need for the Sphere and store them in an array, along with the Normals. For this, I use:
Code: procedure TfrmMain.CalculateVertices ( X, Y, Z : GLFloat; Radius, Precision : Integer ); const Pi = 3.141592654; TwoPi = 6.283185308; PiD2 = Pi / 2; var Loop1, Loop2, Index : Integer; Theta1, Theta2, Theta3 : Double; X1, Y1, Z1, X2, Y2, Z2 : GLFloat; begin Index := 0; for Loop1 := 0 to Precision div 2 do begin Theta1 := Loop1 * TwoPi / Precision - PiD2; Theta2 := (Loop1 + 1) * TwoPi / Precision - PiD2; for Loop2 := 0 to Precision do begin Theta3 := Loop2 * TwoPi / Precision; X1 := Cos (Theta2) * Cos (Theta3); Y1 := Sin (Theta2); Z1 := Cos (Theta2) * Sin (Theta3); X2 := X + Radius * X1; Y2 := Y + Radius * Y1; Z2 := Z + Radius * Z1; Vertices[Index].X := X2; Vertices[Index].Y := Y2; Vertices[Index].Z := Z2; Vertices[Index].NX := X1; Vertices[Index].NY := Y1; Vertices[Index].NZ := Z1; Inc (Index); X1 := Cos (Theta1) * Cos (Theta3); Y1 := Sin (Theta1); Z1 := Cos (Theta1) * Sin (Theta3); X2 := X + Radius * X1; Y2 := Y + Radius * Y1; Z2 := Z + Radius * Z1; Vertices[Index].X := X2; Vertices[Index].Y := Y2; Vertices[Index].Z := Z2; Vertices[Index].NX := X1; Vertices[Index].NY := Y1; Vertices[Index].NZ := Z1; Inc (Index); end; end; FacesForSphere := Index div 2; end;
Then, in my Draw routine, I call:
Code: procedure TfrmMain.DrawSphereFromArray; var Loop, Face : Integer; begin Loop := 0; for Face := 0 to FacesForSphere - 1 do begin glBindTexture (GL_TEXTURE_2D,CoverImages[Face]); glBegin (GL_QUADS); glNormal3f (Vertices[Loop].NX,Vertices[Loop].NY,Vertices[Loop].NZ); glTexCoord2f (1,1); glVertex3f (Vertices[Loop].X,Vertices[Loop].Y,Vertices[Loop].Z); glNormal3f (Vertices[Loop + 1].NX,Vertices[Loop + 1].NY,Vertices[Loop + 1].NZ); glTexCoord2f (1,0); glVertex3f (Vertices[Loop + 1].X,Vertices[Loop + 1].Y,Vertices[Loop + 1].Z); glNormal3f (Vertices[Loop + 3].NX,Vertices[Loop + 3].NY,Vertices[Loop + 3].NZ); glTexCoord2f (0,0); glVertex3f (Vertices[Loop + 3].X,Vertices[Loop + 3].Y,Vertices[Loop + 3].Z); glNormal3f (Vertices[Loop + 2].NX,Vertices[Loop + 2].NY,Vertices[Loop + 2].NZ); glTexCoord2f (0,1); glVertex3f (Vertices[Loop + 2].X,Vertices[Loop + 2].Y,Vertices[Loop + 2].Z); glEnd; Loop := Loop + 2; end; end;
...and I end up with this... yum-yum...
I'm not sure if the code I have produced is very efficient - I'm just happy that it works - so I thought I'd post it up here so people could comment on it, or maybe even learn from it themselves... there certainly doesn't seem to be much on the Internet about creating spheres with GL_QUADS, I guess 'cos it's the most inefficient when compared to GL_QUAD_STRIP and maybe even GL_TRIANGLES.
|