Author Topic: Einfache 3D Objekte  (Read 4210 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10726
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Einfache 3D Objekte
« on: 2004-Mar-05 »
Hier ist der Quellcode für einfache 3D Objekte:

Eine Kugel
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


EinWürfel - jede Seite hat die gleiche Textur
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 Zylinder oder Kegel(stumpf)
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

Ein 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

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10726
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Einfache 3D Objekte
« Reply #1 on: 2004-Mar-05 »
Noch ein paar Helix-Varianten:

Code: (glbasic) [Select]
// ------------------------------------------------------------- //
// -=#  CREATEHELIX  #=-
// Creates a helix aka. spiral object
// R1    : Raduis of Helix
// R2    : Radius of Helix Intersection
// Nu    : Number of elemtents along helix
// Nv    : Number of elements around intersection
// cycles: Number or spiral repetitions
// height: Complete length of the helix
// col   : Color of Helix-Material
// ------------------------------------------------------------- //
FUNCTION CreateHelix: num, R1, R2, Nu, Nv, cycles, height, col
LOCAL du, dv, u, v, x, y, z, tx, ty, q, nu, nv
du = cycles * 360 / Nu
dv = 360 / Nv

X_OBJSTART num
FOR i=0 TO Nu-1
u = i * du
FOR j=0 TO Nv-1
v = j * dv
FOR q = 0 TO 3
nu=0; nv=0 // Use next u/next v

IF q=0 OR q=3 THEN nu=1
IF q>1 THEN nv=1
nu = MOD(q, 2)
nv=INTEGER(q / 2)
qu = u+nu*du
qv = v+nv*dv

x = R1 * COS(qu) * (1 + COS(qv) * R2/R1)
z = R1 * SIN(qu) * (1 + COS(qv) * R2/R1)
y = SIN(qv)*R2 + qu*height/(360 * cycles)
tx = (i+nu)/Nu * cycles
ty = (j+nv)/Nv
X_OBJADDVERTEX x, y, z, tx, ty, col
NEXT
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION

Helicoid
Code: (glbasic) [Select]
// ------------------------------------------------------------- //
// -=#  CREATEHELICOID  #=-
// Creates a helicoid - minimal surface of a helix
// R1    : Raduis of Helix
// Nu    : Number of elemtents along helix
// Nv    : Number of elements around intersection
// cycles: Number or spiral repetitions
// height: Complete length of the helix
// col   : Color of Helix-Material
// ------------------------------------------------------------- //
FUNCTION CreateHelicoid: num, R1, Nu, Nv, cycles, height, col
LOCAL du, dv, u, v, x, y, z, tx, ty, q, nu, nv
du = cycles*360 / Nu
dv = R1 / Nv

X_OBJSTART num
FOR i=0 TO Nu-1
u = i * du
FOR j=0 TO Nv-1
v = j * dv
FOR q = 0 TO 3
nu=0; nv=0 // Use next u/next v

IF q=0 OR q=3 THEN nu=1
IF q>1 THEN nv=1
nu = MOD(q, 2)
nv=INTEGER(q / 2)
qu = u+nu*du
qv = v+nv*dv

x = qv * COS(qu)
z = qv * SIN(qu)
y = qu/(cycles*360) * height
tx = (i+nu)/Nu * cycles
ty = (j+nv)/Nv
X_OBJADDVERTEX x, y, z, tx, ty, col
NEXT
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION