PDA

View Full Version : DirectX DrawPrimitive real image scale



lordzero
17-09-2007, 02:57 PM
Hello

how can I set the real scale of a image for my sprite?
using a single value for scale i can change the width and height of it, but i want the "real scale".

i'm using drawPrimitive to draw it.

my code is here:

http://www.lordzero.co.nr/tmp/RealScale.zip

Greets

chronozphere
17-09-2007, 05:49 PM
I see you transforming the vertices (scaling them) before the are put into the vertexbuffer.
This is not the most efficient way of dealing with transformations.
Matrices are a lot more efficient. ;) Take a look at this:

http://msdn2.microsoft.com/en-us/library/bb206269.aspx

Matrices describe linear transformations (translations, rotations, scale and some other things).
I think you need to study them first... they are not as easy as XY coords.

You can set world, view and projection matrices. In this case, you probably want to set the world matrix. You can set the world matrix as follows:


D3DDevice.SetTransform( D3DTS_WORLD, MyWorldMatrix )


This is probably what you want:


//Scale is a single, the scale factor
//Scalemat is a TD3Dmatrix

D3DXmatrixScaling( ScaleMat, Scale, Scale , 1);

D2DDevice.SetTransform( D3DTS_WORLD, Scalemat );


If you want to use multiple transformations (like Scaling and Translation) you need to multiply the matrices with D3DXMatrixMultiply.

I dont know what you mean with 'real scale', but i think you want to pass the width and height to the render function instead of the scale factor. If so, you need to retrieve the texture size (Check IDirect3dTexture9.GetSurfaceDesc ). You can draw your sprite as a 1x1 rectangle and let a matrix scale, translate and rotate it as you wish.

Hope this helps. :)

lordzero
17-09-2007, 05:58 PM
I see you transforming the vertices (scaling them) before the are put into the vertexbuffer.
This is not the most efficient way of dealing with transformations.
Matrices are a lot more efficient. ;) Take a look at this:

http://msdn2.microsoft.com/en-us/library/bb206269.aspx

Matrices describe linear transformations (translations, rotations, scale and some other things).
I think you need to study them first... they are not as easy as XY coords.

You can set world, view and projection matrices. In this case, you probably want to set the world matrix. You can set the world matrix as follows:


D3DDevice.SetTransform( D3DTS_WORLD, MyWorldMatrix )


This is probably what you want:


//Scale is a single, the scale factor
//Scalemat is a TD3Dmatrix

D3DXmatrixScaling( ScaleMat, Scale, Scale , 1);

D2DDevice.SetTransform( D3DTS_WORLD, Scalemat );


If you want to use multiple transformations (like Scaling and Translation) you need to multiply the matrices with D3DXMatrixMultiply.

Hope this helps. :)

i believe that your code only work with orthogonal camera, because i'm using a vertex with D3DFVF_XYZRHW. with this i think that D3DX transformations dont will work... :(

lordzero
19-09-2007, 04:20 AM
somebody have suggestions?

NecroDOME
21-09-2007, 03:40 PM
Just a question off-topic, but I see much often that previous posts are quoted. Why quote when you just answer? No offense, but please don't.

On-topic: D3DFVF_XYZRHW are screenspace coordinates. If you just try to scale a quad, I think you need to calculate the vertex buffer for yourself. You could consider rendering full 3D. This is done in one of my projects: Uber Zombie. This way it's also very easy to scroll/rotate the camera without recalculating everything.

Uber Zombie: http://necrodome.homeftp.net/Project.nss?project=Game_Uber%20Zombie

lordzero
21-09-2007, 04:15 PM
its already solved... thanks for the attention :P

NecroDOME
21-09-2007, 04:49 PM
what you do to solve?

lordzero
21-09-2007, 07:42 PM
what you do to solve?

Nathan from your country give me this source:



procedure TMainForm.FormCreate(Sender: TObject);
const X = 0;
Y = 0;
Width = 100;
height = 100; //X, Y and width and height of your sprite in pixels
var R: TRect;
_X,_Y,_Width,_Height: single; //X,Y,Width,Height passed to D3D
begin

if initd3d<>d3d_ok then showmessage&#40;'Error'&#41;;

DXBase.Direct3DDevice.SetRenderState&#40;D3DRS_CULLMOD E, D3DCULL_NONE&#41;;
DXBase.Direct3DDevice.SetRenderState&#40;D3DRS_LIGHTIN G, iFalse&#41;;

Windows.GetClientRect&#40;MainForm.Handle,R&#41;;

if FAILED&#40;D3DXCreateTextureFromFile&#40;
DXBase.Direct3DDevice,
'image.jpg',
pTexture&#41;
&#41; then
begin
MessageBox&#40;0, 'failed', 'error', MB_OK&#41;;
Exit;
end;

_X &#58;= &#40;X-&#40;R.Right/2&#41;&#41;;
_Y &#58;= -&#40;Y-&#40;R.Bottom/2&#41;&#41;;

//Crop coordinates into a 2x2 space &#40;the total D3D window&#41;
_X &#58;= &#40;_X * &#40;2/R.Right&#41;&#41;;
_Y &#58;= &#40;_Y * &#40;2/R.Bottom&#41;&#41;;

//calculate width and height
_width &#58;= Width * &#40;2/R.Right&#41;;
_height &#58;= -height * &#40;2/R.Bottom&#41;; //flip Y axis

Vertices&#91;0&#93;.X &#58;= _X;
Vertices&#91;0&#93;.Y &#58;= _Y;
Vertices&#91;0&#93;.Z &#58;= 0;
Vertices&#91;0&#93;.Color &#58;= $FFFFFFFF;
Vertices&#91;0&#93;.tU &#58;= 0;
Vertices&#91;0&#93;.tV &#58;= 0;

Vertices&#91;1&#93;.X &#58;= _X + _Width;
Vertices&#91;1&#93;.Y &#58;= _Y;
Vertices&#91;1&#93;.Z &#58;= 0;
Vertices&#91;1&#93;.Color &#58;= $FFFFFFFF;
Vertices&#91;1&#93;.tU &#58;= 1;
Vertices&#91;1&#93;.tV &#58;= 0;

Vertices&#91;2&#93;.X &#58;= _X;
Vertices&#91;2&#93;.Y &#58;= _Y + _Height;
Vertices&#91;2&#93;.Z &#58;= 0;
Vertices&#91;2&#93;.Color &#58;= $FFFFFFFF;
Vertices&#91;2&#93;.tU &#58;= 0;
Vertices&#91;2&#93;.tV &#58;= 1;

Vertices&#91;3&#93;.X &#58;= _X + _Width;
Vertices&#91;3&#93;.Y &#58;= _Y + _Height;
Vertices&#91;3&#93;.Z &#58;= 0;
Vertices&#91;3&#93;.Color &#58;= $FFFFFFFF;
Vertices&#91;3&#93;.tU &#58;= 1;
Vertices&#91;3&#93;.tV &#58;= 1;

if FAILED&#40;
DXBase.Direct3DDevice.CreateVertexBuffer&#40;
4 * SizeOf&#40;TCustomVertex&#41;,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, pVB, nil&#41;&#41; then
Exit;

if FAILED&#40;
pVB.Lock&#40;0, SizeOf&#40;Vertices&#41;, pVertices, 0&#41;&#41; then
Exit;

CopyMemory&#40;pVertices, @Vertices, SizeOf&#40;Vertices&#41;&#41;;
pVB.Unlock;

NecroDOME
21-09-2007, 08:54 PM
So your rendering "full 3D" :P , no more XYZRHW?

lordzero
21-09-2007, 09:51 PM
So your rendering "full 3D" :P , no more XYZRHW?

i'm using XYZRHW