add create vertex buffer for a triangle
This commit is contained in:
@@ -194,6 +194,7 @@
|
||||
<ClInclude Include="Mesh.h" />
|
||||
<ClInclude Include="Time.h" />
|
||||
<ClInclude Include="Utilities.h" />
|
||||
<ClInclude Include="Vertex.h" />
|
||||
<ClInclude Include="Window.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
||||
@@ -68,5 +68,8 @@
|
||||
<ClInclude Include="Window.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Vertex.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
+2
-11
@@ -1,6 +1,5 @@
|
||||
#include "Direct3D.h"
|
||||
#include "Utilities.h"
|
||||
#include <random>
|
||||
|
||||
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
|
||||
{
|
||||
@@ -49,23 +48,15 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Direct3D::beginScene()
|
||||
void Direct3D::beginScene(BYTE red, BYTE green, BYTE blue)
|
||||
{
|
||||
_pD3DDevice->BeginScene(); // init a frame, after this call we are allowed to do draw calls
|
||||
|
||||
// change background color
|
||||
static std::default_random_engine e;
|
||||
static std::uniform_int_distribution<int> d(0, 255);
|
||||
|
||||
_pD3DDevice->Clear(
|
||||
0, nullptr, // areas to clear, null -> everything
|
||||
D3DCLEAR_TARGET, // what to clear, bitmask
|
||||
|
||||
// how to clean
|
||||
//D3DCOLOR_XRGB(d(e), d(e), d(e)), //random color
|
||||
//D3DCOLOR_XRGB(255, 0, 0), // red color
|
||||
D3DCOLOR_XRGB(0, 0, 0), // black color
|
||||
1.0f, 0xFFFFFFFF
|
||||
D3DCOLOR_XRGB(red, green, blue), 1.0f, 0xFFFFFFFF // how to clean
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ class Direct3D
|
||||
{
|
||||
public:
|
||||
INT init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen);
|
||||
void beginScene();
|
||||
void beginScene(BYTE red, BYTE green, BYTE blue);
|
||||
void endScene();
|
||||
void deInit();
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Mesh.h"
|
||||
#include "Utilities.h"
|
||||
#include "Vertex.h"
|
||||
|
||||
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
@@ -20,6 +21,13 @@ void Mesh::update(FLOAT dt)
|
||||
|
||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
// 1. set mesh data
|
||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
||||
pD3DDevice->SetFVF(FVF);
|
||||
|
||||
// 2. draw call
|
||||
// vertex buffer only
|
||||
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
|
||||
}
|
||||
|
||||
void Mesh::deInit()
|
||||
@@ -30,6 +38,37 @@ void Mesh::deInit()
|
||||
|
||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
// 1. define mesh parameters
|
||||
_vertexCount = 3;
|
||||
_vertexStride = sizeof(Vertex);
|
||||
|
||||
// 2. create vertex buffer object WITHOUT data
|
||||
HRESULT hr = pD3DDevice->CreateVertexBuffer(
|
||||
_vertexCount * _vertexStride, // vertex buffer size in bytes
|
||||
D3DUSAGE_WRITEONLY, // how to access the buffer
|
||||
FVF, // FVF - Flexible Vertex Format
|
||||
D3DPOOL_MANAGED, // video memory management
|
||||
&_pVertexBuffer, // vertex buffer object
|
||||
nullptr // not used anymore
|
||||
);
|
||||
CheckFailed(hr, 300);
|
||||
|
||||
// 3. set vertex data
|
||||
Vertex* pVertices = nullptr;
|
||||
|
||||
hr = _pVertexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pVertices), 0); // transfer vertex buffer from video memory to cpu memory
|
||||
CheckFailed(hr, 310);
|
||||
|
||||
// fill vertex buffer with data
|
||||
pVertices[0] = Vertex(0.0f, 0.5f, 0.0f);
|
||||
pVertices[1] = Vertex(0.5f, -0.5f, 0.0f);
|
||||
pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||
|
||||
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
||||
CheckFailed(hr, 320);
|
||||
|
||||
pVertices = nullptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <d3d9.h>
|
||||
|
||||
#define FVF D3DFVF_XYZ
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
FLOAT x;
|
||||
FLOAT y;
|
||||
FLOAT z;
|
||||
|
||||
Vertex() : x(0.0f), y(0.0f), z(0.0f) {}
|
||||
Vertex(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z) {}
|
||||
};
|
||||
+9
-1
@@ -1,4 +1,5 @@
|
||||
#include <Windows.h>
|
||||
#include <random>
|
||||
#include "Window.h"
|
||||
#include "Direct3D.h"
|
||||
#include "Mesh.h"
|
||||
@@ -19,6 +20,9 @@ int WINAPI WinMain(
|
||||
const UINT width = 640;
|
||||
const UINT height = 480;
|
||||
const BOOL isFullscreen = FALSE;
|
||||
const BYTE red = 255;
|
||||
const BYTE green = 255;
|
||||
const BYTE blue = 255;
|
||||
|
||||
UINT error = 0; // error code for identifying errors
|
||||
|
||||
@@ -67,7 +71,11 @@ int WINAPI WinMain(
|
||||
light.update(time.getDeltaTime());
|
||||
|
||||
// 8.2. render objects
|
||||
d3d.beginScene();
|
||||
static std::default_random_engine e;
|
||||
static std::uniform_int_distribution<int> d(0, 255);
|
||||
|
||||
//d3d.beginScene(d(e), d(e), d(e)); //random color
|
||||
d3d.beginScene(red, green, blue);
|
||||
|
||||
light.render(d3d.getDevice());
|
||||
material.render(d3d.getDevice());
|
||||
|
||||
Reference in New Issue
Block a user