#include "qt4direct3dcontainer.h"
Qt4Direct3DContainer
::Qt4Direct3DContainer(QWidget *parent
){
m_pD3D9 = NULL;
m_pD3D9dev = NULL;
ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
m_pqTimer = NULL;
setAttribute(Qt::WA_PaintOnScreen, true);
Init();
}
Qt4Direct3DContainer::~Qt4Direct3DContainer()
{
Shut();
}
BOOL Qt4Direct3DContainer::Init()
{
Shut();
// Initialize Direct3D 9
m_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
// Test for Success
if(m_pD3D9 == NULL)
{
QMessageBox::information(this,
"Direct 3D Initialization",
"Unable to initialize Direct3D 9.");
return FALSE;
}
// Presentation Parameters
// Backbuffer Parameters
m_d3dPP.BackBufferWidth = width();
m_d3dPP.BackBufferHeight = height();
m_d3dPP.BackBufferFormat = D3DFMT_A8R8G8B8;
m_d3dPP.BackBufferCount = 1;
m_d3dPP.SwapEffect = D3DSWAPEFFECT_FLIP;
m_d3dPP.FullScreen_RefreshRateInHz = 0;
m_d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
// Multisampling(Antialiasing) Parameters
m_d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
m_d3dPP.MultiSampleQuality = 0;
// Window Parameters
m_d3dPP.hDeviceWindow = winId();
m_d3dPP.Windowed = TRUE;
// Depth/Stencil Buffer Parameters
m_d3dPP.EnableAutoDepthStencil = TRUE;
m_d3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
// misc. Parameters
m_d3dPP.Flags = 0;
// Initialize Direct3D 9 Device
if(!SUCCEEDED(m_pD3D9->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
winId(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&m_d3dPP,
&m_pD3D9dev)))
{
QMessageBox::information(this,
"Direct 3D Device Initialization",
"Unable to initialize Direct3D 9 Device.");
return FALSE;
}
//Vectors
m_vEye = D3DXVECTOR3(5.0f, 2.0f, 5.0f);
m_vAt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
//Matrices
m_fTurn = 0.0f;
m_bTurn = false;
D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
D3DXMatrixLookAtLH(&m_mtxView, &m_vEye, &m_vAt, &m_vUp);
D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 0.14f, width() / height(), 0.1f, 10.0f);
if(InitCubeObjects(m_pD3D9dev))
{
cube_effect->SetMatrix(cube_world, &m_mtxWorld);
cube_effect->SetMatrix(cube_view, &m_mtxView);
cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
cube_effect->CommitChanges();
}
//QTimer
connect(m_pqTimer, SIGNAL(timeout()), this, SLOT(repaint()));
m_pqTimer->start(10);
return TRUE;
}
void Qt4Direct3DContainer::turn()
{
m_bTurn = !m_bTurn;
}
void Qt4Direct3DContainer::Shut()
{
// delete QTimer
if(m_pqTimer != NULL && m_pqTimer->isActive())
{
m_pqTimer->stop();
}
delete m_pqTimer;
m_pqTimer = NULL;
ReleaseCubeObjects();
// Release Direct3D 9 Device
if(m_pD3D9dev != NULL)
{
m_pD3D9dev->Release();
}
m_pD3D9dev = NULL;
// Zero Direct3D 9 Presentation Parameters
ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
// Release Direct3D 9
if(m_pD3D9 != NULL)
{
m_pD3D9->Release();
}
m_pD3D9 = NULL;
// Matrices
ZeroMemory(&m_mtxWorld, sizeof(D3DXMATRIX));
ZeroMemory(&m_mtxView, sizeof(D3DXMATRIX));
ZeroMemory(&m_mtxProjection, sizeof(D3DXMATRIX));
// Vectors
ZeroMemory(&m_vEye, sizeof(D3DXVECTOR3));
ZeroMemory(&m_vAt, sizeof(D3DXVECTOR3));
ZeroMemory(&m_vUp, sizeof(D3DXVECTOR3));
}
{
return NULL;
}
void Qt4Direct3DContainer
::paintEvent(QPaintEvent *p_event
) {
m_pD3D9dev->Clear( 0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(64, 64, 96, 255),
1.0f,
0);
m_pD3D9dev->BeginScene();
m_pD3D9dev->SetVertexDeclaration(pDeclarationPosColorNormal);
m_pD3D9dev->SetStreamSource(0, cube_vertexBuffer, 0, sizeof(VertexPosColorNormal));
m_pD3D9dev->SetIndices(cube_indexBuffer);
//m_pD3D9dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
UINT uiNumberOfPasses,
uiPass;
if(m_bTurn)
{
m_fTurn += 0.01f;
D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
}
cube_effect->SetMatrix(cube_world, &m_mtxWorld);
cube_effect->SetMatrix(cube_view, &m_mtxView);
cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
cube_effect->CommitChanges();
cube_effect->Begin(&uiNumberOfPasses, 0);
for(uiPass = 0; uiPass < uiNumberOfPasses; uiPass++)
{
cube_effect->BeginPass(uiPass);
/*m_pD3D9dev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
cube_numberOfVertices,
0,
12);*/
m_pD3D9dev->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST,
0,
cube_numberOfVertices,
12,
cube_indices,
D3DFMT_INDEX32,
&cube_vertices,
sizeof(VertexPosColorNormal));
cube_effect->EndPass();
}
cube_effect->End();
m_pD3D9dev->EndScene();
m_pD3D9dev->Present( NULL,
NULL,
NULL,
NULL);
}
void Qt4Direct3DContainer
::resizeEvent(QResizeEvent *p_event
) {
QSize newSize
= p_event
->size
();
m_d3dPP.BackBufferWidth = newSize.rwidth();
m_d3dPP.BackBufferHeight = newSize.rheight();
D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 1.6f, (float)m_d3dPP.BackBufferWidth / (float)m_d3dPP.BackBufferHeight, 0.1f, 10.0f);
ReleaseCubeObjects();
m_pD3D9dev->Reset(&m_d3dPP);
InitCubeObjects(m_pD3D9dev);
}
#include "qt4direct3dcontainer.h"
Qt4Direct3DContainer::Qt4Direct3DContainer(QWidget *parent)
: QWidget(parent)
{
m_pD3D9 = NULL;
m_pD3D9dev = NULL;
ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
m_pqTimer = NULL;
setAttribute(Qt::WA_PaintOnScreen, true);
Init();
}
Qt4Direct3DContainer::~Qt4Direct3DContainer()
{
Shut();
}
BOOL Qt4Direct3DContainer::Init()
{
Shut();
// Initialize Direct3D 9
m_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
// Test for Success
if(m_pD3D9 == NULL)
{
QMessageBox::information(this, "Direct 3D Initialization",
"Unable to initialize Direct3D 9.");
return FALSE;
}
// Presentation Parameters
// Backbuffer Parameters
m_d3dPP.BackBufferWidth = width();
m_d3dPP.BackBufferHeight = height();
m_d3dPP.BackBufferFormat = D3DFMT_A8R8G8B8;
m_d3dPP.BackBufferCount = 1;
m_d3dPP.SwapEffect = D3DSWAPEFFECT_FLIP;
m_d3dPP.FullScreen_RefreshRateInHz = 0;
m_d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
// Multisampling(Antialiasing) Parameters
m_d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
m_d3dPP.MultiSampleQuality = 0;
// Window Parameters
m_d3dPP.hDeviceWindow = winId();
m_d3dPP.Windowed = TRUE;
// Depth/Stencil Buffer Parameters
m_d3dPP.EnableAutoDepthStencil = TRUE;
m_d3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
// misc. Parameters
m_d3dPP.Flags = 0;
// Initialize Direct3D 9 Device
if(!SUCCEEDED(m_pD3D9->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
winId(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&m_d3dPP,
&m_pD3D9dev)))
{
QMessageBox::information(this, "Direct 3D Device Initialization",
"Unable to initialize Direct3D 9 Device.");
return FALSE;
}
//Vectors
m_vEye = D3DXVECTOR3(5.0f, 2.0f, 5.0f);
m_vAt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
//Matrices
m_fTurn = 0.0f;
m_bTurn = false;
D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
D3DXMatrixLookAtLH(&m_mtxView, &m_vEye, &m_vAt, &m_vUp);
D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 0.14f, width() / height(), 0.1f, 10.0f);
if(InitCubeObjects(m_pD3D9dev))
{
cube_effect->SetMatrix(cube_world, &m_mtxWorld);
cube_effect->SetMatrix(cube_view, &m_mtxView);
cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
cube_effect->CommitChanges();
}
//QTimer
m_pqTimer = new QTimer(this);
connect(m_pqTimer, SIGNAL(timeout()), this, SLOT(repaint()));
m_pqTimer->start(10);
return TRUE;
}
void Qt4Direct3DContainer::turn()
{
m_bTurn = !m_bTurn;
}
void Qt4Direct3DContainer::Shut()
{
// delete QTimer
if(m_pqTimer != NULL && m_pqTimer->isActive())
{
m_pqTimer->stop();
}
delete m_pqTimer;
m_pqTimer = NULL;
ReleaseCubeObjects();
// Release Direct3D 9 Device
if(m_pD3D9dev != NULL)
{
m_pD3D9dev->Release();
}
m_pD3D9dev = NULL;
// Zero Direct3D 9 Presentation Parameters
ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
// Release Direct3D 9
if(m_pD3D9 != NULL)
{
m_pD3D9->Release();
}
m_pD3D9 = NULL;
// Matrices
ZeroMemory(&m_mtxWorld, sizeof(D3DXMATRIX));
ZeroMemory(&m_mtxView, sizeof(D3DXMATRIX));
ZeroMemory(&m_mtxProjection, sizeof(D3DXMATRIX));
// Vectors
ZeroMemory(&m_vEye, sizeof(D3DXVECTOR3));
ZeroMemory(&m_vAt, sizeof(D3DXVECTOR3));
ZeroMemory(&m_vUp, sizeof(D3DXVECTOR3));
}
QPaintEngine *Qt4Direct3DContainer::paintEngine() const
{
return NULL;
}
void Qt4Direct3DContainer::paintEvent(QPaintEvent *p_event)
{
m_pD3D9dev->Clear( 0,
NULL,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(64, 64, 96, 255),
1.0f,
0);
m_pD3D9dev->BeginScene();
m_pD3D9dev->SetVertexDeclaration(pDeclarationPosColorNormal);
m_pD3D9dev->SetStreamSource(0, cube_vertexBuffer, 0, sizeof(VertexPosColorNormal));
m_pD3D9dev->SetIndices(cube_indexBuffer);
//m_pD3D9dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
UINT uiNumberOfPasses,
uiPass;
if(m_bTurn)
{
m_fTurn += 0.01f;
D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
}
cube_effect->SetMatrix(cube_world, &m_mtxWorld);
cube_effect->SetMatrix(cube_view, &m_mtxView);
cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
cube_effect->CommitChanges();
cube_effect->Begin(&uiNumberOfPasses, 0);
for(uiPass = 0; uiPass < uiNumberOfPasses; uiPass++)
{
cube_effect->BeginPass(uiPass);
/*m_pD3D9dev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
cube_numberOfVertices,
0,
12);*/
m_pD3D9dev->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST,
0,
cube_numberOfVertices,
12,
cube_indices,
D3DFMT_INDEX32,
&cube_vertices,
sizeof(VertexPosColorNormal));
cube_effect->EndPass();
}
cube_effect->End();
m_pD3D9dev->EndScene();
m_pD3D9dev->Present( NULL,
NULL,
NULL,
NULL);
}
void Qt4Direct3DContainer::resizeEvent(QResizeEvent *p_event)
{
QSize newSize = p_event->size();
m_d3dPP.BackBufferWidth = newSize.rwidth();
m_d3dPP.BackBufferHeight = newSize.rheight();
D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 1.6f, (float)m_d3dPP.BackBufferWidth / (float)m_d3dPP.BackBufferHeight, 0.1f, 10.0f);
ReleaseCubeObjects();
m_pD3D9dev->Reset(&m_d3dPP);
InitCubeObjects(m_pD3D9dev);
}
To copy to clipboard, switch view to plain text mode
Bookmarks