Jump to content
  • Advertisement
Sign in to follow this  
djnevs

[c#] .X Limb texturing [Fixed]

This topic is 4003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = mtrl.Material3D;
                if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
                {
                    meshTextures = TextureLoader.FromFile(device, Application.StartupPath + @"\Media\Models\Textures\" + mtrl.TextureFilename);
                    Console.WriteLine(mtrl.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;
                    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
Advertisement
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 = mtrl.Material3D;
if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
{
meshTextures = TextureLoader.FromFile(device, Application.StartupPath + @"\Media\Models\Textures\" + mtrl.TextureFilename);
Console.WriteLine(mtrl.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);
device.Material = meshMaterials;
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!