#### Archived

This topic is now archived and is closed to further replies.

# Sphere

This topic is 6255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can I do a Sphere in OpenGL without using gluSphere()?

##### Share on other sites
This code will create a texture mapped sphere in Delphi. It''s easily translated as I translated it from C. Original credit for this code goes to Paul Bourke.

// Create a Sphere at C, of Radius, with N faces (div 2).
procedure CreateSphere(CX, CY, CZ, Radius : glFloat; N : Integer);
var
I, J : integer;
theta1 : GLFloat;
theta2 : GLFloat;
theta3 : GLFloat;
X, Y, Z : GLFloat;
px, py, pz : GLFloat;
begin
if n < 0 then n := -n;
if (n < 4) OR (Radius <= 0) then
begin
glBegin(GL_POINTS);
glVertex3f(CX, CY, CZ);
glEnd();
Exit;
end;

for J := 0 to N DIV 2 - 1 do
begin
theta1 := J * 2 * PI / N - PI / 2;
theta2 := (J + 1) * 2 * PI / n - PI / 2;
for I := 0 to N do
begin
theta3 := i * 2 * PI / N;
x := cos(theta2) * cos(theta3);
y := sin(theta2);
z := cos(theta2) * sin(theta3);
px := CX + Radius * x;
py := CY + Radius * y;
pz := CZ + Radius * z;

glNormal3f(X, Y, Z);
glTexCoord2f(1 - I / n, 2 * (J + 1) / n);
glVertex3f(px, py, pz);

X := cos(theta1) * cos(theta3);
Y := sin(theta1);
Z := cos(theta1) * sin(theta3);
px := CX + Radius * X;
py := CY + Radius * Y;
pz := CZ + Radius * Z;

glNormal3f(X, Y, Z);
glTexCoord2f(1 - i / n, 2 * j / n);
glVertex3f(px, py, pz);
end;
glEnd();
end;
end;

Jason
The original code is here... http://astronomy.swin.edu.au/pbourke/opengl/sphere/

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 90
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009620
• ### Who's Online (See full list)

There are no registered users currently online

×