MoP73

Hi,

I'm just trying to create a couple of walls and apply a texture to them. Unfortunately, when running the program the walls just come out as a grey sort of colour (depending on the texture, the shade of grey seems to change).

I'm assuming there's something obvious I'm missing, so I'm hoping someone can help me out.

Here's snippets of code handling the basiceffect:

//In loadgraphicscontent:

Texture2D wallTexture = content.Load<Texture2D>("Content\\Textures\\em2");
theWalls = new Walls(wallTexture);
wallEffect = new BasicEffect(graphics.GraphicsDevice, null);
//wallEffect.DiffuseColor = theWalls.TheColor.ToVector3();
wallEffect.TextureEnabled = true;
wallEffect.Texture = theWalls.WallTexture;

wallEffect.World = worldMatrix;
wallEffect.View = viewMatrix;
wallEffect.Projection = projectionMatrix;

//In Draw:

graphics.GraphicsDevice.Clear(Color.Black); // Clears the screen
graphics.GraphicsDevice.RenderState.CullMode = CullMode.None; // Rendering backface triangles
graphics.GraphicsDevice.VertexDeclaration = effectVertexDeclaration; // Sets the vertex declaration

wallEffect.Begin();
foreach (EffectPass pass in wallEffect.CurrentTechnique.Passes)
{
pass.Begin();
wallEffect.View = viewMatrix;
//wallEffect.Projection = projectionMatrix;
graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, theWalls.WallVertices, 0, 4);
pass.End();
}
wallEffect.End();

base.Draw(gameTime);

/////------------------

In my walls class I just define the position of the vertices and the texture coordinates (between 0 & 1), if need be I can post that too.

Thanks.


Re: XNA Game Studio Express Textures don't show up when using Basiceffect

Shawn Hargreaves - MSFT

What type are your wall vertices





Re: XNA Game Studio Express Textures don't show up when using Basiceffect

dczraptor

And make sure you have UV coordinates on the correct side of your primitives (facing the camera).




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

MoP73

Thank you for the replies.

Here is part of the constructor for the Walls class. I'm using the VertexPositionTexture class.

I'm not aware of how to differentiate which side the texture appears on, but if I had it on the wrong side, wouldn't I then be able to see it when I move the camera around

wallVertices = new VertexPositionTexture[12];

// Back
wallVertices[0].Position = new Vector3(-25.0f, -25.0f, -25.0f);
wallVertices[1].Position = new Vector3(25.0f, -25.0f, -25.0f);
wallVertices[2].Position = new Vector3(25.0f, 25.0f, -25.0f);
wallVertices[3].Position = new Vector3(25.0f, 25.0f, -25.0f);
wallVertices[4].Position = new Vector3(-25.0f, -25.0f, -25.0f);
wallVertices[5].Position = new Vector3(-25.0f, 25.0f, -25.0f);

wallVertices[0].TextureCoordinate = new Vector2(0, 0);
wallVertices[1].TextureCoordinate = new Vector2(1, 0);
wallVertices[2].TextureCoordinate = new Vector2(1, 1);
wallVertices[3].TextureCoordinate = new Vector2(1, 1);
wallVertices[4].TextureCoordinate = new Vector2(0, 0);
wallVertices[5].TextureCoordinate = new Vector2(0, 1);

Thanks.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

dczraptor

Actually, i think you wouldn't be able to see a wall if they were on the wrong side, so i don't think that's the problem. However, I also noticed that you're using a TriangleList, but you're trying to render 4 primitives, when you only have 2. I don't know if that's part of the problem or not though. The vertex declarations look correct to me as far as I can tell, except for the fact that you declared an array of 12 vertices, but you're only using 6 of them.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

MoP73

dczraptor wrote:
Actually, i think you wouldn't be able to see a wall if they were on the wrong side, so i don't think that's the problem. However, I also noticed that you're using a TriangleList, but you're trying to render 4 primitives, when you only have 2. I don't know if that's part of the problem or not though. The vertex declarations look correct to me as far as I can tell, except for the fact that you declared an array of 12 vertices, but you're only using 6 of them.


The other part of the Walls constructor inits the other primitives.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

Shawn Hargreaves - MSFT

I can't see anything wrong with your code.

A couple of things to try:

- Install the debug DX runtime and see if the output spew says anything interesting.

- Capture a frame of your app using PIX for Windows, then dig into the renderstates and shader debuggers for your wall to see if you can spot where it is going wrong.





Re: XNA Game Studio Express Textures don't show up when using Basiceffect

gvigelet

The code does look correct I would suspect that the issue is with the world-view-projection matrices

check that these are correct:

        wallEffect.World = worldMatrix;
        wallEffect.View = viewMatrix;
        wallEffect.Projection = projectionMatrix;

 






Re: XNA Game Studio Express Textures don't show up when using Basiceffect

errolian

Do you have lighting enabled for your model




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

MoP73

errolian wrote:
Do you have lighting enabled for your model


Nope. I just tried enabling it for the BasicEffect and when running the screen was all black. Probably telling me that it needs a light to see.

gvigelet wrote:

The code does look correct I would suspect that the issue is with the world-view-projection matrices

check that these are correct:

wallEffect.World = worldMatrix;
wallEffect.View = viewMatrix;
wallEffect.Projection = projectionMatrix;



I'm not 100% sure what you're asking me to check on these - are you asking me to make sure I'm looking at the wall when I start the program If so, then yep, I can see the wall.

Shawn Hargreaves - MSFT wrote:
I can't see anything wrong with your code.

A couple of things to try:

- Install the debug DX runtime and see if the output spew says anything interesting.

- Capture a frame of your app using PIX for Windows, then dig into the renderstates and shader debuggers for your wall to see if you can spot where it is going wrong.


I'll check these 2 things out, although they sound like they might be a bit above my current level of expertise.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

dczraptor

It might be your UV coordinates. For example, if one of your coordinates was 0,0, and anther was 0,0.1f, then you would get a LOT of streching in between. So, if you have some messed up coordinates, you would be getting strange stretching results That's the only thing i could think of, because I've had that problem myself. Your coordinates look correct for the 6 that you posted, but the others may be causing trouble.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

Shawn Hargreaves - MSFT

This could also happen if your texture coordinates are very large. If they ranged say from 0 to 1000 rather than just 0 to 1, the texture would be tiled so many times that the GPU will end up just displaying the smallest 1x1 mipmap level, which would have exactly this result.

There are also various renderstates that could be forcing it to use only a small mip, but it's hard to imagine how any of those could have been set by mistake.

PIX can definately help with this sort of question: if you debug the mesh in question, you can examine each vertex to see exactly what texture coordinates were output from the vertex shader, and examine each pixel to see what texture coordinate it was drawn using. Makes it very easy to see if the texture coordinates really are coming through how you think they should be.





Re: XNA Game Studio Express Textures don't show up when using Basiceffect

MoP73

Well, I've finally figured it out.


Figured out that I'm an idiot, at least.

Turns out that I failed to mention using this piece of code in my program:

private void InitializeTransform()
{
// worldMatrix = Matrix.CreateTranslation(Vector3.Zero); //TODO: Is this needed
// viewMatrix = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, -2.5f), Vector3.Zero, Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), // 45 degree FoV
(float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
1.0f, 1000.0f); // Near/far planes

effectVertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
}

The part inside of 'new VertexDeclaration' using VertexPositionTexture said VertexPositionColor before I just noticed it a minute ago - originally I was using VPC for debugging purposes on getting the list of triangles to show up.

You may all now laugh at my expense.


Anyway, thanks for the help everyone. I promise I won't ask for help again for at least a couple of hours.




Re: XNA Game Studio Express Textures don't show up when using Basiceffect

Shawn Hargreaves - MSFT

Aha!

This is exactly the kind of problem that the debug DX runtime makes it very easy to spot.

If you ran this with the debug runtime, the output spew would contain a warning somethling something like "vertex shader reads from TEXCOORD0 input semantic, but current vertex declaration does not contain a value for TEXCOORD0".