Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA Texturing issue

Tags:

c#

xna

Ok, I've been trying a lot of new things lately, and I've had a few stopping points. I decided to leave 3d because I figured I just didn't and couldn't understand the coding involved. I'm pretty good at math though, so I figured I would give it another shot.

I'm trying to learn 3d XNA in c#, I've recently worked out 2d and wish to move on. My problem is that with (in my opinion) the most basic of 3d shapes, a cube, I run into problems. After successfully exporting my cube from blender (after the 7th try >_>) and importing it into XNA, I can't get a texture to correctly show on the cube, so I downloaded a cube model from a sample source code file, and attempted to use that, and it's default texture, and I still have problems.

Basically, the code to draw the cube is:

foreach (ModelMesh mesh in model.Meshes)
{
    GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
    foreach (BasicEffect effect in mesh.Effects)
    {
        effect.TextureEnabled = true;
        //effect.Texture = texture;
        effect.World = world;
        effect.View = view;
        effect.Projection = projection;
        effect.LightingEnabled = false;
    }
    mesh.Draw();
}

The RenderState and LightingEnabled are new, attempting to fix it myself, when I use my own texture, the texture looks like it's being stretched, and isn't showing the entire image on the cube, but all faces look the same, so it's not wrapping it.

Also, to see all the faces I rotate the cube like:

position -= new Vector3(0, 0.00f, 0.0100f);
angley += 0.01f;
anglez += 0.01f;

world = Matrix.CreateScale(1.5f) * Matrix.CreateRotationZ(anglez) * Matrix.CreateRotationY(angley) * Matrix.CreateTranslation(position);

The z change is so I could test another theory.

The default texture is a sandish texture, I'm not sure if it's stretching, because it's almost a solid color. But the box itself seems to be oddly represented (it looks as though I can see through the near faces, and I'm looking at the backs of the opposing ones)

I'm hoping someone can give me a hand, it just seems like it should be much simpler then it seems to be, to draw a simple textured cube, and most of the tutorials online are from older versions of XNA, so the code doesn't match up, and I get lost when trying to replace it with current code. (On tutorials that create a cube in code, rather then a model.)

Anyways, thanks for any answers.

EDIT 1: Drawing this cube with CreateOrthographic makes it look correct (the first one uses perspective) but still no texture love :(

EDIT 2: When I use my cube it's stretched, when I use the one from the source, it's a solid color.

EDIT 3: I probably would have gotten an answer sooner had I mentioned I was displaying FPS using a font/spritebatch. When I was working out why it wouldn't work, and comparing it to a sample that DID work, I found it, Now, does anyone know how to make that work?

like image 895
RyanTimmons91 Avatar asked Feb 18 '11 13:02

RyanTimmons91


1 Answers

To myself, and anyone else encountering this problem >.<

Are you using:

SpriteBatch.begin()

to do anything in your code? If so, this is screwing up with the way your program renders in 3d, check out this link (for pre XNA 4.0):

http://blogs.msdn.com/b/shawnhar/archive/2006/11/13/spritebatch-and-renderstates.aspx

And this link if you using XNA 4.0:

http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

The line in particular that fixed this issue was:

GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

Adding that before the foreach loop fixed this problem for me, but you may need to try the other lines in that article

To anyone else reading this, Good luck with your XNA dreams :D

like image 84
RyanTimmons91 Avatar answered Nov 07 '22 11:11

RyanTimmons91