Skip to main content
gamedev.net
🔒 Locked

viewport

Started by Ninj4D4n Sep 27, 2002 at 2:42 PM 11 replies 1.9k views
Original Post
Ninj4D4n
Ninj4D4n
Hi all, Ok, here''s my delimia (granted i''m very very very new at this). I''m trying to make an incredibly simple window, that is just a square the size of the viewport that has different colors at different corners. Now, my window initializes at 400x400 (haven''t figured out how to change that yet). So if i specify my vertices as: CUSTOMVERTEX cvVertices[] = { {0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color {400.0, 0.0f, 0.5f, 1.0f, 0xffff0000, }, {400.0, 400.0, 0.5f, 1.0f, 0xff00ff00, }, {0.0f, 400.0, 0.5f, 1.0f, 0xff00ffff, }, }; which gives me what i want (diff color corners), sort of. If i resize the window, i have a 400x400 square of what i want, and the rest of the window is garbage (flicker). Now when i try it this way: D3DVIEWPORT8 vp; m_pd3dDevice->GetViewport(&vp); CUSTOMVERTEX cvVertices[] = { {0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000 }, // x, y, z, rhw, color {(float)vp.Width, 0.0f, 0.5f, 1.0f, 0xffff0000 }, {(float)vp.Width, (float)vp.Height, 0.5f, 1.0f, 0xff00ff00 }, {0.0f, (float)vp.Height, 0.5f, 1.0f, 0xff00ffff }, }; I get a square that allways fills my viewport... expcept the square is only the color of the first vertex. So.... what i''m i doing wrong?? (a.k.a F%^$ing up in my state of ignorance??) Thanks, love these forums --Ninj4D4n
Ninj4D4n
Ninj4D4n
Ok, So i''ve been working on this problem for the last 24 hours. Good news is, i learned how to change the dimensions of the window, and get rid of the garbage flicker. The bad news is, i still don''t get a square that fits to my viewport. From what i can tell, the values (float)vp.Width and (float)vp.Height are not 400 and 300 respectively, but instead both are set to the lowest negative integer allowed on my sysem (-8 million something). This in essence is causing all the vertices that use either command to be un predictable, and just stretching away from the viewport to an undertimed location. So, i''m still really confused on how to do this properly, i''ve looked throug every D3D sample in the SDK and can''t find anything usful, so i''d really appreciate any help possible.

--Ninj4D4n
IFooBar
IFooBar
quote:

I get a square that allways fills my viewport... expcept the square is only the color of the first vertex.



I *think* it may be because your square is bigger then the viewport so only part of it gets drawn.

think of a viewport as exactly the same thing as teh front buffer just its smaller. if you draw a 10x10 square in the normal window, and the window is 20x20, then half your windo will be filled up.

Now if you make a viewport of 2x2, and THEN draw the 10x10 square the viewport will _still_ be half fulled. because directX will change the size of your square to match the ration of the normal window to teh square

so your square will become a 1x1 only when drawn inside a 2x2 viewport

If you draw teh 10x10 square in a 5x5 viewport then the square will be 2.5x2.5 inside the viewport but 'normal' (10x10) with out the viewport





Al
"May Trixus (the goddess of programming) guide you "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]


[edited by - alfmga on September 28, 2002 12:06:20 PM]
[size=2]aliak.net
Ninj4D4n
Ninj4D4n
alfmga,
what your saying makes sense.... but sadely it isn''t working like that.

With the code:
CUSTOMVERTEX cvVertices[] =
{
{0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{400.0, 0.0f, 0.5f, 1.0f, 0xffff0000, },
{400.0, 400.0, 0.5f, 1.0f, 0xff00ff00, },
{0.0f, 400.0, 0.5f, 1.0f, 0xff00ffff, },
};

i have a square that is 400x400, in a 400x400 window (full window) if i make the window 800x800, my square only exists as 400x400 leaving the rest of the window blank. Same thing, if i cut my window down to 200x200, then i only see a section of my square.

Like i said... i''m really confused. Because every other program i look at, the points automatically reajust themselves when you change the window size.

--Ninj4D4n
IFooBar
IFooBar
quote:

Like i said... i''m really confused. Because every other program i look at, the points automatically reajust themselves when you change the window size.



thats true.

hmm.

BAH!!! (why dont u just use fullscreen)

Seriously though. I dont think ive ever seen someting like this. (unless im misundersttanding u for some reson). could u post teh code your using in your ''rendering'' area?



Al
"May Trixus (the goddess of programming) guide you "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]
[size=2]aliak.net
Ninj4D4n
Ninj4D4n
here''s the important parts

class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices
DWORD m_dwSizeofVertices;
LPDIRECT3DTEXTURE8 m_pBackgroundTexture;
HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );

protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
HRESULT FrameMove();
HRESULT FinalCleanup();

public:
CMyD3DApplication();
};

CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Basic2");
m_dwCreationWidth = 400;
m_dwCreationHeight = 300;
m_bUseDepthBuffer = TRUE;
m_pVB = NULL;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
}

HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();
// Set the transform matrices
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 2.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIX matWorld, matView, matProj;
D3DXMatrixIdentity( &matWorld );
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt,
&vUpVec );
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)
m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect,
1.0f, 500.0f );

m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// fill the vertex buffer with the new data
D3DVIEWPORT8 vp;
m_pd3dDevice->GetViewport(&vp);
// Initialize four vertices for rendering two triangles
CUSTOMVERTEX cvVertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, },
{ 400.0, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, },
{ 400.0, 300.0, 0.5f, 1.0f, 1.0f, 1.0f, },
{ 0.0f, 300.0, 0.5f, 1.0f, 0.0f, 1.0f},
};
m_dwSizeofVertices = sizeof(cvVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer(
m_dwSizeofVertices, 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;

VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)
&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, cvVertices, m_dwSizeofVertices);
m_pVB->Unlock();

return S_OK;
}

HRESULT CMyD3DApplication::Render()
{
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|
D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
m_pd3dDevice->SetTextureStageState( 0,
D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0,
D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof
(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARG(255,255,255,0),
m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0),
m_strDeviceStats );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}


Again, thanks for the help

bmoberly
bmoberly
Hey -

I can''t find your custom vertex definition in the above, but based on comment, "// x, y, z, rhw, color", it looks like you are using RHW.

With RHW your vertex coordinates = screen coordinates, so a square defined as 0,0 - 400,400 will always be 400 pixels x 400 pixels no matter what the size of your viewport is.

One not-so-elegant solution would be to reset your vertices to equal the size of the newly-resized window. For example, if you reset the size of your window to 800 X 800, then your would set the upper limits of your square to reflect this. The problem with this solution is that you''re using a vertex buffer, so you would have to modify the contents of the buffer at every screen resize. Still, I don''t imagine you''d be resizing the screen that often...

One other thing to look into (but I''m not sure this will work with RHW) is to see if you could apply a scaling transformation to your vertex buffer before you render it. I''m not sure that this will work with RHW, though.

Yet a third thing to look into would be to use a another custom vertex format. There are difficulties here as well, though, as your world coordinates will no longer equal screen coordinates. Here though, if you resize the window, your vertices should scale accordingly.

Maybe one of the more experienced forum users could give you more help on the above.

Good luck-

bm
Ninj4D4n
Ninj4D4n
bmoberly,

in your not too elegent fix, the one where i would reset the vertices of the box to reflect the new window size.... how do i get the new window size??

--Ninj4D4n
IFooBar
IFooBar
ok...now u got me confused (trust me that is easily accomplished)

i thought you were trying to use a viewport, is this correct? but i dont see any SetViewport(...) function anywhere in ur code.

furthermore
as for ur first post i noticed something


  
D3DVIEWPORT8 vp;
m_pd3dDevice->GetViewport(&vp);

CUSTOMVERTEX cvVertices[] =
{
{0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000 }, // x, y, z, rhw, color

{(float)vp.Width, 0.0f, 0.5f, 1.0f, 0xffff0000 },
{(float)vp.Width, (float)vp.Height, 0.5f, 1.0f, 0xff00ff00 },
{0.0f, (float)vp.Height, 0.5f, 1.0f, 0xff00ffff },
};


now lets analyze...your window is 400x400 so whey u call GetViewport(&vp), vp.Width is == 400 and vp.Height == 400 so in actuality ur cvVertices is

  
{
{0.0f, 0.0f, 0.5f, 1.0f, 0xffff0000 }, // x, y, z, rhw, color

{400, 0.0f, 0.5f, 1.0f, 0xffff0000 },
{400, 400, 0.5f, 1.0f, 0xff00ff00 },
{0.0f, 400, 0.5f, 1.0f, 0xff00ffff },
};


now the above is a pretty freaky square if u ask me.

i suggest u do GetViewport(&oldVP) at the start of your program, so that you can set teh viewport to it''s original whenever you want, and then give a call to SetViewport(....)
and _then_ draw your square...lemme know what happens though



Al
"May Trixus (the goddess of programming) guide you "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]
[size=2]aliak.net
Ninj4D4n
Ninj4D4n
Ok, maybe i''m missing something, but nothing seems to fix this. How do i put a scroll box in a forum post?? I''ll post my entire code so that someone can tell me exactly what to fix/add/change/delete... what ever.

--Ninj4D4n
Ninj4D4n
Ninj4D4n
I''d just like to say thanks for all the help so far. Here''s my code. If you could tell me why this textured square doesn''t fit in my view port (it is severly!!! strecthed (all one color)) i''d greatly appreciate it.


  
//-----------------------------------------------------------------------------

// File: basic.cpp

//

// Desc: D3D sample showing the basics of DirectX Graphics Programming

//

// Copyright (c) 1998-2000 Microsoft Corporation. All rights reserved.

// Copyright (c) 1998-2001 wolf@direct3d.net

//-----------------------------------------------------------------------------

#define STRICT
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"


// A structure for our custom vertex type

struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex

FLOAT tu, tv; // texture coordinates

};

// Our custom FVF, which describes our custom vertex structure

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)


//-----------------------------------------------------------------------------

// Name: class CMyD3DApplication

// Desc: Application class. The base class (CD3DApplication) provides the

// generic functionality needed in all Direct3D samples. CMyD3DApplication

// adds functionality specific to this sample program.

//-----------------------------------------------------------------------------

class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices

DWORD m_dwSizeofVertices;

LPDIRECT3DTEXTURE8 m_pBackgroundTexture;

HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );

protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
HRESULT FrameMove();
HRESULT FinalCleanup();

public:
CMyD3DApplication();
};

//-----------------------------------------------------------------------------

// Name: WinMain()

// Desc: Entry point to the program. Initializes everything, and goes into a

// message-processing loop. Idle time is used to render the scene.

//-----------------------------------------------------------------------------

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;

if( FAILED( d3dApp.Create( hInst ) ) )
return 0;

return d3dApp.Run();
}


//-----------------------------------------------------------------------------

// Name: CMyD3DApplication()

// Desc: Application constructor. Sets attributes for the app.

//-----------------------------------------------------------------------------

CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Basic2");
m_dwCreationWidth = 400;
m_dwCreationHeight = 300;
m_bUseDepthBuffer = TRUE;

m_pVB = NULL;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
}

//-----------------------------------------------------------------------------

// Name: OneTimeSceneInit()

// Desc: Called during initial app startup, this function performs all the

// permanent initialization.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::OneTimeSceneInit()
{
return S_OK;
}


//-----------------------------------------------------------------------------

// Name: FrameMove()

// Desc: Called once per frame, the call is the entry point for animating

// the scene.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::FrameMove()
{
return S_OK;
}


//-----------------------------------------------------------------------------

// Name: Render()

// Desc: Called once per frame, the call is the entry point for 3d

// rendering. This function sets up render states, clears the

// viewport, and renders the scene.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::Render()
{
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
// Begin the scene

if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );

m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

// Output statistics

m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene.

m_pd3dDevice->EndScene();
}

return S_OK;
}

//-----------------------------------------------------------------------------

// Name: InitDeviceObjects()

// Desc: Initialize scene objects.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );

// Load the texture for the background image

if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),
&m_pBackgroundTexture) ) )
return D3DAPPERR_MEDIANOTFOUND;

return S_OK;
}


//-----------------------------------------------------------------------------

// Name: RestoreDeviceObjects()

// Desc: Initialize scene objects.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();

// Set the transform matrices

D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 2.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIX matWorld, matView, matProj;

D3DXMatrixIdentity( &matWorld );
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );

m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );


// fill the vertex buffer with the new data

D3DVIEWPORT8 vp;
m_pd3dDevice->GetViewport(&vp);

// Initialize four vertices for rendering two triangles

CUSTOMVERTEX cvVertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, }, // x, y, z, rhw, tu, tv

{ (float)vp.Width, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, },
{ (float)vp.Width, (float)vp.Height, 0.5f, 1.0f, 1.0f, 1.0f, },
{ 0.0f, (float)vp.Height, 0.5f, 1.0f, 0.0f, 1.0f},
};

m_dwSizeofVertices = sizeof(cvVertices);

// Create the vertex buffer

if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;

VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, cvVertices, m_dwSizeofVertices);
m_pVB->Unlock();

return S_OK;
}

//-----------------------------------------------------------------------------

// Name: InvalidateDeviceObjects()

// Desc: Called when the app is exiting, or the device is being changed,

// this function deletes any device dependent objects.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
SAFE_RELEASE( m_pVB );
return S_OK;
}

//-----------------------------------------------------------------------------

// Name: DeleteDeviceObjects()

// Desc: Called when the app is exiting, or the device is being changed,

// this function deletes any device dependent objects.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
SAFE_RELEASE( m_pBackgroundTexture );
return S_OK;
}


//-----------------------------------------------------------------------------

// Name: FinalCleanup()

// Desc: Called before the app exits, this function gives the app the chance

// to cleanup after itself.

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::FinalCleanup()
{
return S_OK;
}

//-----------------------------------------------------------------------------

// Name: ConfirmDevice()

// Desc: Called during device intialization, this code checks the device

// for some minimum set of capabilities

//-----------------------------------------------------------------------------

HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}



as you can see, i got this code from a book cd... that''s why i''m so confused that it doesn''t work right

--Ninj4D4n
bmoberly
bmoberly
Hey - it looks like your "restoredeviceobjects" routine should be resizing the square to fill the viewport, but does your app call this routine when the window is resized? I can''t tell from your code, but maybe I''m missing something. If this routine is only getting called once, and not when the screen resizes, then that would explain why your square isn''t scaling.

bm
IFooBar
IFooBar
ok i know why ur getting part of the image.

you use vp.Width and vp.Height, now these two values are DWORDS and can get messy when converted to float the C way. anyway it turns out that the actual values of vp.Height and vp.Width are like -86473846 and -834986323 (or something to that effect) which is bad. so if you could just replace vp.Width with m_dwCreationWidth and vp.Height with m_dwCreationWidth. The you''ll get the whole image.

but when you resize the image wont resize itself for reasons mentioned by ''bmoberly''. and to get the new window sizes you''ll have to catch the WM_EXITSIZEMOVE in your message handler, then to calculate the new widths of the windows you''d have to do the following


  
//m_cltRect are of type RECT

GetClientRect(m_hwnd, &m_cltRect);

//Store the new width and height values with a small calculation

m_dwCreationWidth = m_cltRect.right - m_cltRect.Left;
m_dwCreationHeight = m_cltRect.bottom - m_cltRect.Top;



In the RECT structure Left is the upper left x coordinate of the client area (the part where the app is being drawn in), Right is the lower right x coordinat, Top is the upper left y coordinate and Bottom is the lower right y coordinate
so something like this


  
(left, top)
---------------
| |
| |
| |
| |
| |
---------------
(right, bottom)






Al
"May Trixus (the goddess of programming) guide you "
[Triple Buffer]|[SCRIPTaGAME]|[My Old Site]
[size=2]aliak.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.