Page 1 of 1

3D Knots

Posted: Fri Mar 01, 2013 5:13 pm
by applePi
this is about plotting 3D Knots using point sprites , the knot curve are going from 0 to 2*Pi radians with steps of 0.003 radian. to give the curve the appearance of the tubes we will texture every point with a picture with solid circle inside and transparent outside the circle.
the first picture used is a color wheel:
Image
http://s21.postimage.org/5mbto9l3b/color_wheel.png
whats annoying is the color moving through the tube as it is a liquid, i can't get rid of this, and every card may present it differently ,please look at the attached material script.
Image

using the second picture:
http://s21.postimage.org/8ph55dpfn/circle.png
since the inside and outside of the circle are transparent
each dot of color make a filament so the whole knot is like this:
Image
be sure your browser will save the circle.png as it is so not to destroy the transparency inside and outside the circle.
if you want to save the mesh press S key so you can use that mesh as a model, you can use the same script file to view it in a model viewer so it resembles the original one. else you will get a very thin knot. if someone can cast a shadow from it please report, it seems not possible.
i have used an old graphics card geforce 210 and the rotation of the model and zooming in/out are good , it may be very slow or will not work without a 3d card

copy the above color_wheel.png to 3D\Data\Textures folder, and copy the following script file to 3D\Data\scripts folder, and the code to the 3D folder.
if you want to use another picture change this line texture color_wheel.png in the material script file
PS: i want to remind you that the MP3D library for purebasic have added the new feature of millions of points , and now textured points. also the ability to change the color and size of points at run time so your mesh can have different point sizes, and all the points behave as one mesh .
pointsprite.material:

Code: Select all

material point_sprites
{
   receive_shadows on
   technique
   {
      pass
      {
	alpha_rejection greater 200 
	scene_blend alpha_blend 
	lighting on
	//depth_write on
	 ambient 1 1 1
	 diffuse 0 0 0 
         specular 0 0 0 0 
         point_sprites on
	 point_size 40
         //polygon_mode points
         diffuse vertexcolour
         texture_unit
          {
             texture color_wheel.png
             //colour_op alpha_blend
          }
      }
   }
}
3d knot example:

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA_ONE
   #BUTTON
   #mainwin
 EndEnumeration

Quit.b = #False
stopFlag = 1
xs.f = 0.3:ys.f = 0.3:zs.f = 0.3
x.f: y.f :z.f
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "PgUp PgD Zoom ..., space: stop/rotate ... S  save the mesh", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;Initialize environment
InitEngine3D()
InitSprite()

OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
WorldShadows(#PB_Shadow_Additive)

InitKeyboard()
SetFrameRate(60)

Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))

CreateCamera(#CAMERA_ONE, 0, 0, 100, 100)
MoveCamera(#CAMERA_ONE, 0, 4, 9)
CameraLookAt(#CAMERA_ONE, 0, 2, 0)

RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf

GetScriptMaterial(0, "point_sprites")
SetMeshMaterial(1, MaterialID(0))
DisableMaterialLighting(0, #True)
CreateMesh(1, #PB_Mesh_PointList, #PB_Mesh_Static )

SetMeshMaterial(1, MaterialID(0))
NumOfCircles = #PI*2/0.003
For PointSprite = 0 To NumOfCircles
        t.f+0.003
        x = Cos(t) + 1.5*Cos(-2*t)
        y = Sin(t) + 1.5*Sin(-2*t)
        ;;;;z = 0.35 * Sin(3*t)
        z = 1 * Sin(3*t)
        ;Trefoil knot:
        ;x = 0.01 *(41 *Cos(t) - 18 *Sin(t) - 83* Cos(2* t)- 83 *Sin(2* t) - 11 *Cos(3* t) + 27* Sin(3* t))
        ;y = 0.01 *(36 *Cos(t) + 27 *Sin(t) - 113* Cos(2* t)+ 30 *Sin(2* t) + 11 *Cos(3* t) - 27 * Sin(3* t))
        ;z = 0.01 *(45 *Sin(t) - 30 *Cos(2* t) + 113* Sin(2* t)- 11 *Cos(3* t) + 27 *Sin(3* t))
        ; another knot
        ;x = Cos(2*t) + 0.75*Cos(5*t)
        ;y = Sin(2*t) + 0.75*Sin(5*t)
        ;z = 0.4*Sin(6*t)
        MeshVertexPosition(x, y, z)
                 
      Next 
NormalizeMesh(1)
FinishMesh(#True)

CreateEntity(1, MeshID(1), MaterialID(0))
ScaleEntity(1,2,2,2)
MoveEntity(1,0,1,-2)

turn.b=0: rot.f=0.5
Repeat
  Event = WindowEvent()
   y+rot
   RotateEntity(1,0,y,0)
   
   RenderWorld()
   FlipBuffers()

   ExamineKeyboard()
   If KeyboardReleased(#PB_Key_Space)
     If turn
       rot=0.5
     Else
       rot=0
    EndIf
    turn ! 1 
  
  ElseIf KeyboardPushed(#PB_Key_PageUp) ; scale up model
    xs.f = 1.1:ys.f = 1.1:zs.f = 1.1
    ScaleEntity(1,xs,ys,zs)
   
  ElseIf KeyboardPushed(#PB_Key_PageDown) ; scale down model
    xs = 0.9:ys = 0.9:zs= 0.9
    ScaleEntity(1,xs,ys,zs)
  ElseIf KeyboardPushed(#PB_Key_S)  
    SaveMesh(1, "knot.mesh")
  EndIf
  
   If KeyboardPushed(#PB_Key_Escape)
      Quit = #True
   EndIf
   
Until Quit = #True Or Event = #PB_Event_CloseWindow