Jump to content
  • Advertisement
Sign in to follow this  
falki02

DirectX9 resolution , scaling , rendering

This topic is 3733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm pretty new in the DirectX world and i've started by learning the DirectX9 basics. From now i would like to load 4 pictures on the back buffer, to present them on a window, without scaling them, and then be able to view each of them by moving the rendered picture with the mouse. The pictures have all the same size (1600x1200) and if a picture is bigger than the screen it should simply be cutted off. Therefore i've modified an example code i've found on the web (see below). Problem:
  • allthout i set up the SetSamplerState to D3DTEXF_LINEAR to avoid the "play station effect" the resolution of the rendered picture isn't as good as the original one.
  • it seems that the GPU ist loading the pictures continously.
Questions:
  • am i using the transformation matrices wrong?
  • is there in DirectX any other way to load a picture instead of using D3DXCreateTextureFromFile..?
  • how do i make sure that the GPU loads the picture just once?
  • why is the picture not occupying the whole space on the window?
additional info: you can try this picture (size 1200x1600) to understand what i mean. thank's for ur help THE CODE
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "resource.h"

//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND              g_hWnd       = NULL;
LPDIRECT3D9       g_pD3D       = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pSquare1_VB     = NULL;
LPDIRECT3DTEXTURE9      g_pTexture1      = NULL;

#define D3DFVF_MY_VERTEX ( D3DFVF_XYZ | D3DFVF_TEX1 )

struct Vertex
{
	float x, y, z;	// Vertex coordinates in 3D space.
    float u, v;		// Texture coordinates.
};


Vertex g_squareVertices1[] =
{
	{-1.0f, 1.0f, 0.0f,  0.0f,0.0f },
	{ 1.0f, 1.0f, 0.0f,  1.0f,0.0f },
	{ 1.0f,-1.0f, 0.0f,  1.0f,1.0f},
	{-1.0f,-1.0f, 0.0f,  0.0f,1.0f  }
};



//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void loadAllTextures( void );
void init(void);
void shutDown(void);
void render(void);

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR     lpCmdLine,
					int       nCmdShow )
{
	WNDCLASSEX winClass; 
	MSG        uMsg;

    memset(&uMsg,0,sizeof(uMsg));
    
	winClass.lpszClassName = "MY_WINDOWS_CLASS";
	winClass.cbSize        = sizeof(WNDCLASSEX);
	winClass.style         = CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc   = WindowProc;
	winClass.hInstance     = hInstance;
	winClass.hIcon	       = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
    winClass.hIconSm	   = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
	winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClass.lpszMenuName  = NULL;
	winClass.cbClsExtra    = 0;
	winClass.cbWndExtra    = 0;

	if( !RegisterClassEx(&winClass) )
		return E_FAIL;

	g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS", 
                             "Direct3D (DX9) - My Tiles",
						     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
					         0, 0, 640, 480, NULL, NULL, hInstance, NULL );

	if( g_hWnd == NULL )
		return E_FAIL;

    ShowWindow( g_hWnd, nCmdShow );
    UpdateWindow( g_hWnd );

	init();

	while( uMsg.message != WM_QUIT )
	{
		if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
		{ 
			TranslateMessage( &uMsg );
			DispatchMessage( &uMsg );
		}
        else
		    render();
	}

	shutDown();

    UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );

	return uMsg.wParam;
}

//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND   hWnd, 
							 UINT   msg, 
							 WPARAM wParam, 
							 LPARAM lParam )
{
    switch( msg )
	{	
        case WM_KEYDOWN:
		{
			switch( wParam )
			{
				case VK_ESCAPE:
					PostQuitMessage(0);
					break;
			}
		}
        break;

		case WM_CLOSE:
		{
			PostQuitMessage(0);	
		}
		
        case WM_DESTROY:
		{
            PostQuitMessage(0);
		}
        break;

		default:
		{
			return DefWindowProc( hWnd, msg, wParam, lParam );
		}
		break;
	}

	return 0;
}

//-----------------------------------------------------------------------------
// Name: init()
// Desc: Initializes Direct3D under DirectX 9.0
//-----------------------------------------------------------------------------
void init( void )
{
    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

    D3DDISPLAYMODE d3ddm;
	float	fAspect;
	
    g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );

    d3dpp.Windowed               = TRUE;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat       = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

    g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                          &d3dpp, &g_pd3dDevice );


	loadAllTextures();

	Vertex *pVertices = NULL;

	//
	// Square 1
	//
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), D3DUSAGE_WRITEONLY , D3DFVF_MY_VERTEX,
									  D3DPOOL_MANAGED, &g_pSquare1_VB, NULL );
	pVertices = NULL;
	g_pSquare1_VB->Lock( 0, sizeof(g_squareVertices1), (void**)&pVertices, 0 );
    memcpy( pVertices, g_squareVertices1, sizeof(g_squareVertices1) );
    g_pSquare1_VB->Unlock();

	fAspect = (float) d3ddm.Width / (float) d3ddm.Height;

	D3DXMATRIX mProjection;
	D3DXMatrixPerspectiveFovLH( &mProjection, D3DX_PI/4 , fAspect, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &mProjection );
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE , D3DCULL_CCW); //CULLING - cutt off invisible triangles.
}

//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc: Release all Direct3D resources.
//-----------------------------------------------------------------------------
void shutDown( void )
{
	if( g_pSquare1_VB != NULL ) 
        g_pSquare1_VB->Release(); 

	if (g_pTexture1 != NULL)
		g_pTexture1->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}

//-----------------------------------------------------------------------------
// Name: render()
// Desc: Render or draw our scene to the monitor.
//-----------------------------------------------------------------------------
void render( void )
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

	D3DXMATRIX mWorld;
	D3DXMatrixTranslation( &mWorld, 0.0f, 0.0f, 2.0f );
	g_pd3dDevice->SetTransform( D3DTS_WORLD, &mWorld );

	g_pd3dDevice->BeginScene();

	g_pd3dDevice->SetTexture(0 , g_pTexture1);
	g_pd3dDevice->SetStreamSource( 0, g_pSquare1_VB, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_MY_VERTEX );
	//g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP  , 0, 4);
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 4);


	g_pd3dDevice->EndScene();
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


void loadAllTextures( void )
{
	D3DXCreateTextureFromFile( g_pd3dDevice, "DSCN0322.jpg", &g_pTexture1 );
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	/*
	D3DXCreateTextureFromFile( g_pd3dDevice, "image2.bmp", &g_pTexture2 );
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

	D3DXCreateTextureFromFile( g_pd3dDevice, "image3.bmp", &g_pTexture3 );
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

	D3DXCreateTextureFromFile( g_pd3dDevice, "image4.bmp", &g_pTexture4 );
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	*/
}

resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by d3d_matrices.rc
//
#define IDC_MYICON                      2
#define IDD_DIRECTX_DIALOG              102
#define IDD_ABOUTBOX                    103
#define IDS_APP_TITLE                   103
#define IDM_ABOUT                       104
#define IDM_EXIT                        105
#define IDS_HELLO                       106
#define IDI_DIRECTX_ICON                107
#define IDC_DIRECTX_MENU                108
#define IDC_DIRECTX_ACCELERATOR         109
#define IDC_DIRECTX                     110
#define IDR_MAINFRAME                   128
#define IDC_STATIC                      -1

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        129
#define _APS_NEXT_COMMAND_VALUE         32771
#define _APS_NEXT_CONTROL_VALUE         1000
#define _APS_NEXT_SYMED_VALUE           110
#endif
#endif

Share this post


Link to post
Share on other sites
Advertisement
Quote:
the resolution of the rendered picture isn't as good as the original one.

If you display a texture at less than it's original resolution, it won't be "as good." You'd be asking for one pixel to look as good as two or more pixels.
Quote:
it seems that the GPU ist loading the pictures continously.

You got me! :-)
Do you mean "the GPU is loading.." or "the GPU isn't loading.."?
And, either way, why do you think that?

General advice: check the return codes from your DirectX calls in case there's an error (a device doesn't get created, or a texture doesn't load, etc.).

Share this post


Link to post
Share on other sites
Quote:
Original post by falki02
am i using the transformation matrices wrong?


No, you're fine as long as you want your camera to be positioned at <0,0,0> and facing forward. If you want to move it or make it look a different direction, you'll have to create and apply a view matrix.

Quote:
Original post by falki02
is there in DirectX any other way to load a picture instead of using D3DXCreateTextureFromFile..?


You can create the texture yourself using IDirect3DDevice9::CreateTexture and then fill in the image data yourself, but there's no point in doing so unless you're trying to do something crazy. For 99% of cases D3DXCreateTextureFromFileEx should suffice.

Quote:
Original post by falki02
how do i make sure that the GPU loads the picture just once?


You will only load the texture when you call D3DXCreateTextureFromFile, which means in your program its only happening once when your init function calls loadAllTextures. Nothing fancy needed. Why is it that you think the textures are being loaded more than once?

Quote:
Original post by falki02
why is the picture not occupying the whole space on the window?


You're using a perspective projection. This means that as things move further away from the camera (in your case <0,0,0>), they get smaller. This is like how your eyes or real camera see things. If you don't want this effect to occur, you should use an orthographic projection. Make the width and height equal to the screen size, make your image vertices the same size, and draw your quad so it's positioned at x = 0, y = 0 (Z won't matter as long as it's within the projection's near-clip and far-clip values).

Share this post


Link to post
Share on other sites
well first thx 4 ur help.
the reason why i thought the picture is been continously loaded is becoz the following happened: by moving the window around the screen (keepin some other windows opened), the rendered picture got wiped off at some places. i can see some peaces of the picture. Just after i released the mouse it took some few seconds till the entire picture got render again. u got me? what's responsible for that?

Share this post


Link to post
Share on other sites
It sounds like you're invalidating the device when you move the window and the device needs to be reset. It may be your app is destroying everything and initializing from scratch with each move.

If you've posted your entire application, you should take a look at some of the SDK examples to see how they detect a lost device or a device that needs to be reset. In particular, the window procedure needs to process WM_SIZE messages.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!