I need to display an image at its actual size on window using D3d. How

Started by
4 comments, last by L. Spiro 9 years, 8 months ago

I have tried. I used the "Textures" example in dx sdk june10, but all my tryings of displaying look so pixelated.

Should I set the back buffer size to the image size? How to do this?

Or other methods should I take?


    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 6*2 * sizeof( CUSTOMVERTEX ),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer. We are setting the tu and tv texture
    // coordinates, which range from 0.0 to 1.0
    CUSTOMVERTEX* pVertices;
    if( FAILED( g_pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
        return E_FAIL;
 
    pVertices[0].position = D3DXVECTOR3( -1.0f, 1.0f, 0.0f);
    pVertices[0].color = 0xffffffff;
    pVertices[1].position = D3DXVECTOR3( -1.0f, -1.0f, 0.0f);
    pVertices[1].color = 0xffffffff;
    pVertices[2].position = D3DXVECTOR3( 1.0f, -1.0f, 0.0f);
    pVertices[2].color = 0xffffffff;
 
    pVertices[3].position = D3DXVECTOR3( -1.0f, 1.0f, 0.0f);
    pVertices[3].color = 0xffffffff;
    pVertices[4].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f);
    pVertices[4].color = 0xffffffff;
    pVertices[5].position = D3DXVECTOR3( 1.0f, -1.0f, 0.0f);
    pVertices[5].color = 0xffffffff;
 
    pVertices[0].tu = 0.0f;
    pVertices[0].tv = 0.0f;
    pVertices[1].tu = 0.0f;
    pVertices[1].tv = 1.0f;
    pVertices[2].tu = 1.0f;
    pVertices[2].tv = 1.0f;  
 
    pVertices[3].tu = 0.0f;
    pVertices[3].tv = 0.0f;
    pVertices[4].tu = 1.0f;
    pVertices[4].tv = 0.0f;
    pVertices[5].tu = 1.0f;
    pVertices[5].tv = 1.0f;

    g_pVB->Unlock();

.........
cvNamedWindow("nihao");
cvResizeWindow(400, 300);
hWnd = (HWND)cvGetWindowHandle("nihao");
.........
in=cvLoadImage("24bpp_1920*1200_1.bmp", 1);
RECT rc;
rc.left = 0,
rc.top = 0;
rc.right = in->width;//1920
rc.bottom = in->height;//1200

g_pd3dDevice->Present( NULL, &rc, hWnd, NULL );

I create a small size window of 400x300px on purpose that I had tested it out that the more pixelated the displaying , the smaller the window is.

post-222996-0-27683700-1408437066.jpg

Advertisement

The way you are rendering right now is that you have vertices that cover the entire viewport, and those vertices map to the entire texture. That means that you will map the texture to the full size of the window, which is probably not what you want. When you load the texture, you can query it for its dimensions, and then ensure that your window conforms to that aspect ratio. As long as you have the same aspect ratio, then the image will not look squished in one direction or the other. Outside of that, you should be able to make your window have the same size (taking into account the client area size) as your image, and then you will get 1-1 mapping.

The way you are rendering right now is that you have vertices that cover the entire viewport, and those vertices map to the entire texture. That means that you will map the texture to the full size of the window, which is probably not what you want. When you load the texture, you can query it for its dimensions, and then ensure that your window conforms to that aspect ratio. As long as you have the same aspect ratio, then the image will not look squished in one direction or the other. Outside of that, you should be able to make your window have the same size (taking into account the client area size) as your image, and then you will get 1-1 mapping.

I got it, but I think having the same size window is hard. Commonly, a window can be created is limited in 1400x800px.

So, I want to query a back buffer whose size is as the same as my source image. Actually, I tried that, but failed, I can't find anymore tutorials or articles about how to query back buffer in specified size.

Your back-buffer should always be the same size as whatever window into which you are rendering.

When you load a texture you can get its size. Place the vertices based on this size. For example, if the image is 800×600:

[0,0]------[800,0]

|\ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \ |

| \|

[0,600]----[800,600]

You could also place the vertices at [-400,-300]…[400,300], etc.

Use an orthographic projection matrix for rendering.

D3DXMatrixOrthoLH() or D3DXMatrixOrthoOffCenterLH().

Never Present() with a specified rectangle. I already told you that it causes scaling and pixelation.

Change:

g_pd3dDevice->Present( NULL, &rc, hWnd, NULL );

to:

g_pd3dDevice->Present( NULL, NULL, hWnd, NULL );

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

 

Your back-buffer should always be the same size as whatever window into which you are rendering.
 
When you load a texture you can get its size.  Place the vertices based on this size.  For example, if the image is 800×600:
[font='courier new'][0,0]------[800,0][/font]
[font='courier new']  |\           |[/font]
[font='courier new']  | \          |[/font]
[font='courier new']  |  \         |[/font]
[font='courier new']  |   \        |[/font]
[font='courier new']  |    \       |[/font]
[font='courier new']  |     \      |[/font]
[font='courier new']  |      \     |[/font]
[font='courier new']  |       \    |[/font]
[font='courier new']  |        \   |[/font]
[font='courier new']  |         \  |[/font]
[font='courier new']  |          \ |[/font]
[font='courier new']  |           \|[/font]
[font='courier new'][0,600]----[800,600][/font]
You could also place the vertices at [font='courier new'][-400,-300]…[400,300][/font], etc.
 
Use an orthographic projection matrix for rendering.
D3DXMatrixOrthoLH() or D3DXMatrixOrthoOffCenterLH().
 
 
Never [font='courier new']Present()[/font] with a specified rectangle.  I already told you that it causes scaling and pixelation.
Change:
[font='courier new']g_pd3dDevice->Present( NULL, &rc, hWnd, NULL );[/font]
to:
[font='courier new']g_pd3dDevice->Present( NULL, NULL, hWnd, NULL );[/font]
 
 
L. Spiro

 

Oh, God, I'm making a GUI that will rendering images captured by an industrial camera in very high resolution.
High resolution means a general size window can't fit that image size.

If the image zoom out to fit the window, any details in the image can't be seen.
If the image display on window with intact size, the users can drag some scrolls to see whole image. This is correct solution.

My GUI shows in the attach file.
Only part area of the whole need to rendering, so I use a RECT to Present().

pVertices[0].position = D3DXVECTOR3( -in->width/2, in->height/2, 0.0f);
pVertices[0].color = 0xffffffff;
pVertices[1].position = D3DXVECTOR3( -in->width/2, -in->height/2, 0.0f);
pVertices[1].color = 0xffffffff;
pVertices[2].position = D3DXVECTOR3( in->width/2, -in->height/2, 0.0f);
pVertices[2].color = 0xffffffff;

pVertices[3].position = D3DXVECTOR3( -in->width/2, in->height/2, 0.0f);
pVertices[3].color = 0xffffffff;
pVertices[4].position = D3DXVECTOR3( in->width/2, in->height/2, 0.0f);
pVertices[4].color = 0xffffffff;
pVertices[5].position = D3DXVECTOR3( in->width/2, -in->height/2, 0.0f);
pVertices[5].color = 0xffffffff;

pVertices[0].tu = 0.0f;
pVertices[0].tv = 0.0f;
pVertices[1].tu = 0.0f;
pVertices[1].tv = in->height;
pVertices[2].tu = in->width;
pVertices[2].tv = in->height;

pVertices[3].tu = 0.0f;
pVertices[3].tv = 0.0f;
pVertices[4].tu = in->width;
pVertices[4].tv = 0.0f;
pVertices[5].tu = in->width;
pVertices[5].tv = in->height;

g_pVB->Unlock();

---------------------------------------

D3DXMATRIXA16 matProj;
D3DXMatrixOrthoLH(&matProj, 2, 2, 0, 1);
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );


how can the window show the whole image. no zooming, no filterring,

Oh, God

Just “L. Spiro” is fine.


If the image zoom out to fit the window, any details in the image can't be seen.
If the image display on window with intact size, the users can drag some scrolls to see whole image. This is correct solution.

Then your solution is not in Direct3D, it is in Windows/Win32 API.

how can the window show the whole image. no zooming, no filterring,

Do everything I said before (back-buffer size must match the window’s dimensions, do not pass a RECT to Present(), etc.)

Except your target HWND is not the window anymore, it is a child window (see OBJECT A below).


#1: Make your window as you would make any other window for any other application.
#2: Add a custom control with the full size of the image you want to display in it. This is “OBJECT A”.
#3: Add scroll bars to the control to contain it within the area you described as “My rendering area”. http://www.functionx.com/win32/controls/scrollbars.htm

Everything in Direct3D you are currently doing on the window’s HWND you need to do instead to OBJECT A’s HWND.
That means the back buffers, presenting, creating the device, everything. Direct3D no longer has any knowledge of your window at all. Only OBJECT A.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement