Related
I used vertexes to draw textures and did not have a problem but now i am trying to rotate some textures and getting black screen.
This is my initial code to draw texture with vertex.
struct CUSTOM_VERTEX { FLOAT X, Y, Z, RHW, U, V; };
#define CUSTOM_FVF (D3DFVF_XYZRHW | D3DFVF_TEX1 )
CUSTOM_VERTEX vertices[4] =
{ //X, Y, Z, RHW, U, V
100.0f, 100.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1000.0f, 100.0f, 0.0f, 1.0f, 1.0f, 0.0f,
100.0f, 750.0f, 0.0f, 1.0f, 0.0f, 1.0f,
1000.0f, 750.0f, 0.0f, 1.0f, 1.0f, 1.0f
};
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
d3d9device->CreateVertexBuffer(4 * sizeof(CUSTOM_VERTEX),
0,
CUSTOM_FVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
d3d9device->SetFVF(CUSTOM_FVF);
d3d9device->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOM_VERTEX));
d3d9device->SetTexture(0, d3d9texture);
d3d9device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
I read that RHW describes pre-transformed vertices. Then i changed CUSTOM_VERTEX, CUSTOM_FVF and added SetTransform.
struct CUSTOM_VERTEX { FLOAT X, Y, Z, U, V; };
#define CUSTOM_FVF (D3DFVF_XYZ | D3DFVF_TEX1 )
CUSTOM_VERTEX vertices[4] =
{ //X, Y, Z, U, V
100.0f, 100.0f, 0.0f, 0.0f, 0.0f,
1000.0f, 100.0f, 0.0f, 1.0f, 0.0f,
100.0f, 750.0f, 0.0f, 0.0f, 1.0f,
1000.0f, 750.0f, 0.0f, 1.0f, 1.0f
};
//CreateVertexBuffer, memcpy, SetFVF and SetStreamsource are the same as above
D3DXMATRIX worldMatrix;
D3DXMatrixRotationZ(&worldMatrix, D3DXToRadian(90));
d3d9device->SetTransform(D3DTS_WORLD, &worldMatrix);
//SetTexture and DrawPrimitive are the same as above
I also tried adding view and projection matrix.
D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH(&viewMatrix,
&D3DXVECTOR3 (0.0f, 0.0f, 10.0f),
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
d3d9device->SetTransform(D3DTS_VIEW, &viewMatrix);
D3DXMATRIX projectionMatrix;
D3DXMatrixPerspectiveFovLH(&projectionMatrix,
D3DXToRadian(45),
width / height,
1.0f,
1000.0f);
d3d9device->SetTransform(D3DTS_PROJECTION, &projectionMatrix);
I changed pEye and pAt inputs of D3DXMatrixLookAtLH method to center of vertex but again black screen.
My problem was using the same points for both pre-transformed (RHW) and non pre-transformed vertices.
Solved problem like below.
struct CUSTOM_VERTEX { FLOAT X, Y, Z, U, V; };
#define CUSTOM_FVF (D3DFVF_XYZ | D3DFVF_TEX1 )
float halfBackBufferWidth = backBufferWidth / 2.0f;
float halfBackBufferHeight = backBufferHeight / 2.0f;
CUSTOM_VERTEX vertices[4] =
{ //X, Y, Z, U, V
{ halfBackBufferWidth - 100.0f, halfBackBufferHeight - 100.0f, 0.0f, 0.0f, 0.0f },
{ halfBackBufferWidth - 1000.0f, halfBackBufferHeight - 100.0f, 0.0f, 1.0f, 0.0f },
{ halfBackBufferWidth - 100.0f, halfBackBufferHeight - 750.0f, 0.0f, 0.0f, 1.0f },
{ halfBackBufferWidth - 1000.0f, halfBackBufferHeight - 750.0f, 0.0f, 1.0f, 1.0f }
};
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
d3d9device->CreateVertexBuffer(4 * sizeof(CUSTOM_VERTEX),
0,
CUSTOM_FVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
d3d9device->SetFVF(CUSTOM_FVF);
d3d9device->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOM_VERTEX));
D3DXMATRIX worldMatrix;
D3DXMatrixRotationZ(&worldMatrix, D3DXToRadian(45));
d3d9device->SetTransform(D3DTS_WORLD, &worldMatrix);
D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH(&viewMatrix,
&D3DXVECTOR3 (0.0f, 0.0f, halfBackBufferHeight),
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
d3d9device->SetTransform(D3DTS_VIEW, &viewMatrix);
D3DXMATRIX projectionMatrix;
D3DXMatrixPerspectiveFovLH(&projectionMatrix,
D3DXToRadian(90),
backBufferWidth / backBufferHeight,
1.0f,
halfBackBufferHeight);
d3d9device->SetTransform(D3DTS_PROJECTION, &projectionMatrix);
d3d9device->SetTexture(0, d3d9texture);
d3d9device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
I dont want to add light so I disable it. If I dont disable it, I get black screen.
d3d9device->SetRenderState(D3DRS_LIGHTING, FALSE);
I have been following this tutorial of DirectX http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-5. I have just started DirectX with C++. I have drawn two rectangles on the screen but cannot translate them.Th rectangles have different position on screen and are stored in Array OurVertices. I have been reading from the tutorial and it says that you first have to apply world transformation to translate it in 3d but I dont want to get into 3d because I have two simple 2d rectangles. How you move simple 2d rectangles in DirectX C++. If I apply World Transformation using the tutorial I dont know why my camera position is tilted. My code is given below:
CUSTOMVERTEX OurVertices[] =
{
// 1
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};
d3ddev->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // the void* we were talking about
v_buffer->Lock(0, 0, (void**)&pVoid, 0); // locks v_buffer, the buffer we made earlier
memcpy(pVoid, OurVertices, sizeof(OurVertices));
v_buffer->Unlock(); // unlock v_buffer
d3ddev->SetFVF(CUSTOMFVF);
void render_frame()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
// select the vertex buffer to display
d3ddev->SetFVF(CUSTOMFVF);
// SET UP THE PIPELINE
D3DXMATRIX matTranslate;
static float index = 0.0f;
index += 0.01f; // an ever-increasing float value
// build a matrix to rotate the model based on the increasing float value
D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);
// tell Direct3D about our matrix
d3ddev->SetTransform(D3DTS_WORLD, &matTranslate);
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 0.0f, 10.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
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(100), // the horizontal field of view
(FLOAT)800 / (FLOAT)600, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
Is there any way to translate two rectangles?
You are already working in 3D, when you specify the position of your vertices you have an extra z component wich is set to 0 at the moment. You can change it and see the effect on your scene.
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
So now each frame you are translating your vertices by an increasing amount on the y axis.
static float index = 0.0f;
index += 0.01f; // an ever-increasing float value
// build a matrix to rotate the model based on the increasing float value
D3DXMatrixTranslation(&matTranslate, 0, index , 0.0f);
If you print index each frame you will see him growing and you use this value to to build a translation matrix which will be apply before view/projection.
I don't know what is the effect you want.
But all the vertices of your scene will go from bottom to top, which you can look like your camera is going down.
I have a problem using DirectX::SpriteFont/DirectX::SpriteBatch (from DirectXTK; exactly the same problem as discussed here: Problems when drawing text using the SpriteFont class).
void DrawScene(void)
{
HRESULT hr;
float bgColor_a[4] = { 0.0f, 0.4f, 0.8f, 0.0f };
g_pDeviceContext->ClearRenderTargetView(g_pRenderTargetView, bgColor_a);
g_pDeviceContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
XMMATRIX cameraProj = XMLoadFloat4x4(&g_camera._cameraProjection);
XMVECTOR pos = XMLoadFloat3(&g_camera._pos);
XMVECTOR target = XMLoadFloat3(&g_camera._target);
XMVECTOR up = XMLoadFloat3(&g_camera._up);
XMMATRIX cameraView = XMMatrixLookAtLH(pos, target, up);
XMMATRIX worldBox2 = XMMatrixIdentity() * (XMMatrixTranslation(2.0f, 0.0f, 0.0f) * XMMatrixRotationY(XMConvertToRadians(g_rotBox2)));
XMMATRIX wvp = worldBox2 * cameraView * cameraProj;
XMMATRIX transposeWvp = XMMatrixTranspose(wvp);
XMStoreFloat4x4(&g_constantBufferPerObject._wvp, transposeWvp);
g_pDeviceContext->UpdateSubresource(g_pConstantBufferPerObject, 0, NULL, &g_constantBufferPerObject, 0, 0);
g_pDeviceContext->VSSetConstantBuffers(0, 1, &g_pConstantBufferPerObject);
g_pDeviceContext->PSSetShaderResources(0, 1, &g_pCageTexture);
g_pDeviceContext->PSSetSamplers(0, 1, &g_pCubeTextureSamplerState);
// box
g_pDeviceContext->DrawIndexed(36, 0, 0);
wchar_t buffer[32];
swprintf_s(buffer, 32, L"%.2f", g_fps._fps);
//g_pSpriteBatch->Begin();
//g_pSpriteFont->DrawString(g_pSpriteBatch, buffer, XMFLOAT2(30, 30));
//g_pSpriteBatch->End();
// Present the backbuffer to the screen
hr = g_pSwapChain->Present(0, 0);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot present without error.");
}
}
Without the calls to SpriteBatch::Begin(), SpriteFont::DrawString and SpriteBatch::End() you will see a textured cube rotating through space (a cage). With the calls to the described functions, you will only see the frames per second in the upper left corner, but no rotating cube.
I followed the tutorials from Chuck Walbourn on github DirectXTK and combined the 2 tutorials Draw String using SpriteFont and rendering a primitive 3D object (a simple triangle). In this example I will see the test string drawing over the triangle. But I do not understand why the rotating cube from my example is not visible while drawing a text string using SpriteFont class of DirectXTK and I cannot even find the root cause.
Pixel shader file (PixelShader.hlsl):
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 TexCoord : TEXCOORD;
};
Texture2D ObjTexture;
SamplerState ObjSamplerState;
float4 PS( VS_OUTPUT input ) : SV_TARGET
{
float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.TexCoord);
clip(diffuse.a - 0.25);
return diffuse;
}
Vertex shader file (VertexShader.hlsl):
cbuffer constantBufferPerObject
{
float4x4 WVP;
float ColorAdjust;
int Mode;
int Pad[2];
};
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 TexCoord : TEXCOORD;
};
VS_OUTPUT VS( float4 pos : POSITION, float4 color : COLOR_ZERO, float4 color2 : COLOR_ONE, float2 texCoord : TEXCOORD )
{
VS_OUTPUT output;
float4 temp;
output.Pos = mul(pos, WVP);
temp = color;
output.Color.r = temp.r * color2.r * (1.0f - ColorAdjust);
output.Color.g = temp.g * color2.g * (1.0f - ColorAdjust);
output.Color.b = temp.b * color2.b * (1.0f - ColorAdjust);
output.Color.a = color.a * color2.a;
output.TexCoord = texCoord;
return output;
}
To reproduce the issue: I am working with Visual Studio 2015 Community Edition. I added DirectXTK (.lib) and DirectXTex (WICTextureLoader .cpp/.h, DDSTextureLoader .cpp/.h, .lib) to my project. Image for cube is .png with alpha.
Note: I created this duplicated question, to
avoid ... Asking for help, clarification, or responding to other answers.
on the other question.
First, as you are using DirectXTK, you don't need DirectXTex. DirectXTex is intended for texture processing tools, while DirectXTK texture loaders are more suited to use in applications. See this blog post.
Second, don't use the ancient timeGetTime function. You can use GetTickCount, although GetTickCount64 is better as it avoids overflow problems. For frame timing, however, these are not really precise enough. You should take a look at the StepTimer.h used in the DirectX Tool Kit tutorials.
Third, you should look at using Microsoft::WRL::ComPtr rather than all the SAFE_RELEASE macro stuff. See this page for details.
Your code was including xinput.h as well. You should take a look at using DirectX Tool Kit's GamePad instead which has a number of benefits. If nothing else, it uses XInput more robustly than you likely would.
With all that said, your problem is quite simple: You set the render state up for your scene in InitScene, but drawing anything else changes the state which is exactly what SpriteBatch does. I document which render states each object in DirectX Toolkit manipulates on the wiki.
You need to set all the state your Draw requires each frame, not assume it is set forever, if you do more than a single draw.
void DrawScene(void)
{
HRESULT hr;
float bgColor_a[4] = { 0.0f, 0.4f, 0.8f, 0.0f };
g_pDeviceContext->ClearRenderTargetView(g_pRenderTargetView, bgColor_a);
g_pDeviceContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
//>>>> THESE WERE MOVED FROM INITSCENE ABOVE!
// Set Vertex and Pixel Shaders
g_pDeviceContext->VSSetShader(g_pVertexShader, 0, 0);
g_pDeviceContext->PSSetShader(g_pPixelShader, 0, 0);
// Set the vertex buffer
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_pDeviceContext->IASetVertexBuffers(0, 1, &g_pTriangleVertexBuffer, &stride, &offset);
g_pDeviceContext->IASetIndexBuffer(g_pSquareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
// Set the Input Layout
g_pDeviceContext->IASetInputLayout(g_pVertexLayout);
// Set Primitive Topology
g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 3 vertices per triangle
//g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); // 1 vertex per point
//g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); // 2 vertices per line
// Create the Viewport
g_pDeviceContext->RSSetState(g_pNoCullSolid);
//THESE WERE MOVED FROM INITSCENE ABOVE! <<<<
XMMATRIX cameraProj = XMLoadFloat4x4(&g_camera._cameraProjection);
XMVECTOR pos = XMLoadFloat3(&g_camera._pos);
XMVECTOR target = XMLoadFloat3(&g_camera._target);
XMVECTOR up = XMLoadFloat3(&g_camera._up);
XMMATRIX cameraView = XMMatrixLookAtLH(pos, target, up);
XMMATRIX worldBox2 = XMMatrixIdentity() * (XMMatrixTranslation(2.0f, 0.0f, 0.0f) * XMMatrixRotationY(XMConvertToRadians(g_rotBox2)));
XMMATRIX wvp = worldBox2 * cameraView * cameraProj;
XMMATRIX transposeWvp = XMMatrixTranspose(wvp);
XMStoreFloat4x4(&g_constantBufferPerObject._wvp, transposeWvp);
g_pDeviceContext->UpdateSubresource(g_pConstantBufferPerObject, 0, NULL, &g_constantBufferPerObject, 0, 0);
g_pDeviceContext->VSSetConstantBuffers(0, 1, &g_pConstantBufferPerObject);
g_pDeviceContext->PSSetShaderResources(0, 1, &g_pCageTexture);
g_pDeviceContext->PSSetSamplers(0, 1, &g_pCubeTextureSamplerState);
// box
g_pDeviceContext->DrawIndexed(36, 0, 0);
wchar_t buffer[32];
swprintf_s(buffer, 32, L"%.2f", g_fps._fps);
g_pSpriteBatch->Begin();
g_pSpriteFont->DrawString(g_pSpriteBatch, buffer, XMFLOAT2(30, 30));
g_pSpriteBatch->End();
// Present the backbuffer to the screen
hr = g_pSwapChain->Present(0, 0);
//>>>> This behavior means the app crashes if you hit a DEVICE_REMOVED or DEVICE_RESET.
if (FAILED(hr))
{
ErrorBoxW(L"Cannot present without error.");
}
}
You can generally 'set it and forget it' for the viewport state RSSetViewports which also implicitly sets the scissors state, but a better usage is to set it at the start of each frame when you do your initial Clear. This is the pattern I use in the Direct3D Game Templates.
Here is the complete code:
#include <Windows.h>
#include <stdio.h>
#include <Xinput.h>
#include <DirectXMath.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXTex.h>
#include <SpriteFont.h>
#include "DDSTextureLoader.h"
#include "WICTextureLoader.h"
using namespace DirectX;
#define CAMERA_ROTATION_SPEED (110.0f)
#define CAMERA_TARGET_DISTANCE (3.0f)
#define CAMERA_TARGET_VIEW_Y_START (-15.0f)
#define CAMERA_TARGET_VIEW_XZ_START (0.0f)
#define CAMERA_TARGET_START (XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f))
#define CAMERA_UP_START (XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f))
#define CAMERA_POS_START (XMVectorSet(0.0f, 0.0f, 3.0f, 0.0f))
#define PLAYER_MOVEMENT_SPEED (5.0f)
#define SAFE_DELETE(p) if (p) { delete (p); (p) = NULL; }
#define SAFE_RELEASE(p) if (p) { (p)->Release(); (p) = NULL; }
#ifdef _DEBUG
#define ErrorBoxW(msg) MessageBox(NULL, (msg), L"Error", MB_OK | MB_ICONERROR)
#else // _DEBUG
#define ErrorBox(msg)
#endif // _DEBUG
#define NORMALIZE_ANGLE(ang) { while ((ang) > 360.0f) { (ang) -= 360.0f; } while ((ang) < -360.0f) { (ang) += 360.0f; } }
#define LIMIT_ANGLE(ang, val) { if ((ang) > (val)) { (ang) = (val); } if ((ang) < -(val)) { (ang) = -(val); } }
#define CHECK_CHANGE_F(curr, prev, flag) { (flag) = (curr) != (prev); }
typedef struct _Camera
{
XMFLOAT3 _target;
XMFLOAT3 _pos;
XMFLOAT3 _up;
float _viewRotXZ;
float _viewRotY;
XMFLOAT4X4 _cameraProjection;
} Camera;
typedef struct _FPS
{
_FPS(void)
: _elapsedTime(0.0f), _frames(0), _fps(0.0f) {}
void Calc(float timeDelta)
{
_elapsedTime += timeDelta;
_frames++;
if (_elapsedTime >= 1.0f)
{
_fps = (float)_frames / _elapsedTime;
_elapsedTime = 0.0f;
_frames = 0;
#ifdef _DEBUG
wchar_t buffer[32];
swprintf_s(buffer, 32, L"FPS: %.2f\n", _fps);
OutputDebugString(buffer);
#endif // _DEBUG
}
}
float _fps;
float _elapsedTime;
int _frames;
} FPS;
typedef struct _Player
{
_Player(void)
: _moveX(0.0f), _moveY(0.0f), _moveZ(0.0f) {}
float _moveX;
float _moveY;
float _moveZ;
} Player;
using namespace DirectX;
typedef struct _SimpleVertex
{
_SimpleVertex() {}
_SimpleVertex(float x, float y, float z, float cr, float cg, float cb, float ca, float cr2, float cg2, float cb2, float ca2, float u, float v)
: _pos(x, y, z), _color0(cr, cg, cb, ca), _color1(cr2, cg2, cb2, ca2), _tex(u, v) {}
XMFLOAT3 _pos;
XMFLOAT4 _color0;
XMFLOAT4 _color1;
XMFLOAT2 _tex;
} SimpleVertex;
// sizeof(ConstantBufferPerObject) = 80; multiple of 16
typedef struct _ConstantBufferPerObject
{
XMFLOAT4X4 _wvp; // sizeof(XMFLOAT4X4) = 64
float _colorAdjust; // sizeof(float) = 4; 68
int _mode; // sizeof(int) = 4; 72
int _pad[2]; // 2 * sizeof(int) = 8; 80
} ConstantBufferPerObject;
LPWSTR const g_windowClassName = L"dx11demo";
LPWSTR const g_windowTitle = L"DirectX11Demo";
HWND g_hwnd = NULL;
HINSTANCE g_hinstance = NULL;
const int g_width = 800;
const int g_height = 600;
IDXGISwapChain *g_pSwapChain = NULL;
ID3D11Device *g_pDevice = NULL;
ID3D11DeviceContext *g_pDeviceContext = NULL;
ID3D11RenderTargetView *g_pRenderTargetView = NULL;
ID3D11Buffer *g_pTriangleVertexBuffer = NULL;
ID3D11Buffer *g_pSquareIndexBuffer = NULL;
ID3D11VertexShader *g_pVertexShader = NULL;
ID3D11PixelShader *g_pPixelShader = NULL;
ID3DBlob *g_pVertexShaderBuffer = NULL;
ID3DBlob *g_pPixelShaderBuffer = NULL;
ID3D11DepthStencilView *g_pDepthStencilView = NULL;
ID3D11Buffer *g_pConstantBufferPerObject = NULL;
ID3D11Texture2D *g_pDepthStencilBuffer = NULL;
ID3D11InputLayout *g_pVertexLayout = NULL;
ID3D11ShaderResourceView *g_pCageTexture = NULL;
ID3D11ShaderResourceView *g_pBrickTexture = NULL;
ID3D11SamplerState *g_pCubeTextureSamplerState = NULL;
bool g_solid = true;
float g_rotBox2 = 0.0f;
ID3D11RasterizerState *g_pNoCullSolid = NULL;
ID3D11RasterizerState *g_pNoCullWireframe = NULL;
ID3D11RasterizerState *g_pCWCullSolid = NULL;
ID3D11RasterizerState *g_pCCWCullSolid = NULL;
ID3D11BlendState *g_pTransparency = NULL;
SpriteBatch *g_pSpriteBatch = NULL;
SpriteFont *g_pSpriteFont = NULL;
D3D11_INPUT_ELEMENT_DESC g_layout_a[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR_ZERO", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR_ONE", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT g_numElements = ARRAYSIZE(g_layout_a);
bool g_isMoving = true;
ConstantBufferPerObject g_constantBufferPerObject;
bool g_enableDraw = true;
Player g_player;
Camera g_camera;
FPS g_fps;
void CleanUp(void);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
bool InitD3D11(void);
bool InitScene(void);
void DrawScene(void);
void UpdateScene(float);
void UpdateScene(float timeDelta)
{
g_rotBox2 += 20.0f * timeDelta;
NORMALIZE_ANGLE(g_rotBox2);
}
void DrawScene(void)
{
HRESULT hr;
float bgColor_a[4] = { 0.0f, 0.4f, 0.8f, 0.0f };
g_pDeviceContext->ClearRenderTargetView(g_pRenderTargetView, bgColor_a);
g_pDeviceContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
XMMATRIX cameraProj = XMLoadFloat4x4(&g_camera._cameraProjection);
XMVECTOR pos = XMLoadFloat3(&g_camera._pos);
XMVECTOR target = XMLoadFloat3(&g_camera._target);
XMVECTOR up = XMLoadFloat3(&g_camera._up);
XMMATRIX cameraView = XMMatrixLookAtLH(pos, target, up);
XMMATRIX worldBox2 = XMMatrixIdentity() * (XMMatrixTranslation(2.0f, 0.0f, 0.0f) * XMMatrixRotationY(XMConvertToRadians(g_rotBox2)));
XMMATRIX wvp = worldBox2 * cameraView * cameraProj;
XMMATRIX transposeWvp = XMMatrixTranspose(wvp);
XMStoreFloat4x4(&g_constantBufferPerObject._wvp, transposeWvp);
g_pDeviceContext->UpdateSubresource(g_pConstantBufferPerObject, 0, NULL, &g_constantBufferPerObject, 0, 0);
g_pDeviceContext->VSSetConstantBuffers(0, 1, &g_pConstantBufferPerObject);
g_pDeviceContext->PSSetShaderResources(0, 1, &g_pCageTexture);
g_pDeviceContext->PSSetSamplers(0, 1, &g_pCubeTextureSamplerState);
// box
g_pDeviceContext->DrawIndexed(36, 0, 0);
wchar_t buffer[32];
swprintf_s(buffer, 32, L"%.2f", g_fps._fps);
//g_pSpriteBatch->Begin();
//g_pSpriteFont->DrawString(g_pSpriteBatch, buffer, XMFLOAT2(30, 30));
//g_pSpriteBatch->End();
// Present the backbuffer to the screen
hr = g_pSwapChain->Present(0, 0);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot present without error.");
}
}
bool InitScene(void)
{
HRESULT hr;
ID3DBlob *pErrorBlob = NULL;
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _DEBUG
flags |= D3DCOMPILE_DEBUG;
#endif // _DEBUG
const D3D_SHADER_MACRO defines_a[] =
{
{ NULL, NULL }
};
// Compile Shaders from shader file
// https://msdn.microsoft.com/de-de/library/windows/desktop/hh968107(v=vs.85).aspx
hr = D3DCompileFromFile(L"VertexShader.hlsl", defines_a, D3D_COMPILE_STANDARD_FILE_INCLUDE, "VS", "vs_5_0", flags, 0, &g_pVertexShaderBuffer, &pErrorBlob);
if (FAILED(hr))
{
SAFE_RELEASE(pErrorBlob);
ErrorBoxW(L"Cannot compile vertex shader VS vs_5_0.");
return false;
}
hr = D3DCompileFromFile(L"PixelShader.hlsl", defines_a, D3D_COMPILE_STANDARD_FILE_INCLUDE, "PS", "ps_5_0", flags, 0, &g_pPixelShaderBuffer, &pErrorBlob);
if (FAILED(hr))
{
SAFE_RELEASE(pErrorBlob);
ErrorBoxW(L"Cannot compile pixel shader PS ps_5_0.");
return false;
}
// Create the Shader Objects
hr = g_pDevice->CreateVertexShader(g_pVertexShaderBuffer->GetBufferPointer(), g_pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create vertex shader.");
return false;
}
hr = g_pDevice->CreatePixelShader(g_pPixelShaderBuffer->GetBufferPointer(), g_pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create pixel shader.");
return false;
}
// Set Vertex and Pixel Shaders
g_pDeviceContext->VSSetShader(g_pVertexShader, 0, 0);
g_pDeviceContext->PSSetShader(g_pPixelShader, 0, 0);
// Create the vertex buffer (vertices must be in clock-wise order)
SimpleVertex vertices_a[] =
{
// Front Face
/* 11 */ SimpleVertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 12 */ SimpleVertex(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 13 */ SimpleVertex(1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
/* 14 */ SimpleVertex(1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
// Back Face
/* 15 */ SimpleVertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
/* 16 */ SimpleVertex(1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 17 */ SimpleVertex(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 18 */ SimpleVertex(-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
// Top Face
/* 19 */ SimpleVertex(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 20 */ SimpleVertex(-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 21 */ SimpleVertex(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
/* 22 */ SimpleVertex(1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
// Bottom Face
/* 23 */ SimpleVertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
/* 24 */ SimpleVertex(1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 25 */ SimpleVertex(1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 26 */ SimpleVertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
// Left Face
/* 27 */ SimpleVertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 28 */ SimpleVertex(-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 29 */ SimpleVertex(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
/* 30 */ SimpleVertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
// Right Face
/* 31 */ SimpleVertex(1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 3.0f),
/* 32 */ SimpleVertex(1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
/* 33 */ SimpleVertex(1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 0.0f),
/* 34 */ SimpleVertex(1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f),
};
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(SimpleVertex) * 24;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
vertexBufferData.pSysMem = vertices_a;
hr = g_pDevice->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &g_pTriangleVertexBuffer);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create triangle buffer.");
return false;
}
// Set the vertex buffer
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_pDeviceContext->IASetVertexBuffers(0, 1, &g_pTriangleVertexBuffer, &stride, &offset);
DWORD indices_a[] =
{
// Front Face
/* 5 */ 0, 1, 2,
/* 6 */ 0, 2, 3,
// Back Face
/* 7 */ 4, 5, 6,
/* 8 */ 4, 6, 7,
// Top Face
/* 9 */ 8, 9, 10,
/* 10 */ 8, 10, 11,
// Bottom Face
/* 11 */ 12, 13, 14,
/* 12 */ 12, 14, 15,
// Left Face
/* 13 */ 16, 17, 18,
/* 14 */ 16, 18, 19,
// Right Face
/* 15 */ 20, 21, 22,
/* 16 */ 20, 22, 23,
};
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(DWORD) * 12 * 3;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA iinitData;
ZeroMemory(&iinitData, sizeof(D3D11_SUBRESOURCE_DATA));
iinitData.pSysMem = indices_a;
hr = g_pDevice->CreateBuffer(&indexBufferDesc, &iinitData, &g_pSquareIndexBuffer);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create index buffer.");
return false;
}
g_pDeviceContext->IASetIndexBuffer(g_pSquareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
// Create the Input Layout
hr = g_pDevice->CreateInputLayout(g_layout_a, g_numElements, g_pVertexShaderBuffer->GetBufferPointer(), g_pVertexShaderBuffer->GetBufferSize(), &g_pVertexLayout);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create input layout.");
return false;
}
// Set the Input Layout
g_pDeviceContext->IASetInputLayout(g_pVertexLayout);
// Set Primitive Topology
g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 3 vertices per triangle
//g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); // 1 vertex per point
//g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); // 2 vertices per line
// Create the Viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = g_width; // divide by 4 to only use 1/4 of client area (width)
viewport.Height = g_height; // divide by 4 to only use 1/4 of client area (height)
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
//Set the Viewport
g_pDeviceContext->RSSetViewports(1, &viewport);
// Create the buffer to send to the cbuffer in effect file
D3D11_BUFFER_DESC constantBufferDesc;
ZeroMemory(&constantBufferDesc, sizeof(D3D11_BUFFER_DESC));
constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
constantBufferDesc.ByteWidth = sizeof(ConstantBufferPerObject);
constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constantBufferDesc.CPUAccessFlags = 0;
constantBufferDesc.MiscFlags = 0;
hr = g_pDevice->CreateBuffer(&constantBufferDesc, NULL, &g_pConstantBufferPerObject);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create constant buffer.");
return false;
}
D3D11_RASTERIZER_DESC rasterizerDesc;
ZeroMemory(&rasterizerDesc, sizeof(D3D11_RASTERIZER_DESC));
rasterizerDesc.FillMode = D3D11_FILL_SOLID;
rasterizerDesc.CullMode = D3D11_CULL_NONE;
rasterizerDesc.FrontCounterClockwise = true;
hr = g_pDevice->CreateRasterizerState(&rasterizerDesc, &g_pNoCullSolid);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create rasterizer state (no culling, solid).");
return false;
}
rasterizerDesc.FillMode = D3D11_FILL_WIREFRAME;
hr = g_pDevice->CreateRasterizerState(&rasterizerDesc, &g_pNoCullWireframe);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create rasterizer state (no culling, wireframe).");
return false;
}
g_pDeviceContext->RSSetState(g_pNoCullSolid);
hr = CreateWICTextureFromFile(g_pDevice, L"tcage.png", NULL, &g_pCageTexture);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create shader resource view (cage).");
return false;
}
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = g_pDevice->CreateSamplerState(&sampDesc, &g_pCubeTextureSamplerState);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create sampler state.");
return false;
}
D3D11_BLEND_DESC blendDesc; // Define the Blending Equation
ZeroMemory(&blendDesc, sizeof(blendDesc));
D3D11_RENDER_TARGET_BLEND_DESC rtbd;
ZeroMemory(&rtbd, sizeof(rtbd));
rtbd.BlendEnable = true;
rtbd.SrcBlend = D3D11_BLEND_SRC_COLOR;
rtbd.DestBlend = D3D11_BLEND_BLEND_FACTOR;
rtbd.BlendOp = D3D11_BLEND_OP_ADD;
rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;
rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;
rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;
rtbd.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
blendDesc.AlphaToCoverageEnable = false;
blendDesc.RenderTarget[0] = rtbd;
hr = g_pDevice->CreateBlendState(&blendDesc, &g_pTransparency);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create blend state.");
return false;
}
// Create the Counter Clockwise and Clockwise Culling States
D3D11_RASTERIZER_DESC cmdesc;
ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));
cmdesc.FillMode = D3D11_FILL_SOLID;
cmdesc.CullMode = D3D11_CULL_BACK;
cmdesc.FrontCounterClockwise = true;
hr = g_pDevice->CreateRasterizerState(&cmdesc, &g_pCCWCullSolid);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create ccw cull mode.");
return false;
}
cmdesc.FrontCounterClockwise = false;
hr = g_pDevice->CreateRasterizerState(&cmdesc, &g_pCWCullSolid);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create cw cull mode.");
return false;
}
g_pSpriteBatch = new SpriteBatch(g_pDeviceContext);
if (!g_pSpriteBatch)
{
ErrorBoxW(L"Cannot create sprite batch.");
return false;
}
g_pSpriteFont = new SpriteFont(g_pDevice, L"arial.font");
if (!g_pSpriteFont)
{
ErrorBoxW(L"Cannot create sprite font.");
return false;
}
XMStoreFloat3(&g_camera._target, CAMERA_TARGET_START);
XMStoreFloat3(&g_camera._pos, CAMERA_POS_START);
XMStoreFloat3(&g_camera._up, CAMERA_UP_START);
g_camera._viewRotXZ = CAMERA_TARGET_VIEW_XZ_START;
g_camera._viewRotY = CAMERA_TARGET_VIEW_Y_START;
// Set the Projection matrix
XMStoreFloat4x4(&g_camera._cameraProjection, XMMatrixPerspectiveFovLH(XMConvertToRadians(45.0f), (float)g_width / (float)g_height, 0.1f, 1000.0f));
return true;
}
bool InitD3D11(void)
{
HRESULT hr;
DXGI_MODE_DESC bufferDesc; // Describe our Buffer
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = g_width;
bufferDesc.Height = g_height;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_SWAP_CHAIN_DESC swapChainDesc; // Describe our SwapChain
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = g_hwnd;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
//Create our SwapChain
hr = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
NULL,
NULL,
D3D11_SDK_VERSION,
&swapChainDesc,
&g_pSwapChain,
&g_pDevice,
NULL,
&g_pDeviceContext);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create swap chain, device and device context.");
return false;
}
ID3D11Texture2D* backBuffer; // Create our BackBuffer
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&backBuffer);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create back buffer.");
return false;
}
// Create our Render Target
hr = g_pDevice->CreateRenderTargetView(backBuffer, NULL, &g_pRenderTargetView);
SAFE_RELEASE(backBuffer); // release back buffer in any case
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create render target view.");
return false;
}
D3D11_TEXTURE2D_DESC depthStencilDesc; // Describe our Depth/Stencil Buffer
ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthStencilDesc.Width = g_width;
depthStencilDesc.Height = g_height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
// Create the Depth/Stencil View
hr = g_pDevice->CreateTexture2D(&depthStencilDesc, NULL, &g_pDepthStencilBuffer);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create depth stencil buffer.");
return false;
}
hr = g_pDevice->CreateDepthStencilView(g_pDepthStencilBuffer, NULL, &g_pDepthStencilView);
if (FAILED(hr))
{
ErrorBoxW(L"Cannot create depth stencil view.");
return false;
}
// Set our Render Target
g_pDeviceContext->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);
return true;
}
void CleanUp(void)
{
SAFE_RELEASE(g_pDevice);
SAFE_RELEASE(g_pDeviceContext);
SAFE_RELEASE(g_pPixelShader);
SAFE_RELEASE(g_pPixelShaderBuffer);
SAFE_RELEASE(g_pRenderTargetView);
SAFE_RELEASE(g_pSwapChain);
SAFE_RELEASE(g_pTriangleVertexBuffer);
SAFE_RELEASE(g_pVertexLayout);
SAFE_RELEASE(g_pVertexShader);
SAFE_RELEASE(g_pVertexShaderBuffer);
SAFE_RELEASE(g_pSquareIndexBuffer);
SAFE_RELEASE(g_pDepthStencilBuffer);
SAFE_RELEASE(g_pDepthStencilView);
SAFE_RELEASE(g_pConstantBufferPerObject);
SAFE_RELEASE(g_pNoCullSolid);
SAFE_RELEASE(g_pNoCullWireframe);
SAFE_RELEASE(g_pCageTexture);
SAFE_RELEASE(g_pBrickTexture);
SAFE_RELEASE(g_pCubeTextureSamplerState);
SAFE_RELEASE(g_pTransparency);
SAFE_RELEASE(g_pCWCullSolid);
SAFE_RELEASE(g_pCCWCullSolid);
SAFE_DELETE(g_pSpriteBatch);
SAFE_DELETE(g_pSpriteFont);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Check message
switch (msg)
{
case WM_KEYDOWN:
// if escape key was pressed, display popup box
if (wParam == VK_ESCAPE)
{
if (MessageBox(0, L"Are you sure you want to exit?", L"Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
{
// Release the windows allocated memory
DestroyWindow(hwnd);
}
}
return 0;
case WM_DESTROY:
// if x button in top right was pressed
PostQuitMessage(0);
return 0;
}
// return the message for windows to handle it
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(hPrevInstance);
g_hinstance = hInstance;
WNDCLASSEX wc; // Create a new extended windows class
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX); // Size of our windows class
wc.style = CS_HREDRAW | CS_VREDRAW; // class styles
wc.lpfnWndProc = WndProc; // Default windows procedure function
wc.cbClsExtra = NULL; // Extra bytes after our wc structure
wc.cbWndExtra = NULL; // Extra bytes after our windows instance
wc.hInstance = hInstance; // Instance to current application
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Title bar Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Default mouse Icon
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2); // Window bg color
wc.lpszMenuName = NULL; // Name of the menu attached to our window
wc.lpszClassName = g_windowClassName; // Name of our windows class
wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // Icon in your taskbar
if (!RegisterClassEx(&wc)) // Register our windows class
{
ErrorBoxW(L"Error registering class");
return 1;
}
// Create our Extended Window
g_hwnd = CreateWindowEx(
NULL, // Extended style
g_windowClassName, // Name of our windows class
g_windowTitle, // Name in the title bar of our window
WS_OVERLAPPEDWINDOW ^ (WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX), // style of our window
CW_USEDEFAULT, CW_USEDEFAULT, // Top left corner of window
g_width, // Width of our window
g_height, // Height of our window
NULL, // Handle to parent window
NULL, // Handle to a Menu
hInstance, // Specifies instance of current program
NULL // used for an MDI client window
);
// Make sure our window has been created
if (!g_hwnd)
{
ErrorBoxW(L"Error creating window");
return 1;
}
ShowWindow(g_hwnd, nShowCmd); // Shows our window
UpdateWindow(g_hwnd); // Its good to update our window
bool result;
result = InitD3D11();
if (result)
{
result = InitScene();
if (result)
{
MSG msg; // Create a new message structure
ZeroMemory(&msg, sizeof(MSG));
DWORD timeLast = timeGetTime();
DWORD timeCurr = timeLast;
float timeDelta = 0.0f;
bool run = true;
//float elapsed = 0.0f;
//float frameLimit = 1.0f / 60.0f;
while (run)
{
// if there was a windows message
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// if the message was WM_QUIT
if (msg.message == WM_QUIT)
{
run = false; // Exit the message loop
}
TranslateMessage(&msg); // Translate the message
DispatchMessage(&msg); // Send the message to default windows procedure
}
timeCurr = timeGetTime();
timeDelta = (float)(timeCurr - timeLast) / 1000.0f;
//elapsed += timeDelta;
//if (elapsed >= frameLimit)
//{
g_fps.Calc(timeDelta);
UpdateScene(timeDelta);
DrawScene();
//elapsed = 0.0f;
//}
timeLast = timeCurr;
}
}
}
CleanUp();
return 0; // return the message
}
Scene without SpriteFont
Scene with SpriteFont
I'm not sure quite how to debug this, and I'd really appreciate some help.
When I exit the program using the X, I get the following error:
Unhandled exception at 0x00007FF854D4E797 (d3d9.dll) in directxtut.exe: 0xC0000005: Access violation reading location 0x0000000040400058.
The call stack shows the problem is occurring at line 195, d3d-Release();
Thanks in advance!
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
// define the screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
// global declarations
LPDIRECT3D9 d3d; // the pointer to the Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
LPDIRECT3DINDEXBUFFER9 i_buffer = NULL; // the pointer to the index buffer
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(); // renders a single frame
void cleanD3D(); // closes Direct3D and releases memory
void init_graphics(); // 3D declarations
void init_light(); // sets up the light and the material
struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL)
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL, "WindowClass", "Jonathan's Direct3D Program",
WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// enter the main loop:
MSG msg;
while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
init_graphics(); // call the function to initialize the models
init_light(); // call the function to initialize the light and material
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // both sides of the triangles
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); //turn on ambient light
}
// 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();
// select which vertex format to use
d3ddev->SetFVF(CUSTOMFVF);
// set the view transform
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 8.0f, 25.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
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
// set the projection transform
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)SCREEN_HEIGHT / (FLOAT)SCREEN_HEIGHT, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// an ever-increasing float value
static float index = 0.0f;
index += 0.03f;
// set the world transform
D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); // set the world transform
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetIndices(i_buffer);
// draw the model
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
v_buffer->Release(); // close and release the vertex buffer
i_buffer->Release(); //close and release the index buffer
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D *****HERE IS WHERE THE PROGRAM BREAKS*****
}
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX
struct CUSTOMVERTEX vertices[] =
{
{ -3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, }, // side 1
{ 3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },
{ -3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, },
{ -3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, }, // side 2
{ -3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },
{ 3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, },
{ -3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, }, // side 3
{ -3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, },
{ -3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, }, // side 4
{ 3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, },
{ -3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, },
{ 3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, },
{ 3.0f, -3.0f, -3.0f, 1.0f, 0.0f, 0.0f, }, // side 5
{ 3.0f, 3.0f, -3.0f, 1.0f, 0.0f, 0.0f, },
{ 3.0f, -3.0f, 3.0f, 1.0f, 0.0f, 0.0f, },
{ 3.0f, 3.0f, 3.0f, 1.0f, 0.0f, 0.0f, },
{ -3.0f, -3.0f, -3.0f, -1.0f, 0.0f, 0.0f, }, // side 6
{ -3.0f, -3.0f, 3.0f, -1.0f, 0.0f, 0.0f, },
{ -3.0f, 3.0f, -3.0f, -1.0f, 0.0f, 0.0f, },
{ -3.0f, 3.0f, 3.0f, -1.0f, 0.0f, 0.0f, },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(10*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
// create the indices using an int array
short indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
// create an index buffer interface called i_buffer
d3ddev->CreateIndexBuffer(18*sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);
//lock the i_buffer and load the indices into it
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
}
// this is the function that sets up the lights and materials
void init_light()
{
D3DLIGHT9 light; // create the light struct
D3DMATERIAL9 material; // create the material struct
ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_DIRECTIONAL; // make the light type directional light
light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // set the light's colour
light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f); // set the light's direction
d3ddev->SetLight(0, &light); // send the light struct properties to light #0
d3ddev->LightEnable(0, TRUE); // turn on light #0
ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set diffuse colour to white
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set ambient colour to white
d3ddev->SetMaterial(&material); // set the globally-used material to &material
}
I have a VC++ DirectX application that renders a 3D cube, with the camera that can be moved around with W,A,S,D keys and mouse. In the top-left corner, a text shows the current camera coordinates; however, after 10-15 seconds, the allocated RAM and CPU increase to 1.6Gb and almost 50% of the total.. This happens only if I enable the text (even if the movement is still "FLOPS-based", so it's anyway a little "rusty" sometimes, with or without text print).
I guess having the text to be rendered over and over again causes the RAM to fill up because it won't "release" the previously written one..?
Here's my code (a bit long, I included everything 'cuz maybe is something else - however, I don't recommend you to run it since it could, in the worst case, freeze all the RAM up!):
#include <windows.h> // form header
#include <windowsx.h> // form header 2
#include <d3d9.h> // Direct3D9 header
#include <d3dx9.h> // DirectX9 header
#include <conio.h>
#include <dinput.h> // DirectInput header
#include <math.h>
#include <string>
using namespace std;
const double PI = 3.1415926;
// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
// include the DirectInput8 Library file
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
// define the screen resolution
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
LPDIRECT3DINDEXBUFFER9 i_buffer; // the pointer to an index buffer
LPDIRECT3DTEXTURE9 texture; // declare a texture
LPDIRECT3DTEXTURE9 bump;
LPDIRECTINPUT8 din; // the pointer to our DirectInput interface
LPDIRECTINPUTDEVICE8 dinkeyboard; // the pointer to the keyboard device
LPDIRECTINPUTDEVICE8 dinmouse; // the pointer to the mouse device
BYTE keystate[256]; // the storage for the key-information
DIMOUSESTATE mousestate; // the storage for the mouse-information
ID3DXFont *dxfont;
VOID* pVoid; // a void pointer
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphics(void); // 3D declarations
void init_light(void); // sets up the light and the material
void initDInput(HINSTANCE hInstance, HWND hWnd); // sets up and initializes DirectInput
void detect_input(void); // gets the current input state
void cleanDInput(void); // closes DirectInput and releases memory
void PrintText(char* str, int size, int x, int y, DWORD color);
inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; FLOAT U,V;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our First Direct3D Program",
WS_OVERLAPPEDWINDOW, // non-fullscreen values
0, 0, // the starting x and y positions should be 0
SCREEN_WIDTH, SCREEN_HEIGHT, // set window to new resolution
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
initDInput(hInstance, hWnd); // initialize DirectInput
// enter the main loop:
MSG msg;
while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;
detect_input(); // update the input data before rendering
render_frame();
if(keystate[DIK_ESCAPE] & 0x80)
PostMessage(hWnd, WM_DESTROY, 0, 0);
}
// clean up DirectX and COM
cleanD3D();
cleanDInput(); // release DirectInput
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = TRUE; // program fullscreen, not windowed
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn off the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light
init_graphics(); // call the function to initialize the triangle
init_light(); // call the function to initialize the light and material
d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light
d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); // handle the normal lenght
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); // turn color blending on
// set filters and samples
d3ddev->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 8);
d3ddev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
d3ddev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
d3ddev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
//d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); // set the blending operation
//d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); // set the source blending
//d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); // set the destination blending
D3DXCreateTextureFromFile(d3ddev, // the Direct3D device
L"brick.bmp", // the filename of the texture
&texture); // the address of the texture storage
D3DXCreateTextureFromFile(d3ddev,
L"bump.bmp",
&bump);
//d3ddev->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_XRGB(200, 200, 200));
//d3ddev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3);
//d3ddev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
//d3ddev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
//d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
//d3ddev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
//d3ddev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
//d3ddev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
//d3ddev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TEXTURE);
//d3ddev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
//d3ddev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
d3ddev->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
d3ddev->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
//D3DXAssembleShaderFromFile("shader1.fx", 0 , NULL, &pCode, NULL );
}
// Hypotenuse function
float hypo(float x,float y)
{
float hypo=sqrt(x*x+y*y);
return hypo;
}
// this is the function used to render a single frame
void render_frame(void)
{
// clear the window to a specified color
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(); // begins the 3D scene
d3ddev->SetFVF(CUSTOMFVF); // tells Direct3D what FVF code we are using currently
// set the texture
d3ddev->SetTexture(1, bump);
d3ddev->SetTexture(0, texture);
///////
D3DXMATRIX matTranslateA; // a matrix to store the translation for triangle A
D3DXMATRIX matTranslateB; // a matrix to store the translation for triangle B
D3DXMATRIX matRotate; // a matrix to store the rotation for each triangle
D3DXMATRIX matRotate2;
D3DXMATRIX matScale;
static float index = 0.0f; // left movement (A)
static float index2 = 10.0f; // upward movement (PRIOR)
static float index3 = 18.0f; // backward movement (S) -- 1.0f
static float index4 = 0.0f; // left lookAt
static float index5 = 10.0f; // upward lookAt
static float index6 = 18.0f; // backward lookAt
// camera variables
static float radius = 30.00f;
static float theta = (3.0f * D3DX_PI) / 2.0f;
static float phi = D3DX_PI/2;
float Cx, Cy, Cz;
static int field = 45;
float slide = 0.001f;
static float dist1 = fabs(index-index4);
static float dist2 = fabs(index2-index5);
static float dist3 = fabs(index3-index6);
// player movements
// mouse movements
field -= 0.01 * mousestate.lZ;
theta += slide * mousestate.lX;
phi -= slide * mousestate.lY;
if(phi >= (D3DX_PI/9) * 8)
{
phi = (D3DX_PI/9) * 8;
}
if(phi <= (D3DX_PI/9))
{
phi = (D3DX_PI/9);
}
Cx = radius * cosf(theta) * sinf(phi);
Cy = radius * cosf(phi);
Cz = radius * sinf(theta) * sinf(phi);
float mov = radius*0.012;
float mov1 = Cz*0.012;
float mov2 = -Cx*0.012;
// previous index. values
float indexb = index;
float index3b = index3;
//if (GetAsyncKeyState(VK_SHIFT)) // --- 0.06f/0.03f
//{
if (keystate[DIK_A] & 0x80)
{
index=index-mov1;
index3=index3+mov2;
}
if (keystate[DIK_D] & 0x80)
{
index=index+mov1;
index3=index3-mov2;
}
if (keystate[DIK_PRIOR] & 0x80) // up +
{
index2+=0.22f;
}
if (keystate[DIK_NEXT] & 0x80) // down -
{
index2-=0.22f;
}
if (keystate[DIK_W] & 0x80) // prior
{
index3=index3+mov1;
index=index+mov2;
}
if (keystate[DIK_S] & 0x80) // next
{
index3=index3-mov1;
index=index-mov2;
}
if (keystate[DIK_SPACE] & 0x80) // next
{
// JUMP!!!!!!!!!!!!
}
if (hypo(index-indexb,index3-index3b)>mov)
{
index=indexb+0.7071*(index-indexb);
index3=index3b+0.7071*(index3-index3b);
}
// print a text
char msg1[64], msg2[64], msg3[64];
sprintf (msg1, "X axis: %f", index);
sprintf (msg2, "Z axis: %f", index3);
PrintText(msg1, 20, 30, 30, D3DCOLOR_XRGB(255,255,255));
PrintText(msg2, 20, 30, 60, D3DCOLOR_XRGB(255,255,255));
//}
/*else
if (GetAsyncKeyState(VK_LEFT))
{
index+=0.06f;
index4+=0.06f;
}
if (GetAsyncKeyState(VK_RIGHT))
{
index-=0.06f;
index4-=0.06f;
}
if (GetAsyncKeyState(VK_PRIOR))
{
index2-=0.06f;
index5-=0.06f;
}
if (GetAsyncKeyState(VK_NEXT))
{
index2+=0.06f;
index5+=0.06f;
}
if (GetAsyncKeyState(VK_UP))
{
index3-=0.03f;
index6-=0.06f;
}
if (GetAsyncKeyState(VK_DOWN))
{
index3+=0.03f;
index6+=0.06f;
}*/
// build MULTIPLE matrices to translate the model and one to rotate
D3DXMatrixTranslation(&matTranslateA, 0.0f, 0.0f, 0.0f);
//D3DXMatrixTranslation(&matTranslateB, 0.0f, 0.0f, -3.0f);
D3DXMatrixRotationY(&matRotate, 0.0f); // the front side --- index
D3DXMatrixRotationX(&matRotate2, 0.0f); // index2
D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f); // index3
///////
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (index, index2, index3), // the camera position --- (0.0f, 10.0f, 18.0f)
&D3DXVECTOR3 (index-Cx, index2-Cy, index3+Cz), // the look-at position --- (0.0f, 0.0f, 0.0f)
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(field), // the horizontal field of view
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// tell Direct3D about each world transform, and then draw another triangle
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslateA * matRotate * matRotate2 * matScale));
///////
/// select the vertex and index buffers to use
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetIndices(i_buffer);
// draw the cube
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
d3ddev->EndScene(); // ends the 3D scene
d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen
}
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
v_buffer->Release(); // close and release the vertex buffer
//i_buffer->Release(); // close and release the index buffer
texture->Release(); // close and release the texture
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ -3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, // side 1
{ 3.0f, -3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
{ -3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
{ 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
{ 3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f }, // side 2
{ -3.0f, -3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f },
{ 3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f },
{ -3.0f, 3.0f, -3.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
{ -3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, // side 3
{ -3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
{ 3.0f, 3.0f, -3.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f },
{ 3.0f, 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f },
{ -3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f }, // side 4
{ 3.0f, -3.0f, -3.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f },
{ -3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
{ 3.0f, -3.0f, 3.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f },
{ 3.0f, -3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, // side 5
{ 3.0f, -3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ 3.0f, 3.0f, 3.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ 3.0f, 3.0f, -3.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
{ -3.0f, -3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, // side 6
{ -3.0f, -3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ -3.0f, 3.0f, -3.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ -3.0f, 3.0f, 3.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
// create the indices using an int array
short indices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
// create an index buffer interface called i_buffer
d3ddev->CreateIndexBuffer(36*sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&i_buffer,
NULL);
// lock i_buffer and load the indices into it
i_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
i_buffer->Unlock();
}
// this is the function that sets up the lights and materials
void init_light(void)
{
D3DLIGHT9 light; // create the light struct
D3DMATERIAL9 material; // create the material struct
ZeroMemory(&light, sizeof(light)); // clear out the light struct for use
light.Type = D3DLIGHT_POINT; // make the light type 'point light'
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set the light's color
//light.Specular = D3DXCOLOR(5.5f, 5.5f, 5.5f, 1.0f);
light.Position = D3DXVECTOR3(10.0f, 10.0f, 5.0f);
light.Range = 100.0f;
//light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
light.Attenuation0 = 0.0f; // constant attenuation
light.Attenuation1 = 0.0f; // inverse attenuation
light.Attenuation2 = 0.01f; // square inverse attenuation
d3ddev->SetLight(0, &light); // send the light struct properties to light #0
d3ddev->LightEnable(0, TRUE); // turn on light #0
ZeroMemory(&material, sizeof(D3DMATERIAL9)); // clear out the struct for use
material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set diffuse color
material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // set ambient color
d3ddev->SetMaterial(&material); // set the globably-used material to &material
}
// this is the function that initializes DirectInput
void initDInput(HINSTANCE hInstance, HWND hWnd)
{
// create the DirectInput interface
DirectInput8Create(hInstance, // the handle to the application
DIRECTINPUT_VERSION, // the compatible version
IID_IDirectInput8, // the DirectInput interface version
(void**)&din, // the pointer to the interface
NULL); // COM stuff, so we'll set it to NULL
// create the keyboard device
din->CreateDevice(GUID_SysKeyboard, // the default keyboard ID being used
&dinkeyboard, // the pointer to the device interface
NULL); // COM stuff, so we'll set it to NULL
din->CreateDevice(GUID_SysMouse,
&dinmouse,
NULL);
// set the data format to keyboard format
dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinmouse->SetDataFormat(&c_dfDIMouse);
// set the control you will have over the keyboard
dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
dinmouse->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
}
// this is the function that gets the latest input data
void detect_input(void)
{
// get access if we don't have it already
dinkeyboard->Acquire();
dinmouse->Acquire();
// get the input data
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);
dinmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate);
}
// this is the function that closes DirectInput
void cleanDInput(void)
{
dinkeyboard->Unacquire(); // make sure the keyboard is unacquired
dinmouse->Unacquire(); // make sure the mouse in unacquired
din->Release(); // close DirectInput before exiting
}
void PrintText(char* str, int size, int x, int y, DWORD color)
{
static RECT textbox;
SetRect(&textbox, x, y, SCREEN_WIDTH, SCREEN_HEIGHT);
D3DXCreateFont(d3ddev, // the D3D Device
size, // font height
0, // default font width
FW_NORMAL, // font weight
1, // not using MipLevels
false, // italic font
DEFAULT_CHARSET, // default character set
OUT_DEFAULT_PRECIS, // default OutputPrecision,
DEFAULT_QUALITY, // default Quality
DEFAULT_PITCH | FF_DONTCARE, // default pitch and family
L"Arial", // use Facename Arial
&dxfont); // the font object
dxfont->DrawTextA(NULL,
str,
strlen(str),
&textbox,
DT_LEFT | DT_TOP,
color);
}
Don't create a new font object every time you print text.
As stefan posted, a new font is being created every time the PrintText function is called.
One way to fix this would be to move the D3DXCreateFont function call into the initD3D function, and add a matching dxfont->Release(); to the cleanD3D function.