here comes my first feasibility study to make a 3D Engine with Opengl. I got me MP3D commands and make a little Lib-testcodes
Her you can get a instruction how to create a lib. These code works fine to create a opengl lib in the opengl subsystem folder
https://www.purebasic.fr/english/viewtopic.php?t=78275
P.S. Here I also want to continue publishing useful Opengl codes and Updates
Some Updates for functions. It is possible to create a working 3d lib with tailbite. Next step ist to manipulate vertex to create meshs and loading mesh...
Greetings Michael Paulwitz
Version V.0.00.07
Changes: Add Rectangle, Pyramid, Sphere, Cylinder and Open Cylinder, Textur, Hide Entity, Color functions, 5 times faster,
AddVertex, Move, Normalize Load x And Load stl funktion, Add Alpha and Blendmode, First sprites added
News tested with PB 5.73 for WIN10 x86/x64, Ubuntu 20.04.4/X64 and macOS Catalina 10.15.5/X64
And now the program is to lib, 81k souce, here the download link
MP3D_OpenGL_Library.pb
https://c.web.de/@334971174352067001/4Y ... YsRLrmR4Gg
needed Image1.bmp for Alphatest
https://c.web.de/@334971174352067001/OW ... JLu9cUiOng
Some test code
Code: Select all
;- ProgrammStart
Titel.s = "3D Print of 3D Meshs with yellow light"
MP_Graphics3D (640,480,0,3) ; Create a Windows with 3D Funktion #Window = 0
SetWindowTitle(0, Titel) ; Name of Windows
NewList Mymesh.i() ; Here comes my Mesh
For m=0 To 1
AddElement(Mymesh())
Mymesh() = MP_CreateCube()
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; position of cube
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
AddElement(Mymesh())
Mymesh() = MP_CreateRectangle(Random(4,1)/4,Random(4,1)/4,Random(4,1)/4)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; position of Rectangle
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
AddElement(Mymesh())
Mymesh() = MP_CreatePyramid (2, 1, 2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
AddElement(Mymesh())
Mymesh() =MP_CreateSphere (10)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
AddElement(Mymesh())
Mymesh() =MP_CreateCylinder (1,2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
AddElement(Mymesh())
Mymesh() =MP_CreateOpenCylinder (1,2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
Next m
light1 = MP_CreateLight(a)
MP_PositionEntity (light1,-20,20,-6)
MP_LightSetColor(Light1,RGBA(240,120,0,0))
MP_AmbientSetLight (RGBA(0,12,66,0))
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder Windows Schliessen
MP_Windows_Fps(Titel)
ForEach Mymesh()
MP_TurnEntity (Mymesh(),0,1,1) ; dreh den Würfel
Next
MP_RenderWorld() ; Create the World
MP_Flip () ; Flip and Time
Wend
Code: Select all
;- ProgrammStart
Titel.s = "MP3D_Create_Meshs_with_color_and_Texture"
MP_Graphics3D (640,480,0,3) ; Create a Windows with 3D Funktion #Window = 0
SetWindowTitle(0, Titel) ; Name of Windows
Texture = MP_LoadTexture(#PB_Compiler_Home + "examples/3d/Data/Textures/Geebee2.bmp")
NewList Mymesh.i() ; Here comes my Mesh
For m=0 To 1
AddElement(Mymesh())
Mymesh() = MP_CreateCube()
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; position of cube
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
AddElement(Mymesh())
Mymesh() = MP_CreateRectangle(Random(4,1)/4,Random(4,1)/4,Random(4,1)/4)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; position of Rectangle
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
AddElement(Mymesh())
Mymesh() = MP_CreatePyramid (2, 1, 2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
AddElement(Mymesh())
Mymesh() =MP_CreateSphere (10)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
AddElement(Mymesh())
Mymesh() =MP_CreateCylinder (1,2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
AddElement(Mymesh())
Mymesh() =MP_CreateOpenCylinder (1,2)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
z.f = -6-Random(100,0)/40
MP_PositionEntity ( Mymesh(),x,y,z) ; Position of Pyramid
MP_RotateEntity( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If Random(1)
MP_EntitySetTexture (Mymesh(), Texture)
Else
MP_EntitySetColor (Mymesh(),RGBA(Random(120),Random(120),Random(120),0))
EndIf
Next m
light1 = MP_CreateLight(a)
MP_PositionEntity (light1,-3,0,0)
MP_AmbientSetLight (RGBA(0,12,66,0))
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder Windows Schliessen
MP_Windows_Fps(Titel)
ForEach Mymesh()
MP_TurnEntity (Mymesh(),0,1,1) ; dreh den Würfel
Next
MP_RenderWorld() ; Create the World
MP_Flip () ; Flip and Time
Wend
Code: Select all
;- ProgrammStart
MP_Graphics3D (640,480,0,3)
SetWindowTitle(0, "MediaView, Return load Mesh, Cursor,a,y/z Bewegt den Mesh") ; Name of Windows
;camera=MP_CreateCamera()
light=MP_CreateLight(1)
MP_PositionEntity (light,-3,0,0)
n=CountProgramParameters()
For i=1 To n
File.s + " " + ProgramParameter() ; Get filename with space too, example = "c:\my space\test.3ds"
File = LTrim (File)
Next
If File.s
SetWindowTitle(0, File.s)
;mesh = MP_LoadMesh("models\Suzanne.x"); my testmeshs
;mesh = MP_LoadMesh("models\cube.x")
;mesh = MP_LoadMesh("models\wispwind.x")
;mesh = MP_LoadMesh("models\rabbit.x")
;mesh = MP_LoadMesh("models\usb.stl")
;mesh = MP_LoadMesh("models\Sphericon.stl")
;mesh = MP_LoadMesh("models\2 sternchen.x")
;mesh = MP_Createsphere(16)
;mesh = MP_CreateCylinder (1, 2)
mesh = MP_LoadMesh (File.s)
max.f = MP_MeshGetHeight(mesh) ; find Maximum of Mesh
If MP_MeshGetWidth(mesh) > max
max = MP_MeshGetWidth(mesh)
EndIf
If MP_MeshGetDepth(mesh) > max
max = MP_MeshGetDepth(mesh)
EndIf
If max
scale.f = 2 / max ;
EndIf
MP_ScaleEntity (mesh,scale,scale,scale) ; Auf Bildschirm maximieren / maximum to Screen
;x.f=0 : y.f=0 : z.f=4 ; Mesh Koordinaten
x.f=0 : y.f=0 : z.f=-6 ; Mesh Koordinaten
Else
SetWindowTitle(0, "Anzahl programm parameter: "+Str(n))
EndIf
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder schliessen
If MP_KeyDown(#PB_Key_Left) : x=x-1 : EndIf
If MP_KeyDown(#PB_Key_Right) : x=x+1 :EndIf
If MP_KeyDown(#PB_Key_Down) : y=y-1 : EndIf
If MP_KeyDown(#PB_Key_Up) : y=y+1 : EndIf
If MP_KeyDown(#PB_Key_Z) : z=z+1 : EndIf ;y ist mit z getauscht
If MP_KeyDown(#PB_Key_A) : z=z-1 : EndIf
If MP_KeyDown(#PB_Key_Return)
Pattern$ = "3D Mesh files|*.x;*.3ds;*.b3d|.x Dateien (*.x)|*.x|3DS Dateien (*.3ds)|*.3ds|B3D Dateien (*.b3d)|*.b3d"
directory$ = "C:\Programme\PureBasic\media\"
File.s = OpenFileRequester("Choose entity", directory$, Pattern$, 0)
If File.s
MP_FreeEntity (mesh)
mesh = MP_LoadMesh (File.s)
max.f = MP_MeshGetHeight(mesh) ; find Maximum of Mesh
If MP_MeshGetWidth(mesh) > max
max = MP_MeshGetWidth(mesh)
EndIf
If MP_MeshGetDepth(mesh) > max
max = MP_MeshGetDepth(mesh)
EndIf
If max : scale.f = 3 / max : EndIf
MP_ScaleEntity (mesh,scale,scale,scale) ; Auf Bildschirm maximieren / maximum to Screen
x.f=0 : y.f=0 : z.f=-6 ; Mesh Koordinaten
EndIf
EndIf ;#Space
; MP_DrawText (2,2,"Triangles: "+Str(MP_CountTriangles(Mesh))+" Vertices: "+Str(MP_CountVertices(Mesh))) ; comming soon
MP_PositionEntity (mesh,0,0,z)
MP_RotateEntity (mesh,x,0,y)
MP_RenderWorld()
MP_Flip ()
Wend
Code: Select all
;- ProgrammStart
Titel.s = "VSync on/off, Key 1 for Vsync off, Key 2 Vsync on"
MP_Graphics3D (640,480,0,3) ; Erstelle ein WindowsFenster mit 3D Funktion #Window = 0
SetWindowTitle(0, Titel) ; Name of Windows
Light = MP_CreateLight(0)
Mymesh = MP_Createcube ()
;Mymesh = MP_CreateMesh ()
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
MP_PositionEntity ( Mymesh,x,y,-6-Random(100,0)/40) ; Position des Würfels
MP_TurnEntity ( Mymesh,Random(90,0),Random(90,0),Random(90,0))
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder Windows Schliessen
MP_Windows_Fps(Titel)
MP_TurnEntity (Mymesh,0,1,1) ; dreh den Würfel
MP_MoveEntity (Mymesh,0,0.003,0.003)
If MP_KeyDown(#PB_Key_1) : mp_Vsync(0) : EndIf
If MP_KeyDown(#PB_Key_2) : mp_Vsync(1) : EndIf
MP_RenderWorld() ; Erstelle die Welt
MP_Flip () ; Stelle Sie dar
Wend
Code: Select all
;- ProgrammStart
Titel.s = "Test how to create a mesh"
MP_Graphics3D (640,480,0,3) ; Erstelle ein WindowsFenster mit 3D Funktion #Window = 0
SetWindowTitle(0, Titel) ; Name of Windows
Light = MP_CreateLight(0)
Mymesh = MP_CreateMesh ()
MP_AddVertex ( Mymesh ,0,1,0,0,0,$FF00)
MP_AddVertex ( Mymesh ,1,1,0,0,0,$FF)
MP_AddVertex ( Mymesh ,0,0,0,0,0,$FF0000)
MP_AddVertex ( Mymesh ,0,1,0,0,0,$FF00)
MP_AddVertex ( Mymesh ,1,1,0,0,0,$FF)
MP_AddVertex ( Mymesh ,1,2,0,0,0,$FF0000)
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
MP_PositionEntity ( Mymesh,x,y,-6-Random(100,0)/40) ; Position des Würfels
MP_TurnEntity ( Mymesh,Random(90,0),Random(90,0),Random(90,0))
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder Windows Schliessen
MP_Windows_Fps(Titel)
MP_TurnEntity (Mymesh,0,1,1) ; dreh den Würfel
If MP_KeyDown(#PB_Key_1) : mp_Vsync(0) : EndIf
If MP_KeyDown(#PB_Key_2) : mp_Vsync(1) : EndIf
MP_RenderWorld() ; Erstelle die Welt
MP_Flip () ; Stelle Sie dar
Wend
Code: Select all
Titel.s = "Print of a Cube and Sprite with Alphamode"
MP_Graphics3D (640,480,0,3) ; Erstelle ein WindowsFenster mit 3D Funktion #Window = 0
SetWindowTitle(0, Titel); Name of Windows
Light = MP_CreateLight(0)
NewList Mymesh.i()
Texture1 = MP_LoadTexture("Image1.bmp")
Texture2 = MP_LoadTexture(#PB_Compiler_Home + "examples/3d/Data/Textures/Grass1.png")
Texture3 = MP_LoadTexture(#PB_Compiler_Home + "examples/3d/Data/Textures/Caisse.png")
Sprite = MP_LoadSprite(#PB_Compiler_Home + "examples/3d/Data/Textures/Grass1.png")
For m = 0 To 3
AddElement(Mymesh())
Mymesh() = MP_CreateCube()
x.f = Random(100,0)/20 -2.5
y.f = Random(100,0)/20 -2.5
MP_PositionEntity ( Mymesh(),x,y,-6-Random(100,0)/40) ; Position des Würfels
MP_TurnEntity ( Mymesh(),Random(90,0),Random(90,0),Random(90,0))
If m = 0 Or m = 1
MP_MeshSetAlpha (Mymesh(), 1)
MP_EntitySetTexture (Mymesh(),Texture1)
EndIf
If m = 2 Or m = 3
MP_MeshSetAlpha (Mymesh(), 2)
MP_EntitySetTexture (Mymesh(),Texture3)
EndIf
Next m
MP_AmbientSetLight (RGBA(0,55,65,0))
;MP_VSync(0)
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow; Code window
SetWindowTitle(0, Titel+" - FPS: "+Str(MP_FPS ())+" ") ; name of Windows
If MP_KeyDown(#PB_Key_W)
If wireFrame=0
MP_Wireframe (1)
wireFrame ! 1
ElseIf wireFrame=1
MP_Wireframe (0)
wireFrame ! 1
EndIf
EndIf
ForEach Mymesh()
MP_TurnEntity (Mymesh(),0,1,1) ; Turn the cube
Next
a.f + 1
MP_SpriteSetAlpha (Sprite, 1)
MP_ScaleSprite(Sprite, 100, 100 )
;MP_TurnSprite(Sprite, 1)
MP_RotateSprite(Sprite, a)
MP_DrawSprite (Sprite, 400, 200)
MP_SpriteSetAlpha (Sprite, 2)
MP_ScaleSprite(Sprite, 50, 200 )
MP_RotateSprite(Sprite, -a)
MP_DrawSprite (Sprite, 200, 200 )
MP_RenderWorld() ; Create the World
MP_Flip () ; Show all
Wend