Solved

sphere mesh texture map using MDX

Posted on 2006-07-09
8
2,084 Views
Last Modified: 2013-12-08
hello,

how can i wrap a given texture to my sphere mesh in direct x? i tried
using the code in the 'Applying Textures part 3 (ZMan)' article (
http://msdn.microsoft.com/coding4fun/zman/zmantextures3/default.aspx )
but it didnt work.

can you please show me some code and make it as brief and simple as
possible?

when i use the following code:

device.SetTexture(0, TextureLoader.FromFile(device,
System.Windows.Forms.Application.StartupPath + @"\woodcrate.bmp" ));

it makes the sphere black in color. i dont see the texture clearly on
the mesh. i just see a black sphere. do i need to set texture
coordinates?

thanks for your time,

Zahid
0
Comment
Question by:zahidayar
  • 5
  • 2
8 Comments
 
LVL 4

Expert Comment

by:joachimc
ID: 17081305
I am guessing you haven't set any lights pointing at the sphere.

You could try making the texture self iluminating to test if that is the problem. It's one of the material settings I just can't remeber at the moment. Could be the emissive property.

But of course you need to set the ambient lighting and some kind of directional light to get it right.
0
 
LVL 3

Accepted Solution

by:
OutsideTheBox earned 500 total points
ID: 17108390
Zahid,

Could you please post your code, there could be a number of things that could cause this problem. It would help us to eliminate possibilities.

Are you sure you have woodcrate.bmp in the correct folder?

Most of the time you create a mesh with textures in a program like 3ds max or maya and these programs set texture coords and you do not have to set them manually because they import with the mesh. It doesn't look like that is your case but I would think in your case they just defaulted to 0,0 at vertex #1 of your sphere and you would have seen 'something' anyways. So I don't think that not setting them is your problem.

You said that you can't see the texture clearly. Can you see it at all? Is it there but somehow incorrect? or is the sphere simply just black with the texture missing?

I have found when following tutorials, that when they do not work, I've made a small, simple mistake in the coding somewhere that just takes a while to find, then I thump my forhead and wonder why I missed it. It rarely is the tutorial.

Steve
0
 

Author Comment

by:zahidayar
ID: 17111107
thanks for the replies....
lighting is set up...the sphere mesh just looks completely black....i didnt notice anything of the texture...i also asked this question on another forum (gamedev.net) and they suggested that i should setup the VertexFormat (and the texture coordinates) of the mesh properly...i will work on that soon hopefully...im working on other things right now...thanks again for the replies...

Zahid
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zahidayar
ID: 17111110
by the way, i meant black with light shining off of it.. :)
0
 

Author Comment

by:zahidayar
ID: 17204470
hello,

this is my code:

            protected void SetupMaterial()
            {
                  this.iconMaterial = new Material();      
                  this.iconMaterial.Diffuse = Color.Blue;
                  this.iconMaterial.Specular = Color.LightGray;
                  this.iconMaterial.SpecularSharpness = 15.0F;
                  this.iconDrawingDevice.Material = this.iconMaterial;
                  this.iconDrawingDevice.RenderState.SpecularEnable = true;
                   this.iconDrawingDevice.SamplerState[0].AddressU = TextureAddress.Wrap;
                          this.iconDrawingDevice.SamplerState[0].AddressV = TextureAddress.Wrap;
           
if ( (addNelementIconMesh.VertexFormat & VertexFormats.Texture1) == 0 )
{
    // create a cloned mesh with texture coordinates attached to it
    Mesh tempMesh = addNelementIconMesh.Clone ( this.addNelementIconMesh.Options.Value , this.addNelementIconMesh.VertexFormat | VertexFormats.Texture1 , this.iconDrawingDevice );
    // get rid of the old mesh
    this.addNelementIconMesh.Dispose();
    // assign the new one
    this.addNelementIconMesh = tempMesh;
    // calculate them
   // CalculateSphereTextureCoordinates();

}

            this.iconDrawingDevice.SetTexture(0, TextureLoader.FromFile(this.iconDrawingDevice, System.Windows.Forms.Application.StartupPath + @"\woodcrate.bmp" ));
            }

as u can see the CalculateSphereTextureCoordinates() method is commented... i got some parts of the code from a member at gamedev.net....i would be happy if someone would provide a possible implementation of the CalculateSphereTextureCoordinates() method...

thanks for your time,

Zahid
0
 

Author Comment

by:zahidayar
ID: 17204473
yeah...woodcrate.bmp is in the correct folder...they are both in the same folder
0
 

Author Comment

by:zahidayar
ID: 17302905
hello,

yeah Steve, i set the texture coordinates and the texture mapping worked. :)

i used the following code:

            private void CalculateTextureCoordinatesForThisSphereMesh ( Mesh sphereMesh )
            {
                  using ( VertexBuffer vb = sphereMesh.VertexBuffer )
                      {
                          CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vb.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, sphereMesh.NumberVertices);                          
                             try
                              {
                                  for (int i = 0; i < verts.Length; i++)
                                  {
                                            verts[i].Tu =  Convert.ToSingle ( Math.Asin( verts[i].Nx ) / Math.PI + 0.5 );
                                            verts[i].Tv = -1 * Convert.ToSingle ( Math.Asin( verts[i].Ny ) / Math.PI + 0.5 );                  
                                  }
                              }
                              finally
                              {
                                  vb.Unlock();
                              }
                    }
            }

thanks for the help everyone..

Zahid
0
 
LVL 3

Expert Comment

by:OutsideTheBox
ID: 17317811
Glad you figured it out.

Thanks,
Steve
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question