|
|
我自已写的D3D程序.什么都不做,只渲个窗口就只有60帧左右.
真不明白为什么~
[em3] [em3]
大家帮帮忙.
下面是代码.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <strsafe.h>
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPD3DXEFFECT g_pEffect = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
D3DXVECTOR4 g_LightDir = D3DXVECTOR4( 0.0f, 0.0f, -5.0f, 1.0f );
LPD3DXMESH g_pMesh = NULL;
ID3DXFont* g_pFpsFont = NULL;
HRESULT CreateObject();
void fun_RenderFps();
int g_FpsValue=0;
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
//创建HLSH
if( FAILED(D3DXCreateEffectFromFile( g_pd3dDevice, "hlsl.fx", NULL, NULL,
D3DXSHADER_DEBUG, NULL, &g_pEffect, NULL ) ) )
{
return E_FAIL;
}
if( g_pEffect != NULL ) g_pEffect->OnResetDevice();
//设定效果:因为只有一个,所以初始时就设了~
D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( "TShader" );
g_pEffect->SetTechnique( hTechnique );
D3DXCreateFont( g_pd3dDevice, // D3D device
15, // Height
0, // Width
FW_BOLD, // Weight
1, // MipLevels, 0 = autogen mipmaps
FALSE, // Italic
DEFAULT_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutputPrecision
DEFAULT_QUALITY, // Quality
DEFAULT_PITCH | FF_DONTCARE, // PitchAndFamily
"Arial", // pFaceName
&g_pFpsFont);
g_pFpsFont->OnResetDevice();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
if(FAILED(CreateObject()))
{
return E_FAIL;
}
//通过GetString取得字符串
const char* pstrBackTexture;
g_pEffect->GetString("TexFileName",&pstrBackTexture);
//创建并设置纹理
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, pstrBackTexture, &g_pTexture ) ) )
{
MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
return E_FAIL;
}
g_pEffect->SetTexture("Texture",g_pTexture);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pFpsFont != NULL )
g_pFpsFont->Release();
if( g_pTexture != NULL )
g_pTexture->Release();
if( g_pEffect != NULL )
g_pEffect->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// For our world matrix, we will just rotate the object about the y-axis.
D3DXMATRIXA16 matWorld;
UINT iTime = timeGetTime() % 3000;
FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
D3DXMatrixRotationY( &matWorld, fAngle );
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
//设定VS矩阵
D3DXMATRIX mWorldViewProj = matWorld * matView * matProj;
g_pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj );
g_pEffect->SetMatrix( "matWorld", &matWorld );
g_pEffect->SetVector("vecLightDir", &-g_LightDir);
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer to a black color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Setup the world, view, and projection matrices
// SetupMatrices();
//按VS,PS渲染
/* if( g_pEffect != NULL )
{
UINT nPasses=0;
UINT iPass=0;
if(SUCCEEDED(g_pEffect->Begin( &nPasses, 0 )))
{
for(iPass = 0; iPass < nPasses; iPass ++ )
{
g_pEffect->BeginPass( 0 );
}
g_pMesh->DrawSubset(0);
g_pEffect->EndPass();
g_pEffect->End();
}
}
*/
fun_RenderFps();
// End the scene
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice-> resent( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
{
switch(wParam)
{
case 'w':
case 'W':
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
break;
case 'S':
case 's':
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
break;
}
}
break;
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "HLSL学习12_1",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();
}
}
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
HRESULT CreateObject()
{
HRESULT hr;
LPD3DXMESH pMeshSysMem = NULL;
: 3DXCreateSphere(g_pd3dDevice,1.0f,40,40,&pMeshSysMem,NULL);
if(!pMeshSysMem)return E_FAIL;
D3DVERTEXELEMENT9 decl[]=
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 24, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 40, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
hr = pMeshSysMem->CloneMesh(D3DXMESH_MANAGED, decl, g_pd3dDevice, &g_pMesh);
if(pMeshSysMem)pMeshSysMem->Release();pMeshSysMem=NULL;
//为新的顶点中色彩数据填充值
LPDIRECT3DVERTEXBUFFER9 t_VB;
g_pMesh->GetVertexBuffer(&t_VB);
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
FLOAT nx, ny, nz; // The untransformed, 3D position for the vertex
FLOAT r, g, b, a; // The vertex color
FLOAT tu, tv;
};
//LOCK并改写数据
VOID* pVertices;
CUSTOMVERTEX *p_VBPtr;
if( FAILED( t_VB->Lock( 0, sizeof(CUSTOMVERTEX)*g_pMesh->GetNumVertices(), (void**)&pVertices, 0 ) ) )
return E_FAIL;
p_VBPtr=(CUSTOMVERTEX *)pVertices;
for(int i=0;i<g_pMesh->GetNumVertices();i++)
{
p_VBPtr.nx=p_VBPtr.x;
p_VBPtr.ny=p_VBPtr.y;
p_VBPtr.nz=p_VBPtr.z;
p_VBPtr.r=1.0f;
p_VBPtr.g=1.0f;
p_VBPtr.b=1.0f;
p_VBPtr.a=1.0f;
p_VBPtr.tu=(i%40+1)/40.00;
p_VBPtr.tv=(i/40+1)/40.00;
}
t_VB->Unlock();
// compute the normals
hr = D3DXComputeNormals(g_pMesh,NULL);
if(FAILED(hr))
return E_FAIL;
return S_OK;
}
void fun_RenderFps()
{
static int m_FrameSize=0;
static DWORD m_LastTime=::GetTickCount();
static char m_FpsText[50];
if(m_FrameSize==100)
{
DWORD m_EndTime=::GetTickCount();
g_FpsValue=(int)(1000.00/((m_EndTime-m_LastTime)/100.00));
m_FrameSize=0;
m_LastTime=::GetTickCount();
}
m_FrameSize++;
wsprintf(m_FpsText,"Fps:%d",g_FpsValue);
RECT t_Rect;
t_Rect.left=0;
t_Rect.top=0;
t_Rect.right=300;
t_Rect.bottom=20;
g_pFpsFont->DrawText( NULL,m_FpsText, -1, &t_Rect, DT_NOCLIP, D3DCOLOR_ARGB(255,255,0,0));
}
我发现Render函数有问题.我做了个计数器.
在主循环中计算时发现如果不调用Render,循环500次几乎感觉不到.但是加上Render就发现1秒也就能执行60多次.
而且跟我想渲的东西没关系.即使我不加入任何三角形.照样是60多次.
很怪的.
我以为我的帧运算有误.我做了很多种方法.也看了DXSDK里封装的程序中的定时器.也改了更精确的方式.比如用处理器频等.但结果依然如此.
好像帧数降下来,是由下面四个函数.造成的.怪呀~,那大家都怎么做的.我看着DXSDK那些例子的帧数那么高,真眼气!!!!!!!!!!!!!!!!!!!!!!!
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
g_pd3dDevice->BeginScene() ;
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
[em4] [em4] |
-
|