D3DXSPRITE problem - wrongly scaled?

Started by
7 comments, last by thebjoern 12 years, 1 month ago
Hi,

I am using D3D9 for rendering some simple things, a movie, as the backmost layer, then on top of that some text messages, and now wanted to add some buttons to that.

Before adding the buttons everything seemed to have worked fine, and I was using a [color=#2A2A2A]D3DXSPRITE for the text as well (D3DXFONT), now I am loading some graphics for the buttons, but they seem to be scaled to something like 1.2 of its original size (in my test window I centered the graphic, but it being too big it just doesnt fit well, for example the client area is 640x360, the graphic is 440, so i expect 100 pixel on left and right, left side is fine [I took screenshot and "counted" the pixels in photoshop], but on the right there is only about 20 pixels)

My rendering code is very simple (I am omitting error checks etc for brevity)

[color=#0000cd]// initially viewport was set to width/height of client area

[color=#0000cd]// clear device
[color=#0000cd]m_d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 );

[color=#0000cd]// begin scene
[color=#0000cd]m_d3dDevice->BeginScene();

[color=#0000cd]// render movie surface (just two triangles to which the movie is rendered)

[color=#0000cd]m_d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
[color=#0000cd]m_d3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); // bilinear filtering
[color=#0000cd]m_d3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); // bilinear filtering
[color=#0000cd]m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
[color=#0000cd]m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); //Ignored
[color=#0000cd]m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
[color=#0000cd]m_d3dDevice->SetTexture( 0, m_movieTexture );
[color=#0000cd]m_d3dDevice->SetStreamSource(0, m_displayPlaneVertexBuffer, 0, sizeof(Vertex));
[color=#0000cd]m_d3dDevice->SetFVF(Vertex::FVF_Flags);
[color=#0000cd]m_d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

[color=#0000cd]// render sprites
[color=#0000cd]m_sprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE | D3DXSPRITE_DO_NOT_ADDREF_TEXTURE);

[color=#0000cd]// text drop shadow

[color=#0000cd]m_font->DrawText( m_playerSprite, m_currentMessage.c_str(), m_currentMessage.size(),
[color=#0000cd]&m_playerFontRectDropShadow, DT_RIGHT|DT_TOP|DT_NOCLIP, m_playerFontColorDropShadow );
[color=#0000cd]// text
[color=#0000cd]m_font->DrawText( m_playerSprite, m_currentMessage.c_str(), m_currentMessage.size(),
[color=#0000cd]&m_playerFontRect, DT_RIGHT|DT_TOP|DT_NOCLIP, m_playerFontColorMessage ) );

[color=#0000cd]// control object
[color=#0000cd]m_sprite->Draw( m_texture, 0, 0, &m_vecPos, 0xFFFFFFFF ); // draws a few objects like this

[color=#0000cd]m_sprite->End()


[color=#0000cd]// end scene
[color=#0000cd]m_d3dDevice->EndScene();


What did I forget to do here? Except for the control objects (play button, pause button etc which are placed on a "panel" which is about 440 pixels wide) everything seems fine, the objects are positioned where I expect them, but just too big. Btw I loaded the images using D3DXCreateTextureFromFileEx (resizing wnidow, and reacting to lost device etc works fine too).

For experimenting, I added some code to take an identity matrix and scale is down on the x/y axis to 0.75f, which then gave me the expected result for the controls (but also made the text smaller and out of position), but I don't know why I would need to scale anything. My rendering code is so simple, I just wanted to draw my 2D objects 1;1 the size they came from the file...

I am really very inexperienced in D3D, so the answer might be very simple...

thanks
Bjoern
Advertisement
I just wanted to illustrate the size problem with a screenshot where you can see the file displayed in 100% and behind that how it is actually rendered
Anyone got an idea? I still can't make sense of it... :-/
How do you load the texture? If you're loading it with D3DXCreateTextureFromFile, then it's getting resized to be a power of two in size. You can either create it as a power of two in width and height, or use D3DXCreateTextureFromFileEx and pass D3DX_DEFAULT_NONPOW2 for the width and height parameters.
Hi Steve,
thanks for your reply. I was loading already with [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

D3DXCreateTextureFromFileEx, but I didn't set the

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

D3DX_DEFAULT_NONPOW2 flag. However I just changed my code for testing, and am checking for the size of the texture (which as you said is rounded to the next power of 2), and I am storing the size of the original image in the file - for example the buttons are 29x29 and therefore end up 32x32, which makes sense). So when I am rendering it now, I am using that rectangle (ie 0,0,29,29) as the source rectangle, however I still got the scaling problem.

[/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

The difference compared to before is that now the textures fill the size of the screen as expected, but because they are still scaled they are now also cut off. Please see the screenshot.

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

So somewhere there must still be some scaling happening. But I don't know where, because I am not setting any view matrices or anything.

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Hmm, I guess I must still be doing something stupid somewhere...

[/font]

Hi Steve,
thanks for your reply. I was loading already with

D3DXCreateTextureFromFileEx, but I didn't set the

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

D3DX_DEFAULT_NONPOW2 flag. However I just changed my code for testing, and am checking for the size of the texture (which as you said is rounded to the next power of 2), and I am storing the size of the original image in the file - for example the buttons are 29x29 and therefore end up 32x32, which makes sense). So when I am rendering it now, I am using that rectangle (ie 0,0,29,29) as the source rectangle, however I still got the scaling problem.

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

The difference compared to before is that now the textures fill the size of the screen as expected, but because they are still scaled they are now also cut off. Please see the screenshot. [/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

So somewhere there must still be some scaling happening. But I don't know where, because I am not setting any view matrices or anything.[/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Hmm, I guess I must still be doing something stupid somewhere...[/font]

Here is the above mentioned screenshot, now with the source rectangle set to the size of the original image file, it just clips the parts that have been scaled up
Ok, finally I understand, thanks again Steve!

It seems that D3DXCreateTextureFromFileEx not only loads the texture and fits it into an area the size of the next higher power of two (in my case my 424x69 texture would be 512x128), but it actually stretches it too. My original expectation was that it puts the 424x69 image into the 512x128 texture, and the excess area would just be either zeroed out or random uninitialized values, and when I draw my sprite and pass in the image size 424x69 it would just copy that section of the original image to the screen. But seeing that it stretches the texture to fill the 512x128 texture, then it also makes sense that it cuts it off. So the D3DX_DEFAULT_NONPOW2 setting actually fixed this.

Is that what it means in the MSDN documentation by "[color=#000000]Mipmapped textures automatically have each level filled with the loaded texture. " ? Even though I only specified one level...

Sorry if this thread seems kinda silly and newbie-ish... but I am a newbie in this ;-) Anyway, it works now, so I can move on :-)

cheers,
Bjoern

Ok, finally I understand, thanks again Steve!

It seems that D3DXCreateTextureFromFileEx not only loads the texture and fits it into an area the size of the next higher power of two (in my case my 424x69 texture would be 512x128), but it actually stretches it too. My original expectation was that it puts the 424x69 image into the 512x128 texture, and the excess area would just be either zeroed out or random uninitialized values, and when I draw my sprite and pass in the image size 424x69 it would just copy that section of the original image to the screen. But seeing that it stretches the texture to fill the 512x128 texture, then it also makes sense that it cuts it off. So the D3DX_DEFAULT_NONPOW2 setting actually fixed this.

Is that what it means in the MSDN documentation by "[color=#000000]Mipmapped textures automatically have each level filled with the loaded texture. " ? Even though I only specified one level...
Ah yes sorry, I should have been clearer. D3DX will resize the texture according to the filter passed in the Filter parameter. You can disable the resizing by using D3DX_FILTER_NONE instead.
That particular line from the docs means that if you load a 400x200 BMP file, and specify 0 for the MipLevels parameter, then D3DX will create all the mip levels (400x200, 200x100, 100x50, 50x25, 25x12, 12x6, 6x3, 3x1, 1x1) and fill them in with resized versions of the BMP file, using the filter specified in the MipFilter patameter.

If you're really picky about compatability, it's worth noting that not all graphics cards support non-power-of-2 textures, although just about every card you'll come across does. Still, it might be worth noting if you're targetting older hardware (GeForce 2 and below for instance). The "CardCaps.pdf" file in the DirectX SDK will list what cards have the D3DPTEXTURECAPS_POW2 and/or D3DPTEXTURECAPS_NONPOW2CONDITIONAL cap bit set.
Ok, got it, thanks again! Yeah, was really wondering which part made it resize it... that it was the filter wasn't that intuitive for me.... thanks!

This topic is closed to new replies.

Advertisement