Sign in to follow this  
djnevs

[c#] .X Limb texturing [Fixed]

Recommended Posts

Hello, I got this model of a head, and i need to texture it.. the model is .x texture files are all .jpg i got 7 textures needed to texture the head model. like, eyes, mouth, teeth etc..
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

public class cObject
{
    bool objfromfile = false;
    private Mesh obj;
    private Material m = new Material();

    private ExtendedMaterial[] mtrl;
    private Material[] meshMaterials;
    private Texture[] meshTextures;

    public void CreateBox(Device device, float x, float y, float z)
    {
        obj = Mesh.Box(device, x, y, z);
        m.Diffuse = m.Ambient = System.Drawing.Color.Red;
        obj.ComputeNormals();
    }


    public void CreateSphere(Device device, float radius, int slices, int stacks)
    {
        obj = Mesh.Sphere(device, radius, slices, stacks);
        m.Diffuse = m.Ambient = System.Drawing.Color.Red;
        obj.ComputeNormals();
    }


    public void CreateFromFile(Device device, string file)
    {
        objfromfile = true;

        obj = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);
        
        if ((mtrl != null) && (mtrl.Length > 0))
        {
            meshMaterials = new Material[mtrl.Length];
            meshTextures = new Texture[mtrl.Length];

            for (int i = 0; i < mtrl.Length; i++)
            {
                meshMaterials[i] = mtrl[i].Material3D;
                if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
                {
                    meshTextures[i] = TextureLoader.FromFile(device, Application.StartupPath + @"\Media\Models\Textures\" + mtrl[i].TextureFilename);
                    Console.WriteLine(mtrl[i].TextureFilename);
                }
            }
        }
    }

    public void OnPaint(Device device, float xpos, float ypos, float zpos)
    {
        device.Transform.World = Matrix.Identity * Matrix.RotationYawPitchRoll(0, 0, 0) * Matrix.Translation(xpos, ypos, zpos);
        if (objfromfile)
        {
            device.SamplerState[0].MipFilter = TextureFilter.Linear;
            device.SamplerState[0].MinFilter = TextureFilter.Linear;
            device.SamplerState[0].MagFilter = TextureFilter.Linear;

            for (int i = 0; i < meshMaterials.Length; i++)
            {

                    device.Material = meshMaterials[i];
                    obj.DrawSubset(i);
                
            }

        }
        else
        {
            device.Material = m;
            obj.DrawSubset(0);
        }
    }
}


[Edited by - djnevs on August 6, 2007 5:18:41 AM]

Share this post


Link to post
Share on other sites
Fixed

for ppl who wanna use it, here's the new source:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

public class cObject
{
bool objfromfile = false;
private Mesh obj;
private Material m = new Material();

private ExtendedMaterial[] mtrl;
private Material[] meshMaterials;
private Texture[] meshTextures;

public void CreateBox(Device device, float x, float y, float z)
{
obj = Mesh.Box(device, x, y, z);
m.Diffuse = m.Ambient = System.Drawing.Color.Red;
obj.ComputeNormals();
}


public void CreateSphere(Device device, float radius, int slices, int stacks)
{
obj = Mesh.Sphere(device, radius, slices, stacks);
m.Diffuse = m.Ambient = System.Drawing.Color.Red;
obj.ComputeNormals();
}


public void CreateFromFile(Device device, string file)
{
objfromfile = true;

obj = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);

if ((mtrl != null) && (mtrl.Length > 0))
{
meshMaterials = new Material[mtrl.Length];
meshTextures = new Texture[mtrl.Length];

for (int i = 0; i < mtrl.Length; i++)
{
meshMaterials[i] = mtrl[i].Material3D;
if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
{
meshTextures[i] = TextureLoader.FromFile(device, Application.StartupPath + @"\Media\Models\Textures\" + mtrl[i].TextureFilename);
Console.WriteLine(mtrl[i].TextureFilename);
}
}
}
}

public void OnPaint(Device device, float xpos, float ypos, float zpos)
{
device.Transform.World = Matrix.Identity * Matrix.RotationYawPitchRoll(0, 0, 0) * Matrix.Translation(xpos, ypos, zpos);
if (objfromfile)
{
device.SamplerState[0].MipFilter = TextureFilter.Linear;
device.SamplerState[0].MinFilter = TextureFilter.Linear;
device.SamplerState[0].MagFilter = TextureFilter.Linear;

for (int i = 0; i < meshMaterials.Length; i++)
{
device.SetTexture(0, meshTextures[i]);
device.Material = meshMaterials[i];
obj.DrawSubset(i);

}

} // meot hier aan liggen dus..
else
{
device.Material = m;
obj.DrawSubset(0);
}
}
}

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