Jump to content
  • Advertisement

Archived

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

emilbbb

Sphere

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 Radius < 0 then Radius := -Radius;
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;
glBegin(GL_QUAD_STRIP);
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/

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!