April 29, 2024, 11:35:27 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


3D Cylinder function By Jolly Roger

Started by pistol350, September 05, 2007, 01:09:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pistol350

A function that you can use to create various custom shapes like cylinders for instance.


'Cylinder function
'Jolly Roger 2003
'Requires IBasic 1.97 or greater.

IF GETDXVERSION < 7
   MESSAGEBOX 0,"This program requires" + chr$(13) + "DirectX 7.0 or greater","Error"
   END
ENDIF

DECLARE "kernel32",GetTickCount(),int
DEF win:window
DEF scene,camera,light,lastframetime,timesincelastframe,cyl:int

'Line to declare the create cylinder function **********************
DECLARE cylinder(numberofsections:INT,topfacepresent:INT,bottomfacepresent:INT,smooth:INT)
'*******************************************************************

'open a window
WINDOW win,0,0,640,480,@CAPTION|@NOAUTODRAW,0,"Cylinder function",mainwindow
IF CREATE3DSCREEN(win,640,480) <> 0
   MESSAGEBOX win, "Could not create Direct3D screen","Error"
   CLOSEWINDOW win
   END
ENDIF

D3DSETQUALITY win,@LIGHTON | @FILLSOLID | @SHADEGOURAUD
FRONTPEN win,RGB(255,255,255)
BACKPEN win,0
DRAWMODE win,@TRANSPARENT

'The parent scene frame
scene = D3DSCENE(win)
D3DCOMMAND scene,@SETSCENEBACKCOLOR,.4,.4,.4

'Create and position the camera
camera = D3DCAMERA(scene)
D3DCOMMAND camera,@SETPOSITION,scene,0,0,-10
D3DCOMMAND camera,@SETORIENTATION,scene,0,0,1,0,1,0

'create and position a light source
light = D3DLIGHT(scene,@LIGHTDIRECTIONAL,1.2,1.2,1.2)
D3DCOMMAND light,@SETORIENTATION,scene,-1,-1,1, -1,1,1

'Line to create cylinder ************************
cyl=cylinder(8,1,1,0)
'************************************************

'Use the line below to save the object as an x file if required
'D3DCOMMAND cyl,@SAVESHAPE,GETSTARTPATH+"cyl.x",@FORMATTEXT,@SAVENORMALS

lastframetime=GetTickCount()-1
run=1
'process messages until somebody closes us
waituntil run=0

'delete all the frames
D3DDELETE light
D3DDELETE camera
D3DDELETE scene
IF cyl<>0 THEN D3DDELETE cyl
closewindow win
end


SUB mainwindow
SELECT @class
   CASE @IDCHAR
      if (@CODE = ASC("Q")) | (@CODE = ASC("q")) THEN run = 0
   CASE @IDCREATE
      centerwindow win
   CASE @IDCLOSEWINDOW
      run=0
   CASE @IDDXUPDATE
      'find time since last frame to make things run at a constant speed
      timesincelastframe=GetTickCount()-lastframetime
      if timesincelastframe>2
      lastframetime=GetTickCount()
          mult=timesincelastframe/8
      'The object can be rotated in the scene using the arrow keys
      IF cyl<>0
      IF(GETKEYSTATE(0x26))
         D3DCOMMAND cyl,@ADDROTATION,1,0,0,(-.5 * (3.1415/180))*mult
      ENDIF
      IF(GETKEYSTATE(0x28))
         D3DCOMMAND cyl,@ADDROTATION,1,0,0,(.5 * (3.1415/180))*mult
      ENDIF
      IF(GETKEYSTATE(0x25))
         D3DCOMMAND cyl,@ADDROTATION,0,1,0,(-.5 * (3.1415/180))*mult
      ENDIF
      IF(GETKEYSTATE(0x27))
         D3DCOMMAND cyl,@ADDROTATION,0,1,0,(.5 * (3.1415/180))*mult
      ENDIF
      ENDIF
      'render the scene to the DirectX surface
      D3DRENDER scene,camera
      'add any 2D elements after the scene is rendered.
      MOVE win,0,10
      PRINT win,"Move the object using the arrow keys"
      'show the DirectX surface   
      DXFLIP win,0,0
  endif
ENDSELECT
RETURN

SUB cylinder(numberofsections,topfacepresent,bottomfacepresent,smooth)
'Function creates a cylinder with the given number of sections.Minimum number of sections is three.
'topfacepresent,bottomfacepresent and smooth should be 0 or 1.
'If smooth is set to one then cylinder sides will appear smooth if @SHADEGOURAUD used.
'If smooth set to zero then sides will appear flat even if @SHADEGOURAUD is used.
'Jolly Roger March 2003
DEF shape:INT
IF numberofsections>2
  shape=D3DSHAPE(scene,@SHAPECUSTOM)
  numberofvertices=numberofsections*2
  DEF vertex[numberofvertices],normal[numberofsections],facedata[((2*numberofsections)+5)]:INT
  'Create vertices and side face normals
  FOR section=0 TO numberofsections-1
    x=SIN(6.28*section/numberofsections)
    z=COS(6.28*section/numberofsections)
    IF smooth=0
      normal[section]=D3DCOMMAND(shape,@ADDNORMAL,SIN(6.28*(section+.5)/numberofsections),0,COS(6.28*(section+.5)/numberofsections))
    ELSE
      normal[section]=D3DCOMMAND(shape,@ADDNORMAL,x,0,z)
    ENDIF
    vertex[section]=D3DCOMMAND (shape,@ADDVERTEX,x,1,z)
    vertex[section+numberofsections]=D3DCOMMAND (shape,@ADDVERTEX,x,-1,z)
  NEXT section
  'Create faces
  IF topfacepresent=1
     'Top face
     facedata[0]=numberofsections
     topnormal=D3DCOMMAND(shape,@ADDNORMAL,0,1,0)
     FOR section=0 TO numberofsections-1
       facedata[section*2+1]=vertex[section],topnormal
     NEXT section
     facedata[2*numberofsections+1]=0
     D3DCOMMAND (shape,@ADDFACES,facedata)
  ENDIF
  IF bottomfacepresent=1
     'Bottom face
     facedata[0]=numberofsections
     bottomnormal=D3DCOMMAND(shape,@ADDNORMAL,0,-1,0)
     FOR section=0 TO numberofsections-1
       facedata[section*2+1]=vertex[2*numberofsections-section-1],bottomnormal
     NEXT section
     facedata[2*numberofsections+1]=0
     D3DCOMMAND (shape,@ADDFACES,facedata)
  ENDIF
  'Side faces
  facedata[0]=4
  FOR section=0 TO numberofsections-1
    facedata[1]=vertex[section],normal[section],vertex[section+numberofsections],normal[section]
    IF section<>numberofsections-1
      facedata[5]=vertex[section+1+numberofsections],normal[section+1],vertex[section+1],normal[section+1]
    ELSE
      facedata[5]=vertex[numberofsections],normal[0],vertex[0],normal[0]
    ENDIF
    IF smooth=0
      facedata[6]=normal[section]
      facedata[8]=normal[section]
    ENDIF
    facedata[9]=0
    D3DCOMMAND (shape,@ADDFACES,facedata)
  NEXT section
  D3DCOMMAND shape,@CUSTOMINIT
ENDIF
RETURN shape
Regards,

Peter B.