Polygon to Circle

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Polygon to Circle

Post by applePi »

an adaptation of this example(*) in 4 flavors
approximating a circle shape by increasing the polygon edges
1- using points list:
i have found this keyword (point_size 2) inside file "PureBasic\Examples\3D\Data\Scripts\Examples.material". its material name
material Examples/FlarePointSprite. so using it for big points size 2 to display the vertexes of the polygon
set Library subsystem to opengl to get more correct point sprite size

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  CompilerIf Subsystem("OpenGL") = #False
    MessageRequester("Error", "Please set the Library subsystem to OpenGL from IDE : Compiler... Compiler Options")
    End
  CompilerEndIf
CompilerEndIf
Declare drawCircle()

#Font = 9
#Info = 10
Global R.f = 10  ; Radius of circle.
Global X.f = 0.0; X-coordinate of center of circle.
Global Y.f = 0.0; Y-coordinate of center of circle.
Global numVertices = 5;  Number of vertices on circle.


Define.f KeyX, KeyY, MouseX, MouseY
  InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  LoadFont(#Font, "Courier New", 10,#PB_Font_Bold) ; for the info text
  
  ExamineDesktops()
  OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "from polygon to sphere,..using pointList.. press +/- to inc/dec number of points", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)   
      
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(".", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    CreateSprite(#Info, 230, 60 )
    
    ;- Material
    ;CreateMaterial(0, LoadTexture(0, "White.jpg"))
    ;DisableMaterialLighting(0, #True)
    CreateMaterial(1, LoadTexture(1, "ValetCoeur.jpg"))
    GetScriptMaterial(0, "Examples/FlarePointSprite")
    DisableMaterialLighting(0, #True)
    
    drawCircle()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 40, #PB_Absolute)
    CameraFOV(0, 40)
    CameraLookAt(0, 0,  0,  0)
    CameraBackColor(0, RGB(0, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(255, 255, 255))
          
    CreateSphere(1,0.5): CreateEntity(1, MeshID(1), MaterialID(1) ) 
    
    Repeat
    
      event = WindowEvent()
      
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Add)
      ;If KeyboardReleased(#PB_Key_Add)
        numVertices + 1
        FreeMesh(0)
        drawCircle()
        
      ElseIf KeyboardPushed(#PB_Key_Subtract)  
      ;ElseIf KeyboardReleased(#PB_Key_Subtract)
        numVertices - 1 
        If numVertices < 3: numVertices = 3: EndIf
        FreeMesh(0)
        drawCircle()
        
        
      EndIf

      rot.f+0.5
      RotateEntity(0, 0.0, 0.0, rot.f, #PB_Absolute) 
      ;rot = EntityRoll(0, #PB_Absolute | #PB_Engine3D_Adjusted)
             
      RenderWorld()
      
      StartDrawing(SpriteOutput(#Info))
          Box(0,0,350,120,RGB(40,40,0))
          DrawingFont(FontID(#Font)) 
          DrawText(2,22,"number of polygon edges = "+Str(numVertices),RGB(0,255,255),RGB(40,40,40))
      StopDrawing()
      DisplaySprite(#Info,20,20)
        
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow
 
    End
 
   
   Procedure drawCircle()
   CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Dynamic)      
   ; Drawing routine.
   t.f = 0  ;Angle parameter.
   i.l;
    
      For i = 0 To numVertices-1 ; try for i = 0 To numVertices
               
        MeshVertexPosition(X + R * Cos(t), Y + R * Sin(t), 0)
        MeshVertexColor(RGB(255, 255, 0))
        ;MeshVertexColor(RGB(Random(255,0),Random(255,0),Random(255,0)))
              
        t + (2 * #PI / numVertices); 
               
      
      Next
   FinishMesh(#True)
    
    SetMeshMaterial(0, MaterialID(0))
    CreateEntity(0, MeshID(0), MaterialID(0))
    
    EndProcedure
    
    
    
2- using Lines strip
Image

set Library subsystem to opengl since we want to draw thick edges using glLineWidth_(5) which affects line thickness in ogre 3D graphics

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  CompilerIf Subsystem("OpenGL") = #False
    MessageRequester("Error", "Please set the Library subsystem to OpenGL from IDE : Compiler... Compiler Options")
    End
  CompilerEndIf
CompilerEndIf


Declare drawCircle()
Declare recreatePolygon()
#Font = 9
#Info = 10
Global R.f = 10  ; Radius of circle.
Global X.f = 0.0; X-coordinate of center of circle.
Global Y.f = 0.0; Y-coordinate of center of circle.
Global numVertices = 5;  Number of vertices on circle.

Define.f KeyX, KeyY, MouseX, MouseY
  InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  LoadFont(#Font, "Courier New", 10,#PB_Font_Bold) ; for the info text
  
  ExamineDesktops()
  OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "from polygon to sphere,...using LineStrip......  press +/- to inc/dec number of edges", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)   
      
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(".", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    CreateSprite(#Info, 230, 60 )
        
  
    
    ;- Material
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    CreateMaterial(1, LoadTexture(1, "Geebee2.bmp"))

    drawCircle()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 40, #PB_Absolute)
    CameraFOV(0, 40)
    CameraLookAt(0, 0,  0,  0)
    CameraBackColor(0, RGB(250, 250, 250))
    
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(255, 255, 255))
    glLineWidth_(5) ; opengl function works in ogre if the subsystem is opengl
      
    CreateSphere(1,0.5): CreateEntity(1, MeshID(1), MaterialID(1) ) 
        
    Repeat
    
      event = WindowEvent()
      
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Add)
      ;If KeyboardReleased(#PB_Key_Add)
        numVertices + 1 
        FreeMesh(0)
        drawCircle()
        
      ElseIf KeyboardPushed(#PB_Key_Subtract)
      ;ElseIf KeyboardReleased(#PB_Key_Subtract)
        numVertices - 1 
        If numVertices < 3: numVertices = 3: EndIf
        FreeMesh(0)
        drawCircle()
        
        
      EndIf
      
      rot.f+0.5
      RotateEntity(0, 0.0, 0.0, rot.f, #PB_Absolute) 
              
      RenderWorld()
      
      StartDrawing(SpriteOutput(#Info))
          Box(0,0,350,120,RGB(40,40,0))
          DrawingFont(FontID(#Font)) 
          DrawText(2,22,"number of polygon edges = "+Str(numVertices),RGB(0,255,255),RGB(40,40,40))
      StopDrawing()
      DisplaySprite(#Info,20,20)
        
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow
 
    End
    
    Procedure drawCircle()
   CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Dynamic)      
   ; Drawing routine.
   t.f = 0  ;Angle parameter.
   i.l;
    
      For i = 0 To numVertices
               
        MeshVertexPosition(X + R * Cos(t), Y + R * Sin(t), 0)
        MeshVertexColor(RGB(Random(255,0),Random(255,0),Random(255,0)))
              
        t + (2 * #PI / numVertices); 
        
      Next
   FinishMesh(#True)
    
    SetMeshMaterial(0, MaterialID(0))
    CreateEntity(0, MeshID(0), MaterialID(0))
    
    EndProcedure
    

    
    
3- using Lines list :
just replace the procedure drawCircle with this

Code: Select all

 Procedure drawCircle()
      CreateMesh(0, #PB_Mesh_LineList, #PB_Mesh_Dynamic)      
      ;CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Dynamic) 
   ; Drawing routine.
   t.f = 0  ;Angle parameter.
   i.l;
    
      For i = 0 To numVertices-1
               
        MeshVertexPosition(X + R * Cos(t), Y + R * Sin(t), 0)
        MeshVertexColor(RGB(Random(255,0),Random(255,0),Random(255,0)))
                
        t + (2 * #PI / numVertices); 
        
        MeshVertexPosition(X + R * Cos(t), Y + R * Sin(t), 0)
        MeshVertexColor(RGB(Random(255,0),Random(255,0),Random(255,0)))
        
      
      Next
   FinishMesh(#True)
    
    SetMeshMaterial(0, MaterialID(0))
    CreateEntity(0, MeshID(0), MaterialID(0))
    
    EndProcedure
 
4- CreateLine3D
note that every line is a mesh, i prefer the line list or the line strip method since we deal with the many lines as a one mesh instead of many

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  CompilerIf Subsystem("OpenGL") = #False
    MessageRequester("Error", "Please set the Library subsystem to OpenGL from IDE : Compiler... Compiler Options")
    End
  CompilerEndIf
CompilerEndIf


Declare drawCircle()
Declare recreatePolygon()
#Font = 9
#Info = 10
Global R.f = 10  ; Radius of circle.
Global X.f = 0.0; X-coordinate of center of circle.
Global Y.f = 0.0; Y-coordinate of center of circle.
Global numVertices = 5;  Number of vertices on circle.

Define.f KeyX, KeyY, MouseX, MouseY
  InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  LoadFont(#Font, "Courier New", 10,#PB_Font_Bold) ; for the info text
  
  ExamineDesktops()
  OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "from polygon to sphere,..using CreateLine3D..... press +/- to inc/dec number of edges", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)   
      
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(".", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    CreateSprite(#Info, 230, 60 )
    
    ;- Material
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    CreateMaterial(1, LoadTexture(1, "Geebee2.bmp"))

    drawCircle()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 40, #PB_Absolute)
    CameraFOV(0, 40)
    CameraLookAt(0, 0,  0,  0)
    CameraBackColor(0, RGB(250, 250, 250))
    
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(255, 255, 255))
    glLineWidth_(5) ; opengl function works in ogre if the subsystem is opengl
      
    CreateSphere(1,0.5): CreateEntity(1, MeshID(1), MaterialID(1) ) 
    
        
    Repeat
    
      event = WindowEvent()
      
      ExamineKeyboard()
      ;If KeyboardPushed(#PB_Key_Add)
      If KeyboardReleased(#PB_Key_Add)
        
        numVertices + 1 
        
        drawCircle()
        
      ;ElseIf KeyboardPushed(#PB_Key_Subtract)
      ElseIf KeyboardReleased(#PB_Key_Subtract)
        For i = 0 To numVertices+1 ; to free the previouly made meshes
          FreeMesh(i)
        Next
        
        numVertices - 1 
        If numVertices < 3: numVertices = 3: EndIf
                
        drawCircle()
        
        
      EndIf
      
      rot.f+0.5
      ;RotateEntity(0, 0.0, 0.0, rot, #PB_Absolute) 
      ;RotateNode(30,0,1,rot, #PB_Absolute)   
      RenderWorld()
      
      StartDrawing(SpriteOutput(#Info))
          Box(0,0,350,120,RGB(40,40,0))
          DrawingFont(FontID(#Font)) 
          DrawText(2,22,"number of polygon edges = "+Str(numVertices),RGB(0,255,255),RGB(40,40,40))
      StopDrawing()
      DisplaySprite(#Info,20,20)
        
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow

    End
    
    Procedure drawCircle()
      
   ; Drawing routine.
   t.f = 0  ;Angle parameter.
   i.l;
    
      For i = 0 To numVertices+1
               
        x1.f = X + R * Cos(t) : y1.f = Y + R * Sin(t)
        t + (2 * #PI / numVertices); increase angle
        x2.f = X + R * Cos(t) : y2.f = Y + R * Sin(t)
         
        CreateLine3D(i, x1, y1, 0, RGB(255,   0,   250), x2,  y2,  0, RGB(255,   255,   0)) 
       
      Next
    
    EndProcedure
 
    
    
(*)ref: http://www.sumantaguha.com/downloads : Experimenter Source.zip : chapter 2: circle