Simple implementation of DirectX12 magic cube

Simple implementation of DirectX12 magic cube

I've been watching DirectX12 recently. I want to write a little thing to practice my hand. There has always been an idea to make a program to simulate the magic cube. I feel it has nothing to do with DirectX12. Let's be familiar with the source code of DirectX12 3D game development practice.
design sketch:

Press A key to change the rotation plane, B key to select the rotation plane, and T key to rotate. You can reach any order by changing parameters (as long as the computer is good.)
As shown in the figure:

1. Code ideas
For the magic cube of order n, it can be regarded as the composition of n* n* n small cubes, and then rotation is actually the rotation of all small cubes in the rotation plane, so just write a class to generate and operate the small cubes. But when you think about it normally, the magic cube only needs six faces. This is indeed a waste of space and will also affect the rendering efficiency (the impact is indeed great), but it is simpler to consider this. There is no pressure for level 7 and below, and the code is simpler.
No more changes have been made in rendering. In principle, because the position of our cube will change, it should be placed in the upload heap, but this is very troublesome. The ready-made MeshGeometry is directly used in DirectX12 3D game development practice. I directly used an n* n*n MeshGeometry array. Each MeshGeometry is only responsible for drawing one cube. In fact, you can put all the cube data in a MeshGeometry, but it's annoying to modify the index when rendering. It's too lazy. Just switch the MeshGeometry data directly during rendering.
2. Key parts
1. Vertex data

struct Vertex
{
	XMFLOAT3 Pos;
	XMFLOAT4 Color;
};

2. Cube

struct Box_part {
	Vertex v[8];//Eight vertices, but not enough
	Vertex colored_v[36];//Fixed point group that can correctly map colors
	XMFLOAT3 center;//core
};

3. Outer line

struct Line_part {
	Vertex v[8];
};

4.BoxController class (the most critical one)

class BoxController
{
private:
	int m_levels;//Order of Rubik's Cube
	float m_size;//The size of the cube, which is 1 / 2 of the side length of the unit cube
	int m_move_step[3];//Indicates the number of times to select x, y and Z planes
	int m_convert_step;//Indicates the number of times to convert a rotating face
	bool if_transformed = false;
	bool if_inited = false;

	XMFLOAT4 m_colors_line_chosed;
	XMFLOAT4 m_colors_line_unchosed;

	XMFLOAT4 m_offset_color;
	//Because the object has been placed at the origin of the world coordinate system, so
	//The direction of rotation is actually x,y,z, which can be used directly, but it may be simpler and more efficient to calculate the rotation matrix first
	XMMATRIX m_rotation_mat[3];//There are certain requirements for the order here

	XMFLOAT4 m_colors_array[6];//Six faces and six colors

	/*
	*	Where 0 represents the front, 1 represents the back, 2 represents the left, 3 represents the right, 4 represents the top and 5 represents the bottom
	*/
	XMFLOAT4 m_default_color;//Default color for vertices without color

	//Internal method
	void InitBox();//This method has defects, but it can correctly calculate the position of each small square and keep it
	void InitSingleCube(int _i,int _j,int _k);//This method is improved to make each cube in the Box produce the same effect as the magic cube
	void UpdateBoxColor();
	//This function is a little difficult and requires linear interpolation. Let's ignore the linear interpolation for the time being
	void UpdateBoxPosition();//position here refers to box_part

	//data
	Box_part box_array[max_levels][max_levels][max_levels];
	Line_part line_array[max_levels][max_levels][max_levels];

	//Auxiliary method
	XMFLOAT3 add(XMFLOAT3& v1, XMFLOAT3& v2);
	XMFLOAT4 add(XMFLOAT4& v1,XMFLOAT4& v2);
	XMFLOAT3 Muliply(XMFLOAT3& v1,float scale);
public:
	BoxController(int _levels,float _size);
	BoxController();
	void SetColors(XMFLOAT4* _colors_array);
	void MovePlane();
	void ConvertPlane();
	void TransformPlane();//It's spinning
	void Update();//A function to the outside world
	void GetBoxVertex(int _i,int _j,int _k,Vertex* _v);//Returns the vertex information of the specified small box
	void GetLineVertex(int _i,int _j,int _k,Vertex* _v);
	//void GetData(Box_part*** box_array);
	~BoxController();
};

4.BoxController details
(1).BoxController constructor

BoxController::BoxController(int _levels,float _size) {
	m_levels = _levels;
	m_size = _size;
	//Default color
	m_colors_array[0] = XMFLOAT4(Colors::Cyan);
	m_colors_array[1] = XMFLOAT4(Colors::PaleVioletRed);
	m_colors_array[2] = XMFLOAT4(Colors::Green);
	m_colors_array[3] = XMFLOAT4(Colors::DarkOrange);
	m_colors_array[4] = XMFLOAT4(Colors::Lavender);
	m_colors_array[5] = XMFLOAT4(Colors::Lime);
	m_default_color = XMFLOAT4(Colors::Blue);
	//Default state
	m_move_step[0]=0,m_move_step[1]=0,m_move_step[2]=0;
	m_convert_step = 0;
	//Calculate rotation matrix
	m_rotation_mat[0] = XMMatrixRotationX(PI * 0.5f);
	m_rotation_mat[1] = XMMatrixRotationY(PI * 0.5f);
	m_rotation_mat[2] = XMMatrixRotationZ(PI * 0.5f);
	//Set line color
	m_colors_line_chosed = XMFLOAT4(Colors::Red);
	m_colors_line_unchosed = XMFLOAT4(Colors::Blue);
	m_offset_color = XMFLOAT4(0.2f,0.2f,0.2f,0.5f);
	InitBox();
}
BoxController::BoxController() {
	BoxController(3,2);
}

(2).MovePlane() / / used to move the rotation plane

void BoxController::MovePlane() {
	m_move_step[m_convert_step]++;
	if (m_move_step[m_convert_step] > m_levels - 1) {
		m_move_step[m_convert_step] = 0;
	}
}

(3).ConvertPlane() / / used to convert the rotation plane

void BoxController::ConvertPlane() {
	m_convert_step++;
	if (m_convert_step > 2) {
		m_convert_step = 0;
	}
}

(4). Transformarplane() / / rotate

void BoxController::TransformPlane() {
	if_transformed = true;
}

(5). Auxiliary function of vector operation
It seems that these operations are built-in, but I wrote them myself when I didn't find them

XMFLOAT3 BoxController::add(XMFLOAT3& v1, XMFLOAT3& v2) {
	XMFLOAT3 temp = XMFLOAT3(0, 0, 0);
	temp.x = v1.x + v2.x;
	temp.y = v1.y + v2.y;
	temp.z = v1.z + v2.z;
	return temp;
}

XMFLOAT4 BoxController::add(XMFLOAT4& v1, XMFLOAT4& v2) {
	XMFLOAT4 temp = XMFLOAT4(0, 0, 0,0);
	temp.x = v1.x + v2.x;
	temp.y = v1.y + v2.y;
	temp.z = v1.z + v2.z;
	temp.w = v1.w + v2.w;
	return temp;
}

XMFLOAT3 BoxController::Muliply(XMFLOAT3& v1, float scale) {
	XMFLOAT3 temp = XMFLOAT3(0, 0, 0);
	temp.x = v1.x * scale;
	temp.y = v1.y * scale;
	temp.z = v1.z * scale;
	return temp;
}

(5).InitBox() / / initialize cube

void BoxController::InitBox() {
	//box_array = new Box_part**[m_levels];
	//At first, I wanted to allocate memory dynamically, but I didn't bother to do it
	//Calculate the position first
	XMFLOAT3 offset_p = XMFLOAT3(-m_size*m_levels,-m_size*m_levels,-m_size*m_levels);//Calculate the amount of offset from the center origin
	XMFLOAT3 ways[8] = { {-1.0f,-1.0f,-1.0f},{-1.0f,1.0f,-1.0f},{1.0f,1.0f,-1.0f},{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f},{-1.0f,1.0f,1.0f},{1.0f,1.0f,1.0f},{1.0f,-1.0f,1.0f} };
	for (int i = 0; i < 8; i++) {
		ways[i] = Muliply(ways[i],m_size);
	}
	for (int i = 0; i < m_levels; i++) {
		for (int j = 0; j < m_levels; j++) {
			for (int k = 0; k < m_levels; k++) {
				//Calculate the center position
				XMFLOAT3 p = XMFLOAT3(m_size+i*m_size*2,m_size+j*m_size*2,m_size+k*m_size*2);
				p = add(p, offset_p);
				box_array[i][j][k].center = p;
				//Calculate vertices in eight directions
				for (int temp = 0; temp < 8; temp++) {
					box_array[i][j][k].v[temp].Pos = add(p, ways[temp]);
				}
				InitSingleCube(i, j, k);
			}
		}
	}
	//UpdateBoxColor();// You have to have a color rendering effect at the beginning, otherwise you can't see it
	if_inited = true;
}

(6).InitSingleCube() / / one of the most annoying functions..

void BoxController::InitSingleCube(int _i,int _j,int _k) {
	/*
*	Where 0 represents the front, 1 represents the back, 2 represents the left, 3 represents the right, 4 represents the top and 5 represents the bottom
*/
	//The definition here is entirely for the convenience of writing...., Too lazy
#define d1 box_array[_i][_j][_k].colored_v
#define d2 box_array[_i][_j][_k].v
	//in front
	d1[0] = Vertex({ d2[0].Pos,m_colors_array[0] });//Set position and color
	d1[1] = Vertex({ d2[1].Pos,m_colors_array[0] });
	d1[2] = Vertex({ d2[2].Pos,m_colors_array[0] });

	d1[3] = Vertex({ d2[0].Pos,m_colors_array[0] });
	d1[4] = Vertex({ d2[2].Pos,m_colors_array[0] });
	d1[5] = Vertex({ d2[3].Pos,m_colors_array[0] });
	//back
	d1[6] = Vertex({ d2[4].Pos,m_colors_array[1] });
	d1[7] = Vertex({ d2[6].Pos,m_colors_array[1] });
	d1[8] = Vertex({ d2[5].Pos,m_colors_array[1] });

	d1[9] = Vertex({ d2[4].Pos,m_colors_array[1] });
	d1[10] = Vertex({ d2[7].Pos,m_colors_array[1] });
	d1[11] = Vertex({ d2[6].Pos,m_colors_array[1] });
	//left side
	d1[12] = Vertex({ d2[4].Pos,m_colors_array[2] });
	d1[13] = Vertex({ d2[5].Pos,m_colors_array[2] });
	d1[14] = Vertex({ d2[1].Pos,m_colors_array[2] });

	d1[15] = Vertex({ d2[4].Pos,m_colors_array[2] });
	d1[16] = Vertex({ d2[1].Pos,m_colors_array[2] });
	d1[17] = Vertex({ d2[0].Pos,m_colors_array[2] });
	//right
	d1[18] = Vertex({ d2[3].Pos,m_colors_array[3] });
	d1[19] = Vertex({ d2[2].Pos,m_colors_array[3] });
	d1[20] = Vertex({ d2[6].Pos,m_colors_array[3] });

	d1[21] = Vertex({ d2[3].Pos,m_colors_array[3] });
	d1[22] = Vertex({ d2[6].Pos,m_colors_array[3] });
	d1[23] = Vertex({ d2[7].Pos,m_colors_array[3] });
	//above
	d1[24] = Vertex({ d2[1].Pos,m_colors_array[4] });
	d1[25] = Vertex({ d2[5].Pos,m_colors_array[4] });
	d1[26] = Vertex({ d2[6].Pos,m_colors_array[4] });

	d1[27] = Vertex({ d2[1].Pos,m_colors_array[4] });
	d1[28] = Vertex({ d2[6].Pos,m_colors_array[4] });
	d1[29] = Vertex({ d2[2].Pos,m_colors_array[4] });
	//Below
	d1[30] = Vertex({ d2[4].Pos,m_colors_array[5] });
	d1[31] = Vertex({ d2[0].Pos,m_colors_array[5] });
	d1[32] = Vertex({ d2[3].Pos,m_colors_array[5] });

	d1[33] = Vertex({ d2[4].Pos,m_colors_array[5] });
	d1[34] = Vertex({ d2[3].Pos,m_colors_array[5] });
	d1[35] = Vertex({ d2[7].Pos,m_colors_array[5] });

#define i1 line_array[_i][_j][_k].v
	for (int i = 0; i < 8; i++) {
		i1[i] = Vertex({d2[i].Pos,m_colors_line_unchosed});
	}
	//The line is also initialized here

}

(7).UpdateBoxColor() / / change the cube color of the unselected plane to the default color

void BoxController::UpdateBoxColor(){
	for (int i = 0; i < m_levels; i++) {
		for (int j = 0; j < m_levels; j++) {
			for (int k = 0; k < m_levels; k++) {
				for (int temp = 0; temp < 8; temp++) {
					line_array[i][j][k].v[temp].Color = m_colors_line_unchosed;
				}
			}
		}
	 }
}

(8).UpdateBoxPosition() / / the most annoying function has been changed for several days just because of bug s

void BoxController::UpdateBoxPosition() {
	//And I'm going to turn it into a vector to do the calculation, and I'm going to turn it back
	int state = m_move_step[m_convert_step];
	//m_convert_step determines which side (x,y,z)
	//state determines which face of the face (0-m_levles)
	XMVECTOR v;
	UpdateBoxColor();
	float sub = 0.0f;
	const float max_sub = 0.1f;
	//Processing x coordinates
	int num = 0;
#define debugv box_array[i][j][k].center
	OutputDebugString(L"\n");
	if (m_convert_step == 0) {
		for (int i = 0; i < m_levels; i++) {
			for (int j = 0; j < m_levels; j++) {
				for (int k = 0; k < m_levels; k++) {
					//Find these box es
					sub=abs(box_array[i][j][k].center.x-(m_size + (float)state * m_size * 2 - m_size * (float)m_levels));
					if (sub<max_sub) {
						for (int temp = 0; temp < 36; temp++) {
							XMFLOAT3 temp_p = box_array[i][j][k].colored_v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[0]);
							XMStoreFloat3(&box_array[i][j][k].colored_v[temp].Pos, v);
							//box_array[i][j][k].colored_v[temp].Color.x += 0.25f;
							//box_array[i][j][k].colored_v[temp].Color = add(box_array[i][j][k].colored_v[temp].Color ,m_offset_color);
							//UpdateBoxColor(i, j, k);
						}
						for (int temp = 0; temp < 8; temp++) {
							line_array[i][j][k].v[temp].Color = m_colors_line_chosed;
							XMFLOAT3 temp_p = line_array[i][j][k].v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[0]);
							XMStoreFloat3(&line_array[i][j][k].v[temp].Pos, v);
						}
						XMFLOAT3 temp_p = box_array[i][j][k].center;
						v = XMLoadFloat3(&temp_p);
						v = XMVector3TransformCoord(v, m_rotation_mat[0]);
						XMStoreFloat3(&box_array[i][j][k].center, v);
					}
				}
			}
		}
		return;
	}
	if (m_convert_step == 1) {
		for (int i = 0; i < m_levels; i++) {
			for (int j = 0; j < m_levels; j++) {
				for (int k = 0; k < m_levels; k++) {
					//Find these box es
					sub = abs(box_array[i][j][k].center.y - (m_size + (float)state * m_size * 2 - m_size * (float)m_levels));
					if (sub<max_sub) {
						for (int temp = 0; temp < 36; temp++) {
							XMFLOAT3 temp_p = box_array[i][j][k].colored_v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[1]);
							XMStoreFloat3(&box_array[i][j][k].colored_v[temp].Pos, v);
							//box_array[i][j][k].colored_v[temp].Color.x += 0.25f;
							//box_array[i][j][k].colored_v[temp].Color = add(box_array[i][j][k].colored_v[temp].Color, m_offset_color);
							//UpdateBoxColor(i,j,k);
						}
						for (int temp = 0; temp < 8; temp++) {
							line_array[i][j][k].v[temp].Color = m_colors_line_chosed;
							XMFLOAT3 temp_p = line_array[i][j][k].v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[1]);
							XMStoreFloat3(&line_array[i][j][k].v[temp].Pos, v);
						}
						XMFLOAT3 temp_p = box_array[i][j][k].center;
						v = XMLoadFloat3(&temp_p);
						v = XMVector3TransformCoord(v, m_rotation_mat[1]);
						XMStoreFloat3(&box_array[i][j][k].center, v);
					}
				}
			}
		}
		return;
	}
	if (m_convert_step == 2) {
		for (int i = 0; i < m_levels; i++) {
			for (int j = 0; j < m_levels; j++) {
				for (int k = 0; k < m_levels; k++) {
					//Find these box es
					sub = abs(box_array[i][j][k].center.z - (m_size + (float)state * m_size * 2 - m_size * (float)m_levels));
					if (sub<max_sub) {
						for (int temp = 0; temp < 36; temp++) {
							XMFLOAT3 temp_p = box_array[i][j][k].colored_v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[2]);
							XMStoreFloat3(&box_array[i][j][k].colored_v[temp].Pos, v);
							//box_array[i][j][k].colored_v[temp].Color.x += 0.25f;
							//box_array[i][j][k].colored_v[temp].Color = add(box_array[i][j][k].colored_v[temp].Color, m_offset_color);
							//UpdateBoxColor(i, j, k);
						}
						for (int temp = 0; temp < 8; temp++) {
							line_array[i][j][k].v[temp].Color = m_colors_line_chosed;
							XMFLOAT3 temp_p = line_array[i][j][k].v[temp].Pos;
							v = XMLoadFloat3(&temp_p);
							v = XMVector3TransformCoord(v, m_rotation_mat[2]);
							XMStoreFloat3(&line_array[i][j][k].v[temp].Pos, v);
						}
						XMFLOAT3 temp_p = box_array[i][j][k].center;
						v = XMLoadFloat3(&temp_p);
						v = XMVector3TransformCoord(v, m_rotation_mat[2]);
						XMStoreFloat3(&box_array[i][j][k].center, v);
					}
				}
			}
		}
		return;
	}
}

(9). Update and get vertex data

void BoxController::Update() {
	if (!if_inited) return;
	//Rotate as you rotate
	if (if_transformed == true ) {
		UpdateBoxPosition();
		if_transformed = false;
	}
}

void BoxController::GetBoxVertex(int _i, int _j, int _k, Vertex* _v) {
	Box_part temp_box_part = box_array[_i][_j][_k];
	for (int i = 0; i < 36; i++) {
		*(_v + i) = Vertex(
			{ XMFLOAT3(temp_box_part.colored_v[i].Pos.x,temp_box_part.colored_v[i].Pos.y,temp_box_part.colored_v[i].Pos.z),
				temp_box_part.colored_v[i].Color }
		);
	}
}

void BoxController::GetLineVertex(int _i, int _j, int _k, Vertex* _v) {
	Line_part temp_line_part = line_array[_i][_j][_k];
	for (int i = 0; i < 8; i++) {
		*(_v + i) = Vertex({temp_line_part.v[i].Pos,temp_line_part.v[i].Color});
	}
}

3. The next step is rendering
Magic changed BoxApp


void BoxApp::BuildBoxes() {
    int p = 0;
    //mBoxGeo = std::make_unique<MeshGeometry>(m_boxes_geo[p]);
    for (int i = 0; i < m_levels; i++) {
        for (int j = 0; j < m_levels; j++) {
            for (int k = 0; k < m_levels; k++) {
                mBoxGeo[p] = std::make_unique<MeshGeometry>();
                mLineGeo[p] = std::make_unique<MeshGeometry>();
                BuildBoxGeometry(i, j, k,p);
                p++;
            }
        }
    }
}
//This is equivalent to one Update for each Update
void BoxApp::BuildBoxGeometry(int _i,int _j,int _k,int _p)
{
    //Some of the original notes have been omitted
    ObjectVertexs v;
    //vertex data 
    m_boxes_controller->GetBoxVertex(_i, _j, _k, v.v);
    std::array<Vertex, 36> vertices;
    for (int i = 0; i < 36; i++) {
        vertices[i] = v.v[i];
    }
    //m_boxes_vertex_ub[_i][_j][_k]->CopyData(0,v);
    /*
    std::array<Vertex, 8> vertices =
    {
        Vertex({ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4(Colors::White) }),
		Vertex({ XMFLOAT3(-1.0f, +1.0f, -1.0f), XMFLOAT4(Colors::Black) }),
		Vertex({ XMFLOAT3(+1.0f, +1.0f, -1.0f), XMFLOAT4(Colors::Red) }),
		Vertex({ XMFLOAT3(+1.0f, -1.0f, -1.0f), XMFLOAT4(Colors::Green) }),
		Vertex({ XMFLOAT3(-1.0f, -1.0f, +1.0f), XMFLOAT4(Colors::Blue) }),
		Vertex({ XMFLOAT3(-1.0f, +1.0f, +1.0f), XMFLOAT4(Colors::Yellow) }),
		Vertex({ XMFLOAT3(+1.0f, +1.0f, +1.0f), XMFLOAT4(Colors::Cyan) }),
		Vertex({ XMFLOAT3(+1.0f, -1.0f, +1.0f), XMFLOAT4(Colors::Magenta) })
    };
    */
    /*
	std::array<std::uint16_t, 36> indices =
	{
		// front face
		0, 1, 2,
		0, 2, 3,

		// back face
		4, 6, 5,
		4, 7, 6,

		// left face
		4, 5, 1,
		4, 1, 0,

		// right face
		3, 2, 6,
		3, 6, 7,

		// top face
		1, 5, 6,
		1, 6, 2,

		// bottom face
		4, 0, 3,
		4, 3, 7
	};
    */
    std::array<std::uint16_t, 108> indices;
    for (int i = 0; i < 108; i++) {
        indices[i] = i;
    }
    const UINT vbByteSize = (UINT)vertices.size() * sizeof(Vertex);
	const UINT ibByteSize = (UINT)indices.size() * sizeof(std::uint16_t);

	mBoxGeo[_p]->Name = "boxGeo";

	ThrowIfFailed(D3DCreateBlob(vbByteSize, &mBoxGeo[_p]->VertexBufferCPU));
	CopyMemory(mBoxGeo[_p]->VertexBufferCPU->GetBufferPointer(), vertices.data(), vbByteSize);

	ThrowIfFailed(D3DCreateBlob(ibByteSize, &mBoxGeo[_p]->IndexBufferCPU));
	CopyMemory(mBoxGeo[_p]->IndexBufferCPU->GetBufferPointer(), indices.data(), ibByteSize);

    //These two places have been changed by magic
    mBoxGeo[_p]->VertexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
		mCommandList.Get(), vertices.data(), vbByteSize, mBoxGeo[_p]->VertexBufferUploader);

    //mBoxGeo[_p]->SetVertexBufferView(m_boxes_vertex_ub[_i][_j][_k]->GetGPU_VIRTUAL_ADDRESS());

    mBoxGeo[_p]->IndexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
		mCommandList.Get(), indices.data(), ibByteSize, mBoxGeo[_p]->IndexBufferUploader);

    mBoxGeo[_p]->VertexByteStride = sizeof(Vertex);
    mBoxGeo[_p]->VertexBufferByteSize = vbByteSize;
    mBoxGeo[_p]->IndexFormat = DXGI_FORMAT_R16_UINT;
    mBoxGeo[_p]->IndexBufferByteSize = ibByteSize;

	SubmeshGeometry submesh;
	submesh.IndexCount = (UINT)indices.size();
	submesh.StartIndexLocation = 0;
	submesh.BaseVertexLocation = 0;

    mBoxGeo[_p]->DrawArgs["box"] = submesh;

    //Next, there are lines
        //Some of the original notes have been omitted
    //vertex data 
    m_boxes_controller->GetLineVertex(_i, _j, _k, v.v);
    std::array<Vertex, 8> vertices_line;
    for (int i = 0; i < vertices_line.size(); i++) {
        vertices_line[i] = v.v[i];
    }

    std::array<std::uint16_t, 25> indices_line;
    indices_line = {
        0,1,1,2,2,3,3,0,
        4,5,5,6,6,7,7,4,
        0,4,1,5,2,6,3,7};

    const UINT vbByteSize_line = (UINT)vertices_line.size() * sizeof(Vertex);
    const UINT ibByteSize_line = (UINT)indices_line.size() * sizeof(std::uint16_t);

    mLineGeo[_p]->Name = "boxGeo";

    ThrowIfFailed(D3DCreateBlob(vbByteSize_line, &mLineGeo[_p]->VertexBufferCPU));
    CopyMemory(mLineGeo[_p]->VertexBufferCPU->GetBufferPointer(), vertices_line.data(), vbByteSize_line);

    ThrowIfFailed(D3DCreateBlob(ibByteSize_line, &mLineGeo[_p]->IndexBufferCPU));
    CopyMemory(mLineGeo[_p]->IndexBufferCPU->GetBufferPointer(), indices_line.data(), ibByteSize_line);

    //These two places have been changed by magic
    mLineGeo[_p]->VertexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
        mCommandList.Get(), vertices_line.data(), vbByteSize_line, mLineGeo[_p]->VertexBufferUploader);

    //mLineGeo[_p]->SetVertexBufferView(m_boxes_vertex_ub[_i][_j][_k]->GetGPU_VIRTUAL_ADDRESS());

    mLineGeo[_p]->IndexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
        mCommandList.Get(), indices_line.data(), ibByteSize_line, mLineGeo[_p]->IndexBufferUploader);

    mLineGeo[_p]->VertexByteStride = sizeof(Vertex);
    mLineGeo[_p]->VertexBufferByteSize = vbByteSize_line;
    mLineGeo[_p]->IndexFormat = DXGI_FORMAT_R16_UINT;
    mLineGeo[_p]->IndexBufferByteSize = ibByteSize_line;

    submesh.IndexCount = (UINT)indices_line.size();
    submesh.StartIndexLocation = 0;
    submesh.BaseVertexLocation = 0;

    mLineGeo[_p]->DrawArgs["box"] = submesh;
    
}


update operation

Min here_ Time is to prevent the response from being too fast and press the a key twice

Finally completed, the code is like shit. I'm going to vomit. My ability is limited. After all, I left tears of no technology, but at least the function is completed. Change the code and optimize? That doesn't exist....

Keywords: Game Development DirectX Direct3D

Added by sx on Sun, 30 Jan 2022 01:24:27 +0200