Post by xenovacivus on Dec 2, 2006 2:09:31 GMT -5
This program uses a set of control points to create bezier curves, then interpolate between the curves to get a smooth set of points. It's pretty late right now, so I'm not going to go into depth about it or apologize for the horrific code.
Click and drag control points to move them,
up and down arrows to change curvature percent,
space bar to toggle control points shown/hidden,
right click and drag mouse to rotate scene.
There's no method to save yet, but you can still take screen shots! I might add a saving feature in there later...
Enjoy!
Click and drag control points to move them,
up and down arrows to change curvature percent,
space bar to toggle control points shown/hidden,
right click and drag mouse to rotate scene.
There's no method to save yet, but you can still take screen shots! I might add a saving feature in there later...
Enjoy!
' 3D Bezier Curves!
' By Ben Porter
CONST S = 7 ' Number of control points along x and z axises
CONST P = 4 ' Number of sub-divided polygons inside grid square
dim x#, y#, z#
dim i, j
' XXXXXXXXXXXXXXXXXXXXX=====================XXXXXXXXXXXXXXXXXXXXX '
' ==================== Ye Olde Lighting! ===================== '
' XXXXXXXXXXXXXXXXXXXXX=====================XXXXXXXXXXXXXXXXXXXXX '
' Set a lightsource up, have it green.
glLightfv(gl_Light1, gl_Ambient, vec4 (.5, .2, .2, 1))' light thats always there.
glLightfv(gl_Light1, gl_Diffuse, vec4 (0, 1, .2, 1)) ' light coming from a source.
glLightfv(gl_Light1, gl_Position,vec4 (0, -10, 50, 10))' where the light source is.
glEnable(gl_Light1)
' Set up another lightsource, have this one blue.
glLightfv(gl_Light2, gl_Ambient, vec4 (.5, .2, .8, 1))
glLightfv(gl_Light2, gl_Diffuse, vec4 (.0, .2, 1, 1))
glLightfv(gl_Light2, gl_Position,vec4 (0, 10, 50, 10))
glEnable (gl_Light2)
' XXXXXXXXXXXXXXXXXXXXX=====================XXXXXXXXXXXXXXXXXXXXX '
' ==================== A SPHERE!!! ===================== '
' XXXXXXXXXXXXXXXXXXXXX=====================XXXXXXXXXXXXXXXXXXXXX '
glNewList (2, GL_COMPILE)
for i = 0 to 10
glBegin (GL_QUAD_STRIP)
for j = 0 to 10
x# = .25 * cos (i * 2*M_PI / 10) * sin (j * M_PI / 10)
y# = .25 * sin (i * 2*M_PI / 10) * sin (j * M_PI / 10)
z# = .25 * cos (j * M_PI / 10)
glNormal3f (x#*2, y#*2, z#*2)
glVertex3f (x#, y#, z#)
i = i + 1
x# = .25 * cos (i * 2*M_PI / 10) * sin (j * M_PI / 10)
y# = .25 * sin (i * 2*M_PI / 10) * sin (j * M_PI / 10)
z# = .25 * cos (j * M_PI / 10)
glNormal3f (x#*2, y#*2, z#*2)
glVertex3f (x#, y#, z#)
i = i - 1
next
glEnd ()
next
glEndList ()
dim Overlap# : Overlap# = .25
TextMode (TEXT_OVERLAID)
dim Mesh# (S)(S)(2)
for i = 0 to S
for j = 0 to S
Mesh# (i)(j) = vec3 (i-(S*.5), 0, j-(S*.5))
next
next
struc BMeshPoint
dim Main# (2)
dim iTangent# (2)
dim jTangent# (2)
endStruc
dim BMeshPoint BMesh (S)(S)
gosub CalcBMesh
dim _1# (2)
dim _2# (2)
dim _3# (2)
dim _4# (2)
dim _t#, _n#, _m#
dim m, n
dim vPoints# (S * P)(S * P)(2)
dim CurvePoint1#(2), CurvePoint2#(2)
dim iPoint1#(2), iPoint2#(2)
for i = 0 to S
for j = 0 to S
gosub CalcVPoints
next
next
dim MousePosition# (1)
dim Point_3D# (2)
dim CameraMatrix# (3)(3) : CameraMatrix# = MatrixIdentity ()
dim TipAngle#, TurnAngle#, TurnAngleStart#, TipAngleStart# : TipAngle# = 30
dim FieldOfView# : FieldOfView# = 60
dim AspectRatio# : AspectRatio# = (1.0*WindowWidth()) / WindowHeight ()
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
gluPerspective (FieldOfView#, AspectRatio#, 1, 3000)
glMatrixMode(GL_MODELVIEW)
dim sI
dim sJ
dim Closest#
' Control Point Control
dim ShowControl : ShowControl= true
dim SpaceDown
while true
if ScanKeyDown (VK_SPACE) then
if not SpaceDown then
ShowControl = not ShowControl
endif
SpaceDown = true
else SpaceDown = false
endif
MousePosition# = Vec2 ((Mouse_X () - .5) * -2 * AspectRatio#, (Mouse_Y () - .5) * 2)
if Mouse_Button (MOUSE_RBUTTON) then
TurnAngle# =-MousePosition# (0) * 90 +TurnAngleStart#
TipAngle# = MousePosition# (1) * 90 +TipAngleStart#
else
TurnAngleStart# = MousePosition# (0) * 90 + TurnAngle#
TipAngleStart# = -MousePosition# (1) * 90 + TipAngle#
endif
CameraMatrix# = MatrixTranslate (0, 0, -10) * MatrixRotateX (TipAngle#) * MatrixRotateY (TurnAngle#)
Point_3D# (0) = MousePosition# (0) * Tand (.5 * FieldOfView#) * -5
Point_3D# (1) = MousePosition# (1) * Tand (.5 * FieldOfView#) * -5
Point_3D# (2) = -5
Point_3D# = Normalize (Point_3D#)
if Mouse_Button (MOUSE_LBUTTON) then
if sI >= 0 then
Mesh# (sI)(sJ) = RTInvert (CameraMatrix#) * (Point_3D# * Closest#)
gosub CalcBMesh
for i = sI-2 to sI +1
for j = sJ-2 to sJ +1
gosub CalcVPoints
next
next
endif
else
sI = -1
Closest# = 100
for i = 0 to S
for j = 0 to S
_2# = CameraMatrix# * BMesh(i)(j).Main#
_1# = Point_3D# * Length (_2#)
if Length (_1# - _2#) < .25 then
if Closest# > Length (_2#) then
Closest# = Length (_2#)
sI = i
sJ = j
endif
endif
next
next
endif
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
cls
glLoadMatrixf (CameraMatrix#)
glEnable (GL_LIGHTING)
glDisable (GL_LIGHT2)
if ShowControl then
for i = 0 to S
for j = 0 to S
if i=sI and j = sJ then
glEnable (GL_LIGHT2)
else
glDisable (GL_LIGHT2)
endif
glPushMatrix ()
_1# = BMesh (i)(j).Main#
glTranslatef (_1#(0), _1#(1), _1#(2))
glCallList (2)
glPopMatrix ()
next
next
endif
glEnable (GL_LIGHT2)
glDisable (GL_LIGHTING)
'//Grid
glColor3f (.3, .3, .3)
glBegin (GL_LINES)
for i = -5 to 5
glVertex3f (i, 0, -5)
glVertex3f (i, 0, 5)
next
glEnd ()
glBegin (GL_LINES)
for i = -5 to 5
glVertex3f (-5, 0, i)
glVertex3f (5, 0, i)
next
glEnd ()
'glBegin (GL_LINES)
' for i = 0 to 3
' for j = 0 to 3
' glColor3f (0, 1, 0)
' glVertex3fv (BMesh (i)(j).Main#)
' glVertex3fv (BMesh (i)(j).Main# + BMesh (i)(j).iTangent# * Overlap#)
'
' glColor3f (1, 0, 0)
' glVertex3fv (BMesh (i)(j).Main#)
' glVertex3fv (BMesh (i)(j).Main# + BMesh (i)(j).jTangent# * Overlap#)
' next
' next
'glEnd ()
glEnable (GL_LIGHTING)
glBegin (GL_QUADS)
for i = 0 to S*P-1
for j = 0 to S*P-1
glNormal3fv (-Normalize (CrossProduct (vPoints# (i)(j) - vPoints# (i+1)(j), vPoints# (i)(j) - vPoints# (i)(j+1))))
glVertex3fv (vPoints# (i)(j))
glVertex3fv (vPoints# (i+1)(j))
glVertex3fv (vPoints# (i+1)(j+1))
glVertex3fv (vPoints# (i)(j+1))
next
next
glEnd ()
glDisable (GL_LIGHTING)
if ScanKeyDown (VK_UP) then
Overlap# = Overlap# + .01
for i = 0 to S-1
for j = 0 to S-1
gosub CalcVPoints
next
next
endif
if ScanKeyDown (VK_DOWN) then
Overlap# = Overlap# - .01
for i = 0 to S-1
for j = 0 to S-1
gosub CalcVPoints
next
next
endif
Printr int (Overlap# *200) + " Percent Curvature"
DrawText ()
SwapBuffers ()
wend
Bezier:
_1# = (_1#*Pow (1-_t#, 3) + 3*_2#*_t#*Pow (1-_t#, 2) + 3*_3#*Pow (_t#, 2)*(1-_t#) + _4#*Pow (_t#, 3))
return
CalcBMesh:
for i = 0 to S
for j = 0 to S
BMesh (i)(j).Main# = Mesh# (i)(j)
if i > 0 then
BMesh (i)(j).iTangent# = Mesh# (i-1)(j)
else
BMesh (i)(j).iTangent# = Mesh# (i)(j)
endif
if i < S then
BMesh (i)(j).iTangent# = BMesh (i)(j).iTangent# - Mesh# (i+1)(j)
else
BMesh (i)(j).iTangent# = BMesh (i)(j).iTangent# - Mesh# (i)(j)
endif
if j > 0 then
BMesh (i)(j).jTangent# = Mesh# (i)(j-1)
else
BMesh (i)(j).jTangent# = Mesh# (i)(j)
endif
if j < S then
BMesh (i)(j).jTangent# = BMesh (i)(j).jTangent# - Mesh# (i)(j+1)
else
BMesh (i)(j).jTangent# = BMesh (i)(j).jTangent# - Mesh# (i)(j)
endif
BMesh (i)(j).iTangent# = BMesh (i)(j).iTangent#
BMesh (i)(j).jTangent# = BMesh (i)(j).jTangent#
next
next
return
CalcVPoints:
if i >= 0 and i<S and j>=0 and j<S then
for m = 0 to (P-1)-(i=S-1)
for n = 0 to (P-1)-(j=S-1)
_m# = (m*1.0)/(P*1.0)
_n# = (n*1.0)/(P*1.0)
_t# = _m#
_1# = BMesh (i)(j).Main#
_2# = BMesh (i)(j).Main# - BMesh (i)(j).iTangent# * Overlap#
_3# = BMesh (i+1)(j).Main# + BMesh (i+1)(j).iTangent# * Overlap#
_4# = BMesh (i+1)(j).Main#
gosub Bezier
CurvePoint1# = _1#
j = j + 1
_1# = BMesh (i)(j).Main#
_2# = BMesh (i)(j).Main# - BMesh (i)(j).iTangent# * Overlap#
_3# = BMesh (i+1)(j).Main# + BMesh (i+1)(j).iTangent# * Overlap#
_4# = BMesh (i+1)(j).Main#
j = j - 1
gosub Bezier
CurvePoint2# = _1#
iPoint1# = CurvePoint1# * (1-_n#) + CurvePoint2# * _n#
_t# = _n#
_1# = BMesh (i)(j).Main#
_2# = BMesh (i)(j).Main# - BMesh (i)(j).jTangent# * Overlap#
_3# = BMesh (i)(j+1).Main# + BMesh (i)(j+1).jTangent# * Overlap#
_4# = BMesh (i)(j+1).Main#
gosub Bezier
CurvePoint1# = _1#
i = i + 1
_1# = BMesh (i)(j).Main#
_2# = BMesh (i)(j).Main# - BMesh (i)(j).jTangent# * Overlap#
_3# = BMesh (i)(j+1).Main# + BMesh (i)(j+1).jTangent# * Overlap#
_4# = BMesh (i)(j+1).Main#
i = i - 1
gosub Bezier
CurvePoint2# = _1#
iPoint2# = CurvePoint1# * (1-_m#) + CurvePoint2# * _m#
vPoints# (i*P+m)(j*P+n) = iPoint2# * .5 + iPoint1# *.5
next
next
endif
return