GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
dxlib_util.cpp
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#include "dxlib_util.h"
9
10#include <Dxlib.h>
11
12#include "math_vector3.h"
13#include "graphic_const.h"
14
15
17{
18
19void InitDxlib3DSetting(const bool high_quality)
20{
21 if (high_quality)
22 {
23 SetUseLighting(TRUE);
24 SetLightEnable(TRUE);
25
26 // ライトの設定.
27 ChangeLightTypeDir(ConvertToDxlibVec(Vector3::GetUpVec()));
28 }
29 else
30 {
31 SetUseLighting(FALSE);
32 SetLightEnable(FALSE);
33 }
34}
35
36
38{
39 // Zバッファを有効にする.
40 SetUseZBuffer3D(TRUE);
41
42 // Zバッファへの書き込みを有効にする.
43 SetWriteZBuffer3D(TRUE);
44}
45
46
47void DrawCube3D(const VECTOR& center_pos,
48 const float side_len, const unsigned int color)
49{
50 // 立方体は8つの頂点を持つので,それらの座標を計算する.
51 const std::array<VECTOR, 8> vertex =
52 {
53 VGet(center_pos.x - side_len / 2, center_pos.y - side_len / 2,
54 center_pos.z - side_len / 2),
55 VGet(center_pos.x + side_len / 2, center_pos.y - side_len / 2,
56 center_pos.z - side_len / 2),
57 VGet(center_pos.x + side_len / 2, center_pos.y - side_len / 2,
58 center_pos.z + side_len / 2),
59 VGet(center_pos.x - side_len / 2, center_pos.y - side_len / 2,
60 center_pos.z + side_len / 2),
61 VGet(center_pos.x - side_len / 2, center_pos.y + side_len / 2,
62 center_pos.z - side_len / 2),
63 VGet(center_pos.x + side_len / 2, center_pos.y + side_len / 2,
64 center_pos.z - side_len / 2),
65 VGet(center_pos.x + side_len / 2, center_pos.y + side_len / 2,
66 center_pos.z + side_len / 2),
67 VGet(center_pos.x - side_len / 2, center_pos.y + side_len / 2,
68 center_pos.z + side_len / 2)
69 };
70
71 // 3D描画の関数は3角形を基本単位とするので,
72 // 4角形の面を張りたい場合は,2つの三角形を組み合わせる必要がある.
73 // つまり,6面×2つ=12個の三角形で立方体が描画できる.
74
75 DrawTriangle3D(vertex[0], vertex[1], vertex[2], color, TRUE);
76 DrawTriangle3D(vertex[2], vertex[3], vertex[0], color, TRUE);
77
78 DrawTriangle3D(vertex[4], vertex[5], vertex[6], color, TRUE);
79 DrawTriangle3D(vertex[6], vertex[7], vertex[4], color, TRUE);
80
81 DrawTriangle3D(vertex[4], vertex[7], vertex[0], color, TRUE);
82 DrawTriangle3D(vertex[0], vertex[7], vertex[3], color, TRUE);
83
84 DrawTriangle3D(vertex[1], vertex[2], vertex[5], color, TRUE);
85 DrawTriangle3D(vertex[5], vertex[6], vertex[2], color, TRUE);
86
87 DrawTriangle3D(vertex[0], vertex[1], vertex[5], color, TRUE);
88 DrawTriangle3D(vertex[5], vertex[4], vertex[0], color, TRUE);
89
90 DrawTriangle3D(vertex[2], vertex[3], vertex[7], color, TRUE);
91 DrawTriangle3D(vertex[7], vertex[6], vertex[2], color, TRUE);
92}
93
94
96 const VECTOR& top_pos, const float side_len, const unsigned int color)
97{
98 DrawCube3D(VSub(top_pos, VGet(0, 0, side_len / 2)), side_len, color);
99}
100
101
102void DrawHexagon(const std::array<VECTOR, 6>& vertex, const unsigned int color)
103{
104 // 3D描画の関数は3角形を基本単位とするので,
105 // 6角形の面を張りたい場合は,4つの三角形を組み合わせる必要がある.
106 DrawTriangle3D(vertex[0], vertex[1], vertex[5], color, TRUE);
107 DrawTriangle3D(vertex[1], vertex[2], vertex[4], color, TRUE);
108 DrawTriangle3D(vertex[1], vertex[4], vertex[5], color, TRUE);
109 DrawTriangle3D(vertex[2], vertex[3], vertex[4], color, TRUE);
110}
111
112
113void DrawHexagonalPrism(const std::array<VECTOR, 6>& vertex, const float height,
114 const unsigned int color)
115{
116 // 6角形面の法線方向のベクトルを取得する.
117 // やっている処理としては,頂点0から1へ行くベクトルをv01,
118 // 同様に頂点0から2へ行くベクトルをv02とすると,
119 // v01とv02の外積(Cross)をとると法線方向のベクトルが取得できるため,
120 // これを単位ベクトルに変換(Norm,ノーマライズのこと)し,高さの半分だけ倍にした.
121 const VECTOR center_to_top = VScale(VNorm(VCross(VSub(vertex[0], vertex[1]),
122 VSub(vertex[0], vertex[2]))), height / 2.0f);
123
124 // 上面の頂点.
125 const std::array<VECTOR, 6> vertex_top =
126 {
127 VAdd(vertex[0], center_to_top),
128 VAdd(vertex[1], center_to_top),
129 VAdd(vertex[2], center_to_top),
130 VAdd(vertex[3], center_to_top),
131 VAdd(vertex[4], center_to_top),
132 VAdd(vertex[5], center_to_top)
133 };
134
135 // 底面の頂点.
136 const std::array<VECTOR, 6> vertex_bottom =
137 {
138 VSub(vertex[0], center_to_top),
139 VSub(vertex[1], center_to_top),
140 VSub(vertex[2], center_to_top),
141 VSub(vertex[3], center_to_top),
142 VSub(vertex[4], center_to_top),
143 VSub(vertex[5], center_to_top)
144 };
145
146 DrawHexagon(vertex_top, color); // 上面を描画する.
147 DrawHexagon(vertex_bottom, color); // 底面を描画する.
148
149 // 側面を描画していく.側面は四角形6つで構成されるので,3角形が12個必要になる.
150 for (int i = 0; i < 6; i++)
151 {
152 DrawTriangle3D(vertex_top[i % 6],
153 vertex_top[(i + 1) % 6],
154 vertex_bottom[i % 6],
155 color,
156 TRUE);
157
158 DrawTriangle3D(vertex_top[(i + 1) % 6],
159 vertex_bottom[i % 6],
160 vertex_bottom[(i + 1) % 6],
161 color,
162 TRUE);
163 }
164}
165
166} // namespace designlab::dxlib_util
Dxlibの3D表示を行う処理を書き直した関数をまとめた名前空間.
void DrawCube3DWithTopPos(const VECTOR &top_pos, const float side_len, const unsigned int color)
3D空間に立方体を描画する.立方体の上面の中心の座標から描画する.
void SetZBufferEnable()
デフォルトだと描画処理を書いた順に描画されるが, これをZバッファを使用して奥行きを考慮して描画するようにする. 毎フレーム実行する必要がある.
void InitDxlib3DSetting(const bool high_quality)
3D処理を行う上で必要な初期化処理をまとめたもの.
void DrawHexagon(const std::array< VECTOR, 6 > &vertex, const unsigned int color)
3D空間に六角形を描画する.
VECTOR ConvertToDxlibVec(const Vector3 &vec)
Dxlibの座標を示すVECTORと,このプログラムで使用しているVectorを変換する. ロボット座標系は右手座標系, Dxlibは左手座標系(工学は右手・ゲームライブラリは左手が多い)なのでyを...
Definition dxlib_util.h:35
void DrawHexagonalPrism(const std::array< VECTOR, 6 > &vertex, const float height, const unsigned int color)
3D空間に六角柱を描画する.
void DrawCube3D(const VECTOR &center_pos, const float side_len, const unsigned int color)
3D空間に立方体を描画する.
static constexpr Vector3 GetUpVec() noexcept
上に進む単位ベクトルを返す. 静的な関数なので,Vector3::GetUpVec() と呼び出せる.