XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Adding texture to a sphere?

Last post 27/07/2007 23:33 by Bapa. 9 replies.
  • 24/07/2007 1:58

    Adding texture to a sphere?

    Ok, I have searched tirelessly the past few days on how to display and texture a sphere. After realizing there is no easy way to create a sphere primitive (which is very sad/surprising) I caved and fiddled around with blender until I made one. You can find it here:

    http://www.wakezero.com/csharp/sphere.x
    http://www.wakezero.com/csharp/sphere.xnb

    Rendering the sphere itself was easy, and I have a first person camera that I can use to move around it which is Pretty cool... but I can't for the life of me get a texture on the darn thing =(

    Here is the texture I am trying to apply:

    http://www.wakezero.com/csharp/earthmap.jpg
    http://www.wakezero.com/csharp/earthmap.xnb

    ...and below is the code to load and draw the model. Any help to get this to work would be much appreciated =/

            Model model;
    Texture2D surface;

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
    if (loadAllContent)
    {
    model = content.Load<Model>("sphere");
    surface = content.Load<Texture2D>("earthmap");
    }
    }

    protected override void Draw(GameTime gameTime)
    {
    device.Clear(Color.DarkSlateBlue);

    foreach (ModelMesh mesh in model.Meshes)
    {
    foreach (BasicEffect beffect in mesh.Effects)
    {
    beffect.EnableDefaultLighting();
    beffect.TextureEnabled = true;
    beffect.Texture = surface;
    beffect.World = Matrix.CreateScale(10, 10, 10) * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition);
    beffect.View = camera.viewMatrix;
    beffect.Projection = camera.projectionMatrix;
    }
    mesh.Draw();
    }

    base.Draw(gameTime);
    }



  • 24/07/2007 3:58 In reply to

    Re: Adding texture to a sphere?

    Answer
    Reply Quote
    I could be wrong but I think the issue is that you need to tell XNA which points on your model correspond to points in the texture so it knows *how* to texture it. In this example, Efron does pretty much exactly what you are doing (in terms of assigning the texture) but he isn't importing a model to do it. Instead he creates the model vertex by vertex setting the normals and, crucially, setting the texture coordinates too. Now this could be solved by you wrapping the texture to the sphere in blender and exporting the model again. I know blender internally defines the texture coords and hopefully during the export, these are preserved.

    This is one reason why I created my own Model class so I could dynamically create vertices at runtime. That way I could create my own sphere and know for 100% certainty whether the normals and texture coords were set to what they should be.

    As for creating a sphere, I have already done some code to accomplish this and I'll put something over the next day or two on my blog that you can use. I just need to strip it down a bit so the concept is not lost somewhere in my growing engine code.
  • 24/07/2007 10:27 In reply to

    Re: Adding texture to a sphere?

    That would be great. As for the blender texturing, I will give it a shot... although the whole point of texturing at runtime was so I could change the textures freely.
  • 24/07/2007 17:42 In reply to

    Re: Adding texture to a sphere?

    You should be able to change the texture at run time by changing the texture parameter you pass to the shader providing it is the same size. May even work if it is just the same proportions/aspect ratio.

    Just to clarify my point, I am suggesting Blender as it will export the models with texture coordinates. In other words, for each vertex on the model, it will provide a coordinate for the texture and effectively *pin* that texture coordinate to that vertex on the sphere. The shader should interpolate the rest resulting in a map stretched properly over the sphere. The texture coordinates are hopefully set in the model file when exported so you should be able to replace the texture with a map of Mars if you wished.

    I'll try to get that sphere generation code up tomorrow. In summary though, I create an array of vertices and and array of indices to link those vertices together. I then generate the normals for each vertex so the lighting works properly.
  • 25/07/2007 0:09 In reply to

    Re: Adding texture to a sphere?

    Well, I can't even get blender to texture the sphere correctly... I do however get some colors in a wierd funky pattern though. Help!

    Here is my blender file:

    http://www.wakezero.com/csharp/earth.blend
    http://www.wakezero.com/csharp/earthmap.jpg

    If you put the blender file and the earthmap.jpg in the same place, and then render the file it appears to be working... then I export it as a directx file and I lose it. Here's the directx file:

    http://www.wakezero.com/csharp/earth.x





  • 25/07/2007 4:11 In reply to

    Re: Adding texture to a sphere?

    It's not exporting any texture coordinates. I was kinda hoping it would, perhaps anyone else has some ideas as I haven't had the chance to really play with Blender exporting yet.
  • 25/07/2007 19:01 In reply to

    Re: Adding texture to a sphere?

    Blender's .X exporter has many faults, but it does export Texture coordinates. However, in your model there does not seem to be any. My guess is that you either used procedural texture or texture mapping in the material setting. This does not do anything once the model is exported :-).

    You have to edit the UV (texture coordinates) map to properly texture your sphere. My advice is that you follow this tutorial :
        http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/UV_Map_Basics
    to learn more about how it works (plus it seems they're doing exactly what you want to do).
  • 26/07/2007 1:13 In reply to

    Re: Adding texture to a sphere?

    Try here:
    The SphereMesh class generates a spherical mesh.
    The EarthGloble class wraps a texture onto it.
    enjoy.
  • 26/07/2007 2:18 In reply to

    Re: Adding texture to a sphere?

    Thanks dteviot, with the sphere object I am able to render and texture a sphere. That's exactly what I wanted =)

    Thanks cannontrodder for all the pointers, bottom-line is that I just suck at Blender. I'll keep an eye out for your blog though ;-)

  • 27/07/2007 23:33 In reply to

    Re: Adding texture to a sphere?

    Hehe, well, in the DirectX (C++) API, in the D3DX library, there is actually a function called "D3DXCreateTeapot" or, D3DXCreateSphere".

    All you have to do is create a mesh, use that function with a couple parameters, and tell it to draw a subset.

    That's prolly no help, but oh well :P

    I've also made my own algorithms to make cubes and pyramids fer me as well.

     

Page 1 of 1 (10 items) Previous Next