Solved

sphere mesh texture map using MDX

Posted on 2006-07-09
8
2,069 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
 

Author Comment

by:zahidayar
ID: 17111110
by the way, i meant black with light shining off of it.. :)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now