Sign in to follow this  
jigen3

simplifying a mesh problem

Recommended Posts

hi, i'm really new to learning directx and i've been working through the dx9 kick start book. it seems the api has changed since it was published because some of the code samples don't always run right. i'm having a particular problem on chapter 7, getting the simplify a mesh code to work right. the code (c#): // Clean our main mesh Mesh tempMesh = Mesh.Clean(CleanType.Optimization, mesh, adj, adj); // Replace our existing mesh with this one mesh.Dispose(); mesh = tempMesh; // Get our new simplified mesh simplifiedMesh = Mesh.Simplify(mesh, adj, null, 1, MeshFlags.SimplifyVertex); gives me a runtime error with additional information: object reference not set to an instance of an object. another function i found through looking through the help docs that came with runtime was: SimplificationMesh simplifiedMesh = new SimplificationMesh(mesh, adj); and that function generates the same runtime error. can anyone help me with this? thx jigen3

Share this post


Link to post
Share on other sites
Oh man, you're learning from the same book as I do. How do you find it so far? I think it's horrible.

I would gladly help you but I skipped that chapter. I mean, in that chapter, he was losing me faster than [insert something here], so I just read it fast fast and didn't bother to write the code. Now, I'm starting chapter 11.

Share this post


Link to post
Share on other sites
Hi there jigen3,
How are you doing?

[What does that error mean?]
Most often than not it's the same thing in .NET. You are trying to access a variable that has it's value null/nothing.

[What could be wrong?]
Your mesh might have not been loaded or lost in the cleaning method.

[Solution]
Get a high detailed mesh to play around with. Try removing the clean method.

Share this post


Link to post
Share on other sites
i think it has been pretty decent so far at getting my feet wet. it's going to take more than one book to become familiar with an api. i realize i'm going to have to look for other resources to find out the concepts he skims over though (like what is cleaning a mesh exactly?) and it's really troublesome when calling a function in the api generates a runtime error which you don't know how to solve, which is the most trouble i found in learning this so far.

Share this post


Link to post
Share on other sites
thanks for the reply, well despite the docs saying that the mesh has to be cleaned before simplified, i removed the call to the clean function, but it still gives me a runtime error. for some reason calling i'm having trouble with the mesh.Dispose function as well...anyway, here's the code he uses, u need your own mesh and texture:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Chapter7Code
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private Mesh mesh = null;
private Mesh simplifiedMesh = null;
private Material[] meshMaterials;
private Texture[] meshTextures;

private bool isSimplified = false;
private bool isClose = true;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private float angle = 0.0f;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);
}

/// <summary>
/// We will initialize our graphics device here
/// </summary>
public void InitializeGraphics()
{
// Set our presentation parameters
PresentParameters presentParams = new PresentParameters();

presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;

// Create our device
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

// Load our mesh
LoadMesh(@"..\..\tiny.x");
}

private void LoadMesh(string file)
{
ExtendedMaterial[] mtrl;
GraphicsStream adj;

// Load our mesh
mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out adj, out mtrl);

// If we have any materials, store them
if ((mtrl != null) && (mtrl.Length > 0))
{
meshMaterials = new Material[mtrl.Length];
meshTextures = new Texture[mtrl.Length];

// Store each material and texture
for (int i = 0; i < mtrl.Length; i++)
{
meshMaterials[i] = mtrl[i].Material3D;
if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
{
// We have a texture, try to load it
meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" + mtrl[i].TextureFilename);
}
}
}

// Clean our main mesh
Mesh tempMesh = Mesh.Clean(mesh, adj, adj);
// Replace our existing mesh with this one
mesh.Dispose();
mesh = tempMesh;

// Get our new simplified mesh
simplifiedMesh = Mesh.Simplify(mesh, adj, null, 1, MeshFlags.SimplifyVertex);

// Save our simplified mesh for later use
// First we need to generate the adjacency for this mesh
int[] simpleAdj = new int[simplifiedMesh.NumberFaces * 3];
simplifiedMesh.GenerateAdjacency(0.0f, simpleAdj);
using (Mesh cleanedMesh = Mesh.Clean(simplifiedMesh,
simpleAdj, out simpleAdj))
{
cleanedMesh.Save(@"..\..\simple.x", simpleAdj,
mtrl, XFileFormat.Text);
}
Console.WriteLine("Number of vertices in original mesh: {0}", mesh.NumberVertices);
Console.WriteLine("Number of vertices in simplified mesh: {0}", simplifiedMesh.NumberVertices);
}

private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 10000.0f);
if (isClose)
{
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 580.0f), new Vector3(), new Vector3(0,1,0));
}
else
{
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 8580.0f), new Vector3(), new Vector3(0,1,0));
}
//device.RenderState.FillMode = FillMode.WireFrame;
device.RenderState.Ambient = Color.DarkBlue;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(0, -1, -1);
device.Lights[0].Commit();
device.Lights[0].Enabled = true;

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);

SetupCamera();

device.BeginScene();

// Draw our Mesh
DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);

device.EndScene();

device.Present();

this.Invalidate();
}

private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
{
angle += 0.11f;

device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
for (int i = 0; i < meshMaterials.Length; i++)
{
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
if (!isSimplified)
{
mesh.DrawSubset(i);
}
else
{
simplifiedMesh.DrawSubset(i);
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
{
isSimplified = !isSimplified;
}
if (e.KeyChar == 'm')
{
isClose = !isClose;
}
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new Size(800,600);
this.Text = "Form1";
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}



[Edited by - Coder on August 2, 2005 5:13:41 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this