![]() |
Tutorial 14: File IO Saving 3D assets to disk, or transporting them across a network, is an important feature of any professional 3D application. The X3 Data Format is a simple open specification that can be used save and transport 3D assets. This tutorial shows you how to save and load 3D models using the X3 Data Format. |
{ "x3model":{ "vertex":[ -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1 ], "polygon":[ {"vi":[0, 3, 1, 2]}, {"vi":[0, 5, 4, 3]}, {"vi":[2, 1, 6, 7]}, {"vi":[2, 7, 5, 0]}, {"vi":[1, 3, 4, 6]}, {"vi":[7, 6, 4, 5]} ] } } |
![]() |
![]() |
![]() |
![]() |
Control | Name | Left | Top | Caption | DoubleBuffer | Maximize Button |
Window | SurfaceWindow | - | - | - | - | ON |
OpenGLSurface | Surface | 123 | 0 | - | ON | - |
Generic Button | OpenButton | 20 | 14 | Open | - | - |
Generic Button | SaveButton | 20 | 48 | Save | - | - |
![]() |
Self.MouseCursor = System.Cursors.StandardPointer |
Surface.Render |
X3_Initialize X3_EnableLight OpenGL.GL_LIGHT0, new X3Core.X3Light(0, 0, 1) |
X3_SetPerspective Surface |
Name | Type |
Model | X3Core.X3Model |
MousePrevX | Integer |
MousePrevY | Integer |
OpenGL.glClearColor(1, 1, 1, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT) OpenGL.glPushMatrix OpenGL.glTranslatef 0, 0, -2.5 if Model <> nil then X3_RenderModel Model end if OpenGL.glPopMatrix |
MousePrevX = x MousePrevY = y return true |
X3_RotateWithXY Model.Rotation, (y - MousePrevY), (x - MousePrevX) Surface.Render MousePrevX = x MousePrevY = y |
Dim dlg As new OpenDialog Dim modFile As FolderItem Dim x3mType As new FileType x3mType.Name = "X3 Model" x3mType.Extensions = "x3m" dlg.Filter = x3mType modFile = dlg.ShowModal() if modFile <> nil then Model = X3IO_LoadModel(modFile) Surface.Render end if |
Dim dlg As new SaveAsDialog Dim modFile As FolderItem Dim x3mType As new FileType x3mType.Name = "X3 Model" x3mType.Extensions = "x3m" dlg.Filter = x3mType modFile = dlg.ShowModal() if modFile <> nil then if Right(modFile.Name, 4) <> ".x3m" then modFile.Name = modFile.Name + ".x3m" end if X3IO_SaveModel(Model, modFile) end if |
Dim dlg As new OpenDialog Dim modFile As FolderItem Dim x3mType As new FileType x3mType.Name = "X3 Model" x3mType.Extensions = "x3m" dlg.Filter = x3mType modFile = dlg.ShowModal() if modFile <> nil then Model = X3IO_LoadModel(modFile) Surface.Render end if |
X3 models are stored in files with the extension .x3m. In the code above the x3mType object defines this extension, and is used together with an OpenDialog object to select a model to load. To load an X3 model from a file is simply a matter of calling the method X3IO_LoadModel, with a FolderItem as a parameter that points to the file, and assigning the result to your model object. |
Dim dlg As new SaveAsDialog Dim modFile As FolderItem Dim x3mType As new FileType x3mType.Name = "X3 Model" x3mType.Extensions = "x3m" dlg.Filter = x3mType modFile = dlg.ShowModal() if modFile <> nil then if Right(modFile.Name, 4) <> ".x3m" then modFile.Name = modFile.Name + ".x3m" end if X3IO_SaveModel(Model, modFile) end if |
Saving an X3 model is just as easy as loading a model. Simply call the X3IO_SaveModel method, with the model to be saved as the first parameter, and a FolderItem that points to the location where the model should be saved as the second parameter. |
![]() |