Introduction

Welcome to the C# version of TrueVision 3D version 6.5!

Before getting started you must put both the DirectX d3dx.dll and the TrueVision .NET MTV3D65.dll in your project Bin folder. Then reference the MTV3D65.dll in Visual Studio to your project. To do this open the Project Menu and then click on Add Reference. You will need to browse for the MTV3D65. Clicking on it will produce the reference.

The following starting code example will use the engine in windowed mode to show a rotating teapot. So make a form on the design page, name the form “frmMain” and place a picture box in it.

Typical Start Code for Windowed Mode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MTV3D65;
 
namespace TV3D_Project_In_Viewport
{
    public partial class frmMain : Form
    {
        #region ------ DECLARE TV3D ENGINE FIELDS ------
 
        // The following are only a small sampling of what TV3D has to offer
        TVEngine TV1;              // Create the engine object
 
        TVViewport Viewport1;      // Create the Viewport object to later associate with the picture box
        TVScene Scene1;            // The scene object defines the context for visual subject interactions
        TVInputEngine Input1;      // TV3D has an extensive library of computer input aids
        TVLightEngine Light1;      // While a basic light is always created by default we will define one anyway
        TVGlobals Global1;         // Unlike VB, C# can't access the Globals function so we must declare an object
        TVMathLibrary Math1;       // Moving things around in a 3D world needs 3D mathematics. Not used in example
        TVTextureFactory Texture1; // Manipulates textures. Not used in this example       
        TVMaterialFactory Material1; // Unlike Textures material are defined using their surface normals relative
        //to the light source. These properties include color, reflection, and shading
        TVCamera Camera1;
        TVAtmosphere Atmos1;       // Needed for the sky box or sky sphere (among other things)	 
        TVMesh TestMesh1;          // A Mesh is a 3D wire frame entity 
        TV_3DVECTOR CameraPos1;    // Camera Position using a TV3D defined math structure         
 
        #endregion
        
        public frmMain()
        {
            InitializeComponent();
        }
 
        // Initialize boolean
        bool ContinueRender = true;
 
        #region ------ INITIALIZE 3DTV ENGINE COMPONENTS ------
 
        // this event method must be created from the form designer page. It is triggered at startup
        // when the mainform loads. The TV engine initializations must be placed here.
        public void MainFormLoad(object sender, System.EventArgs e)
        {
            this.Show(); // We pop the form over everything else.
            this.Focus();	
        	
            // Initialize Engine
            TV1 = new TVEngine();	
            TV1.Init3DWindowed(this.Handle);                           // Initialize windowed mode
            TV1.SetSearchDirectory(Application.StartupPath);           // Set the search directory for objects, textures, etc.
            TV1.SetDebugFile(Application.StartupPath + "\\Debug.txt"); // Put the debug file in the app directory
            TV1.SetAntialiasing(true, CONST_TV_MULTISAMPLE_TYPE.TV_MULTISAMPLE_4_SAMPLES);
            TV1.DisplayFPS(true); // Set the Frames Per Second counter in the upper left hand corner
            TV1.SetAngleSystem(MTV3D65.CONST_TV_ANGLE.TV_ANGLE_RADIAN);// Set angles to radians instead of degrees
            Texture1 = new TVTextureFactory();  
            Math1 = new TVMathLibrary();
            Global1 = new TVGlobals();
         
            // Initialize Scene
            Scene1 = new TVScene();     
         
            // Initialize Input
            Input1 = new TVInputEngine();
            Input1.Initialize (true, true); // Get input from keyboard and mouse   
         
            // Initialize Point Light     
            Light1 = new TVLightEngine();	
            Light1.CreatePointLight(new TV_3DVECTOR(100.0f, 0.0f, 0.0f), 1.0f, 1.0f, 1.0f, 200.0f, "testLight", 1.0f); 
            //Light1.CreateDirectionalLight(new TV_3DVECTOR(0.1f, 0.0f, 1.0f), 1.0f, 0.0f, 0.0f, "testLight", 1.0f);
            Light1.SetSpecularLighting(true);         // You must enable specular (shiny spot) lighting.
            int lightCount = Light1.GetActiveCount(); // Just to see how many lights are really active.
 
            // Initialize Atmosphere
            Atmos1 = new TVAtmosphere();
 
            // Initialize Materials
            Material1 = new TVMaterialFactory();
            int testMatIndex = Material1.CreateMaterial("testMaterial");
            Material1.SetAmbient(testMatIndex, 0.1f, 0.0f, 0.0f, 1.0f);
            Material1.SetDiffuse(testMatIndex, 0.4f, 0.0f, 0.0f, 1.0f);
            Material1.SetSpecular(testMatIndex, 0.9f, 0.0f, 0.0f, 1.0f);
            Material1.SetEmissive(testMatIndex, 0.0f, 0.0f, 0.0f, 1.0f);
            Material1.SetPower(testMatIndex, 10.0f); 
        				
            // Initialize Cameras
            Camera1 = new TVCamera();
            CameraPos1.x = 0.0f;
            CameraPos1.y = 0.0f;
            CameraPos1.z = 0.0f;			 
            Scene1.GetCamera().SetPosition(CameraPos1.x, CameraPos1.y, CameraPos1.z); // Set at default 0.0
            Scene1.GetCamera().SetViewFrustum(90f, 10000f, 0.1f);     
             
            // Initialize Meshes			
            TestMesh1 = Scene1.CreateMeshBuilder("Teapot");
            TestMesh1.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
	        
            TestMesh1.CreateTeapot();
            //TestMesh1.CreateBox(1f,1f,2f);
            //TestMesh1.CreateCone(1f,2f,50,false);
            //TestMesh1.CreateCylinder(1f,2f,50);
            //TestMesh1.CreateSphere(1f,50,50);
            //TestMesh1.Create3DText("Hello World","",0,0.1f);
 
            TestMesh1.SetPosition(0.0f, 0.0f, 100.0f); // x, y, z
            TestMesh1.SetScale(30.0f, 30.0f, 30.0f);   // x, y, z
            TestMesh1.SetMaterial(testMatIndex);
            TestMesh1.SetRotation(0.0f, 0.0f, 0.0f);   // Around x, y, z, axis		    		
        	
            // Initialize Viewport
            Viewport1 = new TVViewport(); 
            Viewport1 = TV1.CreateViewport(pictureBox1.Handle, "Viewport1");
            Viewport1.SetAutoResize(true);
            Viewport1.SetBackgroundColor(Global1.RGBA(0.5f, 0.5f, 1.0f, 0f));	 
          
            Main_Loop(); // Call the render loop (game loop)
        }
        
        #endregion
 
        private void Main_Loop()
        {
            // Initialize algorithm to simulate Brownian motion
            Random random = new Random();
            int count = 0, f = 1000;
            float x = 0, y = 0, z = 100;
            float dx = random.Next(-f, f), dy = random.Next(-f, f), dz = random.Next(-f, f);
                     
            TestMesh1.SetPosition(x, y, z); // Initial position of teapot
 
            while (ContinueRender == true) // The render loop
            {
                // Execute algorithm to simulate Brownian motion - note that the teapot is kept
                // in view by the laws of probability - no viewport boundary checking is performed
                count = count + 1;
                if (count == 200)
                {
                    // Calculate a new random trajectory and reset counter
                    dx = random.Next(-f, f);
                    dy = random.Next(-f, f);
                    dz = random.Next(-f, f);
                    count = 0;
                }
                float g = 0.00002f; // Apply scale factor
                x = x + g * dx; y = y + g * dy; z = z + g * dz; // Calculate new position
                                
                TestMesh1.SetPosition(x, y, z);                // Move teapot
                TestMesh1.RotateY(0.001f * TV1.TimeElapsed()); // Rotate the teapot
                TV1.SetViewport(Viewport1);
                TV1.Clear(false);                              // Clear the viewport from last time          	 
                Scene1.RenderAll(true);
                TV1.RenderToScreen();
 
                // In order to access Windows form events this line of code must remain at the bottom
                System.Windows.Forms.Application.DoEvents();
            }
 
            Application.Exit();
            Dispose(false);
 
        } // End of Main Loop
 
        // Add event on the form to detect when the application has been closed using the cross (X) 
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            ContinueRender = false;
        }
    }
}
 
tv3dbeta/getting_started_with_visual_c-sharp.txt · Last modified: 2013/11/22 13:31