### Author Topic: Make Simple 3d Objects with functions.  (Read 5496 times)

#### Hemlos

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1634
• Particle Hawk
##### Make Simple 3d Objects with functions.
« on: 2006-Oct-10 »
Here's some source code for simple 3D objects with textures.

A Sphere
Code: GLBasic [Select]
// ------------------------------------------------------------- //
// -=#  SPHERE  #=-
// ------------------------------------------------------------- //
FUNCTION CreateSphere: num, r, n, col
LOCAL i,j, theta1, theta2, theta3, pi
pi = ACOS(0)*2
IF r < 0 THEN r = -r
IF n < 3 THEN n = 3

X_OBJSTART num
FOR j=0 TO n/2-1

theta1 = j * 2*pi / n - pi/2;
theta2 = (j + 1) * 2*pi / n - pi/2;
FOR i=0 TO n
theta3 = i * 2*pi / n;
X_OBJADDVERTEX r*COS(theta2) * COS(theta3), r*SIN(theta2), r*COS(theta2) * SIN(theta3), i/n, 2*(j+1)/n, col
X_OBJADDVERTEX r*COS(theta1) * COS(theta3), r*SIN(theta1), r*COS(theta1) * SIN(theta3), i/n, 2*j/n, col
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION // n
A Cube - each side has the same texture
Code: GLBasic [Select]
// ------------------------------------------------------------- //
// -=#  CREATECUBE  #=-
// ------------------------------------------------------------- //
FUNCTION CreateCube: num, sz, col
// Diese Variablen sind als LOCAL definiert:
// num, sz,
sz=sz/2
X_OBJSTART num
// Front Face
X_OBJADDVERTEX  sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz,  sz, 0, 1, col
X_OBJNEWGROUP
// Back Face
X_OBJADDVERTEX -sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 0, 0, col
X_OBJNEWGROUP
// Top Face
X_OBJADDVERTEX -sz,  sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJNEWGROUP
// Bottom Face
X_OBJADDVERTEX  sz, -sz, -sz, 0, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJNEWGROUP
// Right face
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJNEWGROUP
// Left Face
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz, -sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJNEWGROUP
X_OBJEND

ENDFUNCTION // sz
A Clyinder and a Cone
Code: GLBasic [Select]
// ------------------------------------------------------------- //
// -=#  CREATECYLINDER - also creates a cone if any radius = 0  #=-
// ------------------------------------------------------------- //
FUNCTION CreateCylinder: num, baseRadius, topRadius, height, slices, stacks, col
LOCAL da, r, dr, dz, x, y, z, i, j
LOCAL x1, y1, x2, y2
da = 360 / slices
dr = (topRadius-baseRadius) / stacks
dz = height / stacks
X_OBJSTART num
FOR i=0 TO slices-1
x1 = COS(i*da)
y1 = SIN(i*da)
x2 = COS((i+1)*da)
y2 = SIN((i+1)*da)
z = 0
r = baseRadius

// Bottom lid
IF baseRadius>0
X_OBJADDVERTEX 0, 0, 0,  .5, .5, col
X_OBJADDVERTEX x1*r, y1*r, 0, x1/2+.5, y1/2+.5, col
X_OBJADDVERTEX x2*r, y2*r, 0, x2/2+.5, y2/2+.5, col
X_OBJNEWGROUP
ENDIF
// side
FOR j=0 TO stacks
X_OBJADDVERTEX x2*r, y2*r, z, (i+1)/slices,  j/stacks, col
X_OBJADDVERTEX x1*r, y1*r, z,  i   /slices,  j/stacks, col
z = z+dz
r = r+dr
NEXT
// Top lid
IF topRadius>0
X_OBJNEWGROUP
X_OBJADDVERTEX x2*r, y2*r, height, x2/2+.5, y2/2+.5, col
X_OBJADDVERTEX x1*r, y1*r, height, x1/2+.5, y1/2+.5, col
X_OBJADDVERTEX 0, 0, height,  .5, .5, col
ENDIF
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION
A Torus (Donut)
Code: GLBasic [Select]
// ------------------------------------------------------------- //
// -=#  CREATETORUS  #=-
//
// By Samuel R. Buss
// http://math.ucsd.edu/~sbuss/MathCG
// ------------------------------------------------------------- //
FUNCTION CreateTorus: num, MinorRadius, MajorRadius, NumWraps, NumPerWrap, TextureWrapVert, TextureWrapHoriz
// Diese Variablen sind als LOCAL definiert:
// x, y,
// Draw the torus
LOCAL i, di, j, wrapFrac, wrapFracTex, phi, thetaFrac, thetaFracTex, theta
LOCAL x, y, z, r
X_OBJSTART num
FOR di=0 TO NumWraps-1
FOR j=0 TO NumPerWrap
FOR i=di+1 TO di STEP -1
wrapFrac = MOD(j, NumPerWrap)/NumPerWrap
wrapFracTex = j/NumPerWrap
phi = 360*wrapFrac
thetaFrac = (MOD(i, NumWraps)+wrapFracTex)/NumWraps
thetaFracTex = (i+wrapFracTex)/NumWraps
theta = 360*thetaFrac
r = MajorRadius + MinorRadius*COS(phi)
x = SIN(theta)*r
z = COS(theta)*r
y = MinorRadius*SIN(phi)
X_OBJADDVERTEX x,y,z,  thetaFracTex*TextureWrapVert, wrapFracTex*TextureWrapHoriz, RGB(255, 255, 255)
NEXT
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION

« Last Edit: 2008-Sep-01 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### FutureCow

• HelpEditor
• Prof. Inline
• Posts: 680
##### Re: Make Simple 3d Objects with functions.
« Reply #1 on: 2009-Dec-11 »
A variation on CreateCube for creating Rectangular Prisms (ie. a 3D Box with equal or unequal sides)

Creating a 3D Box  (AKA Rectangular prism)
Code: GLBasic [Select]
// ------------------------------------------------------------- //
// -=#  CREATERECTANGULARPRISM #=-
// ------------------------------------------------------------- //
FUNCTION CreateRectangularPrism: num, Length, Width, Height, col
//          +---+
//         /   /|
//        /   / |
//       /   /  |
//      /   /   |
//     /   /    |
// H  +---+     /  h
// e  |   |    /  t
// i  |   |   /  g
// g  |   |  /  n
// h  |   | /  e
// t  |   |/  L
//    +---+
//
//   Width

X_OBJSTART num
// Front Face
X_OBJADDVERTEX  Width,       0,  Length, 1, 0, col
X_OBJADDVERTEX      0,       0,  Length, 0, 0, col
X_OBJADDVERTEX  Width,  Height,  Length, 1, 1, col
X_OBJADDVERTEX      0,  Height,  Length, 0, 1, col
X_OBJNEWGROUP
// Back Face
X_OBJADDVERTEX      0,  Height,       0, 1, 1, col
X_OBJADDVERTEX      0,       0,       0, 1, 0, col
X_OBJADDVERTEX  Width,  Height,       0, 0, 1, col
X_OBJADDVERTEX  Width,       0,       0, 0, 0, col
X_OBJNEWGROUP
// Top Face
X_OBJADDVERTEX      0,  Height,  Length, 0, 0, col
X_OBJADDVERTEX      0,  Height,       0, 0, 1, col
X_OBJADDVERTEX  Width,  Height,  Length, 1, 0, col
X_OBJADDVERTEX  Width,  Height,       0, 1, 1, col
X_OBJNEWGROUP
// Bottom Face
X_OBJADDVERTEX  Width,       0,       0, 0, 1, col
X_OBJADDVERTEX      0,       0,       0, 1, 1, col
X_OBJADDVERTEX  Width,       0,  Length, 0, 0, col
X_OBJADDVERTEX      0,       0,  Length, 1, 0, col
X_OBJNEWGROUP
// Right face
X_OBJADDVERTEX  Width,  Height,       0, 1, 1, col
X_OBJADDVERTEX  Width,       0,       0, 1, 0, col
X_OBJADDVERTEX  Width,  Height,  Length, 0, 1, col
X_OBJADDVERTEX  Width,       0,  Length, 0, 0, col
X_OBJNEWGROUP
// Left Face
X_OBJADDVERTEX      0,       0,  Length, 1, 0, col
X_OBJADDVERTEX      0,       0,       0, 0, 0, col
X_OBJADDVERTEX      0,  Height,  Length, 1, 1, col
X_OBJADDVERTEX      0,  Height,       0, 0, 1, col
X_OBJNEWGROUP
X_OBJEND
ENDFUNCTION // CREATERECTANGULARPRISM