D3DXSPRITE problem - wrongly scaled?
#1 Members - Reputation: 100
Posted 15 March 2012 - 01:54 AM
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 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)
// initially viewport was set to width/height of client area
// clear device
m_d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 );
// begin scene
m_d3dDevice->BeginScene();
// render movie surface (just two triangles to which the movie is rendered)
m_d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
m_d3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); // bilinear filtering
m_d3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); // bilinear filtering
m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); //Ignored
m_d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_d3dDevice->SetTexture( 0, m_movieTexture );
m_d3dDevice->SetStreamSource(0, m_displayPlaneVertexBuffer, 0, sizeof(Vertex));
m_d3dDevice->SetFVF(Vertex::FVF_Flags);
m_d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// render sprites
m_sprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE | D3DXSPRITE_DO_NOT_ADDREF_TEXTURE);
// text drop shadow
m_font->DrawText( m_playerSprite, m_currentMessage.c_str(), m_currentMessage.size(),
&m_playerFontRectDropShadow, DT_RIGHT|DT_TOP|DT_NOCLIP, m_playerFontColorDropShadow );
// text
m_font->DrawText( m_playerSprite, m_currentMessage.c_str(), m_currentMessage.size(),
&m_playerFontRect, DT_RIGHT|DT_TOP|DT_NOCLIP, m_playerFontColorMessage ) );
// control object
m_sprite->Draw( m_texture, 0, 0, &m_vecPos, 0xFFFFFFFF ); // draws a few objects like this
m_sprite->End()
// end scene
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
#4 Moderators - Reputation: 1918
Posted 19 March 2012 - 09:07 AM
Steve Macpherson
Senior programmer, Firebrand Games
#5 Members - Reputation: 100
Posted 19 March 2012 - 09:59 AM
thanks for your reply. I was loading already with
D3DXCreateTextureFromFileEx, but I didn't set the
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.
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.
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.
Hmm, I guess I must still be doing something stupid somewhere...
#7 Members - Reputation: 100
Posted 19 March 2012 - 08:04 PM
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 "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
#8 Moderators - Reputation: 1918
Posted 20 March 2012 - 10:22 AM
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.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 "Mipmapped textures automatically have each level filled with the loaded texture. " ? Even though I only specified one level...
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.
Steve Macpherson
Senior programmer, Firebrand Games






