RE: Display 2D image using textured quad

From: Mark Davis [MSFT] (markda_at_online.microsoft.com)
Date: 06/26/04


Date: Sat, 26 Jun 2004 03:33:11 GMT

Hi Terry ...

Yes, you do need to call Begin() and End() on each sprite object (within
the Begin/EndScene of your device). The reason your code works when those
lines are uncommented is that, internally, GetTransform calls Begin(),
which sets a number of default renderstates - see the documentation for
ID3DXSprite::Begin() for details of the renderstates.

Mark Davis (MSFT)

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included information and code is subject to the terms specified at 
http://www.microsoft.com/info/cpyright.htm.
--------------------
>Thread-Topic: Display 2D image using textured quad
>thread-index: AcRaD58yKb0VtnRaS8WO33D0NlVF1w==
>X-WBNR-Posting-Host: 209.167.167.158
>From: "=?Utf-8?B?VGVycnk=?=" <Terry@discussions.microsoft.com>
>References:  <921723B5-DBE9-4084-B885-DB38B0647844@microsoft.com>
>Subject: RE: Display 2D image using textured quad
>Date: Thu, 24 Jun 2004 10:21:01 -0700
>Lines: 79
>Message-ID: <6B744119-697D-4A71-939A-BBAE6F7E9BA0@microsoft.com>
>MIME-Version: 1.0
>Content-Type: text/plain;
>	charset="Utf-8"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Content-Class: urn:content-classes:message
>Importance: normal
>Priority: normal
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
>Newsgroups: microsoft.public.win32.programmer.directx.graphics
>NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 127.0.0.1
>Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
>Xref: cpmsftngxa10.phx.gbl 
microsoft.public.win32.programmer.directx.graphics:17626
>X-Tomcat-NG: microsoft.public.win32.programmer.directx.graphics
>
>Typo in the original post:
>
>The first line of code to uncomment to get it to work should have been:
>//hr = g_sprite->Begin(D3DXSPRITE_DONOTSAVESTATE);
>NOT
>//hr = g_sprite->GetTransform(&transform);
>
>Any help appreciated,
>
>Terry
>
>
>"Terry" wrote:
>
>> I am new to DirectX and am trying to use a textured quad for display of 
a simple 2D image.  I want the image to show up in the top left corner of 
my window.  I can NOT get the following code to display anything ... 
however, if I simply include the calls for starting and ending the drawing 
of a sprite, it works just fine.  I assume this means that there are some 
calls to setup the RenderState or transform or etc, being done internally 
by the sprite calls that I am not doing.
>> 
>> Here is the code:
>> 
>> // Initialize the basics.
>> g_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
>> 
>> D3DPRESENT_PARAMETERS d3dpp; 
>> ZeroMemory( &d3dpp, sizeof(d3dpp) );
>> d3dpp.Windowed = TRUE;
>> d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
>> d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
>> 
>> HRESULT hr = g_d3d9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 
g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_device );
>> 
>> hr = g_device->Clear( 0, NULL, D3DCLEAR_TARGET, 
D3DCOLOR_XRGB(0,255,255), 1.0f, 0 );
>> 
>> // Read in the image.
>> hr = D3DXGetImageInfoFromFile("c:\\tmp\\image.jpeg", &info);
>> hr = D3DXCreateTextureFromFile(g_device, "c:\\tmp\\image.jpeg", 
&g_texture);
>> 
>> // Setup the vertices
>> struct CUSTOMVERTEX
>> {
>>     float x, y, z, rhw; // The transformed position for the vertex.
>>     float tu, tv;
>> };
>> 
>> #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)
>> 
>> CUSTOMVERTEX vertices[] =
>> {
>>     { 0.0f,  0.0f, 0.5f, 1.0f, 0.0f, 0.0f}, // x, y, z, rhw, u, v
>>     { 0.0f, 512.0f, 0.5f, 1.0f, 0.0f, 1.0f},
>>     {  512.0f, 512.0f, 0.5f, 1.0f, 1.0f, 1.0f },
>>     {  512.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f},
>> };
>> 
>> hr = g_device->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX), 0, 
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_VB, NULL);
>> 
>> VOID* pVertices;
>> hr = g_VB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 );
>> memcpy( pVertices, vertices, sizeof(vertices) );
>> g_VB->Unlock();
>> 
>> hr = g_device->BeginScene();
>> 
>> // Uncomment the following 2 lines to get it to work.
>> //hr = g_sprite->GetTransform(&transform);
>> //hr = g_sprite->End();
>> 
>> hr = g_device->SetRenderState( D3DRS_LIGHTING, FALSE );
>> hr = g_device->SetTexture( 0, g_texture );
>> hr = g_device->SetStreamSource( 0, g_VB, 0, sizeof(CUSTOMVERTEX) );
>> hr = g_device->SetFVF( D3DFVF_CUSTOMVERTEX );
>> hr = g_device->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
>> 
>> hr = g_device->EndScene();
>> hr = g_device->Present( NULL, NULL, NULL, NULL );
>> 
>> 
>> Thanks,
>> 
>> Terry
>> 
>


Relevant Pages

  • Re: LPD3DXSPRITE and display of a .tga with alpha channel ??
    ... > I am display a sprite with the texture loaded from a .tga file. ... > 32 bit file with alpha channel and a resolution of 700x525. ...
    (microsoft.public.win32.programmer.directx.graphics)
  • LPD3DXSPRITE and display of a .tga with alpha channel ??
    ... I am display a sprite with the texture loaded from a .tga file. ... 32 bit file with alpha channel and a resolution of 700x525. ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Genesis color counts, myths shattered?
    ... If that could be used in a game like Sonic 2, then it's more fair to say that the Genesis color limitation is more of a limit per sprite or background, rather than an absolute on screen limitation. ... Well, you have a maximum of 16 colors per pallet line, and four pallet lines for a total of 64 colors as the hardware "limit", so that's why you hear people saying that the Genesis can display 64 colors "max". ...
    (rec.games.video.sega)
  • Re: ID3DXSprite::Draw screen image doesnt match texture???
    ... If you're using D3DX for loading the texture, ... function that stretched the dimensions to powers of two. ... > I am displaying a sprite from a .tga file thats 32 bit with alpha channel ... My laptop display runs 1400x1050. ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: Sprites performance question
    ... >a power of two or a multiple of 4? ... A sprite is just a textured quad. ... "The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ: ...
    (microsoft.public.win32.programmer.directx.graphics)

Quantcast