|
I have done a code on 3D office. I would like to do it in interactive way which by pressing the arrow button, the object can be viewed from different perspective. However, my result come out with static image and my objects all disappear. The object exists if i doesn't include the Checkmovement function inside. I wonder what mistake i have done. Could anyone please kindly help me review my code and give comment. Thanks.
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MTV3D65;
namespace TrueVisionTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Main_Quit();
Application.Exit();
}
//Declare the TrueVision3D 'parts'
TVMesh _meshRoom = null;
TVMesh _meshFan = null;
TVMesh _meshTable = null;
TVMesh _meshSofa = null;
TVMesh _meshComputer = null;
TVMesh _Lamp = null;
TVMesh _Cabinet = null;
TVMesh _meshChair = null;
TVEngine TV = null;
TVScene _scene = null;
TVGlobals global = new TVGlobals();
TVLightEngine _TVLightEngine = null;
TVInputEngine InputEngine;
TVTextureFactory TexFactory = new TVTextureFactory();
TVMaterialFactory MatFactory = new TVMaterialFactory();
TVLandscape Land;
TVScene Scene;
float sngPositionX;
float sngPositionY;
float sngPositionZ;
float sngLookAtX;
float sngLookAtY;
float sngLookAtZ;
float sngWalk;
float sngStrafe;
float sngAngleX, sngAngleY;
int tmpMouseX, tmpMouseY;
bool tmpMouseB1, tmpMouseB2, tmpMouseB3, tmpMouseB4;
int tmpMouseScrollOld, tmpMouseScrollNew;
float x_move = 30.0f, y_move = -30.0f, z_move = 150.0f;
void InitializeTrueVision()
{
TV = new TVEngine();
TV.Init3DWindowed(this.pictureBox1.Handle, true);
TV.SetSearchDirectory(System.Windows.Forms.Application.ExecutablePath);
TV.SetAngleSystem(CONST_TV_ANGLE.TV_ANGLE_DEGREE);
TV.DisplayFPS(true);
}
TV_LIGHT light;
///
/// Set up the lighting for the scene
///
void SetupLighting()
{
_TVLightEngine = new TVLightEngine();
//Initializing the light with values
light.type = CONST_TV_LIGHTTYPE.TV_LIGHT_POINT; ;
light.ambient.a = 0.8f;
light.ambient.r = 0.8f;
light.ambient.g = 0.8f;
light.ambient.b = 0.8f;
light.diffuse.a = 1.0f;
light.diffuse.r = 1.0f;
light.diffuse.g = 1.0f;
light.diffuse.b = 1.0f;
light.specular.a = 1f;
light.specular.r = 0.2f;
light.specular.g = 0.2f;
light.specular.b = 0.2f;
light.direction = global.Vector3(0.0f, 1.0f, 0.0f);
light.position = global.Vector3(0.0f, 400.0f, 10.0f);
light.range = 1000.0f;
//light.Attenuation0 = 0.5f;
light.fFallOff = 1;
light.phi = ((float)Math.PI) / 4.0f;
light.theta = light.phi / 2;
//Create the light in the scene
_TVLightEngine.CreateLight(ref light, "light");
//_scene.SetSpecularLightning(true);
}
//private float _rotationvalue = 5;
private void Form1_Load(object sender, EventArgs e)
{
// set up the initial scene
SetupTheInitialScene();
// show the form and give it focus
this.Show();
this.Focus();
Scene = new TVScene();
InputEngine = new TVInputEngine();
sngPositionX = 0;
sngPositionY = 20;
sngPositionZ = 0;
sngLookAtX = 0;
sngLookAtY = 20;
sngLookAtZ = 50;
sngWalk = 0;
sngStrafe = 0;
//Set the Scene camera
Scene.SetCamera(sngPositionX, sngPositionY, sngPositionZ, sngLookAtX, sngLookAtY, sngLookAtZ);
// start up the main loop, setting the loop guard to ON
DoLoop = true;
Main_Loop();
}
private void SetupTheInitialScene()
{
// initialize true vision details
InitializeTrueVision();
// create a new scene
_scene = new TVScene();
// set the scene background color
_scene.SetBackgroundColor(0f, 0.3f, 0.9f);
// set the lighting of the scene
SetupLighting();
// Load Textures
LoadTextures();
// create wall
CreateWalls();
//Set the position of the room mesh
//_meshRoom.SetPosition(x_move, y_move, z_move);
// create different shapes in the room
CreateMeshShapes();
}
private void CreateMeshShapes()
{
// create a table
CreateTable();
// create coomputer
CreateComputer();
// create a sofa
CreateSofa();
// create cabinet
CreateCabinet();
// create chair
CreateChair();
//CreateDie2();
//create ceiling fan
CreateFan();
// create table lamp
CreateLamp();
}
private void CreateTable()
{
_meshTable = (TVMesh)_scene.CreateMeshBuilder("table");
_meshTable.LoadXFile(@"meshes\table.x", true, true);
_meshTable.SetPosition(130f, -120.0f, 300.0f);
_meshTable.SetScale(2, 2, 2);
_meshTable.SetTexture(global.GetTex("table"), -1);
}
private void CreateChair()
{
_meshChair = (TVMesh)_scene.CreateMeshBuilder("chair");
_meshChair.LoadXFile(@"meshes\chair.x", true, true);
_meshChair.SetPosition(10.0f, -80.0f, 140.0f);
_meshChair.SetScale(1, 1, 1);
_meshChair.RotateY(35.0f, true);
_meshChair.SetTexture(global.GetTex("steel"), -1);
}
private void CreateLamp()
{
_Lamp = (TVMesh)_scene.CreateMeshBuilder("lamp");
_Lamp.LoadXFile(@"meshes\lamp.x", true, true);
_Lamp.SetPosition(70f, 10.0f, 250.0f);
_Lamp.SetScale(2, 2, 2);
_Lamp.SetTexture(global.GetTex("lamp"),-1);
}
private void CreateCabinet()
{
_Cabinet = (TVMesh)_scene.CreateMeshBuilder("cabinet");
_Cabinet.LoadXFile(@"meshes\cabinet.x", true, true);
_Cabinet.SetPosition(200.0f, -140.0f, 340.0f);
_Cabinet.SetScale(4, 4, 4);
// rotate the cabinet slightly
_Cabinet.RotateY(90.0f, true);
_Cabinet.SetTexture(global.GetTex("steel"), -1);
}
private void CreateComputer()
{
_meshComputer = (TVMesh)_scene.CreateMeshBuilder("computer");
_meshComputer.LoadXFile(@"meshes\computer.x", true, true);
_meshComputer.SetPosition(-50.0f, 20.0f, 340.0f);
_meshComputer.SetScale(5, 5, 5);
_meshComputer.RotateY(90.0f, true);
_meshComputer.SetTexture(global.GetTex("black"), -1);
}
private void CreateSofa()
{
_meshSofa = (TVMesh)_scene.CreateMeshBuilder("sofa");
_meshSofa.LoadXFile(@"meshes\sofa.x", true, true);
_meshSofa.SetPosition(-140.0f, -80.0f, 340.0f);
_meshSofa.RotateY(180.0f, true);
_meshSofa.SetTexture(global.GetTex("brown"), -1);
}
private void CreateFan()
{
_meshFan = (TVMesh)_scene.CreateMeshBuilder("fan");
_meshFan.LoadXFile(@"meshes\fan.x",true,true);
_meshFan.SetPosition(-40.0f, 180.0f, 310.0f);
_meshFan.SetScale(3, 3, 3);
//rotate the fan
_meshFan.RotateY(35.0f, true);
_meshFan.RotateX(180.0f, true);
_meshFan.SetTexture(global.GetTex("white"), -1);
}
private void CreateWalls()
{
_meshRoom = (TVMesh)_scene.CreateMeshBuilder("room");
_meshRoom.AddWall3D(global.GetTex("wall"), 350.0f, -350.0f, -350.0f, -350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 5.0f);
_meshRoom.AddWall3D(global.GetTex("wall"), -350.0f, -350.0f, -350.0f, 350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 5.0f);
_meshRoom.AddWall3D(global.GetTex("wall"), -350.0f, 350.0f, 350.0f, 350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 5.0f);
_meshRoom.AddWall3D(global.GetTex("wall"), 350.0f, 350.0f, 350.0f, -350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 5.0f);
_meshRoom.AddFloor(global.GetTex("sandstone"), -350.0f, -350 - 0f, 350.0f, 350.0f, -50.0f, 10.0f, 10.0f, true);
}
private void LoadTextures()
{
int textureID1 = TexFactory.LoadTexture("textures\\lamp.jpg", "lamp", -1, -1);
int textureID2 = TexFactory.LoadTexture("textures\\wall.jpg", "wall", -1, -1);
int textureID3 = TexFactory.LoadTexture("textures\\granite.bmp", "granite", -1, -1);
int textureID4 = TexFactory.LoadTexture("textures\\metal.bmp", "metal", -1, -1);
int textureID5 = TexFactory.LoadTexture("textures\\sandstone.bmp", "sandstone", -1, -1);
int textureID6 = TexFactory.LoadTexture("textures\\table.jpg", "table", -1, -1);
int textureID7 = TexFactory.LoadTexture("textures\\white.jpg", "white", -1, -1);
int textureID8 = TexFactory.LoadTexture("textures\\brown.jpg", "brown", -1, -1);
int textureID9 = TexFactory.LoadTexture("textures\\black.jpg", "black", -1, -1);
int textureID10 = TexFactory.LoadTexture("textures\\steel.jpg", "steel", -1, -1);
}
bool DoLoop = false;
private void Main_Quit()
{
DoLoop = false;
}
void RotateSomeObjects()
{
if (_rotateFan)
{
_meshFan.RotateY(5, true);
}
}
private void Main_Loop()
{
int fps;
while (DoLoop == true)
{
System.Windows.Forms.Application.DoEvents();
//CheckInput();
CheckMovement();
fps = (int)TV.GetFPS();
TV.Clear(false);
RotateSomeObjects();
_scene.RenderAllMeshes(true);
TV.RenderToScreen();
}
Main_Quit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DoLoop = false;
}
private bool _rotateFan = true;
private void CheckMovement()
{
if (InputEngine.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
{
sngWalk = 1.0f;
}
if (InputEngine.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
{
sngWalk = -1.0f;
}
if (InputEngine.IsKeyPressed(CONST_TV_KEY.TV_KEY_LEFT))
{
sngStrafe = 1.0f;
}
if (InputEngine.IsKeyPressed(CONST_TV_KEY.TV_KEY_RIGHT))
{
sngStrafe = -1.0f;
}
tmpMouseScrollOld = tmpMouseScrollNew;
InputEngine.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1, ref tmpMouseB2, ref tmpMouseB3, ref tmpMouseB4, ref tmpMouseScrollNew);
sngAngleX = sngAngleX - ((float)tmpMouseY / 100);
sngAngleY = sngAngleY - ((float)tmpMouseX / 100);
if (sngAngleX > 1.3)
{
sngAngleX = 1.3f;
}
if (sngAngleX < -1.3)
{
sngAngleX = -1.3f;
}
if (sngWalk > 0)
{
sngWalk = sngWalk - 0.05f;
if (sngWalk < 0)
{
sngWalk = 0.0f;
}
}
if (sngWalk < 0)
{
sngWalk = sngWalk + 0.05f;
if (sngWalk > 0)
{
sngWalk = 0;
}
}
if (sngStrafe > 0)
{
sngStrafe = sngStrafe - 0.05f;
if (sngStrafe < 0)
{
sngStrafe = 0;
}
}
if (sngStrafe < 0)
{
sngStrafe = sngStrafe + 0.05f;
if (sngStrafe > 0)
{
sngStrafe = 0;
}
}
sngPositionX = sngPositionX + (float)(System.Math.Cos((double)sngAngleY) * sngWalk * TV.TimeElapsed()) + (float)(System.Math.Cos((double)sngAngleY + 3.141596 / 2) * sngStrafe * TV.TimeElapsed());
sngPositionZ = sngPositionZ + (float)(System.Math.Sin((double)sngAngleY) * sngWalk * TV.TimeElapsed()) + (float)(System.Math.Sin((double)sngAngleY + 3.141596 / 2) * sngStrafe * TV.TimeElapsed());
sngLookAtX = sngPositionX + (float)System.Math.Cos((double)sngAngleY);
sngLookAtY = sngPositionY + (float)System.Math.Tan((double)sngAngleX);
sngLookAtZ = sngPositionZ + (float)System.Math.Sin((double)sngAngleY);
Scene.SetCamera(sngPositionX, sngPositionY, sngPositionZ, sngLookAtX, sngLookAtY, sngLookAtZ);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
|
|
|
|