XNA spritebatch and primative using different axis with same effect - xna

I am developing a side scroller game using monogame 3.0 (XNA-C#). I am using a TriangleStrip to draw the ground and a spritebatch to draw a fence. I want the fence to be on top of the ground but it seems to be using a different axis (negative instead of positive). In order to align things up I need to flip the fence and use a negative x and y. Both the sprite and the primitive are using the same view matrix and project.
Why is the spritebatch need a negative axis while the primitive is using a positive one when they are using the same matrixes?
protected override void Initialize()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, new Vector3(0,1, 0));
projectionMatrix = Matrix.CreateOrthographicOffCenter(0,GraphicsDevice.Viewport.Width,
0, GraphicsDevice.Viewport.Height, 0f, 200f) *Matrix.CreateScale(new Vector3(.5f, .5f, 1f));
GraphicsDevice.RasterizerState.CullMode = CullMode.None;
_groundBasicEffect = new BasicEffect(GraphicsDevice);
_groundBasicEffect.View = viewMatrix;
_groundBasicEffect.Projection = projectionMatrix;
_groundBasicEffect.VertexColorEnabled = true;
_groundBasicEffect.World = world;
_fenceBasicEffect = new BasicEffect(GraphicsDevice);
_fenceBasicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, new Vector3(0,1, 0));
_fenceBasicEffect.Projection = projectionMatrix;
_fenceBasicEffect.TextureEnabled = true;
_fenceBasicEffect.World = world;
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
fenceTexture = this.Content.Load<Texture2D>(#"Fence1");
vertexData = new VertexPositionColor[8];
vertexData[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Black);
vertexData[1] = new VertexPositionColor(new Vector3(0, 400, 0), Color.Black);
vertexData[2] = new VertexPositionColor(new Vector3(200, 0, 0), Color.Black);
vertexData[3] = new VertexPositionColor(new Vector3(200, 400, 0), Color.Black);
vertexData[4] = new VertexPositionColor(new Vector3(300, 0, 0), Color.Black);
vertexData[5] = new VertexPositionColor(new Vector3(300, 430, 0), Color.Black);
vertexData[6] = new VertexPositionColor(new Vector3(1200, 0, 0), Color.Black);
vertexData[7] = new VertexPositionColor(new Vector3(1200, 430, 0), Color.Black);
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertexData.Length, BufferUsage.None);
vertexBuffer.SetData(vertexData);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
_groundBasicEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertexData, 0, vertexData.Length -2);
_spriteBatch.Begin(0, null, null, null, null, _fenceBasicEffect);
_spriteBatch.Draw(fenceTexture, Vector2.Zero, new Rectangle(0, 0, 1130, 221), Color.White, 0, new Vector2(0, 200), 1, SpriteEffects.None, 0);
_spriteBatch.End();
base.Draw(gameTime);
}
How it looks...
How I expect it to look...
In order to get this look I need to change the Draw of the fence to this...
_spriteBatch.Draw(fenceTexture, Vector2.Zero, new Rectangle(0, 0, 1130, 221), Color.White, 0, new Vector2(0, -400), 1, SpriteEffects.FlipVertically, 0);

Related

Direct3D9 not drawing?

I needed to draw some simple shapes and I decided to go with D3D9. After going through a few of the tutorials on directxtutorial.com, I finally have all the code and knowledge I need to make my first shape appear on the screen. The problem is, though, that no image is appearing. I've looked over the code many times and have compared it with the code on the website, and it all checks out. Why is nothing rendering on the screen?
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 vbuffer;
struct CUSTOMVERTEX
{
float x, y, z, rhw;
DWORD color;
};
void InitD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.hDeviceWindow = hWnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
}
void InitGraphics()
{
CUSTOMVERTEX verticies[]
{
{50, 70, 1.0f, 1.0f, D3DCOLOR_XRGB(250, 0, 0),},
{70, 50, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 250, 0),},
{40, 80, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 250),},
};
d3ddev->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), NULL, CUSTOMFVF, D3DPOOL_MANAGED, &vbuffer, NULL);
void* vp;
vbuffer->Lock(0, 0, (void**)&vp, NULL);
memcpy(vp, verticies, sizeof(verticies));
vbuffer->Unlock();
}
void Draw()
{
d3ddev->Clear(NULL, NULL, D3DCLEAR_TARGET, NULL, 1.0f, NULL);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, vbuffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
void ReleaseD3D()
{
d3d->Release();
d3ddev->Release();
vbuffer->Release();
}
Turns out my vertex positions weren't triangle enough to make a triangle. Thanks for the help, though, it reminded me to set up error-checking.

What am I doing wrong in the following WebGL code?

I started from an example WebGL program that shows a single cube on the page. The example code does not use classes.
I want to be able to draw multiple cubes that can move independently. So I added a "Cube" class. Each instance of this class uses its own "program". I create two objects, but I draw only the first one. Unfortunately the later instantiated object is shown instead. E.g. in the code below "ground" is shown instead of "cube1".
Relevant parts of the code is below. Can you see any problem with it? How can I fix it?
...
////
class Cube {
constructor(gl, color) {
this.gl = gl;
this.program = initShaders(gl, "vertex-shader", "fragment-shader");
//// Model buffers and attributes
[this.pointsArray, this.colorsArray] = cubePointsAndColors(color);
this.numVertices = 36;
this.initAttributeBuffers();
//// Camera Related Uniforms Matrices
this.modelViewMatrixLoc = gl.getUniformLocation(
this.program,
"modelViewMatrix"
);
this.projectionMatrixLoc = gl.getUniformLocation(
this.program,
"projectionMatrix"
);
}
draw() {
this.gl.drawArrays(this.gl.TRIANGLES, 0, this.numVertices);
}
initAttributeBuffers() {
// arrange cube color data stuff
var cBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, cBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.colorsArray),
this.gl.STATIC_DRAW
);
var vColor = this.gl.getAttribLocation(this.program, "vColor");
this.gl.vertexAttribPointer(vColor, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vColor);
// arrange cube vertex data stuff
var vBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, vBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.pointsArray),
this.gl.STATIC_DRAW
);
var vPosition = this.gl.getAttribLocation(this.program, "vPosition");
this.gl.vertexAttribPointer(vPosition, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vPosition);
}
}
window.onload = function init() {
//// initialize WebGl System
const canvas = document.getElementById("gl-canvas");
const gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
gl.viewport(0, 0, canvas.width, canvas.height);
aspect = canvas.width / canvas.height;
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.enable(gl.DEPTH_TEST);
//// Initialize game objects
var cube1 = new Cube(gl, vec4(1.0, 0.0, 0.0, 1.0));
var ground = new Cube(gl, vec4(0.0, 1.0, 0.0, 1.0));
let gameObjects = [cube1];
// sliders for viewing parameters
readGUI();
render(gl, gameObjects);
};
////
var render = function(gl, gameObjects) {
//// clear the background
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//// camera settings
eye = vec3(
radius * Math.sin(theta) * Math.cos(phi),
radius * Math.sin(theta) * Math.sin(phi),
radius * Math.cos(theta)
);
modelViewMatrix = lookAt(eye, at, up);
projectionMatrix = perspective(fovy, aspect, near, far);
//// draw all objects
for (let objectI = 0; objectI < gameObjects.length; objectI++) {
const gameObject = gameObjects[objectI];
gl.useProgram(gameObject.program);
gl.uniformMatrix4fv(
gameObject.modelViewMatrixLoc,
false,
flatten(modelViewMatrix)
);
gl.uniformMatrix4fv(
gameObject.projectionMatrixLoc,
false,
flatten(projectionMatrix)
);
gameObject.draw();
}
requestAnimFrame(() => render(gl, gameObjects));
};
...
In WebGL 1.0 drawArrays, uses the vertices which are currently specified by vertexAttribPointer and enabled by enableVertexAttribArray.
Use properties to store the buffer objects (this.cBuffer, this.vBuffer) and attribute indices (this.vColor, this.vPosition):
initAttributeBuffers() {
// arrange cube color data stuff
this.cBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.cBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.colorsArray),
this.gl.STATIC_DRAW
);
this.vColor = this.gl.getAttribLocation(this.program, "vColor");
// arrange cube vertex data stuff
this.vBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.pointsArray),
this.gl.STATIC_DRAW
);
this.vPosition = this.gl.getAttribLocation(this.program, "vPosition");
}
Specify and enable the arrays of generic vertex attribute data right before the draw call:
draw() {
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.cBuffer);
this.gl.vertexAttribPointer(this.vColor, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(this.vColor);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vBuffer);
this.gl.vertexAttribPointer(this.vPosition, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(this.vPosition);
this.gl.drawArrays(this.gl.TRIANGLES, 0, this.numVertices);
}
In WebGL 2.0 (or by the use of the extension OES_vertex_array_object), that can be simplified by the use of WebGLVertexArrayObjects.
The vertex specification is stated in the vertex array object:
initAttributeBuffers() {
// create vertex array object
this.vao = this.gl.createVertexArray();
this.gl.bindVertexArray(this.vao);
// arrange cube color data stuff
var cBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, cBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.colorsArray),
this.gl.STATIC_DRAW
);
var vColor = this.gl.getAttribLocation(this.program, "vColor");
this.gl.vertexAttribPointer(vColor, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vColor);
// arrange cube vertex data stuff
var vBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, vBuffer);
this.gl.bufferData(
this.gl.ARRAY_BUFFER,
flatten(this.pointsArray),
this.gl.STATIC_DRAW
);
var vPosition = this.gl.getAttribLocation(this.program, "vPosition");
this.gl.vertexAttribPointer(vPosition, 4, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vPosition);
}
It is sufficient to bind the vertex array before the draw call:
draw() {
this.gl.bindVertexArray(this.vao);
this.gl.drawArrays(this.gl.TRIANGLES, 0, this.numVertices);
}

Flipping character

I would like to flip character when it walks left\right, I created a character from different body parts so flipping each of them caused this:
the reason of course was because it flipped the body parts in their own position, but not all the player together.
after that I had an idea and it was to draw the player to render target and flip the rendertarget when drawing, it worked (kind of), but when I walked when flipped it walked backwards and it also flipped the player position on the screen. here is the code:
if(mLeavusSprite.isflipped==0)
spriteBatch.Draw(character, rec,rec, Color.White,0,Vector2.Zero,SpriteEffects.None,0);
else
spriteBatch.Draw(character, rec, rec, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
character=render target that the player was drawn to.
is there anything I can do? flipping manually going to be a serious pain, I will need to move manually over 10 animations with 4+ frames each twice!
edit:
here is the code for drawing:
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8;
BodyPosition.Y = HeadPosition.Y + 32;
TopHandPosition.X = HeadPosition.X + 2;
TopHandPosition.Y = HeadPosition.Y + 36;
BackHandPosition.X = HeadPosition.X + 20;
BackHandPosition.Y = HeadPosition.Y + 36;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
Edit 2:
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8 ;
BodyPosition.Y = HeadPosition.Y + 32;
TopHandPosition.X = HeadPosition.X + 2 ;
TopHandPosition.Y = HeadPosition.Y + 36;
BackHandPosition.X = HeadPosition.X + 20 ;
BackHandPosition.Y = HeadPosition.Y + 36;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
int bigx=0;
int smallx=0;
float[] numbers = new[] { HeadPosition.X, BodyPosition.X , TopHandPosition.X, BackHandPosition.X};
float min = numbers.Min();
numbers = new[] { HeadPosition.X+HeadSource.Width, BodyPosition.X + BodySource.Width, TopHandPosition.X + TopHandSource.Width, BackHandPosition.X + BackHandSource.Width };
float max = numbers.Max();
float center = (max - min) / 2;
if (flip==1)
{
HeadPosition.X = Position.X;
BodyPosition.X = HeadPosition.X +center+ 8*flipOffset;
TopHandPosition.X = HeadPosition.X +center+ 2*flipOffset;
BackHandPosition.X = HeadPosition.X +center+ 20*flipOffset;
}
Debug.WriteLine("fff: " + center);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
hmm.. that is a tricky one.
There's probably a few different ways to do this and I think drawing they player to a render target is a reasonable approach if you can get it to work.
However, if you want a quick and dirty approach you might be able to simply flip the offsets of each part like this:
var flipOffset = FlipIs == SpriteEffects.FlipHorizontally ? -1 : 1;
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8 * flipOffset;
BodyPosition.Y = HeadPosition.Y + 32 * flipOffset;
TopHandPosition.X = HeadPosition.X + 2 * flipOffset;
TopHandPosition.Y = HeadPosition.Y + 36 * flipOffset;
BackHandPosition.X = HeadPosition.X + 20 * flipOffset;
BackHandPosition.Y = HeadPosition.Y + 36 * flipOffset;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource,Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
This is assuming that Position is right in the center of the sprite, if not you might need to do some tweaking.
Look, I'll be honest with you. There are better ways to approach this problem, but they'd require a significant redesign of the code and it's difficult to explain in one answer.
The first thing I would do to refactor the design is create a class to represent a single Sprite part to hold the common data points (offset, source rectangle, texture, color, etc) and setup data in one place. The animation code needs to be decoupled from the drawing code, etc.

Create input for cvContourArea in JavaCV

How to create the contour to pass to cvContourArea? I have four 2D points, but don't know to create the CvSeq.
int[] myPolygon = new int[] { point1.x, point1.y, point2.x, point2.y, ... };
cvCountourArea(???, null, 0);
I found the solution:
int[] myPolygon = new int[] { point1.x, point1.y, point2.x, point2.y, ... };
Mat rectMat = cvCreateMat(1, myPolygon.length/2, CV_32SC2);
rectMat.getIntBuffer().put(myPolygon);
cvCountourArea(rectMat, CV_WHOLE_SEQ, 0);
cvReleaseMat(rectMat);

scene became a mess when I drew mirror reflection(dx9)

In my project, I draw a house, which consists of wall, floor, mirror, a switch to turn on or turn off the fire system. Everything is going on well by here.
At last, I want to draw a person's reflection on the mirror, but after I add the drawReflection function, things go wrong, scene became a mess. Why? I write the function according to the book.
void House::draw(float timeDelta)
{
_device->SetFVF(d3d::Vertex::FVF);
_device->SetStreamSource(0, _vb, 0, sizeof(d3d::Vertex));
// draw wall
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _wall);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 10);
// draw floor
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _floor);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 30, 2);
// draw mirror
_device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _mirror);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 36, 2);
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
// draw switch button
D3DXMATRIX T, Ry, P;
D3DXMatrixTranslation(&T, -4.95f, 2.0f, 3.0f);
D3DXMatrixRotationY(&Ry, D3DX_PI * 1.5f);
P = Ry * T;
_device->SetTransform(D3DTS_WORLD, &P);
_device->SetMaterial(&d3d::BLUE_MTRL);
_device->SetTexture(0, NULL);
_switch->DrawSubset(0);
// draw fire system
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
_device->SetTransform(D3DTS_WORLD, &I);
if(_fSwitchOn)
{
if(_fs->isDead())
_fs->reset();
_fs->update(timeDelta);
_fs->render();
}
// draw reflection of person
//drawReflection();
}
But after I add drawReflection() function to it, it became a mess. Why?
drawReflection() function is below:
void House::drawReflection()
{
// enable stencil
_device->SetRenderState(D3DRS_STENCILENABLE, true);
_device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
_device->SetRenderState(D3DRS_STENCILREF, 0x1);
_device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
_device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
_device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
_device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
_device->SetRenderState(D3DRS_STENCILPASS,
D3DSTENCILOP_REPLACE);
// stop write to target buffer and depth buffer
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
_device->SetRenderState(D3DRS_ZWRITEENABLE, false);
// draw the mirror to stencil buffer
_device->SetFVF(d3d::Vertex::FVF);
_device->SetStreamSource(0, _vb, 0, sizeof(d3d::Vertex));
_device->SetMaterial(&d3d::WHITE_MTRL);
_device->SetTexture(0, _mirror);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
_device->SetTransform(D3DTS_WORLD, &I);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 36, 2);
_device->SetRenderState(D3DRS_ZWRITEENABLE, true);
// transform the mesh
_device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
_device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
D3DXMATRIX Reflect, S, T, P;
D3DXPLANE plane(0.0f, 0.0f, 1.0f, -5.0f);
D3DXMatrixReflect(&Reflect, &plane);
// D3DXMatrixScaling(&S, 0.2f, 0.2f, 0.2f);
D3DXVECTOR3 pos;
_person->getPosition(&pos);
D3DXMatrixTranslation(&T, pos.x, pos.y, pos.z);
P = T * Reflect;
_device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
_device->SetTransform(D3DTS_WORLD, &P);
_person->draw();
_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
_device->SetRenderState(D3DRS_STENCILENABLE, false);
_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
}

Resources