C++ Projects
3D Benchmark using DirectX
Table of Contents
Last spring I programmed a 3D benchmark using the DirectX SDK. The goal of the project was to incorporate HLSL that would handle 3D lighting and rendering, in order to utilize the GPU and draw more objects on the screen. It passes each objects matrix information and calculates the outputs in the shader. There is an FPS counter as well as a few other object counters used for benchmarking.
// this is the function used to render a single frame
void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// set frame counter
counter++;
if (GetTickCount() > startTime + 1000)
{
framerate = counter;
counter = 0;
startTime = GetTickCount();
}
// begin drawing the text sprite
textSprite->Begin(D3DXSPRITE_ALPHABLEND);
RECT rect = {20, 20, SCREEN_WIDTH, SCREEN_HEIGHT};
D3DCOLOR color = D3DCOLOR_XRGB(0,255,0);
string fps = "";
static char string[256];
int vertObject = meshTorus->GetNumVertices();
int faceObject = meshTorus->GetNumFaces();
sprintf(string, "FPS: %d\n"
"Vertices per Object: %d\n"
"Faces per Object: %d\n"
"Total Verticies: %d\n"
"Total Faces: %d\n"
"Total Objects: %d\n",
framerate,
vertObject,
faceObject,
vertObject * numObjects,
faceObject * numObjects,
numObjects);
font->DrawText(textSprite, string, -1, &rect, DT_NOCLIP, color);
// set the view transform
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 3.0f, 250.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
// set the projection transform
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,// AR
1.0f, // the near view-plane
2000.0f); // the far view-plane
//set the ambient/diffuse light source vector
D3DXVECTOR3 mLightVecW = D3DXVECTOR3(0.0, 1.0f, 1.0f);
//set the ambient/diffuse colors
float ambLevel = 0.1f;
float difLevel = 0.7f;
D3DXCOLOR mDiffuseLight = D3DXCOLOR(difLevel, difLevel, difLevel, 1.0f);
D3DXCOLOR mAmbientMtrl = WHITE;
D3DXCOLOR mAmbientLight = D3DXCOLOR(ambLevel, ambLevel, ambLevel, 1.0f);
//pass variables to shader
shaderEffect1->SetValue("gLightVecW", &mLightVecW, sizeof(D3DXVECTOR3));
shaderEffect1->SetValue("gDiffuseLight", &mDiffuseLight, sizeof(D3DXCOLOR));
shaderEffect1->SetValue("gAmbientMtrl", &mAmbientMtrl, sizeof(D3DXCOLOR));
shaderEffect1->SetValue("gAmbientLight", &mAmbientLight, sizeof(D3DXCOLOR));
shaderEffect1->SetMatrix("mView", &(matView));
shaderEffect1->SetMatrix("mProjection", &(matProjection));
static float index = 0.0f; index += 0.05f;
/**
** RENDERING ALL OF THE OBJECTS
**/
vector<Torus>::iterator vecIterator;
for (vecIterator = torusVector.begin();
vecIterator != torusVector.end();
vecIterator++)
{
Torus t = *vecIterator;
D3DXMatrixRotationYawPitchRoll( &t.rotY, 0, 0, -index);
//calculate lighting normal based on current transforms
D3DXMATRIX worldInverseTranspose;
D3DXMatrixInverse(&worldInverseTranspose, 0, &(t.rotY * t.pos));
D3DXMatrixTranspose(&worldInverseTranspose, &worldInverseTranspose);
//send data to the shader
shaderEffect1->SetMatrix("mRotation", &(t.rotY));
shaderEffect1->SetMatrix("mTranslation", &(t.pos));
shaderEffect1->SetMatrix("gWorldInverseTranspose", &worldInverseTranspose);
shaderEffect1->SetValue("gDiffuseMtrl", &(t.color), sizeof(D3DXCOLOR));
UINT numPasses = 0;
shaderEffect1->Begin(&numPasses, 0);
for (int i=0; i<numPasses; ++i)
{
shaderEffect1->BeginPass(i);
meshTorus->DrawSubset(0);
shaderEffect1->EndPass();
}
shaderEffect1->End();
}
/**
** RENDERING OBJECTS END
**/
textSprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
return;
}
// Create all torus objects
for (int i = 0; i < numObjects; i++)
{
Torus torusObject;
// Apply movement function
D3DXMatrixTranslation(&torusObject.pos,
(((float)i * 2) * cos(6 * (float)i)), (((float)i * 2) * sin(6 * (float)i)), -(i * 4));
// Get random color
iRandColor = rand() % 6 + 1;
if (iRandColor == 1)
torusObject.color = WHITE;
else if (iRandColor == 2)
torusObject.color = BLUE;
else if (iRandColor == 3)
torusObject.color = GREEN;
else if (iRandColor == 4)
torusObject.color = RED;
else if (iRandColor == 5)
torusObject.color = CYAN;
else if (iRandColor == 6)
torusObject.color = PURPLE;
// Add object
torusVector.push_back(torusObject);
}
}

