?
Solved

sphere mesh texture map using MDX

Posted on 2006-07-09
8
Medium Priority
?
2,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

743 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