tags:

views:

905

answers:

1

Hi all,

I'm attempting to render a textured quad using the example located here.

I can successfully render the quad, but the texture information appears to be lost. The quad takes the color of the underlying texture, though.

I've checked the obvious problems ("Does the BasicEffect rendering the quad have the TextureEnabled property set to true?") and can't immediately see the problem.

Code below:

public class Quad
{
 public VertexPositionNormalTexture[] Vertices;
 public Vector3 Origin;
 public Vector3 Up;
 public Vector3 Normal;
 public Vector3 Left;
 public Vector3 UpperLeft;
 public Vector3 UpperRight;
 public Vector3 LowerLeft;
 public Vector3 LowerRight;
 public int[] Indexes;


 public Quad(Vector3 origin, Vector3 normal, Vector3 up,
   float width, float height)
 {
  this.Vertices = new VertexPositionNormalTexture[4];
  this.Indexes = new int[6];
  this.Origin = origin;
  this.Normal = normal;
  this.Up = up;

  // Calculate the quad corners
  this.Left = Vector3.Cross(normal, this.Up);
  Vector3 uppercenter = (this.Up * height / 2) + origin;
  this.UpperLeft = uppercenter + (this.Left * width / 2);
  this.UpperRight = uppercenter - (this.Left * width / 2);
  this.LowerLeft = this.UpperLeft - (this.Up * height);
  this.LowerRight = this.UpperRight - (this.Up * height);

  this.FillVertices();
 }

 private void FillVertices()
 {
  Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
  Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
  Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
  Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);

  for (int i = 0; i < this.Vertices.Length; i++)
  {
   this.Vertices[i].Normal = this.Normal;
  }

  this.Vertices[0].Position = this.LowerLeft;
  this.Vertices[0].TextureCoordinate = textureLowerLeft;
  this.Vertices[1].Position = this.UpperLeft;
  this.Vertices[1].TextureCoordinate = textureUpperLeft;
  this.Vertices[2].Position = this.LowerRight;
  this.Vertices[2].TextureCoordinate = textureLowerRight;
  this.Vertices[3].Position = this.UpperRight;
  this.Vertices[3].TextureCoordinate = textureUpperRight;

  this.Indexes[0] = 0;
  this.Indexes[1] = 1;
  this.Indexes[2] = 2;
  this.Indexes[3] = 2;
  this.Indexes[4] = 1;
  this.Indexes[5] = 3;
 }
}


this.quadEffect = new BasicEffect(this.GraphicsDevice, null);
this.quadEffect.AmbientLightColor = new Vector3(0.8f, 0.8f, 0.8f);
this.quadEffect.LightingEnabled = true;
this.quadEffect.World = Matrix.Identity;
this.quadEffect.View = this.View;
this.quadEffect.Projection = this.Projection;
this.quadEffect.TextureEnabled = true;
this.quadEffect.Texture = someTexture;

this.quad = new Quad(Vector3.Zero, Vector3.UnitZ, Vector3.Up, 2, 2);

this.quadVertexDecl = new VertexDeclaration(this.GraphicsDevice, VertexPositionNormalTexture.VertexElements);


public override void Draw(GameTime gameTime)
{
    this.GraphicsDevice.Textures[0] = this.SpriteDictionary["B1S1I800"];
    this.GraphicsDevice.VertexDeclaration = quadVertexDecl;
    quadEffect.Begin();

    foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
    {
     pass.Begin();
     GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
       PrimitiveType.TriangleList,
       beamQuad.Vertices, 0, 4,
       beamQuad.Indexes, 0, 2);

     pass.End();
    }

    quadEffect.End();
}
+1  A: 

From what I can see, this should work. The only thing I can imagine, which isn't in this code, is that the loading of the texture goes wrong somewhere. I also can't quite visualize what you mean that the quad has the underlying color of the texture? Do you have a screenshot for us?

Also, if something does show up, a very distorted version of your texture for example, it could be possible that the rendering of other stuff has effect on the rendering of the quad. For example if you draw the quad while the graphicsdevice has another vertex declaration on it, or if the previous thing rendered set some exotic rendering state, or if you're drawing the quad within the drawing code of something else. Try isolating this code, into a fresh project or something, or disable the rendering of everything else.

JulianR
The previous thing did indeed set an exotic render state. The code above is more or less correct, but I neglected to mention that I was using the BasicEffect in conjunction with a SpriteBatch, which mutates render state. I had wrongly assumed that the SpriteBatch was side-effect free.
Gabriel Isenberg