Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8290950
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类:

2006-11-26 19:56:08

' ________________________________________________________________________
' TrueVision3D (web: http://www.truevision3d.com)
' ????????????????????????????????????
' Tutorial 13 : Mouse pick a 3D object
' ??????
' Description : The Teapots are We are going to create 2 Teapots
' ?????? and add the feature to rotate those Teapots (in the
' 3D world) when we are clicking on them.
' And we will give the 3D mouse Position the same as the
' default windows mouse.
 
' Force explicit declarations.
Option Explicit

' We declare TVEngine.
Private TV3D As TVEngine

' You know how do do this, we declare an array of Teapots.
'定义模型数组
Private Teapot(1 To 2) As TVMesh

' We declare the input object.
Private InputEngine As TVInputEngine

' We declare the collision object.
' which is used to get information about the lastest collision.
Private CollisionResult As TVCollisionResult

' We the declare the scene.
Private Scene As TVScene

' The loop.
Private DoLoop As Boolean

' Simple variable to hold the Teapots angle.
Private sngAngle(1 To 2) As Single

' Declare a WinApi function to get the windows mouse position.
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Sub cmdQuit_Click()

    ' We have clicked on the "Quit" button, so we change the DoLoop.
    DoLoop = False

End Sub

Private Sub Form_Load()

    ' We have to create the TV3D object before anything else.
    Set TV3D = New TVEngine

    ' Set search directory of Textures, Objects, ...
    TV3D.SetSearchDirectory App.Path
    
    ' We initialize TV3D in the picture box of the form.
    TV3D.Init3DWindowedMode Picture1.hWnd

    ' We want to see the FPS.
    TV3D.DisplayFPS = True
    
    ' We Set the AngleSystem to degree
    TV3D.SetAngleSystem TV_ANGLE_DEGREE

    ' We create the input object
    Set InputEngine = New TVInputEngine

    ' We create the scene (the world).
    Set Scene = New TVScene

    ' We load a cursor
    Scene.LoadCursor "..\..\..\Media\pointer.bmp", TV_COLORKEY_BLACK, 16, 16

    ' We load a texture for the Teapots
    '为茶壶加载材质
    Scene.LoadTexture "..\..\..\Media\texture.bmp", , , "TeapotTexture"

    ' New : we are going to create the Teapot using a "for" loop
    Dim i As Integer
    For i = 1 To 2

        ' We create the 3D Teapot object.
        Set Teapot(i) = New TVMesh
    
        ' We tell the scene that it will have to take the Teapot
        ' object and create a mesh with it.
        Set Teapot(i) = Scene.CreateMeshBuilder
            
        ' We load the Teapot mesh.
        Teapot(i).CreateTeapot
    
        ' Load and assign texture
        Scene.LoadTexture "..\..\..\Media\waterfall_color.bmp", , , "Texture"
        Teapot(i).SetTexture GetTex("Texture")
            
        ' We enable the Teapot mapping
        Teapot(i).EnableSphereMapping True
    
        ' We scale down the Teapot
        Teapot(i).ScaleMesh 1.5, 1.5, 1.5
    
        ' We set the Teapot position.
        Teapot(i).SetPosition (5 * i) - 7.5, 0, 5
    
        ' We set the Teapot initial rotation
        sngAngle(i) = (Rnd * 360)
        Teapot(i).SetRotation sngAngle(i), sngAngle(i), sngAngle(i)
        
        ' New : Computing the sphere for the mesh is useful for
        ' collision and culling.
        
        'NOTE : Frustum culling is a technique allowing to not render
        ' meshs and objects that are not visible by the camera.
        Teapot(i).ComputeSphere

    Next i

    ' We pop the form over everything else.
    Form1.Show
        
    ' We start the main loop.
    DoLoop = True
    Main_Loop

End Sub

Private Sub Form_Unload(Cancel As Integer)

    ' The user asked to quit but clicked on the 'X' button at up right.
    DoLoop = False
    
    ' And ask to quit.
    Main_Quit

End Sub

Private Sub Main_Loop()

    ' The main loop
    Do
        ' Let us the capacity to use buttons of the form.
        DoEvents
        
        ' We check the input
        Check_Input
                
        ' We start the rendering by clearing everything that we were
        ' seeing on the last frame (image).
        TV3D.Clear
        
        ' We render all the 3D objects contained in the scene.
        Scene.RenderAllMeshes
        
        ' We display everything that we have rendered
        TV3D.RenderToScreen
    
    'We loop all of this until the DoLoop isn't True.
    Loop Until DoLoop = False
    
    ' We ask to quit.
    Main_Quit

End Sub

Private Sub Check_Input()

    ' We start with setting the 3D mouse Cursor to the position of
    ' the windows mouse cursor.
    
    ' Make a variable to store the position of the mouse.
    Dim WindowsMousePosition As POINTAPI
    
    ' Get the mouse position and store it in the
    ' WindowsMousePosition variable
    GetCursorPos WindowsMousePosition
    
    ' Set the 3D mouse cursor to the same position as the variable
    ' hase of the windows mouse (mouseposition - ((window left/top of screen) + RenderPart left/top of form)
    InputEngine.SetMousePosition WindowsMousePosition.x - ((Form1.Left / 14) + 1), WindowsMousePosition.Y - ((Form1.Top / 14) + 20)
    
    ' The we check the mouse status.
    Dim tmpMouseX As Long, tmpMouseY As Long
    Dim tmpMouseB1 As Integer
    
    ' Get the movement of the mouse.
    '获取绝对值
    InputEngine.GetAbsMouseState tmpMouseX, tmpMouseY, tmpMouseB1

    ' If we clicked on the mouse button...
    If tmpMouseB1 <> 0 Then
    
        '...check if the mouse pointer has collided with a Teapot.
        Set CollisionResult = Scene.MousePicking(tmpMouseX, tmpMouseY, TV_COLLIDE_MESH, TV_TESTTYPE_BOUNDINGBOX)
        
        ' Check if we did have a collision with a Teapot.
        If CollisionResult.IsCollision Then
        
            ' Yes, we did have a collision with a Teapot. But which one
            ' is it? We use the collision get mesh to know this.
            ' and we rotate it a little
            CollisionResult.GetCollisionMesh.RotateY ScaleValue(0.1)
            CollisionResult.GetCollisionMesh.RotateX ScaleValue(0.1)
        
        End If

    End If

End Sub

Private Sub Main_Quit()
        
    ' We want to quit the project, so we destroy the Teapot object.
    Set Teapot(1) = Nothing
    Set Teapot(2) = Nothing
    
    ' We destroy the input object
    Set InputEngine = Nothing

    ' we destroy the collision object
    Set CollisionResult = Nothing

    ' Then, we destroy the scene object.
    Set Scene = Nothing
    
    ' We finish the frenetic destroy with the TV3D object.
    Set TV3D = Nothing
    
    ' We end the application.
    End

End Sub

阅读(2397) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~