XNA Quadrender question

Started by
3 comments, last by BCullis 11 years, 7 months ago
Hi, I have some basic questions about quadrender and how things are drawn on the screen, go easy on me, I'm new to XNA . Have a look at the below class:


internal sealed class QuadRender
{
private VertexPositionTexture[] verts;
private GraphicsDevice myDevice;
private short[] ib = null;
///
/// Loads the quad.
///
///
The engine.
public QuadRender(GraphicsDevice device)
{
myDevice = device;
verts = new VertexPositionTexture[]
{
new VertexPositionTexture(
new Vector3(0,0,0),
new Vector2(1,1)),
new VertexPositionTexture(
new Vector3(0,0,0),
new Vector2(0,1)),
new VertexPositionTexture(
new Vector3(0,0,0),
new Vector2(0,0)),
new VertexPositionTexture(
new Vector3(0,0,0),
new Vector2(1,0))
};
ib = new short[] { 0, 1, 2, 2, 3, 0 };
}
///
/// Draws the fullscreen quad.
///
///
The effect.
public void RenderFullScreenQuad(Effect effect)
{
effect.CurrentTechnique.Passes[0].Apply();
RenderQuad(Vector2.One * -1, Vector2.One);
}
public void RenderQuad(Vector2 v1, Vector2 v2)
{
verts[0].Position.X = v2.X;
verts[0].Position.Y = v1.Y;
verts[1].Position.X = v1.X;
verts[1].Position.Y = v1.Y;
verts[2].Position.X = v1.X;
verts[2].Position.Y = v2.Y;
verts[3].Position.X = v2.X;
verts[3].Position.Y = v2.Y;
myDevice.DrawUserIndexedPrimitives
(PrimitiveType.TriangleList, verts, 0, 4, ib, 0, 2);
}
}


What I'm confused about is the co-ordinate system, what I mean is why does (0,0) represent top left and (1,1) represent bottom right? For example if I have a 800x600 screen, bottom right would be (800, 600), so why is (1,1) used instead. Also why are all the vertices initialized with a Vector3(0,0,0). Whilst I'm here, whats the significance of the other methods present in the class and the ib array?
Advertisement
what your are displaying with the quad is a texture that comes from a texture2d variable. Normally as a render target or such.

What you are giving are the texture coordinates that go from 0,0 to 1,1. It will match the texture image to a quad that fits all screen.

what your are displaying with the quad is a texture that comes from a texture2d variable. Normally as a render target or such.

What you are giving are the texture coordinates that go from 0,0 to 1,1. It will match the texture image to a quad that fits all screen.


I'm more or less asking for an explanation for the class, I haven't used it yet.
What I'm confused about is the co-ordinate system, what I mean is why does (0,0) represent top left and (1,1) represent bottom right? For example if I have a 800x600 screen, bottom right would be (800, 600), so why is (1,1) used instead. Also why are all the vertices initialized with a Vector3(0,0,0). Whilst I'm here, whats the significance of the other methods present in the class and the ib array?


The Vector2 structures of (0,0) and (1,1) actually refer to the texture coordinates, and the Vector3 structures refer the coordinate positions. The VertexPositionTexture structure consists of Vector3 for the position and a Vector2 for the texture, in addition to a VertexDeclaration class that defines the vertex info.

The constructor for this QuadRender class sets all the vertices at the origin as a default, so if it were not for the parameters passed in Draw() to reposition the vertices, it would be as if there was no quad visible at all.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

What you failed to mention is where this came from: PloobsEngine. (For reference if anyone else wants to help further)
Do note as well that they have a link to their own tutorials on the front page of that project, if you need extra resources.


I'm more or less asking for an explanation for the class, I haven't used it yet.

Since it's already been coded for you, all you really need to know about it is how it's used: drawing a quad to the screen (useful for deferred rendering techniques, as well as sprite/font drawing, though since XNA already has classes for that I'll assume its intent is the former). If you're trying to learn from the codebase, here's some more info:

The ib array is an index array (ib probably referring to "index buffer"). That's a necessary piece of information for the DrawIndexedPrimitive and DrawUserIndexedPrimitive calls, as it tells the DirectX pipeline which vertices in the vertex buffer correspond to which triangles, and lets you re-use vertices (as otherwise this draw call would need 6 verts to comprise the 2 triangles). The ib array itself { 0, 1, 2, 2, 3, 0 } says the first tri will use verts from the vertex array at indices 0, 1, and 2; and the second will use verts at indices 2, 3, and 0.

(0,0) to (1,1) is uv coordinate space (for texture coordinates) which is always normalized across any-sized texture, so (.5, .5) would always refer to the point right at the middle of the image you're sampling.

RenderFullScreenQuad calls the more arbitrary RenderQuad with specific vector values that make it span the full viewport space (which has normalized coordinates from (-1, -1) to (1,1) being the top left and bottom right corners respectively).

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement