Blendstate

Started by
0 comments, last by broady 12 years, 8 months ago
Hi,

i wanna draw a quad and apply a texture. The texture is a tga file black on the sides and transparent in the middle. Namely it's a frame.

When the quad is drawn it results all black.

The texture mapping i am sure it's coded correctly since if i apply a painted texture the quad is drawn properly.

So it must be a blend issue. I khnow there is the blendstate object but from the books it is always used with spritebeatch objects. In my case the quad id drown via userPrimitive functions.

The quad class


class Quad
{
public Vector3 Origin;
public Vector3 UpperLeft;
public Vector3 LowerLeft;
public Vector3 UpperRight;
public Vector3 LowerRight;
public Vector3 Normal;
public Vector3 Up;
public Vector3 Left;

public VertexPositionNormalTexture[] Vertices;
public int[] Indices;

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

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

FillVertices();
}

private void FillVertices()
{
// Fill in texture coordinates to display full texture
// on quad
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);

// Provide a normal for each vertex
for (int i = 0; i < Vertices.Length; i++)
{
Vertices.Normal = Normal;
}

// Set the position and texture coordinate for each
// vertex
Vertices[0].Position = LowerLeft;
Vertices[0].TextureCoordinate = textureLowerLeft;
Vertices[1].Position = UpperLeft;
Vertices[1].TextureCoordinate = textureUpperLeft;
Vertices[2].Position = LowerRight;
Vertices[2].TextureCoordinate = textureLowerRight;
Vertices[3].Position = UpperRight;
Vertices[3].TextureCoordinate = textureUpperRight;

// Set the index buffer for each vertex, using
// clockwise winding
Indices[0] = 0;
Indices[1] = 1;
Indices[2] = 2;
Indices[3] = 2;
Indices[4] = 1;
Indices[5] = 3;

}



and the tunnel class: the tunnel class is a sort of container of Quads objects and it serves to draw a "tunnel of quads"


public class Tunnel : Microsoft.Xna.Framework.DrawableGameComponent
{
Vector3 _origin, _normal, _up;
float _width, _height;
int _near, _far;
GraphicsDeviceManager _graphics;
ContentManager _content;
Texture2D _texture;
BasicEffect quadEffect;
CameraComponent _camera;
List<Quad> _quads;
string _texname;
public Tunnel(Game game, GraphicsDeviceManager gdm, CameraComponent camera, ContentManager cm, int near, int far, Vector3 origin, Vector3 normal, Vector3 up, float width, float height, string texture)
: base(game)
{
// TODO: Construct any child components here
_origin = origin;
_normal = normal;
_up = up;
_width = width;
_height = height;
_graphics = gdm;
_content = cm;
_camera = camera;
_texname = texture;
_near = near;
_far = far;

}
protected override void LoadContent()
{
BlendState bs = new BlendState();
bs.AlphaSourceBlend = Blend.One;
bs.AlphaDestinationBlend = Blend.Zero;
_texture = _content.Load<Texture2D>(_texname);
quadEffect = new BasicEffect(_graphics.GraphicsDevice);
//quadEffect.EnableDefaultLighting();

quadEffect.World = Matrix.Identity;
quadEffect.View = _camera.View;
quadEffect.Projection = _camera.Proj;
quadEffect.TextureEnabled = true;
quadEffect.Texture = _texture;
quadEffect.Alpha = 1;
quadEffect.VertexColorEnabled = false;


base.LoadContent();
}

/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
_quads = new List<Quad>();
for (int i = _near; i < _far; i++)
{
_quads.Add(new Quad(new Vector3(0,0,(float)i*(-1)), _normal, _up, _width, _height));
}
base.Initialize();
}


private void DrawQuad(Quad quad)
{



foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
{
pass.Apply();

_graphics.GraphicsDevice.DrawUserIndexedPrimitives
<VertexPositionNormalTexture>(
PrimitiveType.TriangleList,
quad.Vertices, 0, 4,
quad.Indices, 0, 2);
}

}

public override void Draw(GameTime gameTime)
{
foreach (Quad quad in _quads)
{
DrawQuad(quad);
}

base.Draw(gameTime);
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here

base.Update(gameTime);
}
}


i attach a file showing quads are full black
Thanks :D
Advertisement
Solved


with GraphicsDevice.BlendState = BlendState.AlphaBlend;

and the trick was to draw first the most far away quad and last the most near quad

This topic is closed to new replies.

Advertisement