Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textures on meshes

Status
Not open for further replies.

Kostarsus

Programmer
Nov 30, 2000
86
DE
Hello,

I want render a texture on a mesh. The texture is loaded from a file. The mesh is created by the primitive mesh methode (e.g. Mesh.Box)
I'd read, that this meshes don't have meshcoordinates. So I cloned the mesh to a mesh with texturecoordinates.
But the texture isn't drawn.
Can anyone help me please? A source example would be helpful, because all examples I found load the mesh from a file, where the textures are defined, too.
Here is the source-code which I use.
Code:
Appearance appearance=(Appearance)m_appearance;
ImageTexture appTexture=(ImageTexture)appearance.Texture;
if (appTexture!=null){
     Texture texture = TextureLoader.FromFile
        (directXDevice,appTexture.Url.GetElementAt(i));
     directXDevice.SetTexture(0,texture);
}	

Microsoft.DirectX.Direct3D.Mesh tempboxMesh=null;
tempboxMesh=Mesh.Box(directXDevice,this.m_size.X,m_size.Y,m_size.Z);
Mesh boxMesh=tempboxMesh.Clone(tempboxMesh.Options.Value, tempboxMesh.VertexFormat | VertexFormats.Normal | VertexFormats.Texture0,directXDevice);

boxMesh.DrawSubset(0);
boxMesh.Dispose();
Thanks

Kostarsus

 
I have never used anything beyond the very basics of D3D with C# (I'm an unmanaged C++ kind of guy), but I'll try to help. You gave it texture coordinates, but you never specified the values of the coordinates. You would have to specify which vertices of the box coorespond to what texture coordinate - there is no way that D3D could know what corners get what texture coords. I would recommend just creating your own index and vertex buffer to render the box yourself rather than using the Mesh class. It wouldn't be too much work at all considering this is just a box.

By the way, PLEASE tell me that you just pasted code from different methods and you aren't creating and releasing a mesh and texture every frame. That's the best way to get fewer frames per second than Doom 3 at 1600x1200 on a TNT2.
 
Hi,

the box is only an example. The program should render other primitives e.g. a sphere or cylinder, too. So to build a index and vertex buffer is right complex.
I don't know, how to get the vertices of the mesh. Are there any methods in C++? Perhaps I would find them in C#, too :)

cu Kostarsus

P.S. I'd pasted the code from different methods.
 
Mesh has a VertexBuffer property which will allow you to get to the VB. Just lock it and set the texture coordinates. In C++ there is a class D3DXMesh that is similar to Mesh, but I have never used it for anything other than loading them from files. I always just create the VBs and IBs myself when doing otherwise. It helps keep my math skills sharp :).
 
I' changed the code to this:
Code:
bool urlFound=false;
int i=0;
Mesh boxMesh=Mesh.Box(directXDevice,this.m_size.X,m_size.Y,m_size.Z);	
Appearance objectAppearance=(Appearance)m_appearance;
if (objectAppearance.Texture!=null)
{
    Texture texture=null;
    ImageTexture imageTexture=(ImageTexture)objectAppearance.Texture;
    Mesh clonedMesh=boxMesh.Clone(boxMesh.Options.Value,boxMesh.VertexFormat | VertexFormats.Normal | VertexFormats.Texture0, directXDevice);
    while (i<imageTexture.Url.Count && urlFound==false)
    {
	if(System.IO.File.Exists(imageTexture.Url.GetElementAt(i)))
	{
		urlFound=true;				
		texture = TextureLoader.FromFile(directXDevice,imageTexture.Url.GetElementAt(i));		}
    }
    for (i=0;i<clonedMesh.NumberVertices;i++)		
    {					directXDevice.SetTexture(0,texture);			clonedMesh.DrawSubset(i);
    }
}
boxMesh.Dispose();

But the texture isn't diplayed. Where is now the error?
Can anyone send an example, please. no matter if it is in managed or unmanaged code.
I'll try to translate unmanaged code to managed code than.

cu Kostarsus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top