Results 1 to 5 of 5

Thread: [RESOLVED] Environment loading-rendering-rotating.

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Resolved [RESOLVED] Environment loading-rendering-rotating.

    I won't work more with black screen and only a 3d gun.
    Now i has 4 images, or a dds's from directx sdk media folder.
    I wanna make it become a "world" inside the window (I mean... landscape).
    And how can i do that?

  2. #2
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Environment loading-rendering-rotating.

    Here is a small sample code I created (in practically 4 programming languages o.O) that allows you to move around like in a FPS. However I haven't created it in vb.net yet, I also have code that renders a skybox as well which is also nice to have. If you give me time I can create it for you. But in the mean time, heres the FPS code. Use W S A D to move around and the mouse to move the camera:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6. Imports Microsoft.DirectX.DirectInput
    7.  
    8. Public Class frmMain
    9.  
    10.     Private Const FOV As Single = Math.PI / 4
    11.     Private Const ASPECT_RATIO As Single = 4 / 3
    12.     Private Const NEAR_Z As Single = 1
    13.     Private Const FAR_Z As Single = 10000
    14.  
    15.     Private Device As Direct3D.Device
    16.     Private Display_Mode As DisplayMode
    17.     Private Screen As New PresentParameters
    18.  
    19.     Private Mouse_Device As DirectInput.Device
    20.     Private Mouse_State As MouseState
    21.     Private Mouse As Vector2
    22.  
    23.     Private Keyboard_Device As DirectInput.Device
    24.     Private Keyboard_State As KeyboardState
    25.  
    26.     Private Vertex_List(3) As CustomVertex.PositionColored
    27.  
    28.     Private View_Matrix As Matrix
    29.     Private Projection_Matrix As Matrix
    30.  
    31.     Private Camera_Position As Vector3
    32.     Private Camera_Angle As Vector3
    33.  
    34.     Private Fullscreen_Enabled As Boolean
    35.     Private Running As Boolean
    36.  
    37.     Private Sub DirectX_Initialize()
    38.  
    39.         If Fullscreen_Enabled = True Then
    40.             Display_Mode.Width = 800
    41.             Display_Mode.Height = 600
    42.             Display_Mode.Format = Direct3D.Format.R5G6B5
    43.             Screen.Windowed = False
    44.             Screen.BackBufferCount = 1
    45.             Screen.BackBufferWidth = Display_Mode.Width
    46.             Screen.BackBufferHeight = Display_Mode.Height
    47.         Else
    48.             Screen.Windowed = True
    49.         End If
    50.  
    51.         Screen.SwapEffect = SwapEffect.Copy
    52.         Screen.BackBufferFormat = Display_Mode.Format
    53.         Screen.AutoDepthStencilFormat = DepthFormat.D16
    54.         Screen.EnableAutoDepthStencil = True
    55.  
    56.         Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    57.  
    58.     End Sub
    59.  
    60.     Private Sub DirectInput_Initialize_Mouse()
    61.  
    62.         Mouse_Device = New DirectInput.Device(SystemGuid.Mouse)
    63.         Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse)
    64.         Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    65.         Mouse_Device.Acquire()
    66.  
    67.     End Sub
    68.  
    69.     Private Sub Mouse_Controls()
    70.  
    71.         Mouse_State = Mouse_Device.CurrentMouseState
    72.  
    73.         Mouse.X = Mouse.X + Mouse_State.X
    74.         Mouse.Y = Mouse.Y + Mouse_State.Y
    75.  
    76.         If Mouse.X > Me.Width Then Mouse.X = Me.Width
    77.         If Mouse.X < 0 Then Mouse.X = 0
    78.  
    79.         If Mouse.Y > Me.Height Then Mouse.Y = Me.Height
    80.         If Mouse.Y < 0 Then Mouse.Y = 0
    81.  
    82.         Camera_Angle.X += Mouse_State.Y
    83.         Camera_Angle.Y += Mouse_State.X
    84.  
    85.         If Camera_Angle.X >= 90 Then Camera_Angle.X = 90
    86.         If Camera_Angle.X <= -90 Then Camera_Angle.X = -90
    87.  
    88.     End Sub
    89.  
    90.     Private Sub DirectInput_Initialize_Keyboard()
    91.  
    92.         Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard)
    93.         Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard)
    94.         Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    95.         Keyboard_Device.Acquire()
    96.  
    97.     End Sub
    98.  
    99.     Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean
    100.  
    101.         Keyboard_State = Keyboard_Device.GetCurrentKeyboardState
    102.         Return Keyboard_State.Item(Key_Code)
    103.  
    104.     End Function
    105.  
    106.     Private Sub Keyboard_Controls()
    107.  
    108.         Const Camera_Speed As Long = 5
    109.  
    110.         If DirectInput_Key_State(Key.W) Then 'Move Forward
    111.             Camera_Position.X -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    112.             Camera_Position.Z -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    113.         End If
    114.  
    115.         If DirectInput_Key_State(Key.S) Then 'Move Backward
    116.             Camera_Position.X += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    117.             Camera_Position.Z += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    118.         End If
    119.  
    120.         If DirectInput_Key_State(Key.A) Then 'Move Left
    121.             Camera_Position.X += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    122.             Camera_Position.Z -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    123.         End If
    124.  
    125.         If DirectInput_Key_State(Key.D) Then 'Move Right
    126.             Camera_Position.X -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    127.             Camera_Position.Z += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    128.         End If
    129.  
    130.         If DirectInput_Key_State(Key.Q) Then 'Look Left
    131.             Camera_Angle.Y -= 1
    132.         End If
    133.  
    134.         If DirectInput_Key_State(Key.E) Then 'Look Right
    135.             Camera_Angle.Y += 1
    136.         End If
    137.  
    138.         If DirectInput_Key_State(Key.R) Then 'Look Up
    139.             Camera_Angle.X -= 1
    140.         End If
    141.  
    142.         If DirectInput_Key_State(Key.F) Then 'Look Down
    143.             Camera_Angle.X += 1
    144.         End If
    145.  
    146.         If DirectInput_Key_State(Key.C) Then 'Move Up
    147.             Camera_Position.Y -= Camera_Speed
    148.         End If
    149.  
    150.         If DirectInput_Key_State(Key.Z) Then 'Move Down
    151.             Camera_Position.Y += Camera_Speed
    152.         End If
    153.  
    154.     End Sub
    155.  
    156.     Private Sub Settings()
    157.  
    158.         Device.SetRenderState(RenderStates.ZEnable, True)
    159.         Device.SetRenderState(RenderStates.ZBufferWriteEnable, True)
    160.         Device.SetRenderState(RenderStates.CullMode, Cull.None)
    161.         Device.SetRenderState(RenderStates.Lighting, False)
    162.  
    163.     End Sub
    164.  
    165.     Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3
    166.  
    167.         Dim Vertex As Vector3
    168.  
    169.         Vertex.X = X
    170.         Vertex.Y = Y
    171.         Vertex.Z = Z
    172.  
    173.         Return Vertex
    174.  
    175.     End Function
    176.  
    177.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored
    178.  
    179.         Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored
    180.  
    181.         Vertex.Position = New Vector3(X, Y, Z)
    182.         Vertex.Color = Color
    183.  
    184.         Return Vertex
    185.  
    186.     End Function
    187.  
    188.     Private Sub Setup_Matrices()
    189.  
    190.         View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0))
    191.         Device.Transform.View = View_Matrix
    192.  
    193.         Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z)
    194.         Device.Transform.Projection = Projection_Matrix
    195.  
    196.     End Sub
    197.  
    198.     Private Sub Camera_Control()
    199.  
    200.         Dim Camera_Translation_Matrix As Matrix
    201.         Dim Camera_Angle_Matrix_X As Matrix
    202.         Dim Camera_Angle_Matrix_Y As Matrix
    203.  
    204.         View_Matrix = Matrix.Identity
    205.         Camera_Translation_Matrix = Matrix.Identity
    206.  
    207.         Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z)
    208.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix)
    209.  
    210.         Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180)
    211.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y)
    212.  
    213.         Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180)
    214.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X)
    215.  
    216.         Device.Transform.View = View_Matrix
    217.  
    218.     End Sub
    219.  
    220.     Private Sub Create_Polygon()
    221.  
    222.         Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    223.         Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    224.         Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    225.         Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    226.  
    227.     End Sub
    228.  
    229.     Private Sub Draw_Polygon()
    230.  
    231.         Device.VertexFormat = CustomVertex.PositionColored.Format
    232.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    233.  
    234.     End Sub
    235.  
    236.     Private Sub Create_Platform()
    237.  
    238.         Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb)
    239.         Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb)
    240.         Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb)
    241.         Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb)
    242.  
    243.     End Sub
    244.  
    245.     Private Sub Reset_Device()
    246.  
    247.         If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then
    248.             Running = False
    249.             Device.Reset(Screen)
    250.             Settings()
    251.             Setup_Matrices()
    252.             Application.DoEvents()
    253.             Running = True
    254.         End If
    255.  
    256.     End Sub
    257.  
    258.     Private Sub Render()
    259.  
    260.         Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1, 0)
    261.         Device.BeginScene()
    262.         Create_Polygon()
    263.         Draw_Polygon()
    264.         Create_Platform()
    265.         Draw_Polygon()
    266.         Device.EndScene()
    267.         Device.Present()
    268.  
    269.     End Sub
    270.  
    271.     Private Sub Game_Loop()
    272.  
    273.         Do While Running = True
    274.             Mouse_Controls()
    275.             Keyboard_Controls()
    276.             Camera_Control()
    277.             Render()
    278.             If DirectInput_Key_State(Key.Escape) Then Shutdown()
    279.             Application.DoEvents()
    280.         Loop
    281.  
    282.     End Sub
    283.  
    284.     Private Sub Main()
    285.  
    286.         If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    287.             Fullscreen_Enabled = True
    288.         End If
    289.  
    290.         With Me
    291.             .Show()
    292.             .Width = 330
    293.             .Height = 250
    294.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
    295.             .Text = "DirectX Tutorial"
    296.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    297.         End With
    298.  
    299.         Camera_Position.X = 0
    300.         Camera_Position.Y = -50
    301.         Camera_Position.Z = 250
    302.  
    303.         DirectX_Initialize()
    304.         DirectInput_Initialize_Mouse()
    305.         DirectInput_Initialize_Keyboard()
    306.         Settings()
    307.         Setup_Matrices()
    308.         Running = True
    309.  
    310.     End Sub
    311.  
    312.     Private Sub Shutdown()
    313.  
    314.         If Running = True Then
    315.             Running = False
    316.             Mouse_Device.Unacquire()
    317.             Mouse_Device.Dispose()
    318.             Keyboard_Device.Unacquire()
    319.             Keyboard_Device.Dispose()
    320.             Device.Dispose()
    321.             Application.Exit()
    322.         End If
    323.  
    324.     End Sub
    325.  
    326.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    327.  
    328.         Main()
    329.  
    330.     End Sub
    331.  
    332.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    333.  
    334.         Shutdown()
    335.  
    336.     End Sub
    337.  
    338.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    339.  
    340.         Game_Loop()
    341.  
    342.     End Sub
    343.  
    344.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    345.  
    346.         Reset_Device()
    347.  
    348.     End Sub
    349.  
    350. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Re: Environment loading-rendering-rotating.

    Works nice. And I wanna code it myself. You make me want to play counter-strike.
    And how can I load the dds environment files - No, they loaded by: Surface.From file, aren't they? How to glue them to the world?
    Or a mesh with mountains, trees, and more?
    Last edited by PAPYRON; Apr 4th, 2013 at 02:37 AM.

  4. #4
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Environment loading-rendering-rotating.

    Hmmm never really worked with dds files. Personally Id be creating my own, like I did with my Bosskillers game for my worlds. Best if you create your own format unless you seriously research the ins and outs and find out how that file works, and managed to find tons of source code in either vb.net, or vb6. Vb6 you can easily port to vb.net mind you and since it been out a hell of a lot longer, you should find code. Worse case scenario is finding source code on dds files and converting C++ to vb.net. Thats what I would do if i were you. Or if you need to, just hardcode the locations of your meshes and create your own terrain/worlds.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2012
    Location
    việt nam (vietnam)
    Posts
    37

    Re: Environment loading-rendering-rotating.

    ground mesh (ground, mountain, houses) may easily 100x easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width