![]() |
Tutorial 8: Model data structure In this tutorial we design and implement a model data structure, that groups polygons into a collection to form a 3D object. |
![]() |
Control | Name | DoubleBuffer | Left | Top | Maximize Button |
Window | SurfaceWindow | - | - | - | ON |
OpenGLSurface | Surface | ON | 0 | 0 | - |
![]() |
Self.MouseCursor = System.Cursors.StandardPointer |
Surface.Render |
X3_Initialize |
X3_SetPerspective Surface |
Name | Type |
Polygon() | X3Polygon |
Sub X3_RenderModel(model As X3Core.X3Model) Dim i, j As Integer Dim poly As X3Core.X3Polygon OpenGL.glBegin OpenGL.GL_TRIANGLES for i = 0 to model.Polygon.Ubound poly = model.Polygon(i) if poly.FillColor <> nil then OpenGL.glColor3d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue) else OpenGL.glColor3d(1, 1, 1) ' set the color of the polygon end if for j = 0 to poly.Vertex.Ubound OpenGL.glVertex3d poly.Vertex(j).X, poly.Vertex(j).Y, poly.Vertex(j).Z next j next i OpenGL.glEnd End Sub |
OpenGL.glPushMatrix OpenGL.glClearColor(0, 0, 0, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT) OpenGL.glTranslatef -2, -2, -8.0 X3_RenderModel X3Test_Cube1 OpenGL.glPopMatrix |
Sub X3_RenderModel(model As X3Core.X3Model) Dim i, j As Integer Dim poly As X3Core.X3Polygon OpenGL.glBegin OpenGL.GL_TRIANGLES for i = 0 to model.Polygon.Ubound poly = model.Polygon(i) if poly.FillColor <> nil then OpenGL.glColor3d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue) else OpenGL.glColor3d(1, 1, 1) ' set the color of the polygon end if for j = 0 to poly.Vertex.Ubound OpenGL.glVertex3d poly.Vertex(j).X, poly.Vertex(j).Y, poly.Vertex(j).Z next j next i OpenGL.glEnd End Sub |
Large scenes have thousands of models. It therefore makes sense to have a dedicated method to render a model with. The X3_RenderModel method is just such a method, and takes as a parameter the model to render. X3_RenderModel starts the drawing of triangular polygons with a call to glBegin. It then loops through all the polygons of the model. For each polygon the color of the polygon is set with a call to glColor3d, and then all the vertices of the polygon is added using a nested loop and the glVertex3d function. |
OpenGL.glPushMatrix OpenGL.glClearColor(0, 0, 0, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT) OpenGL.glTranslatef -2, -2, -8.0 X3_RenderModel X3Test_Cube1 OpenGL.glPopMatrix |
In the Surface.Render event handler, we use the X3_RenderModel method to render a test cube. The X3Test module provides convenient helper methods that can be used for testing during development. One such method is the X3Test_Cube1 method that generates a colored test cube. |
![]() |
![]() |