sokol_shape.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. #if defined(SOKOL_IMPL) && !defined(SOKOL_SHAPE_IMPL)
  2. #define SOKOL_SHAPE_IMPL
  3. #endif
  4. #ifndef SOKOL_SHAPE_INCLUDED
  5. /*
  6. sokol_shape.h -- create simple primitive shapes for sokol_gfx.h
  7. Project URL: https://github.com/floooh/sokol
  8. Do this:
  9. #define SOKOL_IMPL or
  10. #define SOKOL_SHAPE_IMPL
  11. before you include this file in *one* C or C++ file to create the
  12. implementation.
  13. Include the following headers before including sokol_shape.h:
  14. sokol_gfx.h
  15. ...optionally provide the following macros to override defaults:
  16. SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
  17. SOKOL_SHAPE_API_DECL- public function declaration prefix (default: extern)
  18. SOKOL_API_DECL - same as SOKOL_SHAPE_API_DECL
  19. SOKOL_API_IMPL - public function implementation prefix (default: -)
  20. If sokol_shape.h is compiled as a DLL, define the following before
  21. including the declaration or implementation:
  22. SOKOL_DLL
  23. On Windows, SOKOL_DLL will define SOKOL_SHAPE_API_DECL as __declspec(dllexport)
  24. or __declspec(dllimport) as needed.
  25. FEATURE OVERVIEW
  26. ================
  27. sokol_shape.h creates vertices and indices for simple shapes and
  28. builds structs which can be plugged into sokol-gfx resource
  29. creation functions:
  30. The following shape types are supported:
  31. - plane
  32. - cube
  33. - sphere (with poles, not geodesic)
  34. - cylinder
  35. - torus (donut)
  36. Generated vertices look like this:
  37. typedef struct sshape_vertex_t {
  38. float x, y, z;
  39. uint32_t normal; // packed normal as BYTE4N
  40. uint16_t u, v; // packed uv coords as USHORT2N
  41. uint32_t color; // packed color as UBYTE4N (r,g,b,a);
  42. } sshape_vertex_t;
  43. Indices are generally 16-bits wide (SG_INDEXTYPE_UINT16) and the indices
  44. are written as triangle-lists (SG_PRIMITIVETYPE_TRIANGLES).
  45. EXAMPLES:
  46. =========
  47. Create multiple shapes into the same vertex- and index-buffer and
  48. render with separate draw calls:
  49. https://github.com/floooh/sokol-samples/blob/master/sapp/shapes-sapp.c
  50. Same as the above, but pre-transform shapes and merge them into a single
  51. shape that's rendered with a single draw call.
  52. https://github.com/floooh/sokol-samples/blob/master/sapp/shapes-transform-sapp.c
  53. STEP-BY-STEP:
  54. =============
  55. Setup an sshape_buffer_t struct with pointers to memory buffers where
  56. generated vertices and indices will be written to:
  57. ```c
  58. sshape_vertex_t vertices[512];
  59. uint16_t indices[4096];
  60. sshape_buffer_t buf = {
  61. .vertices = {
  62. .buffer = SSHAPE_RANGE(vertices),
  63. },
  64. .indices = {
  65. .buffer = SSHAPE_RANGE(indices),
  66. }
  67. };
  68. ```
  69. To find out how big those memory buffers must be (in case you want
  70. to allocate dynamically) call the following functions:
  71. ```c
  72. sshape_sizes_t sshape_plane_sizes(uint32_t tiles);
  73. sshape_sizes_t sshape_box_sizes(uint32_t tiles);
  74. sshape_sizes_t sshape_sphere_sizes(uint32_t slices, uint32_t stacks);
  75. sshape_sizes_t sshape_cylinder_sizes(uint32_t slices, uint32_t stacks);
  76. sshape_sizes_t sshape_torus_sizes(uint32_t sides, uint32_t rings);
  77. ```
  78. The returned sshape_sizes_t struct contains vertex- and index-counts
  79. as well as the equivalent buffer sizes in bytes. For instance:
  80. ```c
  81. sshape_sizes_t sizes = sshape_sphere_sizes(36, 12);
  82. uint32_t num_vertices = sizes.vertices.num;
  83. uint32_t num_indices = sizes.indices.num;
  84. uint32_t vertex_buffer_size = sizes.vertices.size;
  85. uint32_t index_buffer_size = sizes.indices.size;
  86. ```
  87. With the sshape_buffer_t struct that was setup earlier, call any
  88. of the shape-builder functions:
  89. ```c
  90. sshape_buffer_t sshape_build_plane(const sshape_buffer_t* buf, const sshape_plane_t* params);
  91. sshape_buffer_t sshape_build_box(const sshape_buffer_t* buf, const sshape_box_t* params);
  92. sshape_buffer_t sshape_build_sphere(const sshape_buffer_t* buf, const sshape_sphere_t* params);
  93. sshape_buffer_t sshape_build_cylinder(const sshape_buffer_t* buf, const sshape_cylinder_t* params);
  94. sshape_buffer_t sshape_build_torus(const sshape_buffer_t* buf, const sshape_torus_t* params);
  95. ```
  96. Note how the sshape_buffer_t struct is both an input value and the
  97. return value. This can be used to append multiple shapes into the
  98. same vertex- and index-buffers (more on this later).
  99. The second argument is a struct which holds creation parameters.
  100. For instance to build a sphere with radius 2, 36 "cake slices" and 12 stacks:
  101. ```c
  102. sshape_buffer_t buf = ...;
  103. buf = sshape_build_sphere(&buf, &(sshape_sphere_t){
  104. .radius = 2.0f,
  105. .slices = 36,
  106. .stacks = 12,
  107. });
  108. ```
  109. If the provided buffers are big enough to hold all generated vertices and
  110. indices, the "valid" field in the result will be true:
  111. ```c
  112. assert(buf.valid);
  113. ```
  114. The shape creation parameters have "useful defaults", refer to the
  115. actual C struct declarations below to look up those defaults.
  116. You can also provide additional creation parameters, like a common vertex
  117. color, a debug-helper to randomize colors, tell the shape builder function
  118. to merge the new shape with the previous shape into the same draw-element-range,
  119. or a 4x4 transform matrix to move, rotate and scale the generated vertices:
  120. ```c
  121. sshape_buffer_t buf = ...;
  122. buf = sshape_build_sphere(&buf, &(sshape_sphere_t){
  123. .radius = 2.0f,
  124. .slices = 36,
  125. .stacks = 12,
  126. // merge with previous shape into a single element-range
  127. .merge = true,
  128. // set vertex color to red+opaque
  129. .color = sshape_color_4f(1.0f, 0.0f, 0.0f, 1.0f),
  130. // set position to y = 2.0
  131. .transform = {
  132. .m = {
  133. { 1.0f, 0.0f, 0.0f, 0.0f },
  134. { 0.0f, 1.0f, 0.0f, 0.0f },
  135. { 0.0f, 0.0f, 1.0f, 0.0f },
  136. { 0.0f, 2.0f, 0.0f, 1.0f },
  137. }
  138. }
  139. });
  140. assert(buf.valid);
  141. ```
  142. The following helper functions can be used to build a packed
  143. color value or to convert from external matrix types:
  144. ```c
  145. uint32_t sshape_color_4f(float r, float g, float b, float a);
  146. uint32_t sshape_color_3f(float r, float g, float b);
  147. uint32_t sshape_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
  148. uint32_t sshape_color_3b(uint8_t r, uint8_t g, uint8_t b);
  149. sshape_mat4_t sshape_mat4(const float m[16]);
  150. sshape_mat4_t sshape_mat4_transpose(const float m[16]);
  151. ```
  152. After the shape builder function has been called, the following functions
  153. are used to extract the build result for plugging into sokol_gfx.h:
  154. ```c
  155. sshape_element_range_t sshape_element_range(const sshape_buffer_t* buf);
  156. sg_buffer_desc sshape_vertex_buffer_desc(const sshape_buffer_t* buf);
  157. sg_buffer_desc sshape_index_buffer_desc(const sshape_buffer_t* buf);
  158. sg_buffer_layout_desc sshape_buffer_layout_desc(void);
  159. sg_vertex_attr_desc sshape_position_attr_desc(void);
  160. sg_vertex_attr_desc sshape_normal_attr_desc(void);
  161. sg_vertex_attr_desc sshape_texcoord_attr_desc(void);
  162. sg_vertex_attr_desc sshape_color_attr_desc(void);
  163. ```
  164. The sshape_element_range_t struct contains the base-index and number of
  165. indices which can be plugged into the sg_draw() call:
  166. ```c
  167. sshape_element_range_t elms = sshape_element_range(&buf);
  168. ...
  169. sg_draw(elms.base_element, elms.num_elements, 1);
  170. ```
  171. To create sokol-gfx vertex- and index-buffers from the generated
  172. shape data:
  173. ```c
  174. // create sokol-gfx vertex buffer
  175. sg_buffer_desc vbuf_desc = sshape_vertex_buffer_desc(&buf);
  176. sg_buffer vbuf = sg_make_buffer(&vbuf_desc);
  177. // create sokol-gfx index buffer
  178. sg_buffer_desc ibuf_desc = sshape_index_buffer_desc(&buf);
  179. sg_buffer ibuf = sg_make_buffer(&ibuf_desc);
  180. ```
  181. The remaining functions are used to populate the vertex-layout item
  182. in sg_pipeline_desc, note that these functions don't depend on the
  183. created geometry, they always return the same result:
  184. ```c
  185. sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
  186. .layout = {
  187. .buffers[0] = sshape_buffer_layout_desc(),
  188. .attrs = {
  189. [0] = sshape_position_attr_desc(),
  190. [1] = ssape_normal_attr_desc(),
  191. [2] = sshape_texcoord_attr_desc(),
  192. [3] = sshape_color_attr_desc()
  193. }
  194. },
  195. ...
  196. });
  197. ```
  198. Note that you don't have to use all generated vertex attributes in the
  199. pipeline's vertex layout, the sg_buffer_layout_desc struct returned
  200. by sshape_buffer_layout_desc() contains the correct vertex stride
  201. to skip vertex components.
  202. WRITING MULTIPLE SHAPES INTO THE SAME BUFFER
  203. ============================================
  204. You can merge multiple shapes into the same vertex- and
  205. index-buffers and either render them as a single shape, or
  206. in separate draw calls.
  207. To build a single shape made of two cubes which can be rendered
  208. in a single draw-call:
  209. ```
  210. sshape_vertex_t vertices[128];
  211. uint16_t indices[16];
  212. sshape_buffer_t buf = {
  213. .vertices.buffer = SSHAPE_RANGE(vertices),
  214. .indices.buffer = SSHAPE_RANGE(indices)
  215. };
  216. // first cube at pos x=-2.0 (with default size of 1x1x1)
  217. buf = sshape_build_cube(&buf, &(sshape_box_t){
  218. .transform = {
  219. .m = {
  220. { 1.0f, 0.0f, 0.0f, 0.0f },
  221. { 0.0f, 1.0f, 0.0f, 0.0f },
  222. { 0.0f, 0.0f, 1.0f, 0.0f },
  223. {-2.0f, 0.0f, 0.0f, 1.0f },
  224. }
  225. }
  226. });
  227. // ...and append another cube at pos pos=+1.0
  228. // NOTE the .merge = true, this tells the shape builder
  229. // function to not advance the current shape start offset
  230. buf = sshape_build_cube(&buf, &(sshape_box_t){
  231. .merge = true,
  232. .transform = {
  233. .m = {
  234. { 1.0f, 0.0f, 0.0f, 0.0f },
  235. { 0.0f, 1.0f, 0.0f, 0.0f },
  236. { 0.0f, 0.0f, 1.0f, 0.0f },
  237. {-2.0f, 0.0f, 0.0f, 1.0f },
  238. }
  239. }
  240. });
  241. assert(buf.valid);
  242. // skipping buffer- and pipeline-creation...
  243. sshape_element_range_t elms = sshape_element_range(&buf);
  244. sg_draw(elms.base_element, elms.num_elements, 1);
  245. ```
  246. To render the two cubes in separate draw-calls, the element-ranges used
  247. in the sg_draw() calls must be captured right after calling the
  248. builder-functions:
  249. ```c
  250. sshape_vertex_t vertices[128];
  251. uint16_t indices[16];
  252. sshape_buffer_t buf = {
  253. .vertices.buffer = SSHAPE_RANGE(vertices),
  254. .indices.buffer = SSHAPE_RANGE(indices)
  255. };
  256. // build a red cube...
  257. buf = sshape_build_cube(&buf, &(sshape_box_t){
  258. .color = sshape_color_3b(255, 0, 0)
  259. });
  260. sshape_element_range_t red_cube = sshape_element_range(&buf);
  261. // append a green cube to the same vertex-/index-buffer:
  262. buf = sshape_build_cube(&bud, &sshape_box_t){
  263. .color = sshape_color_3b(0, 255, 0);
  264. });
  265. sshape_element_range_t green_cube = sshape_element_range(&buf);
  266. // skipping buffer- and pipeline-creation...
  267. sg_draw(red_cube.base_element, red_cube.num_elements, 1);
  268. sg_draw(green_cube.base_element, green_cube.num_elements, 1);
  269. ```
  270. ...that's about all :)
  271. LICENSE
  272. =======
  273. zlib/libpng license
  274. Copyright (c) 2020 Andre Weissflog
  275. This software is provided 'as-is', without any express or implied warranty.
  276. In no event will the authors be held liable for any damages arising from the
  277. use of this software.
  278. Permission is granted to anyone to use this software for any purpose,
  279. including commercial applications, and to alter it and redistribute it
  280. freely, subject to the following restrictions:
  281. 1. The origin of this software must not be misrepresented; you must not
  282. claim that you wrote the original software. If you use this software in a
  283. product, an acknowledgment in the product documentation would be
  284. appreciated but is not required.
  285. 2. Altered source versions must be plainly marked as such, and must not
  286. be misrepresented as being the original software.
  287. 3. This notice may not be removed or altered from any source
  288. distribution.
  289. */
  290. #define SOKOL_SHAPE_INCLUDED
  291. #include <stddef.h> // size_t, offsetof
  292. #include <stdint.h>
  293. #include <stdbool.h>
  294. #if !defined(SOKOL_GFX_INCLUDED)
  295. #error "Please include sokol_gfx.h before sokol_shape.h"
  296. #endif
  297. #if defined(SOKOL_API_DECL) && !defined(SOKOL_SHAPE_API_DECL)
  298. #define SOKOL_SHAPE_API_DECL SOKOL_API_DECL
  299. #endif
  300. #ifndef SOKOL_SHAPE_API_DECL
  301. #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_SHAPE_IMPL)
  302. #define SOKOL_SHAPE_API_DECL __declspec(dllexport)
  303. #elif defined(_WIN32) && defined(SOKOL_DLL)
  304. #define SOKOL_SHAPE_API_DECL __declspec(dllimport)
  305. #else
  306. #define SOKOL_SHAPE_API_DECL extern
  307. #endif
  308. #endif
  309. #ifdef __cplusplus
  310. extern "C" {
  311. #endif
  312. /*
  313. sshape_range is a pointer-size-pair struct used to pass memory
  314. blobs into sokol-shape. When initialized from a value type
  315. (array or struct), use the SSHAPE_RANGE() macro to build
  316. an sshape_range struct.
  317. */
  318. typedef struct sshape_range {
  319. const void* ptr;
  320. size_t size;
  321. } sshape_range;
  322. // disabling this for every includer isn't great, but the warning is also quite pointless
  323. #if defined(_MSC_VER)
  324. #pragma warning(disable:4221) /* /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y' */
  325. #endif
  326. #if defined(__cplusplus)
  327. #define SSHAPE_RANGE(x) sshape_range{ &x, sizeof(x) }
  328. #else
  329. #define SSHAPE_RANGE(x) (sshape_range){ &x, sizeof(x) }
  330. #endif
  331. /* a 4x4 matrix wrapper struct */
  332. typedef struct sshape_mat4_t { float m[4][4]; } sshape_mat4_t;
  333. /* vertex layout of the generated geometry */
  334. typedef struct sshape_vertex_t {
  335. float x, y, z;
  336. uint32_t normal; // packed normal as BYTE4N
  337. uint16_t u, v; // packed uv coords as USHORT2N
  338. uint32_t color; // packed color as UBYTE4N (r,g,b,a);
  339. } sshape_vertex_t;
  340. /* a range of draw-elements (sg_draw(int base_element, int num_element, ...)) */
  341. typedef struct sshape_element_range_t {
  342. int base_element;
  343. int num_elements;
  344. #if defined(SOKOL_ZIG_BINDINGS)
  345. uint32_t __pad[3];
  346. #endif
  347. } sshape_element_range_t;
  348. /* number of elements and byte size of build actions */
  349. typedef struct sshape_sizes_item_t {
  350. uint32_t num; // number of elements
  351. uint32_t size; // the same as size in bytes
  352. #if defined(SOKOL_ZIG_BINDINGS)
  353. uint32_t __pad[3];
  354. #endif
  355. } sshape_sizes_item_t;
  356. typedef struct sshape_sizes_t {
  357. sshape_sizes_item_t vertices;
  358. sshape_sizes_item_t indices;
  359. } sshape_sizes_t;
  360. /* in/out struct to keep track of mesh-build state */
  361. typedef struct sshape_buffer_item_t {
  362. sshape_range buffer; // pointer/size pair of output buffer
  363. size_t data_size; // size in bytes of valid data in buffer
  364. size_t shape_offset; // data offset of the most recent shape
  365. } sshape_buffer_item_t;
  366. typedef struct sshape_buffer_t {
  367. bool valid;
  368. sshape_buffer_item_t vertices;
  369. sshape_buffer_item_t indices;
  370. } sshape_buffer_t;
  371. /* creation parameters for the different shape types */
  372. typedef struct sshape_plane_t {
  373. float width, depth; // default: 1.0
  374. uint16_t tiles; // default: 1
  375. uint32_t color; // default: white
  376. bool random_colors; // default: false
  377. bool merge; // if true merge with previous shape (default: false)
  378. sshape_mat4_t transform; // default: identity matrix
  379. } sshape_plane_t;
  380. typedef struct sshape_box_t {
  381. float width, height, depth; // default: 1.0
  382. uint16_t tiles; // default: 1
  383. uint32_t color; // default: white
  384. bool random_colors; // default: false
  385. bool merge; // if true merge with previous shape (default: false)
  386. sshape_mat4_t transform; // default: identity matrix
  387. } sshape_box_t;
  388. typedef struct sshape_sphere_t {
  389. float radius; // default: 0.5
  390. uint16_t slices; // default: 5
  391. uint16_t stacks; // default: 4
  392. uint32_t color; // default: white
  393. bool random_colors; // default: false
  394. bool merge; // if true merge with previous shape (default: false)
  395. sshape_mat4_t transform; // default: identity matrix
  396. } sshape_sphere_t;
  397. typedef struct sshape_cylinder_t {
  398. float radius; // default: 0.5
  399. float height; // default: 1.0
  400. uint16_t slices; // default: 5
  401. uint16_t stacks; // default: 1
  402. uint32_t color; // default: white
  403. bool random_colors; // default: false
  404. bool merge; // if true merge with previous shape (default: false)
  405. sshape_mat4_t transform; // default: identity matrix
  406. } sshape_cylinder_t;
  407. typedef struct sshape_torus_t {
  408. float radius; // default: 0.5f
  409. float ring_radius; // default: 0.2f
  410. uint16_t sides; // default: 5
  411. uint16_t rings; // default: 5
  412. uint32_t color; // default: white
  413. bool random_colors; // default: false
  414. bool merge; // if true merge with previous shape (default: false)
  415. sshape_mat4_t transform; // default: identity matrix
  416. } sshape_torus_t;
  417. /* shape builder functions */
  418. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_plane(const sshape_buffer_t* buf, const sshape_plane_t* params);
  419. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_box(const sshape_buffer_t* buf, const sshape_box_t* params);
  420. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_sphere(const sshape_buffer_t* buf, const sshape_sphere_t* params);
  421. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_cylinder(const sshape_buffer_t* buf, const sshape_cylinder_t* params);
  422. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_torus(const sshape_buffer_t* buf, const sshape_torus_t* params);
  423. /* query required vertex- and index-buffer sizes in bytes */
  424. SOKOL_SHAPE_API_DECL sshape_sizes_t sshape_plane_sizes(uint32_t tiles);
  425. SOKOL_SHAPE_API_DECL sshape_sizes_t sshape_box_sizes(uint32_t tiles);
  426. SOKOL_SHAPE_API_DECL sshape_sizes_t sshape_sphere_sizes(uint32_t slices, uint32_t stacks);
  427. SOKOL_SHAPE_API_DECL sshape_sizes_t sshape_cylinder_sizes(uint32_t slices, uint32_t stacks);
  428. SOKOL_SHAPE_API_DECL sshape_sizes_t sshape_torus_sizes(uint32_t sides, uint32_t rings);
  429. /* extract sokol-gfx desc structs and primitive ranges from build state */
  430. SOKOL_SHAPE_API_DECL sshape_element_range_t sshape_element_range(const sshape_buffer_t* buf);
  431. SOKOL_SHAPE_API_DECL sg_buffer_desc sshape_vertex_buffer_desc(const sshape_buffer_t* buf);
  432. SOKOL_SHAPE_API_DECL sg_buffer_desc sshape_index_buffer_desc(const sshape_buffer_t* buf);
  433. SOKOL_SHAPE_API_DECL sg_buffer_layout_desc sshape_buffer_layout_desc(void);
  434. SOKOL_SHAPE_API_DECL sg_vertex_attr_desc sshape_position_attr_desc(void);
  435. SOKOL_SHAPE_API_DECL sg_vertex_attr_desc sshape_normal_attr_desc(void);
  436. SOKOL_SHAPE_API_DECL sg_vertex_attr_desc sshape_texcoord_attr_desc(void);
  437. SOKOL_SHAPE_API_DECL sg_vertex_attr_desc sshape_color_attr_desc(void);
  438. /* helper functions to build packed color value from floats or bytes */
  439. SOKOL_SHAPE_API_DECL uint32_t sshape_color_4f(float r, float g, float b, float a);
  440. SOKOL_SHAPE_API_DECL uint32_t sshape_color_3f(float r, float g, float b);
  441. SOKOL_SHAPE_API_DECL uint32_t sshape_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
  442. SOKOL_SHAPE_API_DECL uint32_t sshape_color_3b(uint8_t r, uint8_t g, uint8_t b);
  443. /* adapter function for filling matrix struct from generic float[16] array */
  444. SOKOL_SHAPE_API_DECL sshape_mat4_t sshape_mat4(const float m[16]);
  445. SOKOL_SHAPE_API_DECL sshape_mat4_t sshape_mat4_transpose(const float m[16]);
  446. #ifdef __cplusplus
  447. } // extern "C"
  448. // FIXME: C++ helper functions
  449. #endif
  450. #endif // SOKOL_SHAPE_INCLUDED
  451. /*-- IMPLEMENTATION ----------------------------------------------------------*/
  452. #ifdef SOKOL_SHAPE_IMPL
  453. #define SOKOL_SHAPE_IMPL_INCLUDED (1)
  454. #include <string.h> // memcpy
  455. #include <math.h> // sinf, cosf
  456. #ifdef __clang__
  457. #pragma clang diagnostic push
  458. #pragma clang diagnostic ignored "-Wmissing-field-initializers"
  459. #pragma clang diagnostic ignored "-Wmissing-braces"
  460. #endif
  461. #ifndef SOKOL_API_IMPL
  462. #define SOKOL_API_IMPL
  463. #endif
  464. #ifndef SOKOL_ASSERT
  465. #include <assert.h>
  466. #define SOKOL_ASSERT(c) assert(c)
  467. #endif
  468. #define _sshape_def(val, def) (((val) == 0) ? (def) : (val))
  469. #define _sshape_def_flt(val, def) (((val) == 0.0f) ? (def) : (val))
  470. #define _sshape_white (0xFFFFFFFF)
  471. typedef struct { float x, y, z, w; } _sshape_vec4_t;
  472. typedef struct { float x, y; } _sshape_vec2_t;
  473. static inline float _sshape_clamp(float v) {
  474. if (v < 0.0f) return 0.0f;
  475. else if (v > 1.0f) return 1.0f;
  476. else return v;
  477. }
  478. static inline uint32_t _sshape_pack_ub4_ubyte4n(uint8_t x, uint8_t y, uint8_t z, uint8_t w) {
  479. return (uint32_t)(((uint32_t)w<<24)|((uint32_t)z<<16)|((uint32_t)y<<8)|x);
  480. }
  481. static inline uint32_t _sshape_pack_f4_ubyte4n(float x, float y, float z, float w) {
  482. uint8_t x8 = (uint8_t) (x * 255.0f);
  483. uint8_t y8 = (uint8_t) (y * 255.0f);
  484. uint8_t z8 = (uint8_t) (z * 255.0f);
  485. uint8_t w8 = (uint8_t) (w * 255.0f);
  486. return _sshape_pack_ub4_ubyte4n(x8, y8, z8, w8);
  487. }
  488. static inline uint32_t _sshape_pack_f4_byte4n(float x, float y, float z, float w) {
  489. int8_t x8 = (int8_t) (x * 127.0f);
  490. int8_t y8 = (int8_t) (y * 127.0f);
  491. int8_t z8 = (int8_t) (z * 127.0f);
  492. int8_t w8 = (int8_t) (w * 127.0f);
  493. return _sshape_pack_ub4_ubyte4n((uint8_t)x8, (uint8_t)y8, (uint8_t)z8, (uint8_t)w8);
  494. }
  495. static inline uint16_t _sshape_pack_f_ushortn(float x) {
  496. return (uint16_t) (x * 65535.0f);
  497. }
  498. static inline _sshape_vec4_t _sshape_vec4(float x, float y, float z, float w) {
  499. _sshape_vec4_t v = { x, y, z, w };
  500. return v;
  501. }
  502. static inline _sshape_vec4_t _sshape_vec4_norm(_sshape_vec4_t v) {
  503. float l = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
  504. if (l != 0.0f) {
  505. return _sshape_vec4(v.x/l, v.y/l, v.z/l, v.w/l);
  506. }
  507. else {
  508. return _sshape_vec4(0.0f, 1.0f, 0.0f, 0.0f);
  509. }
  510. }
  511. static inline _sshape_vec2_t _sshape_vec2(float x, float y) {
  512. _sshape_vec2_t v = { x, y };
  513. return v;
  514. }
  515. static bool _sshape_mat4_isnull(const sshape_mat4_t* m) {
  516. for (int y = 0; y < 4; y++) {
  517. for (int x = 0; x < 4; x++) {
  518. if (0.0f != m->m[y][x]) {
  519. return false;
  520. }
  521. }
  522. }
  523. return true;
  524. }
  525. static sshape_mat4_t _sshape_mat4_identity(void) {
  526. sshape_mat4_t m = {
  527. {
  528. { 1.0f, 0.0f, 0.0f, 0.0f },
  529. { 0.0f, 1.0f, 0.0f, 0.0f },
  530. { 0.0f, 0.0f, 1.0f, 0.0f },
  531. { 0.0f, 0.0f, 0.0f, 1.0f }
  532. }
  533. };
  534. return m;
  535. }
  536. static _sshape_vec4_t _sshape_mat4_mul(const sshape_mat4_t* m, _sshape_vec4_t v) {
  537. _sshape_vec4_t res = {
  538. m->m[0][0]*v.x + m->m[1][0]*v.y + m->m[2][0]*v.z + m->m[3][0]*v.w,
  539. m->m[0][1]*v.x + m->m[1][1]*v.y + m->m[2][1]*v.z + m->m[3][1]*v.w,
  540. m->m[0][2]*v.x + m->m[1][2]*v.y + m->m[2][2]*v.z + m->m[3][2]*v.w,
  541. m->m[0][3]*v.x + m->m[1][3]*v.y + m->m[2][3]*v.z + m->m[3][3]*v.w
  542. };
  543. return res;
  544. }
  545. static uint32_t _sshape_plane_num_vertices(uint32_t tiles) {
  546. return (tiles + 1) * (tiles + 1);
  547. }
  548. static uint32_t _sshape_plane_num_indices(uint32_t tiles) {
  549. return tiles * tiles * 2 * 3;
  550. }
  551. static uint32_t _sshape_box_num_vertices(uint32_t tiles) {
  552. return (tiles + 1) * (tiles + 1) * 6;
  553. }
  554. static uint32_t _sshape_box_num_indices(uint32_t tiles) {
  555. return tiles * tiles * 2 * 6 * 3;
  556. }
  557. static uint32_t _sshape_sphere_num_vertices(uint32_t slices, uint32_t stacks) {
  558. return (slices + 1) * (stacks + 1);
  559. }
  560. static uint32_t _sshape_sphere_num_indices(uint32_t slices, uint32_t stacks) {
  561. return ((2 * slices * stacks) - (2 * slices)) * 3;
  562. }
  563. static uint32_t _sshape_cylinder_num_vertices(uint32_t slices, uint32_t stacks) {
  564. return (slices + 1) * (stacks + 5);
  565. }
  566. static uint32_t _sshape_cylinder_num_indices(uint32_t slices, uint32_t stacks) {
  567. return ((2 * slices * stacks) + (2 * slices)) * 3;
  568. }
  569. static uint32_t _sshape_torus_num_vertices(uint32_t sides, uint32_t rings) {
  570. return (sides + 1) * (rings + 1);
  571. }
  572. static uint32_t _sshape_torus_num_indices(uint32_t sides, uint32_t rings) {
  573. return sides * rings * 2 * 3;
  574. }
  575. static bool _sshape_validate_buffer_item(const sshape_buffer_item_t* item, uint32_t build_size) {
  576. if (0 == item->buffer.ptr) {
  577. return false;
  578. }
  579. if (0 == item->buffer.size) {
  580. return false;
  581. }
  582. if ((item->data_size + build_size) > item->buffer.size) {
  583. return false;
  584. }
  585. if (item->shape_offset > item->data_size) {
  586. return false;
  587. }
  588. return true;
  589. }
  590. static bool _sshape_validate_buffer(const sshape_buffer_t* buf, uint32_t num_vertices, uint32_t num_indices) {
  591. if (!_sshape_validate_buffer_item(&buf->vertices, num_vertices * sizeof(sshape_vertex_t))) {
  592. return false;
  593. }
  594. if (!_sshape_validate_buffer_item(&buf->indices, num_indices * sizeof(uint16_t))) {
  595. return false;
  596. }
  597. return true;
  598. }
  599. static void _sshape_advance_offset(sshape_buffer_item_t* item) {
  600. item->shape_offset = item->data_size;
  601. }
  602. static uint16_t _sshape_base_index(const sshape_buffer_t* buf) {
  603. return (uint16_t) (buf->vertices.data_size / sizeof(sshape_vertex_t));
  604. }
  605. static sshape_plane_t _sshape_plane_defaults(const sshape_plane_t* params) {
  606. sshape_plane_t res = *params;
  607. res.width = _sshape_def_flt(res.width, 1.0f);
  608. res.depth = _sshape_def_flt(res.depth, 1.0f);
  609. res.tiles = _sshape_def(res.tiles, 1);
  610. res.color = _sshape_def(res.color, _sshape_white);
  611. res.transform = _sshape_mat4_isnull(&res.transform) ? _sshape_mat4_identity() : res.transform;
  612. return res;
  613. }
  614. static sshape_box_t _sshape_box_defaults(const sshape_box_t* params) {
  615. sshape_box_t res = *params;
  616. res.width = _sshape_def_flt(res.width, 1.0f);
  617. res.height = _sshape_def_flt(res.height, 1.0f);
  618. res.depth = _sshape_def_flt(res.depth, 1.0f);
  619. res.tiles = _sshape_def(res.tiles, 1);
  620. res.color = _sshape_def(res.color, _sshape_white);
  621. res.transform = _sshape_mat4_isnull(&res.transform) ? _sshape_mat4_identity() : res.transform;
  622. return res;
  623. }
  624. static sshape_sphere_t _sshape_sphere_defaults(const sshape_sphere_t* params) {
  625. sshape_sphere_t res = *params;
  626. res.radius = _sshape_def_flt(res.radius, 0.5f);
  627. res.slices = _sshape_def(res.slices, 5);
  628. res.stacks = _sshape_def(res.stacks, 4);
  629. res.color = _sshape_def(res.color, _sshape_white);
  630. res.transform = _sshape_mat4_isnull(&res.transform) ? _sshape_mat4_identity() : res.transform;
  631. return res;
  632. }
  633. static sshape_cylinder_t _sshape_cylinder_defaults(const sshape_cylinder_t* params) {
  634. sshape_cylinder_t res = *params;
  635. res.radius = _sshape_def_flt(res.radius, 0.5f);
  636. res.height = _sshape_def_flt(res.height, 1.0f);
  637. res.slices = _sshape_def(res.slices, 5);
  638. res.stacks = _sshape_def(res.stacks, 1);
  639. res.color = _sshape_def(res.color, _sshape_white);
  640. res.transform = _sshape_mat4_isnull(&res.transform) ? _sshape_mat4_identity() : res.transform;
  641. return res;
  642. }
  643. static sshape_torus_t _sshape_torus_defaults(const sshape_torus_t* params) {
  644. sshape_torus_t res = *params;
  645. res.radius = _sshape_def_flt(res.radius, 0.5f);
  646. res.ring_radius = _sshape_def_flt(res.ring_radius, 0.2f);
  647. res.sides = _sshape_def_flt(res.sides, 5);
  648. res.rings = _sshape_def_flt(res.rings, 5);
  649. res.color = _sshape_def(res.color, _sshape_white);
  650. res.transform = _sshape_mat4_isnull(&res.transform) ? _sshape_mat4_identity() : res.transform;
  651. return res;
  652. }
  653. static void _sshape_add_vertex(sshape_buffer_t* buf, _sshape_vec4_t pos, _sshape_vec4_t norm, _sshape_vec2_t uv, uint32_t color) {
  654. size_t offset = buf->vertices.data_size;
  655. SOKOL_ASSERT((offset + sizeof(sshape_vertex_t)) <= buf->vertices.buffer.size);
  656. buf->vertices.data_size += sizeof(sshape_vertex_t);
  657. sshape_vertex_t* v_ptr = (sshape_vertex_t*) ((uint8_t*)buf->vertices.buffer.ptr + offset);
  658. v_ptr->x = pos.x;
  659. v_ptr->y = pos.y;
  660. v_ptr->z = pos.z;
  661. v_ptr->normal = _sshape_pack_f4_byte4n(norm.x, norm.y, norm.z, norm.w);
  662. v_ptr->u = _sshape_pack_f_ushortn(uv.x);
  663. v_ptr->v = _sshape_pack_f_ushortn(uv.y);
  664. v_ptr->color = color;
  665. }
  666. static void _sshape_add_triangle(sshape_buffer_t* buf, uint16_t i0, uint16_t i1, uint16_t i2) {
  667. size_t offset = buf->indices.data_size;
  668. SOKOL_ASSERT((offset + 3*sizeof(uint16_t)) <= buf->indices.buffer.size);
  669. buf->indices.data_size += 3*sizeof(uint16_t);
  670. uint16_t* i_ptr = (uint16_t*) ((uint8_t*)buf->indices.buffer.ptr + offset);
  671. i_ptr[0] = i0;
  672. i_ptr[1] = i1;
  673. i_ptr[2] = i2;
  674. }
  675. static uint32_t _sshape_rand_color(uint32_t* xorshift_state) {
  676. // xorshift32
  677. uint32_t x = *xorshift_state;
  678. x ^= x<<13;
  679. x ^= x>>17;
  680. x ^= x<<5;
  681. *xorshift_state = x;
  682. // rand => bright color with alpha 1.0
  683. x |= 0xFF000000;
  684. return x;
  685. }
  686. /*=== PUBLIC API FUNCTIONS ===================================================*/
  687. SOKOL_API_IMPL uint32_t sshape_color_4f(float r, float g, float b, float a) {
  688. return _sshape_pack_f4_ubyte4n(_sshape_clamp(r), _sshape_clamp(g), _sshape_clamp(b), _sshape_clamp(a));
  689. }
  690. SOKOL_API_IMPL uint32_t sshape_color_3f(float r, float g, float b) {
  691. return _sshape_pack_f4_ubyte4n(_sshape_clamp(r), _sshape_clamp(g), _sshape_clamp(b), 1.0f);
  692. }
  693. SOKOL_API_IMPL uint32_t sshape_color_4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
  694. return _sshape_pack_ub4_ubyte4n(r, g, b, a);
  695. }
  696. SOKOL_API_IMPL uint32_t sshape_color_3b(uint8_t r, uint8_t g, uint8_t b) {
  697. return _sshape_pack_ub4_ubyte4n(r, g, b, 255);
  698. }
  699. SOKOL_API_IMPL sshape_mat4_t sshape_mat4(const float m[16]) {
  700. sshape_mat4_t res;
  701. memcpy(&res.m[0][0], &m[0], 64);
  702. return res;
  703. }
  704. SOKOL_API_IMPL sshape_mat4_t sshape_mat4_transpose(const float m[16]) {
  705. sshape_mat4_t res;
  706. for (int c = 0; c < 4; c++) {
  707. for (int r = 0; r < 4; r++) {
  708. res.m[r][c] = m[c*4 + r];
  709. }
  710. }
  711. return res;
  712. }
  713. SOKOL_API_IMPL sshape_sizes_t sshape_plane_sizes(uint32_t tiles) {
  714. SOKOL_ASSERT(tiles >= 1);
  715. sshape_sizes_t res = { 0 };
  716. res.vertices.num = _sshape_plane_num_vertices(tiles);
  717. res.indices.num = _sshape_plane_num_indices(tiles);
  718. res.vertices.size = res.vertices.num * sizeof(sshape_vertex_t);
  719. res.indices.size = res.indices.num * sizeof(uint16_t);
  720. return res;
  721. }
  722. SOKOL_API_IMPL sshape_sizes_t sshape_box_sizes(uint32_t tiles) {
  723. SOKOL_ASSERT(tiles >= 1);
  724. sshape_sizes_t res = { 0 };
  725. res.vertices.num = _sshape_box_num_vertices(tiles);
  726. res.indices.num = _sshape_box_num_indices(tiles);
  727. res.vertices.size = res.vertices.num * sizeof(sshape_vertex_t);
  728. res.indices.size = res.indices.num * sizeof(uint16_t);
  729. return res;
  730. }
  731. SOKOL_API_IMPL sshape_sizes_t sshape_sphere_sizes(uint32_t slices, uint32_t stacks) {
  732. SOKOL_ASSERT((slices >= 3) && (stacks >= 2));
  733. sshape_sizes_t res = { 0 };
  734. res.vertices.num = _sshape_sphere_num_vertices(slices, stacks);
  735. res.indices.num = _sshape_sphere_num_indices(slices, stacks);
  736. res.vertices.size = res.vertices.num * sizeof(sshape_vertex_t);
  737. res.indices.size = res.indices.num * sizeof(uint16_t);
  738. return res;
  739. }
  740. SOKOL_API_IMPL sshape_sizes_t sshape_cylinder_sizes(uint32_t slices, uint32_t stacks) {
  741. SOKOL_ASSERT((slices >= 3) && (stacks >= 1));
  742. sshape_sizes_t res = { 0 };
  743. res.vertices.num = _sshape_cylinder_num_vertices(slices, stacks);
  744. res.indices.num = _sshape_cylinder_num_indices(slices, stacks);
  745. res.vertices.size = res.vertices.num * sizeof(sshape_vertex_t);
  746. res.indices.size = res.indices.num * sizeof(uint16_t);
  747. return res;
  748. }
  749. SOKOL_API_IMPL sshape_sizes_t sshape_torus_sizes(uint32_t sides, uint32_t rings) {
  750. SOKOL_ASSERT((sides >= 3) && (rings >= 3));
  751. sshape_sizes_t res = { 0 };
  752. res.vertices.num = _sshape_torus_num_vertices(sides, rings);
  753. res.indices.num = _sshape_torus_num_indices(sides, rings);
  754. res.vertices.size = res.vertices.num * sizeof(sshape_vertex_t);
  755. res.indices.size = res.indices.num * sizeof(uint16_t);
  756. return res;
  757. }
  758. /*
  759. Geometry layout for plane (4 tiles):
  760. +--+--+--+--+
  761. |\ |\ |\ |\ |
  762. | \| \| \| \|
  763. +--+--+--+--+ 25 vertices (tiles + 1) * (tiles + 1)
  764. |\ |\ |\ |\ | 32 triangles (tiles + 1) * (tiles + 1) * 2
  765. | \| \| \| \|
  766. +--+--+--+--+
  767. |\ |\ |\ |\ |
  768. | \| \| \| \|
  769. +--+--+--+--+
  770. |\ |\ |\ |\ |
  771. | \| \| \| \|
  772. +--+--+--+--+
  773. */
  774. SOKOL_API_IMPL sshape_buffer_t sshape_build_plane(const sshape_buffer_t* in_buf, const sshape_plane_t* in_params) {
  775. SOKOL_ASSERT(in_buf && in_params);
  776. const sshape_plane_t params = _sshape_plane_defaults(in_params);
  777. const uint32_t num_vertices = _sshape_plane_num_vertices(params.tiles);
  778. const uint32_t num_indices = _sshape_plane_num_indices(params.tiles);
  779. sshape_buffer_t buf = *in_buf;
  780. if (!_sshape_validate_buffer(&buf, num_vertices, num_indices)) {
  781. buf.valid = false;
  782. return buf;
  783. }
  784. buf.valid = true;
  785. const uint16_t start_index = _sshape_base_index(&buf);
  786. if (!params.merge) {
  787. _sshape_advance_offset(&buf.vertices);
  788. _sshape_advance_offset(&buf.indices);
  789. }
  790. // write vertices
  791. uint32_t rand_seed = 0x12345678;
  792. const float x0 = -params.width * 0.5f;
  793. const float z0 = params.depth * 0.5f;
  794. const float dx = params.width / params.tiles;
  795. const float dz = -params.depth / params.tiles;
  796. const float duv = 1.0f / params.tiles;
  797. _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, _sshape_vec4(0.0f, 1.0f, 0.0f, 0.0f)));
  798. for (uint32_t ix = 0; ix <= params.tiles; ix++) {
  799. for (uint32_t iz = 0; iz <= params.tiles; iz++) {
  800. const _sshape_vec4_t pos = _sshape_vec4(x0 + dx*ix, 0.0f, z0 + dz*iz, 1.0f);
  801. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  802. const _sshape_vec2_t uv = _sshape_vec2(duv*ix, duv*iz);
  803. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  804. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  805. }
  806. }
  807. // write indices
  808. for (uint16_t j = 0; j < params.tiles; j++) {
  809. for (uint16_t i = 0; i < params.tiles; i++) {
  810. const uint16_t i0 = start_index + (j * (params.tiles + 1)) + i;
  811. const uint16_t i1 = i0 + 1;
  812. const uint16_t i2 = i0 + params.tiles + 1;
  813. const uint16_t i3 = i2 + 1;
  814. _sshape_add_triangle(&buf, i0, i1, i3);
  815. _sshape_add_triangle(&buf, i0, i3, i2);
  816. }
  817. }
  818. return buf;
  819. }
  820. SOKOL_API_IMPL sshape_buffer_t sshape_build_box(const sshape_buffer_t* in_buf, const sshape_box_t* in_params) {
  821. SOKOL_ASSERT(in_buf && in_params);
  822. const sshape_box_t params = _sshape_box_defaults(in_params);
  823. const uint32_t num_vertices = _sshape_box_num_vertices(params.tiles);
  824. const uint32_t num_indices = _sshape_box_num_indices(params.tiles);
  825. sshape_buffer_t buf = *in_buf;
  826. if (!_sshape_validate_buffer(&buf, num_vertices, num_indices)) {
  827. buf.valid = false;
  828. return buf;
  829. }
  830. buf.valid = true;
  831. const uint16_t start_index = _sshape_base_index(&buf);
  832. if (!params.merge) {
  833. _sshape_advance_offset(&buf.vertices);
  834. _sshape_advance_offset(&buf.indices);
  835. }
  836. // build vertices
  837. uint32_t rand_seed = 0x12345678;
  838. const float x0 = -params.width * 0.5f;
  839. const float x1 = params.width * 0.5f;
  840. const float y0 = -params.height * 0.5f;
  841. const float y1 = params.height * 0.5f;
  842. const float z0 = -params.depth * 0.5f;
  843. const float z1 = params.depth * 0.5f;
  844. const float dx = params.width / params.tiles;
  845. const float dy = params.height / params.tiles;
  846. const float dz = params.depth / params.tiles;
  847. const float duv = 1.0f / params.tiles;
  848. // bottom/top vertices
  849. for (uint32_t top_bottom = 0; top_bottom < 2; top_bottom++) {
  850. _sshape_vec4_t pos = _sshape_vec4(0.0f, (0==top_bottom) ? y0:y1, 0.0f, 1.0f);
  851. const _sshape_vec4_t norm = _sshape_vec4(0.0f, (0==top_bottom) ? -1.0f:1.0f, 0.0f, 0.0f);
  852. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  853. for (uint32_t ix = 0; ix <= params.tiles; ix++) {
  854. pos.x = (0==top_bottom) ? (x0 + dx * ix) : (x1 - dx * ix);
  855. for (uint32_t iz = 0; iz <= params.tiles; iz++) {
  856. pos.z = z0 + dz * iz;
  857. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  858. const _sshape_vec2_t uv = _sshape_vec2(ix * duv, iz * duv);
  859. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  860. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  861. }
  862. }
  863. }
  864. // left/right vertices
  865. for (uint32_t left_right = 0; left_right < 2; left_right++) {
  866. _sshape_vec4_t pos = _sshape_vec4((0==left_right) ? x0:x1, 0.0f, 0.0f, 1.0f);
  867. const _sshape_vec4_t norm = _sshape_vec4((0==left_right) ? -1.0f:1.0f, 0.0f, 0.0f, 0.0f);
  868. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  869. for (uint32_t iy = 0; iy <= params.tiles; iy++) {
  870. pos.y = (0==left_right) ? (y1 - dy * iy) : (y0 + dy * iy);
  871. for (uint32_t iz = 0; iz <= params.tiles; iz++) {
  872. pos.z = z0 + dz * iz;
  873. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  874. const _sshape_vec2_t uv = _sshape_vec2(iy * duv, iz * duv);
  875. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  876. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  877. }
  878. }
  879. }
  880. // front/back vertices
  881. for (uint32_t front_back = 0; front_back < 2; front_back++) {
  882. _sshape_vec4_t pos = _sshape_vec4(0.0f, 0.0f, (0==front_back) ? z0:z1, 1.0f);
  883. const _sshape_vec4_t norm = _sshape_vec4(0.0f, 0.0f, (0==front_back) ? -1.0f:1.0f, 0.0f);
  884. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  885. for (uint32_t ix = 0; ix <= params.tiles; ix++) {
  886. pos.x = (0==front_back) ? (x1 - dx * ix) : (x0 + dx * ix);
  887. for (uint32_t iy = 0; iy <= params.tiles; iy++) {
  888. pos.y = y0 + dy * iy;
  889. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  890. const _sshape_vec2_t uv = _sshape_vec2(ix * duv, iy * duv);
  891. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  892. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  893. }
  894. }
  895. }
  896. // build indices
  897. const uint16_t verts_per_face = (params.tiles + 1) * (params.tiles + 1);
  898. for (uint16_t face = 0; face < 6; face++) {
  899. uint16_t face_start_index = start_index + face * verts_per_face;
  900. for (uint16_t j = 0; j < params.tiles; j++) {
  901. for (uint16_t i = 0; i < params.tiles; i++) {
  902. const uint16_t i0 = face_start_index + (j * (params.tiles + 1)) + i;
  903. const uint16_t i1 = i0 + 1;
  904. const uint16_t i2 = i0 + params.tiles + 1;
  905. const uint16_t i3 = i2 + 1;
  906. _sshape_add_triangle(&buf, i0, i1, i3);
  907. _sshape_add_triangle(&buf, i0, i3, i2);
  908. }
  909. }
  910. }
  911. return buf;
  912. }
  913. /*
  914. Geometry layout for spheres is as follows (for 5 slices, 4 stacks):
  915. + + + + + + north pole
  916. |\ |\ |\ |\ |\
  917. | \| \| \| \| \
  918. +--+--+--+--+--+ 30 vertices (slices + 1) * (stacks + 1)
  919. |\ |\ |\ |\ |\ | 30 triangles (2 * slices * stacks) - (2 * slices)
  920. | \| \| \| \| \| 2 orphaned vertices
  921. +--+--+--+--+--+
  922. |\ |\ |\ |\ |\ |
  923. | \| \| \| \| \|
  924. +--+--+--+--+--+
  925. \ |\ |\ |\ |\ |
  926. \| \| \| \| \|
  927. + + + + + + south pole
  928. */
  929. SOKOL_API_IMPL sshape_buffer_t sshape_build_sphere(const sshape_buffer_t* in_buf, const sshape_sphere_t* in_params) {
  930. SOKOL_ASSERT(in_buf && in_params);
  931. const sshape_sphere_t params = _sshape_sphere_defaults(in_params);
  932. const uint32_t num_vertices = _sshape_sphere_num_vertices(params.slices, params.stacks);
  933. const uint32_t num_indices = _sshape_sphere_num_indices(params.slices, params.stacks);
  934. sshape_buffer_t buf = *in_buf;
  935. if (!_sshape_validate_buffer(&buf, num_vertices, num_indices)) {
  936. buf.valid = false;
  937. return buf;
  938. }
  939. buf.valid = true;
  940. const uint16_t start_index = _sshape_base_index(&buf);
  941. if (!params.merge) {
  942. _sshape_advance_offset(&buf.vertices);
  943. _sshape_advance_offset(&buf.indices);
  944. }
  945. uint32_t rand_seed = 0x12345678;
  946. const float pi = 3.14159265358979323846f;
  947. const float two_pi = 2.0f * pi;
  948. const float du = 1.0f / params.slices;
  949. const float dv = 1.0f / params.stacks;
  950. // generate vertices
  951. for (uint32_t stack = 0; stack <= params.stacks; stack++) {
  952. const float stack_angle = (pi * stack) / params.stacks;
  953. const float sin_stack = sinf(stack_angle);
  954. const float cos_stack = cosf(stack_angle);
  955. for (uint32_t slice = 0; slice <= params.slices; slice++) {
  956. const float slice_angle = (two_pi * slice) / params.slices;
  957. const float sin_slice = sinf(slice_angle);
  958. const float cos_slice = cosf(slice_angle);
  959. const _sshape_vec4_t norm = _sshape_vec4(-sin_slice * sin_stack, cos_stack, cos_slice * sin_stack, 0.0f);
  960. const _sshape_vec4_t pos = _sshape_vec4(norm.x * params.radius, norm.y * params.radius, norm.z * params.radius, 1.0f);
  961. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  962. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  963. const _sshape_vec2_t uv = _sshape_vec2(1.0f - slice * du, 1.0f - stack * dv);
  964. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  965. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  966. }
  967. }
  968. // generate indices
  969. {
  970. // north-pole triangles
  971. const uint16_t row_a = start_index;
  972. const uint16_t row_b = row_a + params.slices + 1;
  973. for (uint16_t slice = 0; slice < params.slices; slice++) {
  974. _sshape_add_triangle(&buf, row_a + slice, row_b + slice, row_b + slice + 1);
  975. }
  976. }
  977. // stack triangles
  978. for (uint16_t stack = 1; stack < params.stacks - 1; stack++) {
  979. const uint16_t row_a = start_index + stack * (params.slices + 1);
  980. const uint16_t row_b = row_a + params.slices + 1;
  981. for (uint16_t slice = 0; slice < params.slices; slice++) {
  982. _sshape_add_triangle(&buf, row_a + slice, row_b + slice + 1, row_a + slice + 1);
  983. _sshape_add_triangle(&buf, row_a + slice, row_b + slice, row_b + slice + 1);
  984. }
  985. }
  986. {
  987. // south-pole triangles
  988. const uint16_t row_a = start_index + (params.stacks - 1) * (params.slices + 1);
  989. const uint16_t row_b = row_a + params.slices + 1;
  990. for (uint16_t slice = 0; slice < params.slices; slice++) {
  991. _sshape_add_triangle(&buf, row_a + slice, row_b + slice + 1, row_a + slice + 1);
  992. }
  993. }
  994. return buf;
  995. }
  996. /*
  997. Geometry for cylinders is as follows (2 stacks, 5 slices):
  998. + + + + + +
  999. |\ |\ |\ |\ |\
  1000. | \| \| \| \| \
  1001. +--+--+--+--+--+
  1002. +--+--+--+--+--+ 42 vertices (2 wasted) (slices + 1) * (stacks + 5)
  1003. |\ |\ |\ |\ |\ | 30 triangles (2 * slices * stacks) + (2 * slices)
  1004. | \| \| \| \| \|
  1005. +--+--+--+--+--+
  1006. |\ |\ |\ |\ |\ |
  1007. | \| \| \| \| \|
  1008. +--+--+--+--+--+
  1009. +--+--+--+--+--+
  1010. \ |\ |\ |\ |\ |
  1011. \| \| \| \| \|
  1012. + + + + + +
  1013. */
  1014. static void _sshape_build_cylinder_cap_pole(sshape_buffer_t* buf, const sshape_cylinder_t* params, float pos_y, float norm_y, float du, float v, uint32_t* rand_seed) {
  1015. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params->transform, _sshape_vec4(0.0f, norm_y, 0.0f, 0.0f)));
  1016. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params->transform, _sshape_vec4(0.0f, pos_y, 0.0f, 1.0f));
  1017. for (uint32_t slice = 0; slice <= params->slices; slice++) {
  1018. const _sshape_vec2_t uv = _sshape_vec2(slice * du, 1.0f - v);
  1019. const uint32_t color = params->random_colors ? _sshape_rand_color(rand_seed) : params->color;
  1020. _sshape_add_vertex(buf, tpos, tnorm, uv, color);
  1021. }
  1022. }
  1023. static void _sshape_build_cylinder_cap_ring(sshape_buffer_t* buf, const sshape_cylinder_t* params, float pos_y, float norm_y, float du, float v, uint32_t* rand_seed) {
  1024. const float two_pi = 2.0f * 3.14159265358979323846f;
  1025. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params->transform, _sshape_vec4(0.0f, norm_y, 0.0f, 0.0f)));
  1026. for (uint32_t slice = 0; slice <= params->slices; slice++) {
  1027. const float slice_angle = (two_pi * slice) / params->slices;
  1028. const float sin_slice = sinf(slice_angle);
  1029. const float cos_slice = cosf(slice_angle);
  1030. const _sshape_vec4_t pos = _sshape_vec4(sin_slice * params->radius, pos_y, cos_slice * params->radius, 1.0f);
  1031. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params->transform, pos);
  1032. const _sshape_vec2_t uv = _sshape_vec2(slice * du, 1.0f - v);
  1033. const uint32_t color = params->random_colors ? _sshape_rand_color(rand_seed) : params->color;
  1034. _sshape_add_vertex(buf, tpos, tnorm, uv, color);
  1035. }
  1036. }
  1037. SOKOL_SHAPE_API_DECL sshape_buffer_t sshape_build_cylinder(const sshape_buffer_t* in_buf, const sshape_cylinder_t* in_params) {
  1038. SOKOL_ASSERT(in_buf && in_params);
  1039. const sshape_cylinder_t params = _sshape_cylinder_defaults(in_params);
  1040. const uint32_t num_vertices = _sshape_cylinder_num_vertices(params.slices, params.stacks);
  1041. const uint32_t num_indices = _sshape_cylinder_num_indices(params.slices, params.stacks);
  1042. sshape_buffer_t buf = *in_buf;
  1043. if (!_sshape_validate_buffer(&buf, num_vertices, num_indices)) {
  1044. buf.valid = false;
  1045. return buf;
  1046. }
  1047. buf.valid = true;
  1048. const uint16_t start_index = _sshape_base_index(&buf);
  1049. if (!params.merge) {
  1050. _sshape_advance_offset(&buf.vertices);
  1051. _sshape_advance_offset(&buf.indices);
  1052. }
  1053. uint32_t rand_seed = 0x12345678;
  1054. const float two_pi = 2.0f * 3.14159265358979323846f;
  1055. const float du = 1.0f / params.slices;
  1056. const float dv = 1.0f / (params.stacks + 2);
  1057. const float y0 = params.height * 0.5f;
  1058. const float y1 = -params.height * 0.5f;
  1059. const float dy = params.height / params.stacks;
  1060. // generate vertices
  1061. _sshape_build_cylinder_cap_pole(&buf, &params, y0, 1.0f, du, 0.0f, &rand_seed);
  1062. _sshape_build_cylinder_cap_ring(&buf, &params, y0, 1.0f, du, dv, &rand_seed);
  1063. for (uint32_t stack = 0; stack <= params.stacks; stack++) {
  1064. const float y = y0 - dy * stack;
  1065. const float v = dv * stack + dv;
  1066. for (uint32_t slice = 0; slice <= params.slices; slice++) {
  1067. const float slice_angle = (two_pi * slice) / params.slices;
  1068. const float sin_slice = sinf(slice_angle);
  1069. const float cos_slice = cosf(slice_angle);
  1070. const _sshape_vec4_t pos = _sshape_vec4(sin_slice * params.radius, y, cos_slice * params.radius, 1.0f);
  1071. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  1072. const _sshape_vec4_t norm = _sshape_vec4(sin_slice, 0.0f, cos_slice, 0.0f);
  1073. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  1074. const _sshape_vec2_t uv = _sshape_vec2(slice * du, 1.0f - v);
  1075. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  1076. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  1077. }
  1078. }
  1079. _sshape_build_cylinder_cap_ring(&buf, &params, y1, -1.0f, du, 1.0f - dv, &rand_seed);
  1080. _sshape_build_cylinder_cap_pole(&buf, &params, y1, -1.0f, du, 1.0f, &rand_seed);
  1081. // generate indices
  1082. {
  1083. // top-cap indices
  1084. const uint16_t row_a = start_index;
  1085. const uint16_t row_b = row_a + params.slices + 1;
  1086. for (uint16_t slice = 0; slice < params.slices; slice++) {
  1087. _sshape_add_triangle(&buf, row_a + slice, row_b + slice + 1, row_b + slice);
  1088. }
  1089. }
  1090. // shaft triangles
  1091. for (uint16_t stack = 0; stack < params.stacks; stack++) {
  1092. const uint16_t row_a = start_index + (stack + 2) * (params.slices + 1);
  1093. const uint16_t row_b = row_a + params.slices + 1;
  1094. for (uint16_t slice = 0; slice < params.slices; slice++) {
  1095. _sshape_add_triangle(&buf, row_a + slice, row_a + slice + 1, row_b + slice + 1);
  1096. _sshape_add_triangle(&buf, row_a + slice, row_b + slice + 1, row_b + slice);
  1097. }
  1098. }
  1099. {
  1100. // bottom-cap indices
  1101. const uint16_t row_a = start_index + (params.stacks + 3) * (params.slices + 1);
  1102. const uint16_t row_b = row_a + params.slices + 1;
  1103. for (uint16_t slice = 0; slice < params.slices; slice++) {
  1104. _sshape_add_triangle(&buf, row_a + slice, row_a + slice + 1, row_b + slice + 1);
  1105. }
  1106. }
  1107. return buf;
  1108. }
  1109. /*
  1110. Geometry layout for torus (sides = 4, rings = 5):
  1111. +--+--+--+--+--+
  1112. |\ |\ |\ |\ |\ |
  1113. | \| \| \| \| \|
  1114. +--+--+--+--+--+ 30 vertices (sides + 1) * (rings + 1)
  1115. |\ |\ |\ |\ |\ | 40 triangles (2 * sides * rings)
  1116. | \| \| \| \| \|
  1117. +--+--+--+--+--+
  1118. |\ |\ |\ |\ |\ |
  1119. | \| \| \| \| \|
  1120. +--+--+--+--+--+
  1121. |\ |\ |\ |\ |\ |
  1122. | \| \| \| \| \|
  1123. +--+--+--+--+--+
  1124. */
  1125. SOKOL_API_IMPL sshape_buffer_t sshape_build_torus(const sshape_buffer_t* in_buf, const sshape_torus_t* in_params) {
  1126. SOKOL_ASSERT(in_buf && in_params);
  1127. const sshape_torus_t params = _sshape_torus_defaults(in_params);
  1128. const uint32_t num_vertices = _sshape_torus_num_vertices(params.sides, params.rings);
  1129. const uint32_t num_indices = _sshape_torus_num_indices(params.sides, params.rings);
  1130. sshape_buffer_t buf = *in_buf;
  1131. if (!_sshape_validate_buffer(&buf, num_vertices, num_indices)) {
  1132. buf.valid = false;
  1133. return buf;
  1134. }
  1135. buf.valid = true;
  1136. const uint16_t start_index = _sshape_base_index(&buf);
  1137. if (!params.merge) {
  1138. _sshape_advance_offset(&buf.vertices);
  1139. _sshape_advance_offset(&buf.indices);
  1140. }
  1141. uint32_t rand_seed = 0x12345678;
  1142. const float two_pi = 2.0f * 3.14159265358979323846f;
  1143. const float dv = 1.0f / params.sides;
  1144. const float du = 1.0f / params.rings;
  1145. // generate vertices
  1146. for (uint32_t side = 0; side <= params.sides; side++) {
  1147. const float phi = (side * two_pi) / params.sides;
  1148. const float sin_phi = sinf(phi);
  1149. const float cos_phi = cosf(phi);
  1150. for (uint32_t ring = 0; ring <= params.rings; ring++) {
  1151. const float theta = (ring * two_pi) / params.rings;
  1152. const float sin_theta = sinf(theta);
  1153. const float cos_theta = cosf(theta);
  1154. // torus surface position
  1155. const float spx = sin_theta * (params.radius - (params.ring_radius * cos_phi));
  1156. const float spy = sin_phi * params.ring_radius;
  1157. const float spz = cos_theta * (params.radius - (params.ring_radius * cos_phi));
  1158. // torus position with ring-radius zero (for normal computation)
  1159. const float ipx = sin_theta * params.radius;
  1160. const float ipy = 0.0f;
  1161. const float ipz = cos_theta * params.radius;
  1162. const _sshape_vec4_t pos = _sshape_vec4(spx, spy, spz, 1.0f);
  1163. const _sshape_vec4_t norm = _sshape_vec4(spx - ipx, spy - ipy, spz - ipz, 0.0f);
  1164. const _sshape_vec4_t tpos = _sshape_mat4_mul(&params.transform, pos);
  1165. const _sshape_vec4_t tnorm = _sshape_vec4_norm(_sshape_mat4_mul(&params.transform, norm));
  1166. const _sshape_vec2_t uv = _sshape_vec2(ring * du, 1.0f - side * dv);
  1167. const uint32_t color = params.random_colors ? _sshape_rand_color(&rand_seed) : params.color;
  1168. _sshape_add_vertex(&buf, tpos, tnorm, uv, color);
  1169. }
  1170. }
  1171. // generate indices
  1172. for (uint16_t side = 0; side < params.sides; side++) {
  1173. const uint16_t row_a = start_index + side * (params.rings + 1);
  1174. const uint16_t row_b = row_a + params.rings + 1;
  1175. for (uint16_t ring = 0; ring < params.rings; ring++) {
  1176. _sshape_add_triangle(&buf, row_a + ring, row_a + ring + 1, row_b + ring + 1);
  1177. _sshape_add_triangle(&buf, row_a + ring, row_b + ring + 1, row_b + ring);
  1178. }
  1179. }
  1180. return buf;
  1181. }
  1182. SOKOL_API_IMPL sg_buffer_desc sshape_vertex_buffer_desc(const sshape_buffer_t* buf) {
  1183. SOKOL_ASSERT(buf && buf->valid);
  1184. sg_buffer_desc desc = { 0 };
  1185. if (buf->valid) {
  1186. desc.type = SG_BUFFERTYPE_VERTEXBUFFER;
  1187. desc.usage = SG_USAGE_IMMUTABLE;
  1188. desc.data.ptr = buf->vertices.buffer.ptr;
  1189. desc.data.size = buf->vertices.data_size;
  1190. }
  1191. return desc;
  1192. }
  1193. SOKOL_API_IMPL sg_buffer_desc sshape_index_buffer_desc(const sshape_buffer_t* buf) {
  1194. SOKOL_ASSERT(buf && buf->valid);
  1195. sg_buffer_desc desc = { 0 };
  1196. if (buf->valid) {
  1197. desc.type = SG_BUFFERTYPE_INDEXBUFFER;
  1198. desc.usage = SG_USAGE_IMMUTABLE;
  1199. desc.data.ptr = buf->indices.buffer.ptr;
  1200. desc.data.size = buf->indices.data_size;
  1201. }
  1202. return desc;
  1203. }
  1204. SOKOL_SHAPE_API_DECL sshape_element_range_t sshape_element_range(const sshape_buffer_t* buf) {
  1205. SOKOL_ASSERT(buf && buf->valid);
  1206. SOKOL_ASSERT(buf->indices.shape_offset < buf->indices.data_size);
  1207. SOKOL_ASSERT(0 == (buf->indices.shape_offset & (sizeof(uint16_t) - 1)));
  1208. SOKOL_ASSERT(0 == (buf->indices.data_size & (sizeof(uint16_t) - 1)));
  1209. sshape_element_range_t range = { 0 };
  1210. range.base_element = (int) (buf->indices.shape_offset / sizeof(uint16_t));
  1211. if (buf->valid) {
  1212. range.num_elements = (int) ((buf->indices.data_size - buf->indices.shape_offset) / sizeof(uint16_t));
  1213. }
  1214. else {
  1215. range.num_elements = 0;
  1216. }
  1217. return range;
  1218. }
  1219. SOKOL_API_IMPL sg_buffer_layout_desc sshape_buffer_layout_desc(void) {
  1220. sg_buffer_layout_desc desc = { 0 };
  1221. desc.stride = sizeof(sshape_vertex_t);
  1222. return desc;
  1223. }
  1224. SOKOL_API_IMPL sg_vertex_attr_desc sshape_position_attr_desc(void) {
  1225. sg_vertex_attr_desc desc = { 0 };
  1226. desc.offset = offsetof(sshape_vertex_t, x);
  1227. desc.format = SG_VERTEXFORMAT_FLOAT3;
  1228. return desc;
  1229. }
  1230. SOKOL_API_IMPL sg_vertex_attr_desc sshape_normal_attr_desc(void) {
  1231. sg_vertex_attr_desc desc = { 0 };
  1232. desc.offset = offsetof(sshape_vertex_t, normal);
  1233. desc.format = SG_VERTEXFORMAT_BYTE4N;
  1234. return desc;
  1235. }
  1236. SOKOL_API_IMPL sg_vertex_attr_desc sshape_texcoord_attr_desc(void) {
  1237. sg_vertex_attr_desc desc = { 0 };
  1238. desc.offset = offsetof(sshape_vertex_t, u);
  1239. desc.format = SG_VERTEXFORMAT_USHORT2N;
  1240. return desc;
  1241. }
  1242. SOKOL_API_IMPL sg_vertex_attr_desc sshape_color_attr_desc(void) {
  1243. sg_vertex_attr_desc desc = { 0 };
  1244. desc.offset = offsetof(sshape_vertex_t, color);
  1245. desc.format = SG_VERTEXFORMAT_UBYTE4N;
  1246. return desc;
  1247. }
  1248. #ifdef __clang__
  1249. #pragma clang diagnostic pop
  1250. #endif
  1251. #endif // SOKOL_SHAPE_IMPL