1 /*
2  * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
3  * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
4  */
5 
6 #include "common.h"
7 #include "bgfx_utils.h"
8 #include "imgui/imgui.h"
9 
10 namespace
11 {
12 
13 struct PosVertex
14 {
15 	float m_x;
16 	float m_y;
17 	float m_z;
18 
init__anon6af44e760111::PosVertex19 	static void init()
20 	{
21 		ms_layout
22 			.begin()
23 			.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
24 			.end();
25 	};
26 
27 	static bgfx::VertexLayout ms_layout;
28 };
29 
30 bgfx::VertexLayout PosVertex::ms_layout;
31 
32 struct ColorVertex
33 {
34 	uint32_t m_abgr;
35 
init__anon6af44e760111::ColorVertex36 	static void init()
37 	{
38 		ms_layout
39 			.begin()
40 			.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
41 			.end();
42 	};
43 
44 	static bgfx::VertexLayout ms_layout;
45 };
46 
47 bgfx::VertexLayout ColorVertex::ms_layout;
48 
49 static PosVertex s_cubePosVertices[] =
50 {
51 	{-1.0f,  1.0f,  1.0f },
52 	{ 1.0f,  1.0f,  1.0f },
53 	{-1.0f, -1.0f,  1.0f },
54 	{ 1.0f, -1.0f,  1.0f },
55 	{-1.0f,  1.0f, -1.0f },
56 	{ 1.0f,  1.0f, -1.0f },
57 	{-1.0f, -1.0f, -1.0f },
58 	{ 1.0f, -1.0f, -1.0f },
59 };
60 
61 static ColorVertex s_cubeColorVertices[] =
62 {
63 	{ 0xff000000 },
64 	{ 0xff0000ff },
65 	{ 0xff00ff00 },
66 	{ 0xff00ffff },
67 	{ 0xffff0000 },
68 	{ 0xffff00ff },
69 	{ 0xffffff00 },
70 	{ 0xffffffff },
71 };
72 
73 static const uint16_t s_cubeTriList[] =
74 {
75 	0, 1, 2, // 0
76 	1, 3, 2,
77 	4, 6, 5, // 2
78 	5, 6, 7,
79 	0, 2, 4, // 4
80 	4, 2, 6,
81 	1, 5, 3, // 6
82 	5, 7, 3,
83 	0, 4, 1, // 8
84 	4, 5, 1,
85 	2, 3, 6, // 10
86 	6, 3, 7,
87 };
88 
89 static const uint16_t s_cubeTriStrip[] =
90 {
91 	0, 1, 2,
92 	3,
93 	7,
94 	1,
95 	5,
96 	0,
97 	4,
98 	2,
99 	6,
100 	7,
101 	4,
102 	5,
103 };
104 
105 class ExampleMvs : public entry::AppI
106 {
107 public:
ExampleMvs(const char * _name,const char * _description,const char * _url)108 	ExampleMvs(const char* _name, const char* _description, const char* _url)
109 		: entry::AppI(_name, _description, _url)
110 	{
111 	}
112 
init(int32_t _argc,const char * const * _argv,uint32_t _width,uint32_t _height)113 	void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
114 	{
115 		BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
116 
117 		Args args(_argc, _argv);
118 
119 		m_width  = _width;
120 		m_height = _height;
121 		m_debug  = BGFX_DEBUG_NONE;
122 		m_reset  = BGFX_RESET_VSYNC;
123 
124 		bgfx::Init init;
125 		init.type     = args.m_type;
126 		init.vendorId = args.m_pciId;
127 		init.resolution.width  = m_width;
128 		init.resolution.height = m_height;
129 		init.resolution.reset  = m_reset;
130 		bgfx::init(init);
131 
132 		// Enable debug text.
133 		bgfx::setDebug(m_debug);
134 
135 		// Set view 0 clear state.
136 		bgfx::setViewClear(0
137 				, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
138 				, 0x303030ff
139 				, 1.0f
140 				, 0
141 				);
142 
143 		// Create vertex stream declaration.
144 		PosVertex::init();
145 		ColorVertex::init();
146 
147 		// Create static vertex buffer.
148 		m_vbh[0] = bgfx::createVertexBuffer(
149 				// Static data can be passed with bgfx::makeRef
150 				  bgfx::makeRef(s_cubePosVertices, sizeof(s_cubePosVertices) )
151 				, PosVertex::ms_layout
152 				);
153 
154 		m_vbh[1] = bgfx::createVertexBuffer(
155 				// Static data can be passed with bgfx::makeRef
156 				  bgfx::makeRef(s_cubeColorVertices, sizeof(s_cubeColorVertices) )
157 				, ColorVertex::ms_layout
158 				);
159 
160 		// Create static index buffer.
161 		m_ibh = bgfx::createIndexBuffer(
162 				// Static data can be passed with bgfx::makeRef
163 				bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) )
164 				);
165 
166 		// Create program from shaders.
167 		m_program = loadProgram("vs_cubes", "fs_cubes");
168 
169 		m_timeOffset = bx::getHPCounter();
170 
171 		imguiCreate();
172 	}
173 
shutdown()174 	virtual int shutdown() override
175 	{
176 		imguiDestroy();
177 
178 		// Cleanup.
179 		bgfx::destroy(m_ibh);
180 		bgfx::destroy(m_vbh[0]);
181 		bgfx::destroy(m_vbh[1]);
182 		bgfx::destroy(m_program);
183 
184 		// Shutdown bgfx.
185 		bgfx::shutdown();
186 
187 		return 0;
188 	}
189 
update()190 	bool update() override
191 	{
192 		if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
193 		{
194 			imguiBeginFrame(m_mouseState.m_mx
195 				,  m_mouseState.m_my
196 				, (m_mouseState.m_buttons[entry::MouseButton::Left  ] ? IMGUI_MBUT_LEFT   : 0)
197 				| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT  : 0)
198 				| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
199 				,  m_mouseState.m_mz
200 				, uint16_t(m_width)
201 				, uint16_t(m_height)
202 				);
203 
204 			showExampleDialog(this);
205 
206 			imguiEndFrame();
207 
208 			float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
209 
210 			const bx::Vec3 at  = { 0.0f, 0.0f,   0.0f };
211 			const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
212 
213 			// Set view and projection matrix for view 0.
214 			{
215 				float view[16];
216 				bx::mtxLookAt(view, eye, at);
217 
218 				float proj[16];
219 				bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
220 				bgfx::setViewTransform(0, view, proj);
221 
222 				// Set view 0 default viewport.
223 				bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
224 			}
225 
226 			// This dummy draw call is here to make sure that view 0 is cleared
227 			// if no other draw calls are submitted to view 0.
228 			bgfx::touch(0);
229 
230 			// Submit 11x11 cubes.
231 			for (uint32_t yy = 0; yy < 11; ++yy)
232 			{
233 				for (uint32_t xx = 0; xx < 11; ++xx)
234 				{
235 					float mtx[16];
236 					bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
237 					mtx[12] = -15.0f + float(xx)*3.0f;
238 					mtx[13] = -15.0f + float(yy)*3.0f;
239 					mtx[14] = 0.0f;
240 
241 					// Set model matrix for rendering.
242 					bgfx::setTransform(mtx);
243 
244 					// Set vertex and index buffer.
245 					bgfx::setVertexBuffer(0, m_vbh[0]);
246 					bgfx::setVertexBuffer(1, m_vbh[1]);
247 					bgfx::setIndexBuffer(m_ibh);
248 
249 					// Set render states.
250 					bgfx::setState(0
251 						| BGFX_STATE_DEFAULT
252 						| BGFX_STATE_PT_TRISTRIP
253 						);
254 
255 					// Submit primitive for rendering to view 0.
256 					bgfx::submit(0, m_program);
257 				}
258 			}
259 
260 			// Advance to next frame. Rendering thread will be kicked to
261 			// process submitted rendering primitives.
262 			bgfx::frame();
263 
264 			return true;
265 		}
266 
267 		return false;
268 	}
269 
270 	entry::MouseState m_mouseState;
271 
272 	uint32_t m_width;
273 	uint32_t m_height;
274 	uint32_t m_debug;
275 	uint32_t m_reset;
276 	bgfx::VertexBufferHandle m_vbh[2];
277 	bgfx::IndexBufferHandle m_ibh;
278 	bgfx::ProgramHandle m_program;
279 	int64_t m_timeOffset;
280 };
281 
282 } // namespace
283 
284 ENTRY_IMPLEMENT_MAIN(
285 	  ExampleMvs
286 	, "34-mvs"
287 	, "Multiple vertex streams."
288 	, "https://bkaradzic.github.io/bgfx/examples.html#mvs"
289 	);
290