yy=((1-Sign(-xx-0.9+Abs(zz*2)))/3*(Sign(0.9-xx)+1)/3)*(Sign(xx+0.65)+1)/2 -((1-Sign(-xx-0.39+Abs(zz*2)))/3*(Sign(0.9-xx)+1)/3) + ((1-Sign(-xx-0.39+Abs(zz*2)))/3*(Sign(0.6-xx)+1)/3)*(Sign(xx-0.35)+1)/2
i will use the example posted in the PB german forum by STARGÅTE called (ger to eng) "Drawing3D - unfinished code for drawing commands in 3D space"
http://forums.purebasic.com/german/view ... 10&t=22128
the function of A available on a site now closed but see it in web.archive.org here with other functions
http://web.archive.org/web/201107040542 ... d/examples
in opengl i think we can texture it.
the benefits of the STARGÅTE example is that is has several 3D functions with an addon: the ability to move the camera viewpoint, rotation, and zoom in_out.
i want to use only the PLOT3D so i take it only from his big example, to plot the 'A' function, just use the (arrow keys and m,n) to rotate the graphics around the graphics own local coordinates and not the global coordinates, if someone provide a way to rotate it in the world coordinates will be great. i admit i'm poor in the 3D math envolved.
the image for the A function is this
to compare your PLOT3D with the PLOT3D of the mathematica wolfram alpha click here to plot the mathematica function Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}]
http://www.wolframalpha.com/input/?i=Pl ... 2C+2%7D%5D
to plot it in the attached code comment the 'A' function and uncomment the "yy=Sin(xx+Pow(zz,2))" your Graphics should be like this:
the example uses a computaional heavy calculations, and it will slow down the pc if we choose the steps for xx,zz less than 0.05, so surely there are possible improvements, what about copying the results of the first calc to memory then after that the rotation, zooming, moving etc will apply to the matrix in the memory and not to calc again and again, just a suggestion.
note:use the mouse to move the Graphics, arrows +(m+n) to rotate, z+x to zoom
below is the STARGATE code, i have just plugged in the functions, and changing the arrow keys to rotate the Graphics in its place
Code: Select all
Structure Drawing3D_Vector
x.f : y.f : z.f
EndStructure
Structure Drawing3D_Matrix
a.f[9]
EndStructure
Structure Drawing3D_Camera
FieldOfVision.f
Position.Drawing3D_Vector
Rotation.Drawing3D_Vector
View.Drawing3D_Matrix
EndStructure
Structure Drawing3D
Camera.Drawing3D_Camera
Rotation.Drawing3D_Vector
View.Drawing3D_Matrix
Distance.f
EndStructure
Macro Drawing3D_UpdateMatrix(Matrix, Rotation)
Matrix\a[0] = Cos(Rotation\y)*Cos(Rotation\z)
Matrix\a[1] = Cos(Rotation\y)*Sin(Rotation\z)
Matrix\a[2] = -Sin(Rotation\y)
Matrix\a[3] = Cos(Rotation\z)*Sin(Rotation\x)*Sin(Rotation\y)-Cos(Rotation\x)*Sin(Rotation\z)
Matrix\a[4] = Cos(Rotation\x)*Cos(Rotation\z)+Sin(Rotation\x)*Sin(Rotation\y)*Sin(Rotation\z)
Matrix\a[5] = Cos(Rotation\y)*Sin(Rotation\x)
Matrix\a[6] = Cos(Rotation\x)*Cos(Rotation\z)*Sin(Rotation\y)+Sin(Rotation\x)*Sin(Rotation\z)
Matrix\a[7] = -Cos(Rotation\z)*Sin(Rotation\x)+Cos(Rotation\x)*Sin(Rotation\y)*Sin(Rotation\z)
Matrix\a[8] = Cos(Rotation\x)*Cos(Rotation\y)
EndMacro
Macro Drawing3D_MatrixTimesVector(Result, Matrix, Vector)
Result\x = Vector\x * Matrix\a[0] + Vector\y * Matrix\a[1] + Vector\z * Matrix\a[2]
Result\y = Vector\x * Matrix\a[3] + Vector\y * Matrix\a[4] + Vector\z * Matrix\a[5]
Result\z = Vector\x * Matrix\a[6] + Vector\y * Matrix\a[7] + Vector\z * Matrix\a[8]
EndMacro
Macro Drawing3D_MoveVector(Vector, Move)
Vector\x + Move\x
Vector\y + Move\y
Vector\z + Move\z
EndMacro
Global Drawing3D.Drawing3D
With Drawing3D
Drawing3D_UpdateMatrix(\Camera\View, \Camera\Rotation)
Drawing3D_UpdateMatrix(\View, \Rotation)
\Camera\FieldOfVision = #PI/3
EndWith
Procedure StartDrawing3D(OutputID)
If StartDrawing(OutputID)
Drawing3D_UpdateMatrix(Drawing3D\Camera\View, Drawing3D\Camera\Rotation)
Drawing3D_UpdateMatrix(Drawing3D\View, Drawing3D\Rotation)
Drawing3D\Distance = Sqr(OutputWidth()*OutputWidth()+OutputHeight()*OutputHeight())/Tan(Drawing3D\Camera\FieldOfVision/2)/2
ProcedureReturn #True
EndIf
EndProcedure
Procedure StopDrawing3D()
StopDrawing()
EndProcedure
Procedure Drawing3DFieldOfVision(Angle)
Drawing3D\Camera\FieldOfVision = Angle*#PI/180
EndProcedure
Procedure Drawing3DCameraPosition(x.f, y.f, z.f, Mode=#PB_Absolute)
With Drawing3D\Camera\Position
If Mode = #PB_Absolute
\x = x : \y = y : \z = z
Else
\x + x : \y + y : \z + z
EndIf
EndWith
EndProcedure
Procedure Drawing3DRotation(x.f, y.f, z.f, Mode=#PB_Absolute)
With Drawing3D\Rotation
If Mode = #PB_Absolute
\x = x : \y = y : \z = z
Else
\x = x : \y = y : \z = z
EndIf
EndWith
Drawing3D_UpdateMatrix(Drawing3D\View, Drawing3D\Rotation)
EndProcedure
Procedure Drawing3DCameraRotation(x.f, y.f, z.f, Mode=#PB_Absolute)
With Drawing3D\Camera\Rotation
If Mode = #PB_Absolute
\x = x : \y = y : \z = z
Else
\x + x : \y + y : \z + z
EndIf
EndWith
Drawing3D_UpdateMatrix(Drawing3D\Camera\View, Drawing3D\Camera\Rotation)
EndProcedure
Procedure Plot3D(x.f, y.f, z.f, Color=#PB_Default)
Protected.Drawing3D_Vector Position, NewPosition, ViewPosition, Distance
Position\x = x : Position\y = y : Position\z = z
Drawing3D_MatrixTimesVector(Distance, Drawing3D\View, Position)
Drawing3D_MoveVector(Distance, -Drawing3D\Camera\Position)
Drawing3D_MatrixTimesVector(NewPosition, Drawing3D\Camera\View, Distance)
ViewPosition\x = NewPosition\x * Drawing3D\Distance / NewPosition\z + OutputWidth()*0.5
ViewPosition\y = NewPosition\y * Drawing3D\Distance / NewPosition\z + OutputHeight()*0.5
If NewPosition\z > 0
If ViewPosition\x >= 0 And ViewPosition\x < OutputWidth()-1 And ViewPosition\y >= 0 And ViewPosition\y < OutputHeight()-1
If Color=#PB_Default
Plot(ViewPosition\x, ViewPosition\y)
Else
Plot(ViewPosition\x, ViewPosition\y, Color)
EndIf
EndIf
EndIf
EndProcedure
InitSprite()
InitKeyboard()
InitMouse()
xP = 800
yP = 800
xP2 = xP/2
yP2 = yP/2
OpenWindow(0, 0, 0, xP, yP, "use mouse to move the plot, arrows +(m+n) to rotate, z+x to zoom", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, xP, yP, 0, 0, 0)
#Size = 15
PosZ.f = -#Size*4
rotX.f=0
rotY.f=0
rotZ.f=0
Procedure.f Tropfen(x.f, y.f, Phase.f)
ProcedureReturn Sin(Sqr(x*x+y*y)*0.5+Phase)*Sqr(1-x*x/#Size/#Size)*Sqr(1-y*y/#Size/#Size)*#Size/5
EndProcedure
Repeat
Delay(5)
WindowEvent()
ClearScreen(0)
Phase.f + 0.1
StartDrawing3D(ScreenOutput())
DrawingMode(1)
Drawing3DFieldOfVision(75)
Drawing3DCameraPosition(PosX, 0, PosZ)
Drawing3DRotation(rotX, rotY, rotZ, #PB_Relative)
; Punkte
xx.f=-3:zz.f=-2:yy.f=0
While zz<=2
While xx <=3
xx=xx+0.05
;yy=Sin(xx+Pow(zz,2))
;function of 'A':
yy=((1-Sign(-xx-0.9+Abs(zz*2)))/3*(Sign(0.9-xx)+1)/3)*(Sign(xx+0.65)+1)/2 -((1-Sign(-xx-0.39+Abs(zz*2)))/3*(Sign(0.9-xx)+1)/3) + ((1-Sign(-xx-0.39+Abs(zz*2)))/3*(Sign(0.6-xx)+1)/3)*(Sign(xx-0.35)+1)/2
Plot3D(xx*5, yy*5, zz*5, $50F0A0)
Wend
xx=-3:zz=zz+0.05
Wend
Drawing3DCameraRotation(-MouseDeltaY()/100, MouseDeltaX()/100, 0, #PB_Relative)
StopDrawing3D()
FlipBuffers()
ExamineMouse()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : End : EndIf
If KeyboardPushed(#PB_Key_Z) : PosZ + 1 : EndIf
If KeyboardPushed(#PB_Key_X) : PosZ - 1 : EndIf
If KeyboardPushed(#PB_Key_Up) : rotX + 0.05 : EndIf
If KeyboardPushed(#PB_Key_Down) : rotX - 0.05 : EndIf
If KeyboardPushed(#PB_Key_Left) : rotY - 0.05 : EndIf
If KeyboardPushed(#PB_Key_Right) : rotY + 0.05 : EndIf
If KeyboardPushed(#PB_Key_M) : rotZ + 0.05 : EndIf
If KeyboardPushed(#PB_Key_N) : rotZ - 0.05 : EndIf
ForEver