Sign in to follow this  
sainimu78

Why the texture blurred ? I need it keeps the same resolution of the source image

Recommended Posts

sainimu78    121

The source image is 1920x1200 px. 

 

Here is my render funtion:

VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
    
        g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0x000000ff);
        g_pd3dDevice->SetTexture( 0, g_pTexture );

        // Render the vertex buffer contents
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
        g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2);//2 * 50 - 2 );

        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    // shows in two windows
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    HWND hWnd = WindowFromDC(g_hDc);
    RECT rc; rc.bottom = 600, rc.left = 100, rc.right = 600, rc.top = 100;
    //RECT rc; rc.bottom = in->height, rc.left = 0, rc.right = in->width, rc.top = 0;
    g_pd3dDevice->Present( NULL, &rc, hWnd, NULL );
}

What will be seen shows in the attach file

 

Why does the image the program displays look so blurred? How to set the texture to look better. The source image is 1920x1200 px !

Edited by sainimu78

Share this post


Link to post
Share on other sites
kauna    2922

The image seems more like pixelated than blurred. How is your texture sampler set for drawing?

 

Cheers!

Share this post


Link to post
Share on other sites
tonemgub    2008

Your texture looks pixelated to me rather than blurred. If you want it to look smooth, you should use a texture filtering mode other than point-sampling.

For your simple example, try linear sampling first, just to see the difference:

 

g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

 

You could also use mipmapping to get even better results...

Share this post


Link to post
Share on other sites
kauna    2922

Well, it depends greatly on your needs ... since you can have any size of window, you can't expect the image to look the same in all the cases. Maybe you should add the sampler states as hinted earlier and tell us after it if the image looks the way you want it to look and enable mipmaps too if your haven't already.

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites
sainimu78    121

Well, it depends greatly on your needs ... since you can have any size of window, you can't expect the image to look the same in all the cases. Maybe you should add the sampler states as hinted earlier and tell us after it if the image looks the way you want it to look and enable mipmaps too if your haven't already.

 

Cheers!

 

I need a window displaying a image by using dx.

 

I added. 

these tow lines 

.........
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetTexture( 0, g_pTexture );
..........

and they make no effects.

The image displaying on the window is blurred or, say, pixelated as before.

 

 

 

 

I don't know how to enable mipmaps, could you?

Share this post


Link to post
Share on other sites
sainimu78    121

How do you load the texture?

My program is modified from the sample code "texture" in dxsdk_june10.

 

This sample code load a texture using CreateTextureFromFile().

I substituted that function with CreateTexture() that loads a texture from memory

Share this post


Link to post
Share on other sites
Hodgman    51341
Are you trying to display it at actual size (one texture-pixel = one screen-pixel), or stretch it over the whole window?

If the first, you need to very carefully place your vertices at the corners of the screen pixels, which means shifting the vertices up+left by half a screen pixel.

If the second, you have to blur it, because you're resizing...

Share this post


Link to post
Share on other sites
kauna    2922

Ok, I think I got it:

 

There are 2 windows and the same image is drawn in both of them :

 

- In the first window the image seems to be correct (it is stretched over the whole window, mipmapping / filtering seems to work as intended). 

- In the second window the back buffer is streched over a 500x500 region. Probably the back buffer isn't 500x500 pixels, so the image to be presented is stretched without any filtering. I'm not sure if there is a way to stretch with some filtering operation - anyhow, if the back buffer isn't the same size as the destination rectangle the image quality won't be the same.

 

Cheers!

 

[edit] to fix the problem, you'll need another swap chain for the other window, with the correct back buffer size 

[edit 2] or fix your RECT rc to match the back buffer size

Edited by kauna

Share this post


Link to post
Share on other sites
L. Spiro    25638

You are Present()’ing the image to 2 different windows, so texture filters and mipmaps are unrelated.

 

Present() is what is resizing your image.  It only resizes 1 way: with a point filter.

Therefor the only possible result you can get with your current method is the one you see (which is pixelated, not blurred).

 

 

You have to render to the 2nd window separately (and using a linear filter), ensuring that its back buffer stays the correct width and height for Present() not to resize.

This means maintaining 2 back buffers, performing 2 renders, and performing 2 Present()’s.

 

 

L. Spiro

Edited by L. Spiro

Share this post


Link to post
Share on other sites
sainimu78    121

Ok, I think I got it:

 

There are 2 windows and the same image is drawn in both of them :

 

- In the first window the image seems to be correct (it is stretched over the whole window, mipmapping / filtering seems to work as intended). 

- In the second window the back buffer is streched over a 500x500 region. Probably the back buffer isn't 500x500 pixels, so the image to be presented is stretched without any filtering. I'm not sure if there is a way to stretch with some filtering operation - anyhow, if the back buffer isn't the same size as the destination rectangle the image quality won't be the same.

 

Cheers!

 

[edit] to fix the problem, you'll need another swap chain for the other window, with the correct back buffer size 

[edit 2] or fix your RECT rc to match the back buffer size

 

I test it out

The buffer size is equal to the window size. 

But a window size is limited, commonly, in 1400x800

 

I tried pDevice->SetScissorRect(), It doesn't work.

 

How to set the buffer size?

Share this post


Link to post
Share on other sites
sainimu78    121

You are Present()’ing the image to 2 different windows, so texture filters and mipmaps are unrelated.

 

Present() is what is resizing your image.  It only resizes 1 way: with a point filter.

Therefor the only possible result you can get with your current method is the one you see (which is pixelated, not blurred).

 

 

You have to render to the 2nd window separately (and using a linear filter), ensuring that its back buffer stays the correct width and height for Present() not to resize.

This means maintaining 2 back buffers, performing 2 renders, and performing 2 Present()’s.

 

 

L. Spiro

 

 

How to ensure that?

 

I have tried :

    in = cvLoadImage("24bpp_1920x1200_1.bmp", 1);
    rc.left = 0; rc.top =0; rc.bottom = in->height; rc.right = in->width;
    cvReleaseImage(&in);
    g_pd3dDevice->SetScissorRect(&rc);
    g_pd3dDevice->GetScissorRect(&rc);
    IDirect3DSurface9* pSurf = NULL;
    IDirect3DSurface9* pBB = NULL;
    IDirect3DSwapChain9* pSwapChain = NULL;
    g_pd3dDevice->GetSwapChain(0, &pSwapChain);
    pSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf);
    g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBB);
 
    hr = g_pd3dDevice->StretchRect(pSurf, NULL, pBB, &rc, D3DTEXF_LINEAR);

Both methods totally make no effects.

There are still pixelated 

Share this post


Link to post
Share on other sites
kauna    2922

When you draw to your 400x300 window, is your back buffer size 400x300?

 

- if you use Present to scale (RECT of a different size than the backbuffer) the backbuffer when flipping it will get pixelated (regardless of the resolution of the image)

 

Cheers!

Share this post


Link to post
Share on other sites
sainimu78    121

When you draw to your 400x300 window, is your back buffer size 400x300?

 

- if you use Present to scale (RECT of a different size than the backbuffer) the backbuffer when flipping it will get pixelated (regardless of the resolution of the image)

 

Cheers!

 

 

That it is.

 

How to allocate a back buffer in specified size matters now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this