TUTORIAL 11:   This tutorial explains how To load DirectX .X models . NemoX provide an interface to load and render with visibility clipping polygons from DirectX Models.


----TUTORIAL 11---

Loading and Rendering DirectX .x models

You will Need The NemoX engine Get it Here

download the Visual Basic Source code here




1. InitEngine modification

2. How to Load .X Models

3. Game Loop Modification

4. Collision Detection Code



1. InitEngine modification

In this tutorial we will use New Classes cNemo_XFile  for managing .X files.

For objects declaration we use vb classic class declaration  for 3 Objects:

''here is the class for our .X objects
Dim Mesh_X1 As cNemo_XFile
Dim Mesh_X2 As cNemo_XFile



'we will used that sub for the engine initialization

'we will used that sub for the engine initialization

Sub InitEngine()

 'first thing allocate memory for the main Object
  Set Nemo = New NemoX
  'check for the good version IMPORTANT only version 0.072 or above
 If NemoX.Get_EngineVersion < 0.072 Then
   MsgBox "NemoX version 1.072 is required as minimum" , vbInformation
 End If
  Set Tool = New cNemo_Tools
   'allocate memory for our meshbuilder
    Set Mesh = New cNemo_Mesh
    '====New code======
    '.......MEMORY ALLOCATION....
    Set KAMERA = New cNemo_Camera
    Set KEY = New cNemo_Input
    Set Mesh_X1 = New cNemo_XFile
    Set Mesh_X2 = New cNemo_XFile
'we use this method
'now we allow the user to choose options
'32/16 bit backbuffer
  If Not (Nemo.INIT_ShowDeviceDLG(Form1.hWnd)) Then
   End 'terminate here if error
  End If
  'Nemo.Initialize Me.hWnd
  'set the back clearcolor
  Nemo.BackBuffer_ClearCOLOR = RGB(125, 125, 125) 'Gray
  'set some parameters
  Nemo.Set_ViewFrustum 10, 5500, 3.14 / 4, 1.01
  Nemo.Set_light True
  'set our camera
  KAMERA.Set_YRotation 45
  KAMERA.Set_EYE Tool.Vector(480, 50, -450) 'Starting Position
End Sub


2. Loading .x Ojects

'  IN THIS sub we will Load ,rescale,objects
Sub Load_Meshes()
  'load  mesh #1
  Mesh_X1.LoadXmesh App.Path + "\data\" + _
  Mesh_X1.Set_Position 400, -30, 200 'To translate the mesh in world
  Mesh_X1.Set_Scale 11, 11, 12 'To Rescale the mesh 
End Sub
In this sub we use LoadXmesh function to load our .X object.
We just pass the Model long pathName to the function.


3. Game Loop Modification


'this sub is the main loop for a game or 3d apllication
Sub gameLoop()

            'loop untill player press 'ESCAPE'
      'Nemo.Set_CullMode D3DCULL_NONE
      Nemo.Set_EngineRenderState D3DRS_ZENABLE, 1
      Nemo.Set_light 1
               '=====Keyboard handler can be added here
               Call GetKey
               'check collision here
               'start the 3d renderer
                     '===============ADD game rendering mrthod here
               'draw our ground here
               'render X meshes here
               Mesh_X1.Render 0
               'Mesh_X2.Render 0
               'show the FPS at pixel(5,10) color White
               Nemo.Draw_Text "FPS:" + Str(Nemo.Framesperseconde), 5, 10, &HFFFFFFFF
               'end the 3d renderer
            'check the player keyPressed
    Loop Until KEY.Get_KeyBoardKeyPressed(NEMO_KEY_ESCAPE)
    Call EndGame

End Sub




4. Collision Detection Code

In this code we're checking if a collision occurs between the player camera postion and Our world and 3DS meshes


'  IN THIS sub we will handle with collision
'  detection
Sub CheckCollision()
    Dim Dest As D3DVECTOR
    'if any collision with our world we slide along
    If Mesh.CheckCollisionSliding(KAMERA.Get_position, Dest, 40) Then
       KAMERA.Set_EYE Dest
    End If

     'if any collision with our X mesh 1 we slide along

     If Mesh_X1.Get_ColisionSliding(KAMERA.Get_position, Dest, 20) Then
              KAMERA.Set_EYE Dest
     End If

End Sub

We use  this function:CheckCollisionSliding

PointVec : the point coordonates to test

RetPos:  A Point (D3DVECTOR) to receive the collision response

Radius: The radius of the Sphere to be collided with Mesh Polygons




Here is a screenshot for our tutorial project

download the Visual Basic Source code here

See you in the next Tutorial.....sincerly Polaris..Don't forget Any Bugs detected mailMe


                      The Next: Tutorial 12 :Loading and Rendering Quake 2 (.md2) Models

The Previous: Tutorial 10 :Loading MilkShape3D models


Tutorial Written on April, 28 th 2003 by Polaris: johna_pop@yahoo.fr