Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3803 lines
157 KiB

  1. // stb_voxel_render.h - v0.85 - Sean Barrett, 2015 - public domain
  2. //
  3. // This library helps render large-scale "voxel" worlds for games,
  4. // in this case, one with blocks that can have textures and that
  5. // can also be a few shapes other than cubes.
  6. //
  7. // Video introduction:
  8. // http://www.youtube.com/watch?v=2vnTtiLrV1w
  9. //
  10. // Minecraft-viewer sample app (not very simple though):
  11. // http://github.com/nothings/stb/tree/master/tests/caveview
  12. //
  13. // It works by creating triangle meshes. The library includes
  14. //
  15. // - converter from dense 3D arrays of block info to vertex mesh
  16. // - vertex & fragment shaders for the vertex mesh
  17. // - assistance in setting up shader state
  18. //
  19. // For portability, none of the library code actually accesses
  20. // the 3D graphics API. (At the moment, it's not actually portable
  21. // since the shaders are GLSL only, but patches are welcome.)
  22. //
  23. // You have to do all the caching and tracking of vertex buffers
  24. // yourself. However, you could also try making a game with
  25. // a small enough world that it's fully loaded rather than
  26. // streaming. Currently the preferred vertex format is 20 bytes
  27. // per quad. There are designs to allow much more compact formats
  28. // with a slight reduction in shader features, but no roadmap
  29. // for actually implementing them.
  30. //
  31. //
  32. // USAGE
  33. //
  34. // #define the symbol STB_VOXEL_RENDER_IMPLEMENTATION in *one*
  35. // C/C++ file before the #include of this file; the implementation
  36. // will be generated in that file.
  37. //
  38. // If you define the symbols STB_VOXEL_RENDER_STATIC, then the
  39. // implementation will be private to that file.
  40. //
  41. //
  42. // FEATURES
  43. //
  44. // - you can choose textured blocks with the features below,
  45. // or colored voxels with 2^24 colors and no textures.
  46. //
  47. // - voxels are mostly just cubes, but there's support for
  48. // half-height cubes and diagonal slopes, half-height
  49. // diagonals, and even odder shapes especially for doing
  50. // more-continuous "ground".
  51. //
  52. // - texture coordinates are projections along one of the major
  53. // axes, with the per-texture scaling.
  54. //
  55. // - a number of aspects of the shader and the vertex format
  56. // are configurable; the library generally takes care of
  57. // coordinating the vertex format with the mesh for you.
  58. //
  59. //
  60. // FEATURES (SHADER PERSPECTIVE)
  61. //
  62. // - vertices aligned on integer lattice, z on multiples of 0.5
  63. // - per-vertex "lighting" or "ambient occlusion" value (6 bits)
  64. // - per-vertex texture crossfade (3 bits)
  65. //
  66. // - per-face texture #1 id (8-bit index into array texture)
  67. // - per-face texture #2 id (8-bit index into second array texture)
  68. // - per-face color (6-bit palette index, 2 bits of per-texture boolean enable)
  69. // - per-face 5-bit normal for lighting calculations & texture coord computation
  70. // - per-face 2-bit texture matrix rotation to rotate faces
  71. //
  72. // - indexed-by-texture-id scale factor (separate for texture #1 and texture #2)
  73. // - indexed-by-texture-#2-id blend mode (alpha composite or modulate/multiply);
  74. // the first is good for decals, the second for detail textures, "light maps",
  75. // etc; both modes are controlled by texture #2's alpha, scaled by the
  76. // per-vertex texture crossfade and the per-face color (if enabled on texture #2);
  77. // modulate/multiply multiplies by an extra factor of 2.0 so that if you
  78. // make detail maps whose average brightness is 0.5 everything works nicely.
  79. //
  80. // - ambient lighting: half-lambert directional plus constant, all scaled by vertex ao
  81. // - face can be fullbright (emissive), controlled by per-face color
  82. // - installable lighting, with default single-point-light
  83. // - installable fog, with default hacked smoothstep
  84. //
  85. // Note that all the variations of lighting selection and texture
  86. // blending are run-time conditions in the shader, so they can be
  87. // intermixed in a single mesh.
  88. //
  89. //
  90. // INTEGRATION ARC
  91. //
  92. // The way to get this library to work from scratch is to do the following:
  93. //
  94. // Step 1. define STBVOX_CONFIG_MODE to 0
  95. //
  96. // This mode uses only vertex attributes and uniforms, and is easiest
  97. // to get working. It requires 32 bytes per quad and limits the
  98. // size of some tables to avoid hitting uniform limits.
  99. //
  100. // Step 2. define STBVOX_CONFIG_MODE to 1
  101. //
  102. // This requires using a texture buffer to store the quad data,
  103. // reducing the size to 20 bytes per quad.
  104. //
  105. // Step 3: define STBVOX_CONFIG_PREFER_TEXBUFFER
  106. //
  107. // This causes some uniforms to be stored as texture buffers
  108. // instead. This increases the size of some of those tables,
  109. // and avoids a potential slow path (gathering non-uniform
  110. // data from uniforms) on some hardware.
  111. //
  112. // In the future I might add additional modes that have significantly
  113. // smaller meshes but reduce features, down as small as 6 bytes per quad.
  114. // See elsewhere in this file for a table of candidate modes. Switching
  115. // to a mode will require changing some of your mesh creation code, but
  116. // everything else should be seamless. (And I'd like to change the API
  117. // so that mesh creation is data-driven the way the uniforms are, and
  118. // then you wouldn't even have to change anything but the mode number.)
  119. //
  120. //
  121. // IMPROVEMENTS FOR SHIP-WORTHY PROGRAMS USING THIS LIBRARY
  122. //
  123. // I currently tolerate a certain level of "bugginess" in this library.
  124. //
  125. // I'm referring to things which look a little wrong (as long as they
  126. // don't cause holes or cracks in the output meshes), or things which
  127. // do not produce as optimal a mesh as possible. Notable examples:
  128. //
  129. // - incorrect lighting on slopes
  130. // - inefficient meshes for vheight blocks
  131. //
  132. // I am willing to do the work to improve these things if someone is
  133. // going to ship a substantial program that would be improved by them.
  134. // (It need not be commercial, nor need it be a game.) I just didn't
  135. // want to do the work up front if it might never be leveraged. So just
  136. // submit a bug report as usual (github is preferred), but add a note
  137. // that this is for a thing that is really going to ship. (That means
  138. // you need to be far enough into the project that it's clear you're
  139. // committed to it; not during early exploratory development.)
  140. //
  141. //
  142. // VOXEL MESH API
  143. //
  144. // Context
  145. //
  146. // To understand the API, make sure you first understand the feature set
  147. // listed above.
  148. //
  149. // Because the vertices are compact, they have very limited spatial
  150. // precision. Thus a single mesh can only contain the data for a limited
  151. // area. To make very large voxel maps, you'll need to build multiple
  152. // vertex buffers. (But you want this anyway for frustum culling.)
  153. //
  154. // Each generated mesh has three components:
  155. // - vertex data (vertex buffer)
  156. // - face data (optional, stored in texture buffer)
  157. // - mesh transform (uniforms)
  158. //
  159. // Once you've generated the mesh with this library, it's up to you
  160. // to upload it to the GPU, to keep track of the state, and to render
  161. // it.
  162. //
  163. // Concept
  164. //
  165. // The basic design is that you pass in one or more 3D arrays; each array
  166. // is (typically) one-byte-per-voxel and contains information about one
  167. // or more properties of some particular voxel property.
  168. //
  169. // Because there is so much per-vertex and per-face data possible
  170. // in the output, and each voxel can have 6 faces and 8 vertices, it
  171. // would require an very large data structure to describe all
  172. // of the possibilities, and this would cause the mesh-creation
  173. // process to be slow. Instead, the API provides multiple ways
  174. // to express each property, some more compact, others less so;
  175. // each such way has some limitations on what it can express.
  176. //
  177. // Note that there are so many paths and combinations, not all of them
  178. // have been tested. Just report bugs and I'll fix 'em.
  179. //
  180. // Details
  181. //
  182. // See the API documentation in the header-file section.
  183. //
  184. //
  185. // CONTRIBUTORS
  186. //
  187. // Features Porting Bugfixes & Warnings
  188. // Sean Barrett github:r-leyh Jesus Fernandez
  189. // Miguel Lechon github:Arbeiterunfallversicherungsgesetz
  190. // Thomas Frase James Hofmann
  191. // Stephen Olsen github:guitarfreak
  192. //
  193. // VERSION HISTORY
  194. //
  195. // 0.85 (2017-03-03) add block_selector (by guitarfreak)
  196. // 0.84 (2016-04-02) fix GLSL syntax error on glModelView path
  197. // 0.83 (2015-09-13) remove non-constant struct initializers to support more compilers
  198. // 0.82 (2015-08-01) added input.packed_compact to store rot, vheight & texlerp efficiently
  199. // fix broken tex_overlay2
  200. // 0.81 (2015-05-28) fix broken STBVOX_CONFIG_OPTIMIZED_VHEIGHT
  201. // 0.80 (2015-04-11) fix broken STBVOX_CONFIG_ROTATION_IN_LIGHTING refactoring
  202. // change STBVOX_MAKE_LIGHTING to STBVOX_MAKE_LIGHTING_EXT so
  203. // that header defs don't need to see config vars
  204. // add STBVOX_CONFIG_VHEIGHT_IN_LIGHTING and other vheight fixes
  205. // added documentation for vheight ("weird slopes")
  206. // 0.79 (2015-04-01) fix the missing types from 0.78; fix string constants being const
  207. // 0.78 (2015-04-02) bad "#else", compile as C++
  208. // 0.77 (2015-04-01) documentation tweaks, rename config var to STB_VOXEL_RENDER_STATIC
  209. // 0.76 (2015-04-01) typos, signed/unsigned shader issue, more documentation
  210. // 0.75 (2015-04-01) initial release
  211. //
  212. //
  213. // HISTORICAL FOUNDATION
  214. //
  215. // stb_voxel_render 20-byte quads 2015/01
  216. // zmc engine 32-byte quads 2013/12
  217. // zmc engine 96-byte quads 2011/10
  218. //
  219. //
  220. // LICENSE
  221. //
  222. // See end of file for license information.
  223. #ifndef INCLUDE_STB_VOXEL_RENDER_H
  224. #define INCLUDE_STB_VOXEL_RENDER_H
  225. #include <stdlib.h>
  226. typedef struct stbvox_mesh_maker stbvox_mesh_maker;
  227. typedef struct stbvox_input_description stbvox_input_description;
  228. #ifdef STB_VOXEL_RENDER_STATIC
  229. #define STBVXDEC static
  230. #else
  231. #define STBVXDEC extern
  232. #endif
  233. #ifdef __cplusplus
  234. extern "C" {
  235. #endif
  236. //////////////////////////////////////////////////////////////////////////////
  237. //
  238. // CONFIGURATION MACROS
  239. //
  240. // #define STBVOX_CONFIG_MODE <integer> // REQUIRED
  241. // Configures the overall behavior of stb_voxel_render. This
  242. // can affect the shaders, the uniform info, and other things.
  243. // (If you need more than one mode in the same app, you can
  244. // use STB_VOXEL_RENDER_STATIC to create multiple versions
  245. // in separate files, and then wrap them.)
  246. //
  247. // Mode value Meaning
  248. // 0 Textured blocks, 32-byte quads
  249. // 1 Textured blocks, 20-byte quads
  250. // 20 Untextured blocks, 32-byte quads
  251. // 21 Untextured blocks, 20-byte quads
  252. //
  253. //
  254. // #define STBVOX_CONFIG_PRECISION_Z <integer> // OPTIONAL
  255. // Defines the number of bits of fractional position for Z.
  256. // Only 0 or 1 are valid. 1 is the default. If 0, then a
  257. // single mesh has twice the legal Z range; e.g. in
  258. // modes 0,1,20,21, Z in the mesh can extend to 511 instead
  259. // of 255. However, half-height blocks cannot be used.
  260. //
  261. // All of the following are just #ifdef tested so need no values, and are optional.
  262. //
  263. // STBVOX_CONFIG_BLOCKTYPE_SHORT
  264. // use unsigned 16-bit values for 'blocktype' in the input instead of 8-bit values
  265. //
  266. // STBVOX_CONFIG_OPENGL_MODELVIEW
  267. // use the gl_ModelView matrix rather than the explicit uniform
  268. //
  269. // STBVOX_CONFIG_HLSL
  270. // NOT IMPLEMENTED! Define HLSL shaders instead of GLSL shaders
  271. //
  272. // STBVOX_CONFIG_PREFER_TEXBUFFER
  273. // Stores many of the uniform arrays in texture buffers intead,
  274. // so they can be larger and may be more efficient on some hardware.
  275. //
  276. // STBVOX_CONFIG_LIGHTING_SIMPLE
  277. // Creates a simple lighting engine with a single point light source
  278. // in addition to the default half-lambert ambient light.
  279. //
  280. // STBVOX_CONFIG_LIGHTING
  281. // Declares a lighting function hook; you must append a lighting function
  282. // to the shader before compiling it:
  283. // vec3 compute_lighting(vec3 pos, vec3 norm, vec3 albedo, vec3 ambient);
  284. // 'ambient' is the half-lambert ambient light with vertex ambient-occlusion applied
  285. //
  286. // STBVOX_CONFIG_FOG_SMOOTHSTEP
  287. // Defines a simple unrealistic fog system designed to maximize
  288. // unobscured view distance while not looking too weird when things
  289. // emerge from the fog. Configured using an extra array element
  290. // in the STBVOX_UNIFORM_ambient uniform.
  291. //
  292. // STBVOX_CONFIG_FOG
  293. // Defines a fog function hook; you must append a fog function to
  294. // the shader before compiling it:
  295. // vec3 compute_fog(vec3 color, vec3 relative_pos, float fragment_alpha);
  296. // "color" is the incoming pre-fogged color, fragment_alpha is the alpha value,
  297. // and relative_pos is the vector from the point to the camera in worldspace
  298. //
  299. // STBVOX_CONFIG_DISABLE_TEX2
  300. // This disables all processing of texture 2 in the shader in case
  301. // you don't use it. Eventually this could be replaced with a mode
  302. // that omits the unused data entirely.
  303. //
  304. // STBVOX_CONFIG_TEX1_EDGE_CLAMP
  305. // STBVOX_CONFIG_TEX2_EDGE_CLAMP
  306. // If you want to edge clamp the textures, instead of letting them wrap,
  307. // set this flag. By default stb_voxel_render relies on texture wrapping
  308. // to simplify texture coordinate generation. This flag forces it to do
  309. // it correctly, although there can still be minor artifacts.
  310. //
  311. // STBVOX_CONFIG_ROTATION_IN_LIGHTING
  312. // Changes the meaning of the 'lighting' mesher input variable to also
  313. // store the rotation; see later discussion.
  314. //
  315. // STBVOX_CONFIG_VHEIGHT_IN_LIGHTING
  316. // Changes the meaning of the 'lighting' mesher input variable to also
  317. // store the vheight; see later discussion. Cannot use both this and
  318. // the previous variable.
  319. //
  320. // STBVOX_CONFIG_PREMULTIPLIED_ALPHA
  321. // Adjusts the shader calculations on the assumption that tex1.rgba,
  322. // tex2.rgba, and color.rgba all use premultiplied values, and that
  323. // the output of the fragment shader should be premultiplied.
  324. //
  325. // STBVOX_CONFIG_UNPREMULTIPLY
  326. // Only meaningful if STBVOX_CONFIG_PREMULTIPLIED_ALPHA is defined.
  327. // Changes the behavior described above so that the inputs are
  328. // still premultiplied alpha, but the output of the fragment
  329. // shader is not premultiplied alpha. This is needed when allowing
  330. // non-unit alpha values but not doing alpha-blending (for example
  331. // when alpha testing).
  332. //
  333. //////////////////////////////////////////////////////////////////////////////
  334. //
  335. // MESHING
  336. //
  337. // A mesh represents a (typically) small chunk of a larger world.
  338. // Meshes encode coordinates using small integers, so those
  339. // coordinates must be relative to some base location.
  340. // All of the coordinates in the functions below use
  341. // these relative coordinates unless explicitly stated
  342. // otherwise.
  343. //
  344. // Input to the meshing step is documented further down
  345. STBVXDEC void stbvox_init_mesh_maker(stbvox_mesh_maker *mm);
  346. // Call this function to initialize a mesh-maker context structure
  347. // used to build meshes. You should have one context per thread
  348. // that's building meshes.
  349. STBVXDEC void stbvox_set_buffer(stbvox_mesh_maker *mm, int mesh, int slot, void *buffer, size_t len);
  350. // Call this to set the buffer into which stbvox will write the mesh
  351. // it creates. It can build more than one mesh in parallel (distinguished
  352. // by the 'mesh' parameter), and each mesh can be made up of more than
  353. // one buffer (distinguished by the 'slot' parameter).
  354. //
  355. // Multiple meshes are under your control; use the 'selector' input
  356. // variable to choose which mesh each voxel's vertices are written to.
  357. // For example, you can use this to generate separate meshes for opaque
  358. // and transparent data.
  359. //
  360. // You can query the number of slots by calling stbvox_get_buffer_count
  361. // described below. The meaning of the buffer for each slot depends
  362. // on STBVOX_CONFIG_MODE.
  363. //
  364. // In mode 0 & mode 20, there is only one slot. The mesh data for that
  365. // slot is two interleaved vertex attributes: attr_vertex, a single
  366. // 32-bit uint, and attr_face, a single 32-bit uint.
  367. //
  368. // In mode 1 & mode 21, there are two slots. The first buffer should
  369. // be four times as large as the second buffer. The first buffer
  370. // contains a single vertex attribute: 'attr_vertex', a single 32-bit uint.
  371. // The second buffer contains texture buffer data (an array of 32-bit uints)
  372. // that will be accessed through the sampler identified by STBVOX_UNIFORM_face_data.
  373. STBVXDEC int stbvox_get_buffer_count(stbvox_mesh_maker *mm);
  374. // Returns the number of buffers needed per mesh as described above.
  375. STBVXDEC int stbvox_get_buffer_size_per_quad(stbvox_mesh_maker *mm, int slot);
  376. // Returns how much of a given buffer will get used per quad. This
  377. // allows you to choose correct relative sizes for each buffer, although
  378. // the values are fixed based on the configuration you've selected at
  379. // compile time, and the details are described in stbvox_set_buffer.
  380. STBVXDEC void stbvox_set_default_mesh(stbvox_mesh_maker *mm, int mesh);
  381. // Selects which mesh the mesher will output to (see previous function)
  382. // if the input doesn't specify a per-voxel selector. (I doubt this is
  383. // useful, but it's here just in case.)
  384. STBVXDEC stbvox_input_description *stbvox_get_input_description(stbvox_mesh_maker *mm);
  385. // This function call returns a pointer to the stbvox_input_description part
  386. // of stbvox_mesh_maker (which you should otherwise treat as opaque). You
  387. // zero this structure, then fill out the relevant pointers to the data
  388. // describing your voxel object/world.
  389. //
  390. // See further documentation at the description of stbvox_input_description below.
  391. STBVXDEC void stbvox_set_input_stride(stbvox_mesh_maker *mm, int x_stride_in_elements, int y_stride_in_elements);
  392. // This sets the stride between successive elements of the 3D arrays
  393. // in the stbvox_input_description. Z values are always stored consecutively.
  394. // (The preferred coordinate system for stbvox is X right, Y forwards, Z up.)
  395. STBVXDEC void stbvox_set_input_range(stbvox_mesh_maker *mm, int x0, int y0, int z0, int x1, int y1, int z1);
  396. // This sets the range of values in the 3D array for the voxels that
  397. // the mesh generator will convert. The lower values are inclusive,
  398. // the higher values are exclusive, so (0,0,0) to (16,16,16) generates
  399. // mesh data associated with voxels up to (15,15,15) but no higher.
  400. //
  401. // The mesh generate generates faces at the boundary between open space
  402. // and solid space but associates them with the solid space, so if (15,0,0)
  403. // is open and (16,0,0) is solid, then the mesh will contain the boundary
  404. // between them if x0 <= 16 and x1 > 16.
  405. //
  406. // Note that the mesh generator will access array elements 1 beyond the
  407. // limits set in these parameters. For example, if you set the limits
  408. // to be (0,0,0) and (16,16,16), then the generator will access all of
  409. // the voxels between (-1,-1,-1) and (16,16,16), including (16,16,16).
  410. // You may have to do pointer arithmetic to make it work.
  411. //
  412. // For example, caveview processes mesh chunks that are 32x32x16, but it
  413. // does this using input buffers that are 34x34x18.
  414. //
  415. // The lower limits are x0 >= 0, y0 >= 0, and z0 >= 0.
  416. //
  417. // The upper limits are mode dependent, but all the current methods are
  418. // limited to x1 < 127, y1 < 127, z1 < 255. Note that these are not
  419. // powers of two; if you want to use power-of-two chunks (to make
  420. // it efficient to decide which chunk a coordinate falls in), you're
  421. // limited to at most x1=64, y1=64, z1=128. For classic Minecraft-style
  422. // worlds with limited vertical extent, I recommend using a single
  423. // chunk for the entire height, which limits the height to 255 blocks
  424. // (one less than Minecraft), and only chunk the map in X & Y.
  425. STBVXDEC int stbvox_make_mesh(stbvox_mesh_maker *mm);
  426. // Call this function to create mesh data for the currently configured
  427. // set of input data. This appends to the currently configured mesh output
  428. // buffer. Returns 1 on success. If there is not enough room in the buffer,
  429. // it outputs as much as it can, and returns 0; you need to switch output
  430. // buffers (either by calling stbvox_set_buffer to set new buffers, or
  431. // by copying the data out and calling stbvox_reset_buffers), and then
  432. // call this function again without changing any of the input parameters.
  433. //
  434. // Note that this function appends; you can call it multiple times to
  435. // build a single mesh. For example, caveview uses chunks that are
  436. // 32x32x255, but builds the mesh for it by processing 32x32x16 at atime
  437. // (this is faster as it is reuses the same 34x34x18 input buffers rather
  438. // than needing 34x34x257 input buffers).
  439. // Once you're done creating a mesh into a given buffer,
  440. // consider the following functions:
  441. STBVXDEC int stbvox_get_quad_count(stbvox_mesh_maker *mm, int mesh);
  442. // Returns the number of quads in the mesh currently generated by mm.
  443. // This is the sum of all consecutive stbvox_make_mesh runs appending
  444. // to the same buffer. 'mesh' distinguishes between the multiple user
  445. // meshes available via 'selector' or stbvox_set_default_mesh.
  446. //
  447. // Typically you use this function when you're done building the mesh
  448. // and want to record how to draw it.
  449. //
  450. // Note that there are no index buffers; the data stored in the buffers
  451. // should be drawn as quads (e.g. with GL_QUAD); if your API does not
  452. // support quads, you can create a single index buffer large enough to
  453. // draw your largest vertex buffer, and reuse it for every rendering.
  454. // (Note that if you use 32-bit indices, you'll use 24 bytes of bandwidth
  455. // per quad, more than the 20 bytes for the vertex/face mesh data.)
  456. STBVXDEC void stbvox_set_mesh_coordinates(stbvox_mesh_maker *mm, int x, int y, int z);
  457. // Sets the global coordinates for this chunk, such that (0,0,0) relative
  458. // coordinates will be at (x,y,z) in global coordinates.
  459. STBVXDEC void stbvox_get_bounds(stbvox_mesh_maker *mm, float bounds[2][3]);
  460. // Returns the bounds for the mesh in global coordinates. Use this
  461. // for e.g. frustum culling the mesh. @BUG: this just uses the
  462. // values from stbvox_set_input_range(), so if you build by
  463. // appending multiple values, this will be wrong, and you need to
  464. // set stbvox_set_input_range() to the full size. Someday this
  465. // will switch to tracking the actual bounds of the *mesh*, though.
  466. STBVXDEC void stbvox_get_transform(stbvox_mesh_maker *mm, float transform[3][3]);
  467. // Returns the 'transform' data for the shader uniforms. It is your
  468. // job to set this to the shader before drawing the mesh. It is the
  469. // only uniform that needs to change per-mesh. Note that it is not
  470. // a 3x3 matrix, but rather a scale to decode fixed point numbers as
  471. // floats, a translate from relative to global space, and a special
  472. // translation for texture coordinate generation that avoids
  473. // floating-point precision issues. @TODO: currently we add the
  474. // global translation to the vertex, than multiply by modelview,
  475. // but this means if camera location and vertex are far from the
  476. // origin, we lose precision. Need to make a special modelview with
  477. // the translation (or some of it) factored out to avoid this.
  478. STBVXDEC void stbvox_reset_buffers(stbvox_mesh_maker *mm);
  479. // Call this function if you're done with the current output buffer
  480. // but want to reuse it (e.g. you're done appending with
  481. // stbvox_make_mesh and you've copied the data out to your graphics API
  482. // so can reuse the buffer).
  483. //////////////////////////////////////////////////////////////////////////////
  484. //
  485. // RENDERING
  486. //
  487. STBVXDEC char *stbvox_get_vertex_shader(void);
  488. // Returns the (currently GLSL-only) vertex shader.
  489. STBVXDEC char *stbvox_get_fragment_shader(void);
  490. // Returns the (currently GLSL-only) fragment shader.
  491. // You can override the lighting and fogging calculations
  492. // by appending data to the end of these; see the #define
  493. // documentation for more information.
  494. STBVXDEC char *stbvox_get_fragment_shader_alpha_only(void);
  495. // Returns a slightly cheaper fragment shader that computes
  496. // alpha but not color. This is useful for e.g. a depth-only
  497. // pass when using alpha test.
  498. typedef struct stbvox_uniform_info stbvox_uniform_info;
  499. STBVXDEC int stbvox_get_uniform_info(stbvox_uniform_info *info, int uniform);
  500. // Gets the information about a uniform necessary for you to
  501. // set up each uniform with a minimal amount of explicit code.
  502. // See the sample code after the structure definition for stbvox_uniform_info,
  503. // further down in this header section.
  504. //
  505. // "uniform" is from the list immediately following. For many
  506. // of these, default values are provided which you can set.
  507. // Most values are shared for most draw calls; e.g. for stateful
  508. // APIs you can set most of the state only once. Only
  509. // STBVOX_UNIFORM_transform needs to change per draw call.
  510. //
  511. // STBVOX_UNIFORM_texscale
  512. // 64- or 128-long vec4 array. (128 only if STBVOX_CONFIG_PREFER_TEXBUFFER)
  513. // x: scale factor to apply to texture #1. must be a power of two. 1.0 means 'face-sized'
  514. // y: scale factor to apply to texture #2. must be a power of two. 1.0 means 'face-sized'
  515. // z: blend mode indexed by texture #2. 0.0 is alpha compositing; 1.0 is multiplication.
  516. // w: unused currently. @TODO use to support texture animation?
  517. //
  518. // Texscale is indexed by the bottom 6 or 7 bits of the texture id; thus for
  519. // example the texture at index 0 in the array and the texture in index 128 of
  520. // the array must be scaled the same. This means that if you only have 64 or 128
  521. // unique textures, they all get distinct values anyway; otherwise you have
  522. // to group them in pairs or sets of four.
  523. //
  524. // STBVOX_UNIFORM_ambient
  525. // 4-long vec4 array:
  526. // ambient[0].xyz - negative of direction of a directional light for half-lambert
  527. // ambient[1].rgb - color of light scaled by NdotL (can be negative)
  528. // ambient[2].rgb - constant light added to above calculation;
  529. // effectively light ranges from ambient[2]-ambient[1] to ambient[2]+ambient[1]
  530. // ambient[3].rgb - fog color for STBVOX_CONFIG_FOG_SMOOTHSTEP
  531. // ambient[3].a - reciprocal of squared distance of farthest fog point (viewing distance)
  532. // +----- has a default value
  533. // | +-- you should always use the default value
  534. enum // V V
  535. { // ------------------------------------------------
  536. STBVOX_UNIFORM_face_data, // n the sampler with the face texture buffer
  537. STBVOX_UNIFORM_transform, // n the transform data from stbvox_get_transform
  538. STBVOX_UNIFORM_tex_array, // n an array of two texture samplers containing the two texture arrays
  539. STBVOX_UNIFORM_texscale, // Y a table of texture properties, see above
  540. STBVOX_UNIFORM_color_table, // Y 64 vec4 RGBA values; a default palette is provided; if A > 1.0, fullbright
  541. STBVOX_UNIFORM_normals, // Y Y table of normals, internal-only
  542. STBVOX_UNIFORM_texgen, // Y Y table of texgen vectors, internal-only
  543. STBVOX_UNIFORM_ambient, // n lighting & fog info, see above
  544. STBVOX_UNIFORM_camera_pos, // Y camera position in global voxel space (for lighting & fog)
  545. STBVOX_UNIFORM_count,
  546. };
  547. enum
  548. {
  549. STBVOX_UNIFORM_TYPE_none,
  550. STBVOX_UNIFORM_TYPE_sampler,
  551. STBVOX_UNIFORM_TYPE_vec2,
  552. STBVOX_UNIFORM_TYPE_vec3,
  553. STBVOX_UNIFORM_TYPE_vec4,
  554. };
  555. struct stbvox_uniform_info
  556. {
  557. int type; // which type of uniform
  558. int bytes_per_element; // the size of each uniform array element (e.g. vec3 = 12 bytes)
  559. int array_length; // length of the uniform array
  560. char *name; // name in the shader @TODO use numeric binding
  561. float *default_value; // if not NULL, you can use this as the uniform pointer
  562. int use_tex_buffer; // if true, then the uniform is a sampler but the data can come from default_value
  563. };
  564. //////////////////////////////////////////////////////////////////////////////
  565. //
  566. // Uniform sample code
  567. //
  568. #if 0
  569. // Run this once per frame before drawing all the meshes.
  570. // You still need to separately set the 'transform' uniform for every mesh.
  571. void setup_uniforms(GLuint shader, float camera_pos[4], GLuint tex1, GLuint tex2)
  572. {
  573. int i;
  574. glUseProgram(shader); // so uniform binding works
  575. for (i=0; i < STBVOX_UNIFORM_count; ++i) {
  576. stbvox_uniform_info sui;
  577. if (stbvox_get_uniform_info(&sui, i)) {
  578. GLint loc = glGetUniformLocation(shader, sui.name);
  579. if (loc != 0) {
  580. switch (i) {
  581. case STBVOX_UNIFORM_camera_pos: // only needed for fog
  582. glUniform4fv(loc, sui.array_length, camera_pos);
  583. break;
  584. case STBVOX_UNIFORM_tex_array: {
  585. GLuint tex_unit[2] = { 0, 1 }; // your choice of samplers
  586. glUniform1iv(loc, 2, tex_unit);
  587. glActiveTexture(GL_TEXTURE0 + tex_unit[0]); glBindTexture(GL_TEXTURE_2D_ARRAY, tex1);
  588. glActiveTexture(GL_TEXTURE0 + tex_unit[1]); glBindTexture(GL_TEXTURE_2D_ARRAY, tex2);
  589. glActiveTexture(GL_TEXTURE0); // reset to default
  590. break;
  591. }
  592. case STBVOX_UNIFORM_face_data:
  593. glUniform1i(loc, SAMPLER_YOU_WILL_BIND_PER_MESH_FACE_DATA_TO);
  594. break;
  595. case STBVOX_UNIFORM_ambient: // you definitely want to override this
  596. case STBVOX_UNIFORM_color_table: // you might want to override this
  597. case STBVOX_UNIFORM_texscale: // you may want to override this
  598. glUniform4fv(loc, sui.array_length, sui.default_value);
  599. break;
  600. case STBVOX_UNIFORM_normals: // you never want to override this
  601. case STBVOX_UNIFORM_texgen: // you never want to override this
  602. glUniform3fv(loc, sui.array_length, sui.default_value);
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. }
  609. #endif
  610. #ifdef __cplusplus
  611. }
  612. #endif
  613. //////////////////////////////////////////////////////////////////////////////
  614. //
  615. // INPUT TO MESHING
  616. //
  617. // Shapes of blocks that aren't always cubes
  618. enum
  619. {
  620. STBVOX_GEOM_empty,
  621. STBVOX_GEOM_knockout, // creates a hole in the mesh
  622. STBVOX_GEOM_solid,
  623. STBVOX_GEOM_transp, // solid geometry, but transparent contents so neighbors generate normally, unless same blocktype
  624. // following 4 can be represented by vheight as well
  625. STBVOX_GEOM_slab_upper,
  626. STBVOX_GEOM_slab_lower,
  627. STBVOX_GEOM_floor_slope_north_is_top,
  628. STBVOX_GEOM_ceil_slope_north_is_bottom,
  629. STBVOX_GEOM_floor_slope_north_is_top_as_wall_UNIMPLEMENTED, // same as floor_slope above, but uses wall's texture & texture projection
  630. STBVOX_GEOM_ceil_slope_north_is_bottom_as_wall_UNIMPLEMENTED,
  631. STBVOX_GEOM_crossed_pair, // corner-to-corner pairs, with normal vector bumped upwards
  632. STBVOX_GEOM_force, // like GEOM_transp, but faces visible even if neighbor is same type, e.g. minecraft fancy leaves
  633. // these access vheight input
  634. STBVOX_GEOM_floor_vheight_03 = 12, // diagonal is SW-NE
  635. STBVOX_GEOM_floor_vheight_12, // diagonal is SE-NW
  636. STBVOX_GEOM_ceil_vheight_03,
  637. STBVOX_GEOM_ceil_vheight_12,
  638. STBVOX_GEOM_count, // number of geom cases
  639. };
  640. enum
  641. {
  642. STBVOX_FACE_east,
  643. STBVOX_FACE_north,
  644. STBVOX_FACE_west,
  645. STBVOX_FACE_south,
  646. STBVOX_FACE_up,
  647. STBVOX_FACE_down,
  648. STBVOX_FACE_count,
  649. };
  650. #ifdef STBVOX_CONFIG_BLOCKTYPE_SHORT
  651. typedef unsigned short stbvox_block_type;
  652. #else
  653. typedef unsigned char stbvox_block_type;
  654. #endif
  655. // 24-bit color
  656. typedef struct
  657. {
  658. unsigned char r,g,b;
  659. } stbvox_rgb;
  660. #define STBVOX_COLOR_TEX1_ENABLE 64
  661. #define STBVOX_COLOR_TEX2_ENABLE 128
  662. // This is the data structure you fill out. Most of the arrays can be
  663. // NULL, except when one is required to get the value to index another.
  664. //
  665. // The compass system used in the following descriptions is:
  666. // east means increasing x
  667. // north means increasing y
  668. // up means increasing z
  669. struct stbvox_input_description
  670. {
  671. unsigned char lighting_at_vertices;
  672. // The default is lighting values (i.e. ambient occlusion) are at block
  673. // center, and the vertex light is gathered from those adjacent block
  674. // centers that the vertex is facing. This makes smooth lighting
  675. // consistent across adjacent faces with the same orientation.
  676. //
  677. // Setting this flag to non-zero gives you explicit control
  678. // of light at each vertex, but now the lighting/ao will be
  679. // shared by all vertices at the same point, even if they
  680. // have different normals.
  681. // these are mostly 3D maps you use to define your voxel world, using x_stride and y_stride
  682. // note that for cache efficiency, you want to use the block_foo palettes as much as possible instead
  683. stbvox_rgb *rgb;
  684. // Indexed by 3D coordinate.
  685. // 24-bit voxel color for STBVOX_CONFIG_MODE = 20 or 21 only
  686. unsigned char *lighting;
  687. // Indexed by 3D coordinate. The lighting value / ambient occlusion
  688. // value that is used to define the vertex lighting values.
  689. // The raw lighting values are defined at the center of blocks
  690. // (or at vertex if 'lighting_at_vertices' is true).
  691. //
  692. // If the macro STBVOX_CONFIG_ROTATION_IN_LIGHTING is defined,
  693. // then an additional 2-bit block rotation value is stored
  694. // in this field as well.
  695. //
  696. // Encode with STBVOX_MAKE_LIGHTING_EXT(lighting,rot)--here
  697. // 'lighting' should still be 8 bits, as the macro will
  698. // discard the bottom bits automatically. Similarly, if
  699. // using STBVOX_CONFIG_VHEIGHT_IN_LIGHTING, encode with
  700. // STBVOX_MAKE_LIGHTING_EXT(lighting,vheight).
  701. //
  702. // (Rationale: rotation needs to be independent of blocktype,
  703. // but is only 2 bits so doesn't want to be its own array.
  704. // Lighting is the one thing that was likely to already be
  705. // in use and that I could easily steal 2 bits from.)
  706. stbvox_block_type *blocktype;
  707. // Indexed by 3D coordinate. This is a core "block type" value, which is used
  708. // to index into other arrays; essentially a "palette". This is much more
  709. // memory-efficient and performance-friendly than storing the values explicitly,
  710. // but only makes sense if the values are always synchronized.
  711. //
  712. // If a voxel's blocktype is 0, it is assumed to be empty (STBVOX_GEOM_empty),
  713. // and no other blocktypes should be STBVOX_GEOM_empty. (Only if you do not
  714. // have blocktypes should STBVOX_GEOM_empty ever used.)
  715. //
  716. // Normally it is an unsigned byte, but you can override it to be
  717. // a short if you have too many blocktypes.
  718. unsigned char *geometry;
  719. // Indexed by 3D coordinate. Contains the geometry type for the block.
  720. // Also contains a 2-bit rotation for how the whole block is rotated.
  721. // Also includes a 2-bit vheight value when using shared vheight values.
  722. // See the separate vheight documentation.
  723. // Encode with STBVOX_MAKE_GEOMETRY(geom, rot, vheight)
  724. unsigned char *block_geometry;
  725. // Array indexed by blocktype containing the geometry for this block, plus
  726. // a 2-bit "simple rotation". Note rotation has limited use since it's not
  727. // independent of blocktype.
  728. //
  729. // Encode with STBVOX_MAKE_GEOMETRY(geom,simple_rot,0)
  730. unsigned char *block_tex1;
  731. // Array indexed by blocktype containing the texture id for texture #1.
  732. unsigned char (*block_tex1_face)[6];
  733. // Array indexed by blocktype and face containing the texture id for texture #1.
  734. // The N/E/S/W face choices can be rotated by one of the rotation selectors;
  735. // The top & bottom face textures will rotate to match.
  736. // Note that it only makes sense to use one of block_tex1 or block_tex1_face;
  737. // this pattern repeats throughout and this notice is not repeated.
  738. unsigned char *tex2;
  739. // Indexed by 3D coordinate. Contains the texture id for texture #2
  740. // to use on all faces of the block.
  741. unsigned char *block_tex2;
  742. // Array indexed by blocktype containing the texture id for texture #2.
  743. unsigned char (*block_tex2_face)[6];
  744. // Array indexed by blocktype and face containing the texture id for texture #2.
  745. // The N/E/S/W face choices can be rotated by one of the rotation selectors;
  746. // The top & bottom face textures will rotate to match.
  747. unsigned char *color;
  748. // Indexed by 3D coordinate. Contains the color for all faces of the block.
  749. // The core color value is 0..63.
  750. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  751. unsigned char *block_color;
  752. // Array indexed by blocktype containing the color value to apply to the faces.
  753. // The core color value is 0..63.
  754. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  755. unsigned char (*block_color_face)[6];
  756. // Array indexed by blocktype and face containing the color value to apply to that face.
  757. // The core color value is 0..63.
  758. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  759. unsigned char *block_texlerp;
  760. // Array indexed by blocktype containing 3-bit scalar for texture #2 alpha
  761. // (known throughout as 'texlerp'). This is constant over every face even
  762. // though the property is potentially per-vertex.
  763. unsigned char (*block_texlerp_face)[6];
  764. // Array indexed by blocktype and face containing 3-bit scalar for texture #2 alpha.
  765. // This is constant over the face even though the property is potentially per-vertex.
  766. unsigned char *block_vheight;
  767. // Array indexed by blocktype containing the vheight values for the
  768. // top or bottom face of this block. These will rotate properly if the
  769. // block is rotated. See discussion of vheight.
  770. // Encode with STBVOX_MAKE_VHEIGHT(sw_height, se_height, nw_height, ne_height)
  771. unsigned char *selector;
  772. // Array indexed by 3D coordinates indicating which output mesh to select.
  773. unsigned char *block_selector;
  774. // Array indexed by blocktype indicating which output mesh to select.
  775. unsigned char *side_texrot;
  776. // Array indexed by 3D coordinates encoding 2-bit texture rotations for the
  777. // faces on the E/N/W/S sides of the block.
  778. // Encode with STBVOX_MAKE_SIDE_TEXROT(rot_e, rot_n, rot_w, rot_s)
  779. unsigned char *block_side_texrot;
  780. // Array indexed by blocktype encoding 2-bit texture rotations for the faces
  781. // on the E/N/W/S sides of the block.
  782. // Encode with STBVOX_MAKE_SIDE_TEXROT(rot_e, rot_n, rot_w, rot_s)
  783. unsigned char *overlay; // index into palettes listed below
  784. // Indexed by 3D coordinate. If 0, there is no overlay. If non-zero,
  785. // it indexes into to the below arrays and overrides the values
  786. // defined by the blocktype.
  787. unsigned char (*overlay_tex1)[6];
  788. // Array indexed by overlay value and face, containing an override value
  789. // for the texture id for texture #1. If 0, the value defined by blocktype
  790. // is used.
  791. unsigned char (*overlay_tex2)[6];
  792. // Array indexed by overlay value and face, containing an override value
  793. // for the texture id for texture #2. If 0, the value defined by blocktype
  794. // is used.
  795. unsigned char (*overlay_color)[6];
  796. // Array indexed by overlay value and face, containing an override value
  797. // for the face color. If 0, the value defined by blocktype is used.
  798. unsigned char *overlay_side_texrot;
  799. // Array indexed by overlay value, encoding 2-bit texture rotations for the faces
  800. // on the E/N/W/S sides of the block.
  801. // Encode with STBVOX_MAKE_SIDE_TEXROT(rot_e, rot_n, rot_w, rot_s)
  802. unsigned char *rotate;
  803. // Indexed by 3D coordinate. Allows independent rotation of several
  804. // parts of the voxel, where by rotation I mean swapping textures
  805. // and colors between E/N/S/W faces.
  806. // Block: rotates anything indexed by blocktype
  807. // Overlay: rotates anything indexed by overlay
  808. // EColor: rotates faces defined in ecolor_facemask
  809. // Encode with STBVOX_MAKE_MATROT(block,overlay,ecolor)
  810. unsigned char *tex2_for_tex1;
  811. // Array indexed by tex1 containing the texture id for texture #2.
  812. // You can use this if the two are always/almost-always strictly
  813. // correlated (e.g. if tex2 is a detail texture for tex1), as it
  814. // will be more efficient (touching fewer cache lines) than using
  815. // e.g. block_tex2_face.
  816. unsigned char *tex2_replace;
  817. // Indexed by 3D coordinate. Specifies the texture id for texture #2
  818. // to use on a single face of the voxel, which must be E/N/W/S (not U/D).
  819. // The texture id is limited to 6 bits unless tex2_facemask is also
  820. // defined (see below).
  821. // Encode with STBVOX_MAKE_TEX2_REPLACE(tex2, face)
  822. unsigned char *tex2_facemask;
  823. // Indexed by 3D coordinate. Specifies which of the six faces should
  824. // have their tex2 replaced by the value of tex2_replace. In this
  825. // case, all 8 bits of tex2_replace are used as the texture id.
  826. // Encode with STBVOX_MAKE_FACE_MASK(east,north,west,south,up,down)
  827. unsigned char *extended_color;
  828. // Indexed by 3D coordinate. Specifies a value that indexes into
  829. // the ecolor arrays below (both of which must be defined).
  830. unsigned char *ecolor_color;
  831. // Indexed by extended_color value, specifies an optional override
  832. // for the color value on some faces.
  833. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  834. unsigned char *ecolor_facemask;
  835. // Indexed by extended_color value, this specifies which faces the
  836. // color in ecolor_color should be applied to. The faces can be
  837. // independently rotated by the ecolor value of 'rotate', if it exists.
  838. // Encode with STBVOX_MAKE_FACE_MASK(e,n,w,s,u,d)
  839. unsigned char *color2;
  840. // Indexed by 3D coordinates, specifies an alternative color to apply
  841. // to some of the faces of the block.
  842. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  843. unsigned char *color2_facemask;
  844. // Indexed by 3D coordinates, specifies which faces should use the
  845. // color defined in color2. No rotation value is applied.
  846. // Encode with STBVOX_MAKE_FACE_MASK(e,n,w,s,u,d)
  847. unsigned char *color3;
  848. // Indexed by 3D coordinates, specifies an alternative color to apply
  849. // to some of the faces of the block.
  850. // Encode with STBVOX_MAKE_COLOR(color_number, tex1_enable, tex2_enable)
  851. unsigned char *color3_facemask;
  852. // Indexed by 3D coordinates, specifies which faces should use the
  853. // color defined in color3. No rotation value is applied.
  854. // Encode with STBVOX_MAKE_FACE_MASK(e,n,w,s,u,d)
  855. unsigned char *texlerp_simple;
  856. // Indexed by 3D coordinates, this is the smallest texlerp encoding
  857. // that can do useful work. It consits of three values: baselerp,
  858. // vertlerp, and face_vertlerp. Baselerp defines the value
  859. // to use on all of the faces but one, from the STBVOX_TEXLERP_BASE
  860. // values. face_vertlerp is one of the 6 face values (or STBVOX_FACE_NONE)
  861. // which specifies the face should use the vertlerp values.
  862. // Vertlerp defines a lerp value at every vertex of the mesh.
  863. // Thus, one face can have per-vertex texlerp values, and those
  864. // values are encoded in the space so that they will be shared
  865. // by adjacent faces that also use vertlerp, allowing continuity
  866. // (this is used for the "texture crossfade" bit of the release video).
  867. // Encode with STBVOX_MAKE_TEXLERP_SIMPLE(baselerp, vertlerp, face_vertlerp)
  868. // The following texlerp encodings are experimental and maybe not
  869. // that useful.
  870. unsigned char *texlerp;
  871. // Indexed by 3D coordinates, this defines four values:
  872. // vertlerp is a lerp value at every vertex of the mesh (using STBVOX_TEXLERP_BASE values).
  873. // ud is the value to use on up and down faces, from STBVOX_TEXLERP_FACE values
  874. // ew is the value to use on east and west faces, from STBVOX_TEXLERP_FACE values
  875. // ns is the value to use on north and south faces, from STBVOX_TEXLERP_FACE values
  876. // If any of ud, ew, or ns is STBVOX_TEXLERP_FACE_use_vert, then the
  877. // vertlerp values for the vertices are gathered and used for those faces.
  878. // Encode with STBVOX_MAKE_TEXLERP(vertlerp,ud,ew,sw)
  879. unsigned short *texlerp_vert3;
  880. // Indexed by 3D coordinates, this works with texlerp and
  881. // provides a unique texlerp value for every direction at
  882. // every vertex. The same rules of whether faces share values
  883. // applies. The STBVOX_TEXLERP_FACE vertlerp value defined in
  884. // texlerp is only used for the down direction. The values at
  885. // each vertex in other directions are defined in this array,
  886. // and each uses the STBVOX_TEXLERP3 values (i.e. full precision
  887. // 3-bit texlerp values).
  888. // Encode with STBVOX_MAKE_VERT3(vertlerp_e,vertlerp_n,vertlerp_w,vertlerp_s,vertlerp_u)
  889. unsigned short *texlerp_face3; // e:3,n:3,w:3,s:3,u:2,d:2
  890. // Indexed by 3D coordinates, this provides a compact way to
  891. // fully specify the texlerp value indepenendly for every face,
  892. // but doesn't allow per-vertex variation. E/N/W/S values are
  893. // encoded using STBVOX_TEXLERP3 values, whereas up and down
  894. // use STBVOX_TEXLERP_SIMPLE values.
  895. // Encode with STBVOX_MAKE_FACE3(face_e,face_n,face_w,face_s,face_u,face_d)
  896. unsigned char *vheight; // STBVOX_MAKE_VHEIGHT -- sw:2, se:2, nw:2, ne:2, doesn't rotate
  897. // Indexed by 3D coordinates, this defines the four
  898. // vheight values to use if the geometry is STBVOX_GEOM_vheight*.
  899. // See the vheight discussion.
  900. unsigned char *packed_compact;
  901. // Stores block rotation, vheight, and texlerp values:
  902. // block rotation: 2 bits
  903. // vertex vheight: 2 bits
  904. // use_texlerp : 1 bit
  905. // vertex texlerp: 3 bits
  906. // If STBVOX_CONFIG_UP_TEXLERP_PACKED is defined, then 'vertex texlerp' is
  907. // used for up faces if use_texlerp is 1. If STBVOX_CONFIG_DOWN_TEXLERP_PACKED
  908. // is defined, then 'vertex texlerp' is used for down faces if use_texlerp is 1.
  909. // Note if those symbols are defined but packed_compact is NULL, the normal
  910. // texlerp default will be used.
  911. // Encode with STBVOX_MAKE_PACKED_COMPACT(rot, vheight, texlerp, use_texlerp)
  912. };
  913. // @OPTIMIZE allow specializing; build a single struct with all of the
  914. // 3D-indexed arrays combined so it's AoS instead of SoA for better
  915. // cache efficiency
  916. //////////////////////////////////////////////////////////////////////////////
  917. //
  918. // VHEIGHT DOCUMENTATION
  919. //
  920. // "vheight" is the internal name for the special block types
  921. // with sloped tops or bottoms. "vheight" stands for "vertex height".
  922. //
  923. // Note that these blocks are very flexible (there are 256 of them,
  924. // although at least 17 of them should never be used), but they
  925. // also have a disadvantage that they generate extra invisible
  926. // faces; the generator does not currently detect whether adjacent
  927. // vheight blocks hide each others sides, so those side faces are
  928. // always generated. For a continuous ground terrain, this means
  929. // that you may generate 5x as many quads as needed. See notes
  930. // on "improvements for shipping products" in the introduction.
  931. enum
  932. {
  933. STBVOX_VERTEX_HEIGHT_0,
  934. STBVOX_VERTEX_HEIGHT_half,
  935. STBVOX_VERTEX_HEIGHT_1,
  936. STBVOX_VERTEX_HEIGHT_one_and_a_half,
  937. };
  938. // These are the "vheight" values. Vheight stands for "vertex height".
  939. // The idea is that for a "floor vheight" block, you take a cube and
  940. // reposition the top-most vertices at various heights as specified by
  941. // the vheight values. Similarly, a "ceiling vheight" block takes a
  942. // cube and repositions the bottom-most vertices.
  943. //
  944. // A floor block only adjusts the top four vertices; the bottom four vertices
  945. // remain at the bottom of the block. The height values are 2 bits,
  946. // measured in halves of a block; so you can specify heights of 0/2,
  947. // 1/2, 2/2, or 3/2. 0 is the bottom of the block, 1 is halfway
  948. // up the block, 2 is the top of the block, and 3 is halfway up the
  949. // next block (and actually outside of the block). The value 3 is
  950. // actually legal for floor vheight (but not ceiling), and allows you to:
  951. //
  952. // (A) have smoother terrain by having slopes that cross blocks,
  953. // e.g. (1,1,3,3) is a regular-seeming slope halfway between blocks
  954. // (B) make slopes steeper than 45-degrees, e.g. (0,0,3,3)
  955. //
  956. // (Because only z coordinates have half-block precision, and x&y are
  957. // limited to block corner precision, it's not possible to make these
  958. // things "properly" out of blocks, e.g. a half-slope block on its side
  959. // or a sloped block halfway between blocks that's made out of two blocks.)
  960. //
  961. // If you define STBVOX_CONFIG_OPTIMIZED_VHEIGHT, then the top face
  962. // (or bottom face for a ceiling vheight block) will be drawn as a
  963. // single quad even if the four vertex heights aren't planar, and a
  964. // single normal will be used over the entire quad. If you
  965. // don't define it, then if the top face is non-planar, it will be
  966. // split into two triangles, each with their own normal/lighting.
  967. // (Note that since all output from stb_voxel_render is quad meshes,
  968. // triangles are actually rendered as degenerate quads.) In this case,
  969. // the distinction betwen STBVOX_GEOM_floor_vheight_03 and
  970. // STBVOX_GEOM_floor_vheight_12 comes into play; the former introduces
  971. // an edge from the SW to NE corner (i.e. from <0,0,?> to <1,1,?>),
  972. // while the latter introduces an edge from the NW to SE corner
  973. // (i.e. from <0,1,?> to <1,0,?>.) For a "lazy mesh" look, use
  974. // exclusively _03 or _12. For a "classic mesh" look, alternate
  975. // _03 and _12 in a checkerboard pattern. For a "smoothest surface"
  976. // look, choose the edge based on actual vertex heights.
  977. //
  978. // The four vertex heights can come from several places. The simplest
  979. // encoding is to just use the 'vheight' parameter which stores four
  980. // explicit vertex heights for every block. This allows total independence,
  981. // but at the cost of the largest memory usage, 1 byte per 3D block.
  982. // Encode this with STBVOX_MAKE_VHEIGHT(vh_sw, vh_se, vh_nw, vh_ne).
  983. // These coordinates are absolute, not affected by block rotations.
  984. //
  985. // An alternative if you just want to encode some very specific block
  986. // types, not all the possibilities--say you just want half-height slopes,
  987. // so you want (0,0,1,1) and (1,1,2,2)--then you can use block_vheight
  988. // to specify them. The geometry rotation will cause block_vheight values
  989. // to be rotated (because it's as if you're just defining a type of
  990. // block). This value is also encoded with STBVOX_MAKE_VHEIGHT.
  991. //
  992. // If you want to save memory and you're creating a "continuous ground"
  993. // sort of effect, you can make each vertex of the lattice share the
  994. // vheight value; that is, two adjacent blocks that share a vertex will
  995. // always get the same vheight value for that vertex. Then you need to
  996. // store two bits of vheight for every block, which you do by storing it
  997. // as part another data structure. Store the south-west vertex's vheight
  998. // with the block. You can either use the "geometry" mesh variable (it's
  999. // a parameter to STBVOX_MAKE_GEOMETRY) or you can store it in the
  1000. // "lighting" mesh variable if you defined STBVOX_CONFIG_VHEIGHT_IN_LIGHTING,
  1001. // using STBVOX_MAKE_LIGHTING_EXT(lighting,vheight).
  1002. //
  1003. // Note that if you start with a 2D height map and generate vheight data from
  1004. // it, you don't necessarily store only one value per (x,y) coordinate,
  1005. // as the same value may need to be set up at multiple z heights. For
  1006. // example, if height(8,8) = 13.5, then you want the block at (8,8,13)
  1007. // to store STBVOX_VERTEX_HEIGHT_half, and this will be used by blocks
  1008. // at (7,7,13), (8,7,13), (7,8,13), and (8,8,13). However, if you're
  1009. // allowing steep slopes, it might be the case that you have a block
  1010. // at (7,7,12) which is supposed to stick up to 13.5; that means
  1011. // you also need to store STBVOX_VERTEX_HEIGHT_one_and_a_half at (8,8,12).
  1012. enum
  1013. {
  1014. STBVOX_TEXLERP_FACE_0,
  1015. STBVOX_TEXLERP_FACE_half,
  1016. STBVOX_TEXLERP_FACE_1,
  1017. STBVOX_TEXLERP_FACE_use_vert,
  1018. };
  1019. enum
  1020. {
  1021. STBVOX_TEXLERP_BASE_0, // 0.0
  1022. STBVOX_TEXLERP_BASE_2_7, // 2/7
  1023. STBVOX_TEXLERP_BASE_5_7, // 4/7
  1024. STBVOX_TEXLERP_BASE_1 // 1.0
  1025. };
  1026. enum
  1027. {
  1028. STBVOX_TEXLERP3_0_8,
  1029. STBVOX_TEXLERP3_1_8,
  1030. STBVOX_TEXLERP3_2_8,
  1031. STBVOX_TEXLERP3_3_8,
  1032. STBVOX_TEXLERP3_4_8,
  1033. STBVOX_TEXLERP3_5_8,
  1034. STBVOX_TEXLERP3_6_8,
  1035. STBVOX_TEXLERP3_7_8,
  1036. };
  1037. #define STBVOX_FACE_NONE 7
  1038. #define STBVOX_BLOCKTYPE_EMPTY 0
  1039. #ifdef STBVOX_BLOCKTYPE_SHORT
  1040. #define STBVOX_BLOCKTYPE_HOLE 65535
  1041. #else
  1042. #define STBVOX_BLOCKTYPE_HOLE 255
  1043. #endif
  1044. #define STBVOX_MAKE_GEOMETRY(geom, rotate, vheight) ((geom) + (rotate)*16 + (vheight)*64)
  1045. #define STBVOX_MAKE_VHEIGHT(v_sw, v_se, v_nw, v_ne) ((v_sw) + (v_se)*4 + (v_nw)*16 + (v_ne)*64)
  1046. #define STBVOX_MAKE_MATROT(block, overlay, color) ((block) + (overlay)*4 + (color)*64)
  1047. #define STBVOX_MAKE_TEX2_REPLACE(tex2, tex2_replace_face) ((tex2) + ((tex2_replace_face) & 3)*64)
  1048. #define STBVOX_MAKE_TEXLERP(ns2, ew2, ud2, vert) ((ew2) + (ns2)*4 + (ud2)*16 + (vert)*64)
  1049. #define STBVOX_MAKE_TEXLERP_SIMPLE(baselerp,vert,face) ((vert)*32 + (face)*4 + (baselerp))
  1050. #define STBVOX_MAKE_TEXLERP1(vert,e2,n2,w2,s2,u4,d2) STBVOX_MAKE_TEXLERP(s2, w2, d2, vert)
  1051. #define STBVOX_MAKE_TEXLERP2(vert,e2,n2,w2,s2,u4,d2) ((u2)*16 + (n2)*4 + (s2))
  1052. #define STBVOX_MAKE_FACE_MASK(e,n,w,s,u,d) ((e)+(n)*2+(w)*4+(s)*8+(u)*16+(d)*32)
  1053. #define STBVOX_MAKE_SIDE_TEXROT(e,n,w,s) ((e)+(n)*4+(w)*16+(s)*64)
  1054. #define STBVOX_MAKE_COLOR(color,t1,t2) ((color)+(t1)*64+(t2)*128)
  1055. #define STBVOX_MAKE_TEXLERP_VERT3(e,n,w,s,u) ((e)+(n)*8+(w)*64+(s)*512+(u)*4096)
  1056. #define STBVOX_MAKE_TEXLERP_FACE3(e,n,w,s,u,d) ((e)+(n)*8+(w)*64+(s)*512+(u)*4096+(d)*16384)
  1057. #define STBVOX_MAKE_PACKED_COMPACT(rot, vheight, texlerp, def) ((rot)+4*(vheight)+16*(use)+32*(texlerp))
  1058. #define STBVOX_MAKE_LIGHTING_EXT(lighting, rot) (((lighting)&~3)+(rot))
  1059. #define STBVOX_MAKE_LIGHTING(lighting) (lighting)
  1060. #ifndef STBVOX_MAX_MESHES
  1061. #define STBVOX_MAX_MESHES 2 // opaque & transparent
  1062. #endif
  1063. #define STBVOX_MAX_MESH_SLOTS 3 // one vertex & two faces, or two vertex and one face
  1064. // don't mess with this directly, it's just here so you can
  1065. // declare stbvox_mesh_maker on the stack or as a global
  1066. struct stbvox_mesh_maker
  1067. {
  1068. stbvox_input_description input;
  1069. int cur_x, cur_y, cur_z; // last unprocessed voxel if it splits into multiple buffers
  1070. int x0,y0,z0,x1,y1,z1;
  1071. int x_stride_in_bytes;
  1072. int y_stride_in_bytes;
  1073. int config_dirty;
  1074. int default_mesh;
  1075. unsigned int tags;
  1076. int cube_vertex_offset[6][4]; // this allows access per-vertex data stored block-centered (like texlerp, ambient)
  1077. int vertex_gather_offset[6][4];
  1078. int pos_x,pos_y,pos_z;
  1079. int full;
  1080. // computed from user input
  1081. char *output_cur [STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS];
  1082. char *output_end [STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS];
  1083. char *output_buffer[STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS];
  1084. int output_len [STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS];
  1085. // computed from config
  1086. int output_size [STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS]; // per quad
  1087. int output_step [STBVOX_MAX_MESHES][STBVOX_MAX_MESH_SLOTS]; // per vertex or per face, depending
  1088. int num_mesh_slots;
  1089. float default_tex_scale[128][2];
  1090. };
  1091. #endif // INCLUDE_STB_VOXEL_RENDER_H
  1092. #ifdef STB_VOXEL_RENDER_IMPLEMENTATION
  1093. #include <stdlib.h>
  1094. #include <assert.h>
  1095. #include <string.h> // memset
  1096. // have to use our own names to avoid the _MSC_VER path having conflicting type names
  1097. #ifndef _MSC_VER
  1098. #include <stdint.h>
  1099. typedef uint16_t stbvox_uint16;
  1100. typedef uint32_t stbvox_uint32;
  1101. #else
  1102. typedef unsigned short stbvox_uint16;
  1103. typedef unsigned int stbvox_uint32;
  1104. #endif
  1105. #ifdef _MSC_VER
  1106. #define STBVOX_NOTUSED(v) (void)(v)
  1107. #else
  1108. #define STBVOX_NOTUSED(v) (void)sizeof(v)
  1109. #endif
  1110. #ifndef STBVOX_CONFIG_MODE
  1111. #error "Must defined STBVOX_CONFIG_MODE to select the mode"
  1112. #endif
  1113. #if defined(STBVOX_CONFIG_ROTATION_IN_LIGHTING) && defined(STBVOX_CONFIG_VHEIGHT_IN_LIGHTING)
  1114. #error "Can't store both rotation and vheight in lighting"
  1115. #endif
  1116. // The following are candidate voxel modes. Only modes 0, 1, and 20, and 21 are
  1117. // currently implemented. Reducing the storage-per-quad further
  1118. // shouldn't improve performance, although obviously it allow you
  1119. // to create larger worlds without streaming.
  1120. //
  1121. //
  1122. // ----------- Two textures ----------- -- One texture -- ---- Color only ----
  1123. // Mode: 0 1 2 3 4 5 6 10 11 12 20 21 22 23 24
  1124. // ============================================================================================================
  1125. // uses Tex Buffer n Y Y Y Y Y Y Y Y Y n Y Y Y Y
  1126. // bytes per quad 32 20 14 12 10 6 6 8 8 4 32 20 10 6 4
  1127. // non-blocks all all some some some slabs stairs some some none all all slabs slabs none
  1128. // tex1 256 256 256 256 256 256 256 256 256 256 n n n n n
  1129. // tex2 256 256 256 256 256 256 128 n n n n n n n n
  1130. // colors 64 64 64 64 64 64 64 8 n n 2^24 2^24 2^24 2^24 256
  1131. // vertex ao Y Y Y Y Y n n Y Y n Y Y Y n n
  1132. // vertex texlerp Y Y Y n n n n - - - - - - - -
  1133. // x&y extents 127 127 128 64 64 128 64 64 128 128 127 127 128 128 128
  1134. // z extents 255 255 128 64? 64? 64 64 32 64 128 255 255 128 64 128
  1135. // not sure why I only wrote down the above "result data" and didn't preserve
  1136. // the vertex formats, but here I've tried to reconstruct the designs...
  1137. // mode # 3 is wrong, one byte too large, but they may have been an error originally
  1138. // Mode: 0 1 2 3 4 5 6 10 11 12 20 21 22 23 24
  1139. // =============================================================================================================
  1140. // bytes per quad 32 20 14 12 10 6 6 8 8 4 20 10 6 4
  1141. //
  1142. // vertex x bits 7 7 0 6 0 0 0 0 0 0 7 0 0 0
  1143. // vertex y bits 7 7 0 0 0 0 0 0 0 0 7 0 0 0
  1144. // vertex z bits 9 9 7 4 2 0 0 2 2 0 9 2 0 0
  1145. // vertex ao bits 6 6 6 6 6 0 0 6 6 0 6 6 0 0
  1146. // vertex txl bits 3 3 3 0 0 0 0 0 0 0 (3) 0 0 0
  1147. //
  1148. // face tex1 bits (8) 8 8 8 8 8 8 8 8 8
  1149. // face tex2 bits (8) 8 8 8 8 8 7 - - -
  1150. // face color bits (8) 8 8 8 8 8 8 3 0 0 24 24 24 8
  1151. // face normal bits (8) 8 8 8 6 4 7 4 4 3 8 3 4 3
  1152. // face x bits 7 0 6 7 6 6 7 7 0 7 7 7
  1153. // face y bits 7 6 6 7 6 6 7 7 0 7 7 7
  1154. // face z bits 2 2 6 6 6 5 6 7 0 7 6 7
  1155. #if STBVOX_CONFIG_MODE==0 || STBVOX_CONFIG_MODE==1
  1156. #define STBVOX_ICONFIG_VERTEX_32
  1157. #define STBVOX_ICONFIG_FACE1_1
  1158. #elif STBVOX_CONFIG_MODE==20 || STBVOX_CONFIG_MODE==21
  1159. #define STBVOX_ICONFIG_VERTEX_32
  1160. #define STBVOX_ICONFIG_FACE1_1
  1161. #define STBVOX_ICONFIG_UNTEXTURED
  1162. #else
  1163. #error "Selected value of STBVOX_CONFIG_MODE is not supported"
  1164. #endif
  1165. #if STBVOX_CONFIG_MODE==0 || STBVOX_CONFIG_MODE==20
  1166. #define STBVOX_ICONFIG_FACE_ATTRIBUTE
  1167. #endif
  1168. #ifndef STBVOX_CONFIG_HLSL
  1169. // the fallback if all others are exhausted is GLSL
  1170. #define STBVOX_ICONFIG_GLSL
  1171. #endif
  1172. #ifdef STBVOX_CONFIG_OPENGL_MODELVIEW
  1173. #define STBVOX_ICONFIG_OPENGL_3_1_COMPATIBILITY
  1174. #endif
  1175. #if defined(STBVOX_ICONFIG_VERTEX_32)
  1176. typedef stbvox_uint32 stbvox_mesh_vertex;
  1177. #define stbvox_vertex_encode(x,y,z,ao,texlerp) \
  1178. ((stbvox_uint32) ((x)+((y)<<7)+((z)<<14)+((ao)<<23)+((texlerp)<<29)))
  1179. #elif defined(STBVOX_ICONFIG_VERTEX_16_1) // mode=2
  1180. typedef stbvox_uint16 stbvox_mesh_vertex;
  1181. #define stbvox_vertex_encode(x,y,z,ao,texlerp) \
  1182. ((stbvox_uint16) ((z)+((ao)<<7)+((texlerp)<<13)
  1183. #elif defined(STBVOX_ICONFIG_VERTEX_16_2) // mode=3
  1184. typedef stbvox_uint16 stbvox_mesh_vertex;
  1185. #define stbvox_vertex_encode(x,y,z,ao,texlerp) \
  1186. ((stbvox_uint16) ((x)+((z)<<6))+((ao)<<10))
  1187. #elif defined(STBVOX_ICONFIG_VERTEX_8)
  1188. typedef stbvox_uint8 stbvox_mesh_vertex;
  1189. #define stbvox_vertex_encode(x,y,z,ao,texlerp) \
  1190. ((stbvox_uint8) ((z)+((ao)<<6))
  1191. #else
  1192. #error "internal error, no vertex type"
  1193. #endif
  1194. #ifdef STBVOX_ICONFIG_FACE1_1
  1195. typedef struct
  1196. {
  1197. unsigned char tex1,tex2,color,face_info;
  1198. } stbvox_mesh_face;
  1199. #else
  1200. #error "internal error, no face type"
  1201. #endif
  1202. // 20-byte quad format:
  1203. //
  1204. // per vertex:
  1205. //
  1206. // x:7
  1207. // y:7
  1208. // z:9
  1209. // ao:6
  1210. // tex_lerp:3
  1211. //
  1212. // per face:
  1213. //
  1214. // tex1:8
  1215. // tex2:8
  1216. // face:8
  1217. // color:8
  1218. // Faces:
  1219. //
  1220. // Faces use the bottom 3 bits to choose the texgen
  1221. // mode, and all the bits to choose the normal.
  1222. // Thus the bottom 3 bits have to be:
  1223. // e, n, w, s, u, d, u, d
  1224. //
  1225. // These use compact names so tables are readable
  1226. enum
  1227. {
  1228. STBVF_e,
  1229. STBVF_n,
  1230. STBVF_w,
  1231. STBVF_s,
  1232. STBVF_u,
  1233. STBVF_d,
  1234. STBVF_eu,
  1235. STBVF_ed,
  1236. STBVF_eu_wall,
  1237. STBVF_nu_wall,
  1238. STBVF_wu_wall,
  1239. STBVF_su_wall,
  1240. STBVF_ne_u,
  1241. STBVF_ne_d,
  1242. STBVF_nu,
  1243. STBVF_nd,
  1244. STBVF_ed_wall,
  1245. STBVF_nd_wall,
  1246. STBVF_wd_wall,
  1247. STBVF_sd_wall,
  1248. STBVF_nw_u,
  1249. STBVF_nw_d,
  1250. STBVF_wu,
  1251. STBVF_wd,
  1252. STBVF_ne_u_cross,
  1253. STBVF_nw_u_cross,
  1254. STBVF_sw_u_cross,
  1255. STBVF_se_u_cross,
  1256. STBVF_sw_u,
  1257. STBVF_sw_d,
  1258. STBVF_su,
  1259. STBVF_sd,
  1260. // @TODO we need more than 5 bits to encode the normal to fit the following
  1261. // so for now we use the right projection but the wrong normal
  1262. STBVF_se_u = STBVF_su,
  1263. STBVF_se_d = STBVF_sd,
  1264. STBVF_count,
  1265. };
  1266. /////////////////////////////////////////////////////////////////////////////
  1267. //
  1268. // tables -- i'd prefer if these were at the end of the file, but: C++
  1269. //
  1270. static float stbvox_default_texgen[2][32][3] =
  1271. {
  1272. { { 0, 1,0 }, { 0, 0, 1 }, { 0,-1,0 }, { 0, 0,-1 },
  1273. { -1, 0,0 }, { 0, 0, 1 }, { 1, 0,0 }, { 0, 0,-1 },
  1274. { 0,-1,0 }, { 0, 0, 1 }, { 0, 1,0 }, { 0, 0,-1 },
  1275. { 1, 0,0 }, { 0, 0, 1 }, { -1, 0,0 }, { 0, 0,-1 },
  1276. { 1, 0,0 }, { 0, 1, 0 }, { -1, 0,0 }, { 0,-1, 0 },
  1277. { -1, 0,0 }, { 0,-1, 0 }, { 1, 0,0 }, { 0, 1, 0 },
  1278. { 1, 0,0 }, { 0, 1, 0 }, { -1, 0,0 }, { 0,-1, 0 },
  1279. { -1, 0,0 }, { 0,-1, 0 }, { 1, 0,0 }, { 0, 1, 0 },
  1280. },
  1281. { { 0, 0,-1 }, { 0, 1,0 }, { 0, 0, 1 }, { 0,-1,0 },
  1282. { 0, 0,-1 }, { -1, 0,0 }, { 0, 0, 1 }, { 1, 0,0 },
  1283. { 0, 0,-1 }, { 0,-1,0 }, { 0, 0, 1 }, { 0, 1,0 },
  1284. { 0, 0,-1 }, { 1, 0,0 }, { 0, 0, 1 }, { -1, 0,0 },
  1285. { 0,-1, 0 }, { 1, 0,0 }, { 0, 1, 0 }, { -1, 0,0 },
  1286. { 0, 1, 0 }, { -1, 0,0 }, { 0,-1, 0 }, { 1, 0,0 },
  1287. { 0,-1, 0 }, { 1, 0,0 }, { 0, 1, 0 }, { -1, 0,0 },
  1288. { 0, 1, 0 }, { -1, 0,0 }, { 0,-1, 0 }, { 1, 0,0 },
  1289. },
  1290. };
  1291. #define STBVOX_RSQRT2 0.7071067811865f
  1292. #define STBVOX_RSQRT3 0.5773502691896f
  1293. static float stbvox_default_normals[32][3] =
  1294. {
  1295. { 1,0,0 }, // east
  1296. { 0,1,0 }, // north
  1297. { -1,0,0 }, // west
  1298. { 0,-1,0 }, // south
  1299. { 0,0,1 }, // up
  1300. { 0,0,-1 }, // down
  1301. { STBVOX_RSQRT2,0, STBVOX_RSQRT2 }, // east & up
  1302. { STBVOX_RSQRT2,0, -STBVOX_RSQRT2 }, // east & down
  1303. { STBVOX_RSQRT2,0, STBVOX_RSQRT2 }, // east & up
  1304. { 0, STBVOX_RSQRT2, STBVOX_RSQRT2 }, // north & up
  1305. { -STBVOX_RSQRT2,0, STBVOX_RSQRT2 }, // west & up
  1306. { 0,-STBVOX_RSQRT2, STBVOX_RSQRT2 }, // south & up
  1307. { STBVOX_RSQRT3, STBVOX_RSQRT3, STBVOX_RSQRT3 }, // ne & up
  1308. { STBVOX_RSQRT3, STBVOX_RSQRT3,-STBVOX_RSQRT3 }, // ne & down
  1309. { 0, STBVOX_RSQRT2, STBVOX_RSQRT2 }, // north & up
  1310. { 0, STBVOX_RSQRT2, -STBVOX_RSQRT2 }, // north & down
  1311. { STBVOX_RSQRT2,0, -STBVOX_RSQRT2 }, // east & down
  1312. { 0, STBVOX_RSQRT2, -STBVOX_RSQRT2 }, // north & down
  1313. { -STBVOX_RSQRT2,0, -STBVOX_RSQRT2 }, // west & down
  1314. { 0,-STBVOX_RSQRT2, -STBVOX_RSQRT2 }, // south & down
  1315. { -STBVOX_RSQRT3, STBVOX_RSQRT3, STBVOX_RSQRT3 }, // NW & up
  1316. { -STBVOX_RSQRT3, STBVOX_RSQRT3,-STBVOX_RSQRT3 }, // NW & down
  1317. { -STBVOX_RSQRT2,0, STBVOX_RSQRT2 }, // west & up
  1318. { -STBVOX_RSQRT2,0, -STBVOX_RSQRT2 }, // west & down
  1319. { STBVOX_RSQRT3, STBVOX_RSQRT3,STBVOX_RSQRT3 }, // NE & up crossed
  1320. { -STBVOX_RSQRT3, STBVOX_RSQRT3,STBVOX_RSQRT3 }, // NW & up crossed
  1321. { -STBVOX_RSQRT3,-STBVOX_RSQRT3,STBVOX_RSQRT3 }, // SW & up crossed
  1322. { STBVOX_RSQRT3,-STBVOX_RSQRT3,STBVOX_RSQRT3 }, // SE & up crossed
  1323. { -STBVOX_RSQRT3,-STBVOX_RSQRT3, STBVOX_RSQRT3 }, // SW & up
  1324. { -STBVOX_RSQRT3,-STBVOX_RSQRT3,-STBVOX_RSQRT3 }, // SW & up
  1325. { 0,-STBVOX_RSQRT2, STBVOX_RSQRT2 }, // south & up
  1326. { 0,-STBVOX_RSQRT2, -STBVOX_RSQRT2 }, // south & down
  1327. };
  1328. static float stbvox_default_texscale[128][4] =
  1329. {
  1330. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1331. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1332. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1333. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1334. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1335. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1336. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1337. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1338. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1339. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1340. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1341. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1342. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1343. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1344. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1345. {1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0},
  1346. };
  1347. static unsigned char stbvox_default_palette_compact[64][3] =
  1348. {
  1349. { 255,255,255 }, { 238,238,238 }, { 221,221,221 }, { 204,204,204 },
  1350. { 187,187,187 }, { 170,170,170 }, { 153,153,153 }, { 136,136,136 },
  1351. { 119,119,119 }, { 102,102,102 }, { 85, 85, 85 }, { 68, 68, 68 },
  1352. { 51, 51, 51 }, { 34, 34, 34 }, { 17, 17, 17 }, { 0, 0, 0 },
  1353. { 255,240,240 }, { 255,220,220 }, { 255,160,160 }, { 255, 32, 32 },
  1354. { 200,120,160 }, { 200, 60,150 }, { 220,100,130 }, { 255, 0,128 },
  1355. { 240,240,255 }, { 220,220,255 }, { 160,160,255 }, { 32, 32,255 },
  1356. { 120,160,200 }, { 60,150,200 }, { 100,130,220 }, { 0,128,255 },
  1357. { 240,255,240 }, { 220,255,220 }, { 160,255,160 }, { 32,255, 32 },
  1358. { 160,200,120 }, { 150,200, 60 }, { 130,220,100 }, { 128,255, 0 },
  1359. { 255,255,240 }, { 255,255,220 }, { 220,220,180 }, { 255,255, 32 },
  1360. { 200,160,120 }, { 200,150, 60 }, { 220,130,100 }, { 255,128, 0 },
  1361. { 255,240,255 }, { 255,220,255 }, { 220,180,220 }, { 255, 32,255 },
  1362. { 160,120,200 }, { 150, 60,200 }, { 130,100,220 }, { 128, 0,255 },
  1363. { 240,255,255 }, { 220,255,255 }, { 180,220,220 }, { 32,255,255 },
  1364. { 120,200,160 }, { 60,200,150 }, { 100,220,130 }, { 0,255,128 },
  1365. };
  1366. static float stbvox_default_ambient[4][4] =
  1367. {
  1368. { 0,0,1 ,0 }, // reversed lighting direction
  1369. { 0.5,0.5,0.5,0 }, // directional color
  1370. { 0.5,0.5,0.5,0 }, // constant color
  1371. { 0.5,0.5,0.5,1.0f/1000.0f/1000.0f }, // fog data for simple_fog
  1372. };
  1373. static float stbvox_default_palette[64][4];
  1374. static void stbvox_build_default_palette(void)
  1375. {
  1376. int i;
  1377. for (i=0; i < 64; ++i) {
  1378. stbvox_default_palette[i][0] = stbvox_default_palette_compact[i][0] / 255.0f;
  1379. stbvox_default_palette[i][1] = stbvox_default_palette_compact[i][1] / 255.0f;
  1380. stbvox_default_palette[i][2] = stbvox_default_palette_compact[i][2] / 255.0f;
  1381. stbvox_default_palette[i][3] = 1.0f;
  1382. }
  1383. }
  1384. //////////////////////////////////////////////////////////////////////////////
  1385. //
  1386. // Shaders
  1387. //
  1388. #if defined(STBVOX_ICONFIG_OPENGL_3_1_COMPATIBILITY)
  1389. #define STBVOX_SHADER_VERSION "#version 150 compatibility\n"
  1390. #elif defined(STBVOX_ICONFIG_OPENGL_3_0)
  1391. #define STBVOX_SHADER_VERSION "#version 130\n"
  1392. #elif defined(STBVOX_ICONFIG_GLSL)
  1393. #define STBVOX_SHADER_VERSION "#version 150\n"
  1394. #else
  1395. #define STBVOX_SHADER_VERSION ""
  1396. #endif
  1397. static const char *stbvox_vertex_program =
  1398. {
  1399. STBVOX_SHADER_VERSION
  1400. #ifdef STBVOX_ICONFIG_FACE_ATTRIBUTE // NOT TAG_face_sampled
  1401. "in uvec4 attr_face;\n"
  1402. #else
  1403. "uniform usamplerBuffer facearray;\n"
  1404. #endif
  1405. #ifdef STBVOX_ICONFIG_FACE_ARRAY_2
  1406. "uniform usamplerBuffer facearray2;\n"
  1407. #endif
  1408. // vertex input data
  1409. "in uint attr_vertex;\n"
  1410. // per-buffer data
  1411. "uniform vec3 transform[3];\n"
  1412. // per-frame data
  1413. "uniform vec4 camera_pos;\n" // 4th value is used for arbitrary hacking
  1414. // to simplify things, we avoid using more than 256 uniform vectors
  1415. // in fragment shader to avoid possible 1024 component limit, so
  1416. // we access this table in the fragment shader.
  1417. "uniform vec3 normal_table[32];\n"
  1418. #ifndef STBVOX_CONFIG_OPENGL_MODELVIEW
  1419. "uniform mat4x4 model_view;\n"
  1420. #endif
  1421. // fragment output data
  1422. "flat out uvec4 facedata;\n"
  1423. " out vec3 voxelspace_pos;\n"
  1424. " out vec3 vnormal;\n"
  1425. " out float texlerp;\n"
  1426. " out float amb_occ;\n"
  1427. // @TODO handle the HLSL way to do this
  1428. "void main()\n"
  1429. "{\n"
  1430. #ifdef STBVOX_ICONFIG_FACE_ATTRIBUTE
  1431. " facedata = attr_face;\n"
  1432. #else
  1433. " int faceID = gl_VertexID >> 2;\n"
  1434. " facedata = texelFetch(facearray, faceID);\n"
  1435. #endif
  1436. // extract data for vertex
  1437. " vec3 offset;\n"
  1438. " offset.x = float( (attr_vertex ) & 127u );\n" // a[0..6]
  1439. " offset.y = float( (attr_vertex >> 7u) & 127u );\n" // a[7..13]
  1440. " offset.z = float( (attr_vertex >> 14u) & 511u );\n" // a[14..22]
  1441. " amb_occ = float( (attr_vertex >> 23u) & 63u ) / 63.0;\n" // a[23..28]
  1442. " texlerp = float( (attr_vertex >> 29u) ) / 7.0;\n" // a[29..31]
  1443. " vnormal = normal_table[(facedata.w>>2u) & 31u];\n"
  1444. " voxelspace_pos = offset * transform[0];\n" // mesh-to-object scale
  1445. " vec3 position = voxelspace_pos + transform[1];\n" // mesh-to-object translate
  1446. #ifdef STBVOX_DEBUG_TEST_NORMALS
  1447. " if ((facedata.w & 28u) == 16u || (facedata.w & 28u) == 24u)\n"
  1448. " position += vnormal.xyz * camera_pos.w;\n"
  1449. #endif
  1450. #ifndef STBVOX_CONFIG_OPENGL_MODELVIEW
  1451. " gl_Position = model_view * vec4(position,1.0);\n"
  1452. #else
  1453. " gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n"
  1454. #endif
  1455. "}\n"
  1456. };
  1457. static const char *stbvox_fragment_program =
  1458. {
  1459. STBVOX_SHADER_VERSION
  1460. // rlerp is lerp but with t on the left, like god intended
  1461. #if defined(STBVOX_ICONFIG_GLSL)
  1462. "#define rlerp(t,x,y) mix(x,y,t)\n"
  1463. #elif defined(STBVOX_CONFIG_HLSL)
  1464. "#define rlerp(t,x,y) lerp(x,y,t)\n"
  1465. #else
  1466. #error "need definition of rlerp()"
  1467. #endif
  1468. // vertex-shader output data
  1469. "flat in uvec4 facedata;\n"
  1470. " in vec3 voxelspace_pos;\n"
  1471. " in vec3 vnormal;\n"
  1472. " in float texlerp;\n"
  1473. " in float amb_occ;\n"
  1474. // per-buffer data
  1475. "uniform vec3 transform[3];\n"
  1476. // per-frame data
  1477. "uniform vec4 camera_pos;\n" // 4th value is used for arbitrary hacking
  1478. // probably constant data
  1479. "uniform vec4 ambient[4];\n"
  1480. #ifndef STBVOX_ICONFIG_UNTEXTURED
  1481. // generally constant data
  1482. "uniform sampler2DArray tex_array[2];\n"
  1483. #ifdef STBVOX_CONFIG_PREFER_TEXBUFFER
  1484. "uniform samplerBuffer color_table;\n"
  1485. "uniform samplerBuffer texscale;\n"
  1486. "uniform samplerBuffer texgen;\n"
  1487. #else
  1488. "uniform vec4 color_table[64];\n"
  1489. "uniform vec4 texscale[64];\n" // instead of 128, to avoid running out of uniforms
  1490. "uniform vec3 texgen[64];\n"
  1491. #endif
  1492. #endif
  1493. "out vec4 outcolor;\n"
  1494. #if defined(STBVOX_CONFIG_LIGHTING) || defined(STBVOX_CONFIG_LIGHTING_SIMPLE)
  1495. "vec3 compute_lighting(vec3 pos, vec3 norm, vec3 albedo, vec3 ambient);\n"
  1496. #endif
  1497. #if defined(STBVOX_CONFIG_FOG) || defined(STBVOX_CONFIG_FOG_SMOOTHSTEP)
  1498. "vec3 compute_fog(vec3 color, vec3 relative_pos, float fragment_alpha);\n"
  1499. #endif
  1500. "void main()\n"
  1501. "{\n"
  1502. " vec3 albedo;\n"
  1503. " float fragment_alpha;\n"
  1504. #ifndef STBVOX_ICONFIG_UNTEXTURED
  1505. // unpack the values
  1506. " uint tex1_id = facedata.x;\n"
  1507. " uint tex2_id = facedata.y;\n"
  1508. " uint texprojid = facedata.w & 31u;\n"
  1509. " uint color_id = facedata.z;\n"
  1510. #ifndef STBVOX_CONFIG_PREFER_TEXBUFFER
  1511. // load from uniforms / texture buffers
  1512. " vec3 texgen_s = texgen[texprojid];\n"
  1513. " vec3 texgen_t = texgen[texprojid+32u];\n"
  1514. " float tex1_scale = texscale[tex1_id & 63u].x;\n"
  1515. " vec4 color = color_table[color_id & 63u];\n"
  1516. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1517. " vec4 tex2_props = texscale[tex2_id & 63u];\n"
  1518. #endif
  1519. #else
  1520. " vec3 texgen_s = texelFetch(texgen, int(texprojid)).xyz;\n"
  1521. " vec3 texgen_t = texelFetch(texgen, int(texprojid+32u)).xyz;\n"
  1522. " float tex1_scale = texelFetch(texscale, int(tex1_id & 127u)).x;\n"
  1523. " vec4 color = texelFetch(color_table, int(color_id & 63u));\n"
  1524. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1525. " vec4 tex2_props = texelFetch(texscale, int(tex1_id & 127u));\n"
  1526. #endif
  1527. #endif
  1528. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1529. " float tex2_scale = tex2_props.y;\n"
  1530. " bool texblend_mode = tex2_props.z != 0.0;\n"
  1531. #endif
  1532. " vec2 texcoord;\n"
  1533. " vec3 texturespace_pos = voxelspace_pos + transform[2].xyz;\n"
  1534. " texcoord.s = dot(texturespace_pos, texgen_s);\n"
  1535. " texcoord.t = dot(texturespace_pos, texgen_t);\n"
  1536. " vec2 texcoord_1 = tex1_scale * texcoord;\n"
  1537. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1538. " vec2 texcoord_2 = tex2_scale * texcoord;\n"
  1539. #endif
  1540. #ifdef STBVOX_CONFIG_TEX1_EDGE_CLAMP
  1541. " texcoord_1 = texcoord_1 - floor(texcoord_1);\n"
  1542. " vec4 tex1 = textureGrad(tex_array[0], vec3(texcoord_1, float(tex1_id)), dFdx(tex1_scale*texcoord), dFdy(tex1_scale*texcoord));\n"
  1543. #else
  1544. " vec4 tex1 = texture(tex_array[0], vec3(texcoord_1, float(tex1_id)));\n"
  1545. #endif
  1546. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1547. #ifdef STBVOX_CONFIG_TEX2_EDGE_CLAMP
  1548. " texcoord_2 = texcoord_2 - floor(texcoord_2);\n"
  1549. " vec4 tex2 = textureGrad(tex_array[0], vec3(texcoord_2, float(tex2_id)), dFdx(tex2_scale*texcoord), dFdy(tex2_scale*texcoord));\n"
  1550. #else
  1551. " vec4 tex2 = texture(tex_array[1], vec3(texcoord_2, float(tex2_id)));\n"
  1552. #endif
  1553. #endif
  1554. " bool emissive = (color.a > 1.0);\n"
  1555. " color.a = min(color.a, 1.0);\n"
  1556. // recolor textures
  1557. " if ((color_id & 64u) != 0u) tex1.rgba *= color.rgba;\n"
  1558. " fragment_alpha = tex1.a;\n"
  1559. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1560. " if ((color_id & 128u) != 0u) tex2.rgba *= color.rgba;\n"
  1561. #ifdef STBVOX_CONFIG_PREMULTIPLIED_ALPHA
  1562. " tex2.rgba *= texlerp;\n"
  1563. #else
  1564. " tex2.a *= texlerp;\n"
  1565. #endif
  1566. " if (texblend_mode)\n"
  1567. " albedo = tex1.xyz * rlerp(tex2.a, vec3(1.0,1.0,1.0), 2.0*tex2.xyz);\n"
  1568. " else {\n"
  1569. #ifdef STBVOX_CONFIG_PREMULTIPLIED_ALPHA
  1570. " albedo = (1.0-tex2.a)*tex1.xyz + tex2.xyz;\n"
  1571. #else
  1572. " albedo = rlerp(tex2.a, tex1.xyz, tex2.xyz);\n"
  1573. #endif
  1574. " fragment_alpha = tex1.a*(1-tex2.a)+tex2.a;\n"
  1575. " }\n"
  1576. #else
  1577. " albedo = tex1.xyz;\n"
  1578. #endif
  1579. #else // UNTEXTURED
  1580. " vec4 color;"
  1581. " color.xyz = vec3(facedata.xyz) / 255.0;\n"
  1582. " bool emissive = false;\n"
  1583. " albedo = color.xyz;\n"
  1584. " fragment_alpha = 1.0;\n"
  1585. #endif
  1586. #ifdef STBVOX_ICONFIG_VARYING_VERTEX_NORMALS
  1587. // currently, there are no modes that trigger this path; idea is that there
  1588. // could be a couple of bits per vertex to perturb the normal to e.g. get curved look
  1589. " vec3 normal = normalize(vnormal);\n"
  1590. #else
  1591. " vec3 normal = vnormal;\n"
  1592. #endif
  1593. " vec3 ambient_color = dot(normal, ambient[0].xyz) * ambient[1].xyz + ambient[2].xyz;\n"
  1594. " ambient_color = clamp(ambient_color, 0.0, 1.0);"
  1595. " ambient_color *= amb_occ;\n"
  1596. " vec3 lit_color;\n"
  1597. " if (!emissive)\n"
  1598. #if defined(STBVOX_ICONFIG_LIGHTING) || defined(STBVOX_CONFIG_LIGHTING_SIMPLE)
  1599. " lit_color = compute_lighting(voxelspace_pos + transform[1], normal, albedo, ambient_color);\n"
  1600. #else
  1601. " lit_color = albedo * ambient_color ;\n"
  1602. #endif
  1603. " else\n"
  1604. " lit_color = albedo;\n"
  1605. #if defined(STBVOX_ICONFIG_FOG) || defined(STBVOX_CONFIG_FOG_SMOOTHSTEP)
  1606. " vec3 dist = voxelspace_pos + (transform[1] - camera_pos.xyz);\n"
  1607. " lit_color = compute_fog(lit_color, dist, fragment_alpha);\n"
  1608. #endif
  1609. #ifdef STBVOX_CONFIG_UNPREMULTIPLY
  1610. " vec4 final_color = vec4(lit_color/fragment_alpha, fragment_alpha);\n"
  1611. #else
  1612. " vec4 final_color = vec4(lit_color, fragment_alpha);\n"
  1613. #endif
  1614. " outcolor = final_color;\n"
  1615. "}\n"
  1616. #ifdef STBVOX_CONFIG_LIGHTING_SIMPLE
  1617. "\n"
  1618. "uniform vec3 light_source[2];\n"
  1619. "vec3 compute_lighting(vec3 pos, vec3 norm, vec3 albedo, vec3 ambient)\n"
  1620. "{\n"
  1621. " vec3 light_dir = light_source[0] - pos;\n"
  1622. " float lambert = dot(light_dir, norm) / dot(light_dir, light_dir);\n"
  1623. " vec3 diffuse = clamp(light_source[1] * clamp(lambert, 0.0, 1.0), 0.0, 1.0);\n"
  1624. " return (diffuse + ambient) * albedo;\n"
  1625. "}\n"
  1626. #endif
  1627. #ifdef STBVOX_CONFIG_FOG_SMOOTHSTEP
  1628. "\n"
  1629. "vec3 compute_fog(vec3 color, vec3 relative_pos, float fragment_alpha)\n"
  1630. "{\n"
  1631. " float f = dot(relative_pos,relative_pos)*ambient[3].w;\n"
  1632. //" f = rlerp(f, -2,1);\n"
  1633. " f = clamp(f, 0.0, 1.0);\n"
  1634. " f = 3.0*f*f - 2.0*f*f*f;\n" // smoothstep
  1635. //" f = f*f;\n" // fade in more smoothly
  1636. #ifdef STBVOX_CONFIG_PREMULTIPLIED_ALPHA
  1637. " return rlerp(f, color.xyz, ambient[3].xyz*fragment_alpha);\n"
  1638. #else
  1639. " return rlerp(f, color.xyz, ambient[3].xyz);\n"
  1640. #endif
  1641. "}\n"
  1642. #endif
  1643. };
  1644. // still requires full alpha lookups, including tex2 if texblend is enabled
  1645. static const char *stbvox_fragment_program_alpha_only =
  1646. {
  1647. STBVOX_SHADER_VERSION
  1648. // vertex-shader output data
  1649. "flat in uvec4 facedata;\n"
  1650. " in vec3 voxelspace_pos;\n"
  1651. " in float texlerp;\n"
  1652. // per-buffer data
  1653. "uniform vec3 transform[3];\n"
  1654. #ifndef STBVOX_ICONFIG_UNTEXTURED
  1655. // generally constant data
  1656. "uniform sampler2DArray tex_array[2];\n"
  1657. #ifdef STBVOX_CONFIG_PREFER_TEXBUFFER
  1658. "uniform samplerBuffer texscale;\n"
  1659. "uniform samplerBuffer texgen;\n"
  1660. #else
  1661. "uniform vec4 texscale[64];\n" // instead of 128, to avoid running out of uniforms
  1662. "uniform vec3 texgen[64];\n"
  1663. #endif
  1664. #endif
  1665. "out vec4 outcolor;\n"
  1666. "void main()\n"
  1667. "{\n"
  1668. " vec3 albedo;\n"
  1669. " float fragment_alpha;\n"
  1670. #ifndef STBVOX_ICONFIG_UNTEXTURED
  1671. // unpack the values
  1672. " uint tex1_id = facedata.x;\n"
  1673. " uint tex2_id = facedata.y;\n"
  1674. " uint texprojid = facedata.w & 31u;\n"
  1675. " uint color_id = facedata.z;\n"
  1676. #ifndef STBVOX_CONFIG_PREFER_TEXBUFFER
  1677. // load from uniforms / texture buffers
  1678. " vec3 texgen_s = texgen[texprojid];\n"
  1679. " vec3 texgen_t = texgen[texprojid+32u];\n"
  1680. " float tex1_scale = texscale[tex1_id & 63u].x;\n"
  1681. " vec4 color = color_table[color_id & 63u];\n"
  1682. " vec4 tex2_props = texscale[tex2_id & 63u];\n"
  1683. #else
  1684. " vec3 texgen_s = texelFetch(texgen, int(texprojid)).xyz;\n"
  1685. " vec3 texgen_t = texelFetch(texgen, int(texprojid+32u)).xyz;\n"
  1686. " float tex1_scale = texelFetch(texscale, int(tex1_id & 127u)).x;\n"
  1687. " vec4 color = texelFetch(color_table, int(color_id & 63u));\n"
  1688. " vec4 tex2_props = texelFetch(texscale, int(tex2_id & 127u));\n"
  1689. #endif
  1690. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1691. " float tex2_scale = tex2_props.y;\n"
  1692. " bool texblend_mode = tex2_props.z &((facedata.w & 128u) != 0u);\n"
  1693. #endif
  1694. " color.a = min(color.a, 1.0);\n"
  1695. " vec2 texcoord;\n"
  1696. " vec3 texturespace_pos = voxelspace_pos + transform[2].xyz;\n"
  1697. " texcoord.s = dot(texturespace_pos, texgen_s);\n"
  1698. " texcoord.t = dot(texturespace_pos, texgen_t);\n"
  1699. " vec2 texcoord_1 = tex1_scale * texcoord;\n"
  1700. " vec2 texcoord_2 = tex2_scale * texcoord;\n"
  1701. #ifdef STBVOX_CONFIG_TEX1_EDGE_CLAMP
  1702. " texcoord_1 = texcoord_1 - floor(texcoord_1);\n"
  1703. " vec4 tex1 = textureGrad(tex_array[0], vec3(texcoord_1, float(tex1_id)), dFdx(tex1_scale*texcoord), dFdy(tex1_scale*texcoord));\n"
  1704. #else
  1705. " vec4 tex1 = texture(tex_array[0], vec3(texcoord_1, float(tex1_id)));\n"
  1706. #endif
  1707. " if ((color_id & 64u) != 0u) tex1.a *= color.a;\n"
  1708. " fragment_alpha = tex1.a;\n"
  1709. #ifndef STBVOX_CONFIG_DISABLE_TEX2
  1710. " if (!texblend_mode) {\n"
  1711. #ifdef STBVOX_CONFIG_TEX2_EDGE_CLAMP
  1712. " texcoord_2 = texcoord_2 - floor(texcoord_2);\n"
  1713. " vec4 tex2 = textureGrad(tex_array[0], vec3(texcoord_2, float(tex2_id)), dFdx(tex2_scale*texcoord), dFdy(tex2_scale*texcoord));\n"
  1714. #else
  1715. " vec4 tex2 = texture(tex_array[1], vec3(texcoord_2, float(tex2_id)));\n"
  1716. #endif
  1717. " tex2.a *= texlerp;\n"
  1718. " if ((color_id & 128u) != 0u) tex2.rgba *= color.a;\n"
  1719. " fragment_alpha = tex1.a*(1-tex2.a)+tex2.a;\n"
  1720. "}\n"
  1721. "\n"
  1722. #endif
  1723. #else // UNTEXTURED
  1724. " fragment_alpha = 1.0;\n"
  1725. #endif
  1726. " outcolor = vec4(0.0, 0.0, 0.0, fragment_alpha);\n"
  1727. "}\n"
  1728. };
  1729. STBVXDEC char *stbvox_get_vertex_shader(void)
  1730. {
  1731. return (char *) stbvox_vertex_program;
  1732. }
  1733. STBVXDEC char *stbvox_get_fragment_shader(void)
  1734. {
  1735. return (char *) stbvox_fragment_program;
  1736. }
  1737. STBVXDEC char *stbvox_get_fragment_shader_alpha_only(void)
  1738. {
  1739. return (char *) stbvox_fragment_program_alpha_only;
  1740. }
  1741. static float stbvox_dummy_transform[3][3];
  1742. #ifdef STBVOX_CONFIG_PREFER_TEXBUFFER
  1743. #define STBVOX_TEXBUF 1
  1744. #else
  1745. #define STBVOX_TEXBUF 0
  1746. #endif
  1747. static stbvox_uniform_info stbvox_uniforms[] =
  1748. {
  1749. { STBVOX_UNIFORM_TYPE_sampler , 4, 1, (char*) "facearray" , 0 },
  1750. { STBVOX_UNIFORM_TYPE_vec3 , 12, 3, (char*) "transform" , stbvox_dummy_transform[0] },
  1751. { STBVOX_UNIFORM_TYPE_sampler , 4, 2, (char*) "tex_array" , 0 },
  1752. { STBVOX_UNIFORM_TYPE_vec4 , 16, 128, (char*) "texscale" , stbvox_default_texscale[0] , STBVOX_TEXBUF },
  1753. { STBVOX_UNIFORM_TYPE_vec4 , 16, 64, (char*) "color_table" , stbvox_default_palette[0] , STBVOX_TEXBUF },
  1754. { STBVOX_UNIFORM_TYPE_vec3 , 12, 32, (char*) "normal_table" , stbvox_default_normals[0] },
  1755. { STBVOX_UNIFORM_TYPE_vec3 , 12, 64, (char*) "texgen" , stbvox_default_texgen[0][0], STBVOX_TEXBUF },
  1756. { STBVOX_UNIFORM_TYPE_vec4 , 16, 4, (char*) "ambient" , stbvox_default_ambient[0] },
  1757. { STBVOX_UNIFORM_TYPE_vec4 , 16, 1, (char*) "camera_pos" , stbvox_dummy_transform[0] },
  1758. };
  1759. STBVXDEC int stbvox_get_uniform_info(stbvox_uniform_info *info, int uniform)
  1760. {
  1761. if (uniform < 0 || uniform >= STBVOX_UNIFORM_count)
  1762. return 0;
  1763. *info = stbvox_uniforms[uniform];
  1764. return 1;
  1765. }
  1766. #define STBVOX_GET_GEO(geom_data) ((geom_data) & 15)
  1767. typedef struct
  1768. {
  1769. unsigned char block:2;
  1770. unsigned char overlay:2;
  1771. unsigned char facerot:2;
  1772. unsigned char ecolor:2;
  1773. } stbvox_rotate;
  1774. typedef struct
  1775. {
  1776. unsigned char x,y,z;
  1777. } stbvox_pos;
  1778. static unsigned char stbvox_rotate_face[6][4] =
  1779. {
  1780. { 0,1,2,3 },
  1781. { 1,2,3,0 },
  1782. { 2,3,0,1 },
  1783. { 3,0,1,2 },
  1784. { 4,4,4,4 },
  1785. { 5,5,5,5 },
  1786. };
  1787. #define STBVOX_ROTATE(x,r) stbvox_rotate_face[x][r] // (((x)+(r))&3)
  1788. stbvox_mesh_face stbvox_compute_mesh_face_value(stbvox_mesh_maker *mm, stbvox_rotate rot, int face, int v_off, int normal)
  1789. {
  1790. stbvox_mesh_face face_data = { 0 };
  1791. stbvox_block_type bt = mm->input.blocktype[v_off];
  1792. unsigned char bt_face = STBVOX_ROTATE(face, rot.block);
  1793. int facerot = rot.facerot;
  1794. #ifdef STBVOX_ICONFIG_UNTEXTURED
  1795. if (mm->input.rgb) {
  1796. face_data.tex1 = mm->input.rgb[v_off].r;
  1797. face_data.tex2 = mm->input.rgb[v_off].g;
  1798. face_data.color = mm->input.rgb[v_off].b;
  1799. face_data.face_info = (normal<<2);
  1800. return face_data;
  1801. }
  1802. #else
  1803. unsigned char color_face;
  1804. if (mm->input.color)
  1805. face_data.color = mm->input.color[v_off];
  1806. if (mm->input.block_tex1)
  1807. face_data.tex1 = mm->input.block_tex1[bt];
  1808. else if (mm->input.block_tex1_face)
  1809. face_data.tex1 = mm->input.block_tex1_face[bt][bt_face];
  1810. else
  1811. face_data.tex1 = bt;
  1812. if (mm->input.block_tex2)
  1813. face_data.tex2 = mm->input.block_tex2[bt];
  1814. else if (mm->input.block_tex2_face)
  1815. face_data.tex2 = mm->input.block_tex2_face[bt][bt_face];
  1816. if (mm->input.block_color) {
  1817. unsigned char mcol = mm->input.block_color[bt];
  1818. if (mcol)
  1819. face_data.color = mcol;
  1820. } else if (mm->input.block_color_face) {
  1821. unsigned char mcol = mm->input.block_color_face[bt][bt_face];
  1822. if (mcol)
  1823. face_data.color = mcol;
  1824. }
  1825. if (face <= STBVOX_FACE_south) {
  1826. if (mm->input.side_texrot)
  1827. facerot = mm->input.side_texrot[v_off] >> (2 * face);
  1828. else if (mm->input.block_side_texrot)
  1829. facerot = mm->input.block_side_texrot[v_off] >> (2 * bt_face);
  1830. }
  1831. if (mm->input.overlay) {
  1832. int over_face = STBVOX_ROTATE(face, rot.overlay);
  1833. unsigned char over = mm->input.overlay[v_off];
  1834. if (over) {
  1835. if (mm->input.overlay_tex1) {
  1836. unsigned char rep1 = mm->input.overlay_tex1[over][over_face];
  1837. if (rep1)
  1838. face_data.tex1 = rep1;
  1839. }
  1840. if (mm->input.overlay_tex2) {
  1841. unsigned char rep2 = mm->input.overlay_tex2[over][over_face];
  1842. if (rep2)
  1843. face_data.tex2 = rep2;
  1844. }
  1845. if (mm->input.overlay_color) {
  1846. unsigned char rep3 = mm->input.overlay_color[over][over_face];
  1847. if (rep3)
  1848. face_data.color = rep3;
  1849. }
  1850. if (mm->input.overlay_side_texrot && face <= STBVOX_FACE_south)
  1851. facerot = mm->input.overlay_side_texrot[over] >> (2*over_face);
  1852. }
  1853. }
  1854. if (mm->input.tex2_for_tex1)
  1855. face_data.tex2 = mm->input.tex2_for_tex1[face_data.tex1];
  1856. if (mm->input.tex2)
  1857. face_data.tex2 = mm->input.tex2[v_off];
  1858. if (mm->input.tex2_replace) {
  1859. if (mm->input.tex2_facemask[v_off] & (1 << face))
  1860. face_data.tex2 = mm->input.tex2_replace[v_off];
  1861. }
  1862. color_face = STBVOX_ROTATE(face, rot.ecolor);
  1863. if (mm->input.extended_color) {
  1864. unsigned char ec = mm->input.extended_color[v_off];
  1865. if (mm->input.ecolor_facemask[ec] & (1 << color_face))
  1866. face_data.color = mm->input.ecolor_color[ec];
  1867. }
  1868. if (mm->input.color2) {
  1869. if (mm->input.color2_facemask[v_off] & (1 << color_face))
  1870. face_data.color = mm->input.color2[v_off];
  1871. if (mm->input.color3 && (mm->input.color3_facemask[v_off] & (1 << color_face)))
  1872. face_data.color = mm->input.color3[v_off];
  1873. }
  1874. #endif
  1875. face_data.face_info = (normal<<2) + facerot;
  1876. return face_data;
  1877. }
  1878. // these are the types of faces each block can have
  1879. enum
  1880. {
  1881. STBVOX_FT_none ,
  1882. STBVOX_FT_upper ,
  1883. STBVOX_FT_lower ,
  1884. STBVOX_FT_solid ,
  1885. STBVOX_FT_diag_012,
  1886. STBVOX_FT_diag_023,
  1887. STBVOX_FT_diag_013,
  1888. STBVOX_FT_diag_123,
  1889. STBVOX_FT_force , // can't be covered up, used for internal faces, also hides nothing
  1890. STBVOX_FT_partial , // only covered by solid, never covers anything else
  1891. STBVOX_FT_count
  1892. };
  1893. static unsigned char stbvox_face_lerp[6] = { 0,2,0,2,4,4 };
  1894. static unsigned char stbvox_vert3_lerp[5] = { 0,3,6,9,12 };
  1895. static unsigned char stbvox_vert_lerp_for_face_lerp[4] = { 0, 4, 7, 7 };
  1896. static unsigned char stbvox_face3_lerp[6] = { 0,3,6,9,12,14 };
  1897. static unsigned char stbvox_vert_lerp_for_simple[4] = { 0,2,5,7 };
  1898. static unsigned char stbvox_face3_updown[8] = { 0,2,5,7,0,2,5,7 }; // ignore top bit
  1899. // vertex offsets for face vertices
  1900. static unsigned char stbvox_vertex_vector[6][4][3] =
  1901. {
  1902. { { 1,0,1 }, { 1,1,1 }, { 1,1,0 }, { 1,0,0 } }, // east
  1903. { { 1,1,1 }, { 0,1,1 }, { 0,1,0 }, { 1,1,0 } }, // north
  1904. { { 0,1,1 }, { 0,0,1 }, { 0,0,0 }, { 0,1,0 } }, // west
  1905. { { 0,0,1 }, { 1,0,1 }, { 1,0,0 }, { 0,0,0 } }, // south
  1906. { { 0,1,1 }, { 1,1,1 }, { 1,0,1 }, { 0,0,1 } }, // up
  1907. { { 0,0,0 }, { 1,0,0 }, { 1,1,0 }, { 0,1,0 } }, // down
  1908. };
  1909. // stbvox_vertex_vector, but read coordinates as binary numbers, zyx
  1910. static unsigned char stbvox_vertex_selector[6][4] =
  1911. {
  1912. { 5,7,3,1 },
  1913. { 7,6,2,3 },
  1914. { 6,4,0,2 },
  1915. { 4,5,1,0 },
  1916. { 6,7,5,4 },
  1917. { 0,1,3,2 },
  1918. };
  1919. static stbvox_mesh_vertex stbvox_vmesh_delta_normal[6][4] =
  1920. {
  1921. { stbvox_vertex_encode(1,0,1,0,0) ,
  1922. stbvox_vertex_encode(1,1,1,0,0) ,
  1923. stbvox_vertex_encode(1,1,0,0,0) ,
  1924. stbvox_vertex_encode(1,0,0,0,0) },
  1925. { stbvox_vertex_encode(1,1,1,0,0) ,
  1926. stbvox_vertex_encode(0,1,1,0,0) ,
  1927. stbvox_vertex_encode(0,1,0,0,0) ,
  1928. stbvox_vertex_encode(1,1,0,0,0) },
  1929. { stbvox_vertex_encode(0,1,1,0,0) ,
  1930. stbvox_vertex_encode(0,0,1,0,0) ,
  1931. stbvox_vertex_encode(0,0,0,0,0) ,
  1932. stbvox_vertex_encode(0,1,0,0,0) },
  1933. { stbvox_vertex_encode(0,0,1,0,0) ,
  1934. stbvox_vertex_encode(1,0,1,0,0) ,
  1935. stbvox_vertex_encode(1,0,0,0,0) ,
  1936. stbvox_vertex_encode(0,0,0,0,0) },
  1937. { stbvox_vertex_encode(0,1,1,0,0) ,
  1938. stbvox_vertex_encode(1,1,1,0,0) ,
  1939. stbvox_vertex_encode(1,0,1,0,0) ,
  1940. stbvox_vertex_encode(0,0,1,0,0) },
  1941. { stbvox_vertex_encode(0,0,0,0,0) ,
  1942. stbvox_vertex_encode(1,0,0,0,0) ,
  1943. stbvox_vertex_encode(1,1,0,0,0) ,
  1944. stbvox_vertex_encode(0,1,0,0,0) }
  1945. };
  1946. static stbvox_mesh_vertex stbvox_vmesh_pre_vheight[6][4] =
  1947. {
  1948. { stbvox_vertex_encode(1,0,0,0,0) ,
  1949. stbvox_vertex_encode(1,1,0,0,0) ,
  1950. stbvox_vertex_encode(1,1,0,0,0) ,
  1951. stbvox_vertex_encode(1,0,0,0,0) },
  1952. { stbvox_vertex_encode(1,1,0,0,0) ,
  1953. stbvox_vertex_encode(0,1,0,0,0) ,
  1954. stbvox_vertex_encode(0,1,0,0,0) ,
  1955. stbvox_vertex_encode(1,1,0,0,0) },
  1956. { stbvox_vertex_encode(0,1,0,0,0) ,
  1957. stbvox_vertex_encode(0,0,0,0,0) ,
  1958. stbvox_vertex_encode(0,0,0,0,0) ,
  1959. stbvox_vertex_encode(0,1,0,0,0) },
  1960. { stbvox_vertex_encode(0,0,0,0,0) ,
  1961. stbvox_vertex_encode(1,0,0,0,0) ,
  1962. stbvox_vertex_encode(1,0,0,0,0) ,
  1963. stbvox_vertex_encode(0,0,0,0,0) },
  1964. { stbvox_vertex_encode(0,1,0,0,0) ,
  1965. stbvox_vertex_encode(1,1,0,0,0) ,
  1966. stbvox_vertex_encode(1,0,0,0,0) ,
  1967. stbvox_vertex_encode(0,0,0,0,0) },
  1968. { stbvox_vertex_encode(0,0,0,0,0) ,
  1969. stbvox_vertex_encode(1,0,0,0,0) ,
  1970. stbvox_vertex_encode(1,1,0,0,0) ,
  1971. stbvox_vertex_encode(0,1,0,0,0) }
  1972. };
  1973. static stbvox_mesh_vertex stbvox_vmesh_delta_half_z[6][4] =
  1974. {
  1975. { stbvox_vertex_encode(1,0,2,0,0) ,
  1976. stbvox_vertex_encode(1,1,2,0,0) ,
  1977. stbvox_vertex_encode(1,1,0,0,0) ,
  1978. stbvox_vertex_encode(1,0,0,0,0) },
  1979. { stbvox_vertex_encode(1,1,2,0,0) ,
  1980. stbvox_vertex_encode(0,1,2,0,0) ,
  1981. stbvox_vertex_encode(0,1,0,0,0) ,
  1982. stbvox_vertex_encode(1,1,0,0,0) },
  1983. { stbvox_vertex_encode(0,1,2,0,0) ,
  1984. stbvox_vertex_encode(0,0,2,0,0) ,
  1985. stbvox_vertex_encode(0,0,0,0,0) ,
  1986. stbvox_vertex_encode(0,1,0,0,0) },
  1987. { stbvox_vertex_encode(0,0,2,0,0) ,
  1988. stbvox_vertex_encode(1,0,2,0,0) ,
  1989. stbvox_vertex_encode(1,0,0,0,0) ,
  1990. stbvox_vertex_encode(0,0,0,0,0) },
  1991. { stbvox_vertex_encode(0,1,2,0,0) ,
  1992. stbvox_vertex_encode(1,1,2,0,0) ,
  1993. stbvox_vertex_encode(1,0,2,0,0) ,
  1994. stbvox_vertex_encode(0,0,2,0,0) },
  1995. { stbvox_vertex_encode(0,0,0,0,0) ,
  1996. stbvox_vertex_encode(1,0,0,0,0) ,
  1997. stbvox_vertex_encode(1,1,0,0,0) ,
  1998. stbvox_vertex_encode(0,1,0,0,0) }
  1999. };
  2000. static stbvox_mesh_vertex stbvox_vmesh_crossed_pair[6][4] =
  2001. {
  2002. { stbvox_vertex_encode(1,0,2,0,0) ,
  2003. stbvox_vertex_encode(0,1,2,0,0) ,
  2004. stbvox_vertex_encode(0,1,0,0,0) ,
  2005. stbvox_vertex_encode(1,0,0,0,0) },
  2006. { stbvox_vertex_encode(1,1,2,0,0) ,
  2007. stbvox_vertex_encode(0,0,2,0,0) ,
  2008. stbvox_vertex_encode(0,0,0,0,0) ,
  2009. stbvox_vertex_encode(1,1,0,0,0) },
  2010. { stbvox_vertex_encode(0,1,2,0,0) ,
  2011. stbvox_vertex_encode(1,0,2,0,0) ,
  2012. stbvox_vertex_encode(1,0,0,0,0) ,
  2013. stbvox_vertex_encode(0,1,0,0,0) },
  2014. { stbvox_vertex_encode(0,0,2,0,0) ,
  2015. stbvox_vertex_encode(1,1,2,0,0) ,
  2016. stbvox_vertex_encode(1,1,0,0,0) ,
  2017. stbvox_vertex_encode(0,0,0,0,0) },
  2018. // not used, so we leave it non-degenerate to make sure it doesn't get gen'd accidentally
  2019. { stbvox_vertex_encode(0,1,2,0,0) ,
  2020. stbvox_vertex_encode(1,1,2,0,0) ,
  2021. stbvox_vertex_encode(1,0,2,0,0) ,
  2022. stbvox_vertex_encode(0,0,2,0,0) },
  2023. { stbvox_vertex_encode(0,0,0,0,0) ,
  2024. stbvox_vertex_encode(1,0,0,0,0) ,
  2025. stbvox_vertex_encode(1,1,0,0,0) ,
  2026. stbvox_vertex_encode(0,1,0,0,0) }
  2027. };
  2028. #define STBVOX_MAX_GEOM 16
  2029. #define STBVOX_NUM_ROTATION 4
  2030. // this is used to determine if a face is ever generated at all
  2031. static unsigned char stbvox_hasface[STBVOX_MAX_GEOM][STBVOX_NUM_ROTATION] =
  2032. {
  2033. { 0,0,0,0 }, // empty
  2034. { 0,0,0,0 }, // knockout
  2035. { 63,63,63,63 }, // solid
  2036. { 63,63,63,63 }, // transp
  2037. { 63,63,63,63 }, // slab
  2038. { 63,63,63,63 }, // slab
  2039. { 1|2|4|48, 8|1|2|48, 4|8|1|48, 2|4|8|48, }, // floor slopes
  2040. { 1|2|4|48, 8|1|2|48, 4|8|1|48, 2|4|8|48, }, // ceil slopes
  2041. { 47,47,47,47 }, // wall-projected diagonal with down face
  2042. { 31,31,31,31 }, // wall-projected diagonal with up face
  2043. { 63,63,63,63 }, // crossed-pair has special handling, but avoid early-out
  2044. { 63,63,63,63 }, // force
  2045. { 63,63,63,63 }, // vheight
  2046. { 63,63,63,63 }, // vheight
  2047. { 63,63,63,63 }, // vheight
  2048. { 63,63,63,63 }, // vheight
  2049. };
  2050. // this determines which face type above is visible on each side of the geometry
  2051. static unsigned char stbvox_facetype[STBVOX_GEOM_count][6] =
  2052. {
  2053. { 0, }, // STBVOX_GEOM_empty
  2054. { STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid }, // knockout
  2055. { STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid, STBVOX_FT_solid }, // solid
  2056. { STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force }, // transp
  2057. { STBVOX_FT_upper, STBVOX_FT_upper, STBVOX_FT_upper, STBVOX_FT_upper, STBVOX_FT_solid, STBVOX_FT_force },
  2058. { STBVOX_FT_lower, STBVOX_FT_lower, STBVOX_FT_lower, STBVOX_FT_lower, STBVOX_FT_force, STBVOX_FT_solid },
  2059. { STBVOX_FT_diag_123, STBVOX_FT_solid, STBVOX_FT_diag_023, STBVOX_FT_none, STBVOX_FT_force, STBVOX_FT_solid },
  2060. { STBVOX_FT_diag_012, STBVOX_FT_solid, STBVOX_FT_diag_013, STBVOX_FT_none, STBVOX_FT_solid, STBVOX_FT_force },
  2061. { STBVOX_FT_diag_123, STBVOX_FT_solid, STBVOX_FT_diag_023, STBVOX_FT_force, STBVOX_FT_none, STBVOX_FT_solid },
  2062. { STBVOX_FT_diag_012, STBVOX_FT_solid, STBVOX_FT_diag_013, STBVOX_FT_force, STBVOX_FT_solid, STBVOX_FT_none },
  2063. { STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, 0,0 }, // crossed pair
  2064. { STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force, STBVOX_FT_force }, // GEOM_force
  2065. { STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial, STBVOX_FT_force, STBVOX_FT_solid }, // floor vheight, all neighbors forced
  2066. { STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial, STBVOX_FT_force, STBVOX_FT_solid }, // floor vheight, all neighbors forced
  2067. { STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial, STBVOX_FT_solid, STBVOX_FT_force }, // ceil vheight, all neighbors forced
  2068. { STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial,STBVOX_FT_partial, STBVOX_FT_solid, STBVOX_FT_force }, // ceil vheight, all neighbors forced
  2069. };
  2070. // This table indicates what normal to use for the "up" face of a sloped geom
  2071. // @TODO this could be done with math given the current arrangement of the enum, but let's not require it
  2072. static unsigned char stbvox_floor_slope_for_rot[4] =
  2073. {
  2074. STBVF_su,
  2075. STBVF_wu, // @TODO: why is this reversed from what it should be? this is a north-is-up face, so slope should be south&up
  2076. STBVF_nu,
  2077. STBVF_eu,
  2078. };
  2079. static unsigned char stbvox_ceil_slope_for_rot[4] =
  2080. {
  2081. STBVF_sd,
  2082. STBVF_ed,
  2083. STBVF_nd,
  2084. STBVF_wd,
  2085. };
  2086. // this table indicates whether, for each pair of types above, a face is visible.
  2087. // each value indicates whether a given type is visible for all neighbor types
  2088. static unsigned short stbvox_face_visible[STBVOX_FT_count] =
  2089. {
  2090. // we encode the table by listing which cases cause *obscuration*, and bitwise inverting that
  2091. // table is pre-shifted by 5 to save a shift when it's accessed
  2092. (unsigned short) ((~0x07ff )<<5), // none is completely obscured by everything
  2093. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_upper) ))<<5), // upper
  2094. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_lower) ))<<5), // lower
  2095. (unsigned short) ((~((1<<STBVOX_FT_solid) ))<<5), // solid is only completely obscured only by solid
  2096. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_diag_013)))<<5), // diag012 matches diag013
  2097. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_diag_123)))<<5), // diag023 matches diag123
  2098. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_diag_012)))<<5), // diag013 matches diag012
  2099. (unsigned short) ((~((1<<STBVOX_FT_solid) | (1<<STBVOX_FT_diag_023)))<<5), // diag123 matches diag023
  2100. (unsigned short) ((~0 )<<5), // force is always rendered regardless, always forces neighbor
  2101. (unsigned short) ((~((1<<STBVOX_FT_solid) ))<<5), // partial is only completely obscured only by solid
  2102. };
  2103. // the vertex heights of the block types, in binary vertex order (zyx):
  2104. // lower: SW, SE, NW, NE; upper: SW, SE, NW, NE
  2105. static stbvox_mesh_vertex stbvox_geometry_vheight[8][8] =
  2106. {
  2107. #define STBVOX_HEIGHTS(a,b,c,d,e,f,g,h) \
  2108. { stbvox_vertex_encode(0,0,a,0,0), \
  2109. stbvox_vertex_encode(0,0,b,0,0), \
  2110. stbvox_vertex_encode(0,0,c,0,0), \
  2111. stbvox_vertex_encode(0,0,d,0,0), \
  2112. stbvox_vertex_encode(0,0,e,0,0), \
  2113. stbvox_vertex_encode(0,0,f,0,0), \
  2114. stbvox_vertex_encode(0,0,g,0,0), \
  2115. stbvox_vertex_encode(0,0,h,0,0) }
  2116. STBVOX_HEIGHTS(0,0,0,0, 2,2,2,2),
  2117. STBVOX_HEIGHTS(0,0,0,0, 2,2,2,2),
  2118. STBVOX_HEIGHTS(0,0,0,0, 2,2,2,2),
  2119. STBVOX_HEIGHTS(0,0,0,0, 2,2,2,2),
  2120. STBVOX_HEIGHTS(1,1,1,1, 2,2,2,2),
  2121. STBVOX_HEIGHTS(0,0,0,0, 1,1,1,1),
  2122. STBVOX_HEIGHTS(0,0,0,0, 0,0,2,2),
  2123. STBVOX_HEIGHTS(2,2,0,0, 2,2,2,2),
  2124. };
  2125. // rotate vertices defined as [z][y][x] coords
  2126. static unsigned char stbvox_rotate_vertex[8][4] =
  2127. {
  2128. { 0,1,3,2 }, // zyx=000
  2129. { 1,3,2,0 }, // zyx=001
  2130. { 2,0,1,3 }, // zyx=010
  2131. { 3,2,0,1 }, // zyx=011
  2132. { 4,5,7,6 }, // zyx=100
  2133. { 5,7,6,4 }, // zyx=101
  2134. { 6,4,5,7 }, // zyx=110
  2135. { 7,6,4,5 }, // zyx=111
  2136. };
  2137. #ifdef STBVOX_CONFIG_OPTIMIZED_VHEIGHT
  2138. // optimized vheight generates a single normal over the entire face, even if it's not planar
  2139. static unsigned char stbvox_optimized_face_up_normal[4][4][4][4] =
  2140. {
  2141. {
  2142. {
  2143. { STBVF_u , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2144. { STBVF_nw_u, STBVF_nu , STBVF_nu , STBVF_ne_u, },
  2145. { STBVF_nw_u, STBVF_nu , STBVF_nu , STBVF_nu , },
  2146. { STBVF_nw_u, STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2147. },{
  2148. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2149. { STBVF_u , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2150. { STBVF_nw_u, STBVF_nu , STBVF_nu , STBVF_ne_u, },
  2151. { STBVF_nw_u, STBVF_nu , STBVF_nu , STBVF_nu , },
  2152. },{
  2153. { STBVF_eu , STBVF_eu , STBVF_eu , STBVF_eu , },
  2154. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2155. { STBVF_u , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2156. { STBVF_nw_u, STBVF_nu , STBVF_nu , STBVF_ne_u, },
  2157. },{
  2158. { STBVF_eu , STBVF_eu , STBVF_eu , STBVF_eu , },
  2159. { STBVF_eu , STBVF_eu , STBVF_eu , STBVF_eu , },
  2160. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2161. { STBVF_u , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2162. },
  2163. },{
  2164. {
  2165. { STBVF_sw_u, STBVF_u , STBVF_ne_u, STBVF_ne_u, },
  2166. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2167. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2168. { STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, STBVF_nu , },
  2169. },{
  2170. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2171. { STBVF_sw_u, STBVF_u , STBVF_ne_u, STBVF_ne_u, },
  2172. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2173. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2174. },{
  2175. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_eu , },
  2176. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2177. { STBVF_sw_u, STBVF_u , STBVF_ne_u, STBVF_ne_u, },
  2178. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_nu , },
  2179. },{
  2180. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_eu , },
  2181. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_eu , },
  2182. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2183. { STBVF_sw_u, STBVF_u , STBVF_ne_u, STBVF_ne_u, },
  2184. },
  2185. },{
  2186. {
  2187. { STBVF_sw_u, STBVF_sw_u, STBVF_u , STBVF_ne_u, },
  2188. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2189. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2190. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, },
  2191. },{
  2192. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2193. { STBVF_sw_u, STBVF_sw_u, STBVF_u , STBVF_ne_u, },
  2194. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2195. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2196. },{
  2197. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2198. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2199. { STBVF_sw_u, STBVF_sw_u, STBVF_u , STBVF_ne_u, },
  2200. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2201. },{
  2202. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2203. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2204. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2205. { STBVF_sw_u, STBVF_sw_u, STBVF_u , STBVF_ne_u, },
  2206. },
  2207. },{
  2208. {
  2209. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_u , },
  2210. { STBVF_sw_u, STBVF_wu , STBVF_wu , STBVF_nw_u, },
  2211. { STBVF_wu , STBVF_wu , STBVF_wu , STBVF_nw_u, },
  2212. { STBVF_wu , STBVF_wu , STBVF_nw_u, STBVF_nw_u, },
  2213. },{
  2214. { STBVF_sw_u, STBVF_su , STBVF_su , STBVF_su , },
  2215. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_u , },
  2216. { STBVF_sw_u, STBVF_wu , STBVF_wu , STBVF_nw_u, },
  2217. { STBVF_wu , STBVF_wu , STBVF_wu , STBVF_nw_u, },
  2218. },{
  2219. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2220. { STBVF_sw_u, STBVF_su , STBVF_su , STBVF_su , },
  2221. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_u , },
  2222. { STBVF_sw_u, STBVF_wu , STBVF_wu , STBVF_nw_u, },
  2223. },{
  2224. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2225. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2226. { STBVF_sw_u, STBVF_su , STBVF_su , STBVF_su , },
  2227. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_u , },
  2228. },
  2229. },
  2230. };
  2231. #else
  2232. // which normal to use for a given vheight that's planar
  2233. // @TODO: this table was constructed by hand and may have bugs
  2234. // nw se sw
  2235. static unsigned char stbvox_planar_face_up_normal[4][4][4] =
  2236. {
  2237. { // sw,se,nw,ne; ne = se+nw-sw
  2238. { STBVF_u , 0 , 0 , 0 }, // 0,0,0,0; 1,0,0,-1; 2,0,0,-2; 3,0,0,-3;
  2239. { STBVF_u , STBVF_u , 0 , 0 }, // 0,1,0,1; 1,1,0, 0; 2,1,0,-1; 3,1,0,-2;
  2240. { STBVF_wu , STBVF_nw_u, STBVF_nu , 0 }, // 0,2,0,2; 1,2,0, 1; 2,2,0, 0; 3,2,0,-1;
  2241. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nu }, // 0,3,0,3; 1,3,0, 2; 2,3,0, 1; 3,3,0, 0;
  2242. },{
  2243. { STBVF_u , STBVF_u , 0 , 0 }, // 0,0,1,1; 1,0,1, 0; 2,0,1,-1; 3,0,1,-2;
  2244. { STBVF_sw_u, STBVF_u , STBVF_ne_u, 0 }, // 0,1,1,2; 1,1,1, 1; 2,1,1, 0; 3,1,1,-1;
  2245. { STBVF_sw_u, STBVF_u , STBVF_u , STBVF_ne_u }, // 0,2,1,3; 1,2,1, 2; 2,2,1, 1; 3,2,1, 0;
  2246. { 0 , STBVF_wu , STBVF_nw_u, STBVF_nu }, // 0,3,1,4; 1,3,1, 3; 2,3,1, 2; 3,3,1, 1;
  2247. },{
  2248. { STBVF_su , STBVF_se_u, STBVF_eu , 0 }, // 0,0,2,2; 1,0,2, 1; 2,0,2, 0; 3,0,2,-1;
  2249. { STBVF_sw_u, STBVF_u , STBVF_u , STBVF_ne_u }, // 0,1,2,3; 1,1,2, 2; 2,1,2, 1; 3,1,2, 0;
  2250. { 0 , STBVF_sw_u, STBVF_u , STBVF_ne_u }, // 0,2,2,4; 1,2,2, 3; 2,2,2, 2; 3,2,2, 1;
  2251. { 0 , 0 , STBVF_u , STBVF_u }, // 0,3,2,5; 1,3,2, 4; 2,3,2, 3; 3,3,2, 2;
  2252. },{
  2253. { STBVF_su , STBVF_se_u, STBVF_se_u, STBVF_eu }, // 0,0,3,3; 1,0,3, 2; 2,0,3, 1; 3,0,3, 0;
  2254. { 0 , STBVF_su , STBVF_se_u, STBVF_eu }, // 0,1,3,4; 1,1,3, 3; 2,1,3, 2; 3,1,3, 1;
  2255. { 0 , 0 , STBVF_u , STBVF_u }, // 0,2,3,5; 1,2,3, 4; 2,2,3, 3; 3,2,3, 2;
  2256. { 0 , 0 , 0 , STBVF_u }, // 0,3,3,6; 1,3,3, 5; 2,3,3, 4; 3,3,3, 3;
  2257. }
  2258. };
  2259. // these tables were constructed automatically using a variant of the code
  2260. // below; however, they seem wrong, so who knows
  2261. static unsigned char stbvox_face_up_normal_012[4][4][4] =
  2262. {
  2263. {
  2264. { STBVF_u , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2265. { STBVF_wu , STBVF_nu , STBVF_ne_u, STBVF_ne_u, },
  2266. { STBVF_wu , STBVF_nw_u, STBVF_nu , STBVF_ne_u, },
  2267. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nu , },
  2268. },{
  2269. { STBVF_su , STBVF_eu , STBVF_ne_u, STBVF_ne_u, },
  2270. { STBVF_sw_u, STBVF_u , STBVF_ne_u, STBVF_ne_u, },
  2271. { STBVF_sw_u, STBVF_wu , STBVF_nu , STBVF_ne_u, },
  2272. { STBVF_sw_u, STBVF_wu , STBVF_nw_u, STBVF_nu , },
  2273. },{
  2274. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2275. { STBVF_sw_u, STBVF_su , STBVF_eu , STBVF_ne_u, },
  2276. { STBVF_sw_u, STBVF_sw_u, STBVF_u , STBVF_ne_u, },
  2277. { STBVF_sw_u, STBVF_sw_u, STBVF_wu , STBVF_nu , },
  2278. },{
  2279. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2280. { STBVF_sw_u, STBVF_su , STBVF_eu , STBVF_eu , },
  2281. { STBVF_sw_u, STBVF_sw_u, STBVF_su , STBVF_eu , },
  2282. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_u , },
  2283. }
  2284. };
  2285. static unsigned char stbvox_face_up_normal_013[4][4][4] =
  2286. {
  2287. {
  2288. { STBVF_u , STBVF_eu , STBVF_eu , STBVF_eu , },
  2289. { STBVF_nw_u, STBVF_nu , STBVF_ne_u, STBVF_ne_u, },
  2290. { STBVF_nw_u, STBVF_nw_u, STBVF_nu , STBVF_ne_u, },
  2291. { STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, STBVF_nu , },
  2292. },{
  2293. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_eu , },
  2294. { STBVF_wu , STBVF_u , STBVF_eu , STBVF_eu , },
  2295. { STBVF_nw_u, STBVF_nw_u, STBVF_nu , STBVF_ne_u, },
  2296. { STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, STBVF_nu , },
  2297. },{
  2298. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2299. { STBVF_sw_u, STBVF_su , STBVF_eu , STBVF_eu , },
  2300. { STBVF_wu , STBVF_wu , STBVF_u , STBVF_eu , },
  2301. { STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, STBVF_nu , },
  2302. },{
  2303. { STBVF_su , STBVF_su , STBVF_su , STBVF_eu , },
  2304. { STBVF_sw_u, STBVF_su , STBVF_su , STBVF_su , },
  2305. { STBVF_sw_u, STBVF_sw_u, STBVF_su , STBVF_eu , },
  2306. { STBVF_wu , STBVF_wu , STBVF_wu , STBVF_u , },
  2307. }
  2308. };
  2309. static unsigned char stbvox_face_up_normal_023[4][4][4] =
  2310. {
  2311. {
  2312. { STBVF_u , STBVF_nu , STBVF_nu , STBVF_nu , },
  2313. { STBVF_eu , STBVF_eu , STBVF_ne_u, STBVF_ne_u, },
  2314. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2315. { STBVF_eu , STBVF_eu , STBVF_eu , STBVF_eu , },
  2316. },{
  2317. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, },
  2318. { STBVF_su , STBVF_u , STBVF_nu , STBVF_nu , },
  2319. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2320. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2321. },{
  2322. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, },
  2323. { STBVF_sw_u, STBVF_wu , STBVF_nw_u, STBVF_nw_u, },
  2324. { STBVF_su , STBVF_su , STBVF_u , STBVF_nu , },
  2325. { STBVF_su , STBVF_su , STBVF_eu , STBVF_eu , },
  2326. },{
  2327. { STBVF_wu , STBVF_nw_u, STBVF_nw_u, STBVF_nw_u, },
  2328. { STBVF_sw_u, STBVF_wu , STBVF_nw_u, STBVF_nw_u, },
  2329. { STBVF_sw_u, STBVF_sw_u, STBVF_wu , STBVF_nw_u, },
  2330. { STBVF_su , STBVF_su , STBVF_su , STBVF_u , },
  2331. }
  2332. };
  2333. static unsigned char stbvox_face_up_normal_123[4][4][4] =
  2334. {
  2335. {
  2336. { STBVF_u , STBVF_nu , STBVF_nu , STBVF_nu , },
  2337. { STBVF_eu , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2338. { STBVF_eu , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2339. { STBVF_eu , STBVF_ne_u, STBVF_ne_u, STBVF_ne_u, },
  2340. },{
  2341. { STBVF_sw_u, STBVF_wu , STBVF_nw_u, STBVF_nw_u, },
  2342. { STBVF_su , STBVF_u , STBVF_nu , STBVF_nu , },
  2343. { STBVF_eu , STBVF_eu , STBVF_ne_u, STBVF_ne_u, },
  2344. { STBVF_eu , STBVF_eu , STBVF_ne_u, STBVF_ne_u, },
  2345. },{
  2346. { STBVF_sw_u, STBVF_sw_u, STBVF_wu , STBVF_nw_u, },
  2347. { STBVF_sw_u, STBVF_sw_u, STBVF_wu , STBVF_nw_u, },
  2348. { STBVF_su , STBVF_su , STBVF_u , STBVF_nu , },
  2349. { STBVF_su , STBVF_eu , STBVF_eu , STBVF_ne_u, },
  2350. },{
  2351. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_wu , },
  2352. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_wu , },
  2353. { STBVF_sw_u, STBVF_sw_u, STBVF_sw_u, STBVF_wu , },
  2354. { STBVF_su , STBVF_su , STBVF_su , STBVF_u , },
  2355. }
  2356. };
  2357. #endif
  2358. void stbvox_get_quad_vertex_pointer(stbvox_mesh_maker *mm, int mesh, stbvox_mesh_vertex **vertices, stbvox_mesh_face face)
  2359. {
  2360. char *p = mm->output_cur[mesh][0];
  2361. int step = mm->output_step[mesh][0];
  2362. // allocate a new quad from the mesh
  2363. vertices[0] = (stbvox_mesh_vertex *) p; p += step;
  2364. vertices[1] = (stbvox_mesh_vertex *) p; p += step;
  2365. vertices[2] = (stbvox_mesh_vertex *) p; p += step;
  2366. vertices[3] = (stbvox_mesh_vertex *) p; p += step;
  2367. mm->output_cur[mesh][0] = p;
  2368. // output the face
  2369. #ifdef STBVOX_ICONFIG_FACE_ATTRIBUTE
  2370. // write face as interleaved vertex data
  2371. *(stbvox_mesh_face *) (vertices[0]+1) = face;
  2372. *(stbvox_mesh_face *) (vertices[1]+1) = face;
  2373. *(stbvox_mesh_face *) (vertices[2]+1) = face;
  2374. *(stbvox_mesh_face *) (vertices[3]+1) = face;
  2375. #else
  2376. *(stbvox_mesh_face *) mm->output_cur[mesh][1] = face;
  2377. mm->output_cur[mesh][1] += 4;
  2378. #endif
  2379. }
  2380. void stbvox_make_mesh_for_face(stbvox_mesh_maker *mm, stbvox_rotate rot, int face, int v_off, stbvox_pos pos, stbvox_mesh_vertex vertbase, stbvox_mesh_vertex *face_coord, unsigned char mesh, int normal)
  2381. {
  2382. stbvox_mesh_face face_data = stbvox_compute_mesh_face_value(mm,rot,face,v_off, normal);
  2383. // still need to compute ao & texlerp for each vertex
  2384. // first compute texlerp into p1
  2385. stbvox_mesh_vertex p1[4] = { 0 };
  2386. #if defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED) && defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)
  2387. #define STBVOX_USE_PACKED(f) ((f) == STBVOX_FACE_up || (f) == STBVOX_FACE_down)
  2388. #elif defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)
  2389. #define STBVOX_USE_PACKED(f) ((f) == STBVOX_FACE_up )
  2390. #elif defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)
  2391. #define STBVOX_USE_PACKED(f) ( (f) == STBVOX_FACE_down)
  2392. #endif
  2393. #if defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED) || defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)
  2394. if (STBVOX_USE_PACKED(face)) {
  2395. if (!mm->input.packed_compact || 0==(mm->input.packed_compact[v_off]&16))
  2396. goto set_default;
  2397. p1[0] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][0]] >> 5);
  2398. p1[1] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][1]] >> 5);
  2399. p1[2] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][2]] >> 5);
  2400. p1[3] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][3]] >> 5);
  2401. p1[0] = stbvox_vertex_encode(0,0,0,0,p1[0]);
  2402. p1[1] = stbvox_vertex_encode(0,0,0,0,p1[1]);
  2403. p1[2] = stbvox_vertex_encode(0,0,0,0,p1[2]);
  2404. p1[3] = stbvox_vertex_encode(0,0,0,0,p1[3]);
  2405. goto skip;
  2406. }
  2407. #endif
  2408. if (mm->input.block_texlerp) {
  2409. stbvox_block_type bt = mm->input.blocktype[v_off];
  2410. unsigned char val = mm->input.block_texlerp[bt];
  2411. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,val);
  2412. } else if (mm->input.block_texlerp_face) {
  2413. stbvox_block_type bt = mm->input.blocktype[v_off];
  2414. unsigned char bt_face = STBVOX_ROTATE(face, rot.block);
  2415. unsigned char val = mm->input.block_texlerp_face[bt][bt_face];
  2416. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,val);
  2417. } else if (mm->input.texlerp_face3) {
  2418. unsigned char val = (mm->input.texlerp_face3[v_off] >> stbvox_face3_lerp[face]) & 7;
  2419. if (face >= STBVOX_FACE_up)
  2420. val = stbvox_face3_updown[val];
  2421. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,val);
  2422. } else if (mm->input.texlerp_simple) {
  2423. unsigned char val = mm->input.texlerp_simple[v_off];
  2424. unsigned char lerp_face = (val >> 2) & 7;
  2425. if (lerp_face == face) {
  2426. p1[0] = (mm->input.texlerp_simple[v_off + mm->cube_vertex_offset[face][0]] >> 5) & 7;
  2427. p1[1] = (mm->input.texlerp_simple[v_off + mm->cube_vertex_offset[face][1]] >> 5) & 7;
  2428. p1[2] = (mm->input.texlerp_simple[v_off + mm->cube_vertex_offset[face][2]] >> 5) & 7;
  2429. p1[3] = (mm->input.texlerp_simple[v_off + mm->cube_vertex_offset[face][3]] >> 5) & 7;
  2430. p1[0] = stbvox_vertex_encode(0,0,0,0,p1[0]);
  2431. p1[1] = stbvox_vertex_encode(0,0,0,0,p1[1]);
  2432. p1[2] = stbvox_vertex_encode(0,0,0,0,p1[2]);
  2433. p1[3] = stbvox_vertex_encode(0,0,0,0,p1[3]);
  2434. } else {
  2435. unsigned char base = stbvox_vert_lerp_for_simple[val&3];
  2436. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,base);
  2437. }
  2438. } else if (mm->input.texlerp) {
  2439. unsigned char facelerp = (mm->input.texlerp[v_off] >> stbvox_face_lerp[face]) & 3;
  2440. if (facelerp == STBVOX_TEXLERP_FACE_use_vert) {
  2441. if (mm->input.texlerp_vert3 && face != STBVOX_FACE_down) {
  2442. unsigned char shift = stbvox_vert3_lerp[face];
  2443. p1[0] = (mm->input.texlerp_vert3[mm->cube_vertex_offset[face][0]] >> shift) & 7;
  2444. p1[1] = (mm->input.texlerp_vert3[mm->cube_vertex_offset[face][1]] >> shift) & 7;
  2445. p1[2] = (mm->input.texlerp_vert3[mm->cube_vertex_offset[face][2]] >> shift) & 7;
  2446. p1[3] = (mm->input.texlerp_vert3[mm->cube_vertex_offset[face][3]] >> shift) & 7;
  2447. } else {
  2448. p1[0] = stbvox_vert_lerp_for_simple[mm->input.texlerp[mm->cube_vertex_offset[face][0]]>>6];
  2449. p1[1] = stbvox_vert_lerp_for_simple[mm->input.texlerp[mm->cube_vertex_offset[face][1]]>>6];
  2450. p1[2] = stbvox_vert_lerp_for_simple[mm->input.texlerp[mm->cube_vertex_offset[face][2]]>>6];
  2451. p1[3] = stbvox_vert_lerp_for_simple[mm->input.texlerp[mm->cube_vertex_offset[face][3]]>>6];
  2452. }
  2453. p1[0] = stbvox_vertex_encode(0,0,0,0,p1[0]);
  2454. p1[1] = stbvox_vertex_encode(0,0,0,0,p1[1]);
  2455. p1[2] = stbvox_vertex_encode(0,0,0,0,p1[2]);
  2456. p1[3] = stbvox_vertex_encode(0,0,0,0,p1[3]);
  2457. } else {
  2458. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,stbvox_vert_lerp_for_face_lerp[facelerp]);
  2459. }
  2460. } else {
  2461. #if defined(STBVOX_CONFIG_UP_TEXLERP_PACKED) || defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)
  2462. set_default:
  2463. #endif
  2464. p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,7); // @TODO make this configurable
  2465. }
  2466. #if defined(STBVOX_CONFIG_UP_TEXLERP_PACKED) || defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)
  2467. skip:
  2468. #endif
  2469. // now compute lighting and store to vertices
  2470. {
  2471. stbvox_mesh_vertex *mv[4];
  2472. stbvox_get_quad_vertex_pointer(mm, mesh, mv, face_data);
  2473. if (mm->input.lighting) {
  2474. // @TODO: lighting at block centers, but not gathered, instead constant-per-face
  2475. if (mm->input.lighting_at_vertices) {
  2476. int i;
  2477. for (i=0; i < 4; ++i) {
  2478. *mv[i] = vertbase + face_coord[i]
  2479. + stbvox_vertex_encode(0,0,0,mm->input.lighting[v_off + mm->cube_vertex_offset[face][i]] & 63,0)
  2480. + p1[i];
  2481. }
  2482. } else {
  2483. unsigned char *amb = &mm->input.lighting[v_off];
  2484. int i,j;
  2485. #if defined(STBVOX_CONFIG_ROTATION_IN_LIGHTING) || defined(STBVOX_CONFIG_VHEIGHT_IN_LIGHTING)
  2486. #define STBVOX_GET_LIGHTING(light) ((light) & ~3)
  2487. #define STBVOX_LIGHTING_ROUNDOFF 8
  2488. #else
  2489. #define STBVOX_GET_LIGHTING(light) (light)
  2490. #define STBVOX_LIGHTING_ROUNDOFF 2
  2491. #endif
  2492. for (i=0; i < 4; ++i) {
  2493. // for each vertex, gather from the four neighbor blocks it's facing
  2494. unsigned char *vamb = &amb[mm->cube_vertex_offset[face][i]];
  2495. int total=0;
  2496. for (j=0; j < 4; ++j)
  2497. total += STBVOX_GET_LIGHTING(vamb[mm->vertex_gather_offset[face][j]]);
  2498. *mv[i] = vertbase + face_coord[i]
  2499. + stbvox_vertex_encode(0,0,0,(total+STBVOX_LIGHTING_ROUNDOFF)>>4,0)
  2500. + p1[i];
  2501. // >> 4 is because:
  2502. // >> 2 to divide by 4 to get average over 4 samples
  2503. // >> 2 because input is 8 bits, output is 6 bits
  2504. }
  2505. // @TODO: note that gathering baked *lighting*
  2506. // is different from gathering baked ao; baked ao can count
  2507. // solid blocks as 0 ao, but baked lighting wants average
  2508. // of non-blocked--not take average & treat blocked as 0. And
  2509. // we can't bake the right value into the solid blocks
  2510. // because they can have different lighting values on
  2511. // different sides. So we need to actually gather and
  2512. // then divide by 0..4 (which we can do with a table-driven
  2513. // multiply, or have an 'if' for the 3 case)
  2514. }
  2515. } else {
  2516. vertbase += stbvox_vertex_encode(0,0,0,63,0);
  2517. *mv[0] = vertbase + face_coord[0] + p1[0];
  2518. *mv[1] = vertbase + face_coord[1] + p1[1];
  2519. *mv[2] = vertbase + face_coord[2] + p1[2];
  2520. *mv[3] = vertbase + face_coord[3] + p1[3];
  2521. }
  2522. }
  2523. }
  2524. // get opposite-facing normal & texgen for opposite face, used to map up-facing vheight data to down-facing data
  2525. static unsigned char stbvox_reverse_face[STBVF_count] =
  2526. {
  2527. STBVF_w, STBVF_s, STBVF_e, STBVF_n, STBVF_d , STBVF_u , STBVF_wd, STBVF_wu,
  2528. 0, 0, 0, 0, STBVF_sw_d, STBVF_sw_u, STBVF_sd, STBVF_su,
  2529. 0, 0, 0, 0, STBVF_se_d, STBVF_se_u, STBVF_ed, STBVF_eu,
  2530. 0, 0, 0, 0, STBVF_ne_d, STBVF_ne_d, STBVF_nd, STBVF_nu
  2531. };
  2532. #ifndef STBVOX_CONFIG_OPTIMIZED_VHEIGHT
  2533. // render non-planar quads by splitting into two triangles, rendering each as a degenerate quad
  2534. static void stbvox_make_12_split_mesh_for_face(stbvox_mesh_maker *mm, stbvox_rotate rot, int face, int v_off, stbvox_pos pos, stbvox_mesh_vertex vertbase, stbvox_mesh_vertex *face_coord, unsigned char mesh, unsigned char *ht)
  2535. {
  2536. stbvox_mesh_vertex v[4];
  2537. unsigned char normal1 = stbvox_face_up_normal_012[ht[2]][ht[1]][ht[0]];
  2538. unsigned char normal2 = stbvox_face_up_normal_123[ht[3]][ht[2]][ht[1]];
  2539. if (face == STBVOX_FACE_down) {
  2540. normal1 = stbvox_reverse_face[normal1];
  2541. normal2 = stbvox_reverse_face[normal2];
  2542. }
  2543. // the floor side face_coord is stored in order NW,NE,SE,SW, but ht[] is stored SW,SE,NW,NE
  2544. v[0] = face_coord[2];
  2545. v[1] = face_coord[3];
  2546. v[2] = face_coord[0];
  2547. v[3] = face_coord[2];
  2548. stbvox_make_mesh_for_face(mm, rot, face, v_off, pos, vertbase, v, mesh, normal1);
  2549. v[1] = face_coord[0];
  2550. v[2] = face_coord[1];
  2551. stbvox_make_mesh_for_face(mm, rot, face, v_off, pos, vertbase, v, mesh, normal2);
  2552. }
  2553. static void stbvox_make_03_split_mesh_for_face(stbvox_mesh_maker *mm, stbvox_rotate rot, int face, int v_off, stbvox_pos pos, stbvox_mesh_vertex vertbase, stbvox_mesh_vertex *face_coord, unsigned char mesh, unsigned char *ht)
  2554. {
  2555. stbvox_mesh_vertex v[4];
  2556. unsigned char normal1 = stbvox_face_up_normal_013[ht[3]][ht[1]][ht[0]];
  2557. unsigned char normal2 = stbvox_face_up_normal_023[ht[3]][ht[2]][ht[0]];
  2558. if (face == STBVOX_FACE_down) {
  2559. normal1 = stbvox_reverse_face[normal1];
  2560. normal2 = stbvox_reverse_face[normal2];
  2561. }
  2562. v[0] = face_coord[1];
  2563. v[1] = face_coord[2];
  2564. v[2] = face_coord[3];
  2565. v[3] = face_coord[1];
  2566. stbvox_make_mesh_for_face(mm, rot, face, v_off, pos, vertbase, v, mesh, normal1);
  2567. v[1] = face_coord[3];
  2568. v[2] = face_coord[0];
  2569. stbvox_make_mesh_for_face(mm, rot, face, v_off, pos, vertbase, v, mesh, normal2); // this one is correct!
  2570. }
  2571. #endif
  2572. #ifndef STBVOX_CONFIG_PRECISION_Z
  2573. #define STBVOX_CONFIG_PRECISION_Z 1
  2574. #endif
  2575. // simple case for mesh generation: we have only solid and empty blocks
  2576. static void stbvox_make_mesh_for_block(stbvox_mesh_maker *mm, stbvox_pos pos, int v_off, stbvox_mesh_vertex *vmesh)
  2577. {
  2578. int ns_off = mm->y_stride_in_bytes;
  2579. int ew_off = mm->x_stride_in_bytes;
  2580. unsigned char *blockptr = &mm->input.blocktype[v_off];
  2581. stbvox_mesh_vertex basevert = stbvox_vertex_encode(pos.x, pos.y, pos.z << STBVOX_CONFIG_PRECISION_Z , 0,0);
  2582. stbvox_rotate rot = { 0,0,0,0 };
  2583. unsigned char simple_rot = 0;
  2584. unsigned char mesh = mm->default_mesh;
  2585. if (mm->input.selector)
  2586. mesh = mm->input.selector[v_off];
  2587. else if (mm->input.block_selector)
  2588. mesh = mm->input.block_selector[mm->input.blocktype[v_off]];
  2589. // check if we're going off the end
  2590. if (mm->output_cur[mesh][0] + mm->output_size[mesh][0]*6 > mm->output_end[mesh][0]) {
  2591. mm->full = 1;
  2592. return;
  2593. }
  2594. #ifdef STBVOX_CONFIG_ROTATION_IN_LIGHTING
  2595. simple_rot = mm->input.lighting[v_off] & 3;
  2596. #endif
  2597. if (mm->input.packed_compact)
  2598. simple_rot = mm->input.packed_compact[v_off] & 3;
  2599. if (blockptr[ 1]==0) {
  2600. rot.facerot = simple_rot;
  2601. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_up , v_off, pos, basevert, vmesh+4*STBVOX_FACE_up, mesh, STBVOX_FACE_up);
  2602. }
  2603. if (blockptr[-1]==0) {
  2604. rot.facerot = (-simple_rot) & 3;
  2605. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_down, v_off, pos, basevert, vmesh+4*STBVOX_FACE_down, mesh, STBVOX_FACE_down);
  2606. }
  2607. if (mm->input.rotate) {
  2608. unsigned char val = mm->input.rotate[v_off];
  2609. rot.block = (val >> 0) & 3;
  2610. rot.overlay = (val >> 2) & 3;
  2611. //rot.tex2 = (val >> 4) & 3;
  2612. rot.ecolor = (val >> 6) & 3;
  2613. } else {
  2614. rot.block = rot.overlay = rot.ecolor = simple_rot;
  2615. }
  2616. rot.facerot = 0;
  2617. if (blockptr[ ns_off]==0)
  2618. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_north, v_off, pos, basevert, vmesh+4*STBVOX_FACE_north, mesh, STBVOX_FACE_north);
  2619. if (blockptr[-ns_off]==0)
  2620. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_south, v_off, pos, basevert, vmesh+4*STBVOX_FACE_south, mesh, STBVOX_FACE_south);
  2621. if (blockptr[ ew_off]==0)
  2622. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_east , v_off, pos, basevert, vmesh+4*STBVOX_FACE_east, mesh, STBVOX_FACE_east);
  2623. if (blockptr[-ew_off]==0)
  2624. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_west , v_off, pos, basevert, vmesh+4*STBVOX_FACE_west, mesh, STBVOX_FACE_west);
  2625. }
  2626. // complex case for mesh generation: we have lots of different
  2627. // block types, and we don't want to generate faces of blocks
  2628. // if they're hidden by neighbors.
  2629. //
  2630. // we use lots of tables to determine this: we have a table
  2631. // which tells us what face type is generated for each type of
  2632. // geometry, and then a table that tells us whether that type
  2633. // is hidden by a neighbor.
  2634. static void stbvox_make_mesh_for_block_with_geo(stbvox_mesh_maker *mm, stbvox_pos pos, int v_off)
  2635. {
  2636. int ns_off = mm->y_stride_in_bytes;
  2637. int ew_off = mm->x_stride_in_bytes;
  2638. int visible_faces, visible_base;
  2639. unsigned char mesh;
  2640. // first gather the geometry info for this block and all neighbors
  2641. unsigned char bt, nbt[6];
  2642. unsigned char geo, ngeo[6];
  2643. unsigned char rot, nrot[6];
  2644. bt = mm->input.blocktype[v_off];
  2645. nbt[0] = mm->input.blocktype[v_off + ew_off];
  2646. nbt[1] = mm->input.blocktype[v_off + ns_off];
  2647. nbt[2] = mm->input.blocktype[v_off - ew_off];
  2648. nbt[3] = mm->input.blocktype[v_off - ns_off];
  2649. nbt[4] = mm->input.blocktype[v_off + 1];
  2650. nbt[5] = mm->input.blocktype[v_off - 1];
  2651. if (mm->input.geometry) {
  2652. int i;
  2653. geo = mm->input.geometry[v_off];
  2654. ngeo[0] = mm->input.geometry[v_off + ew_off];
  2655. ngeo[1] = mm->input.geometry[v_off + ns_off];
  2656. ngeo[2] = mm->input.geometry[v_off - ew_off];
  2657. ngeo[3] = mm->input.geometry[v_off - ns_off];
  2658. ngeo[4] = mm->input.geometry[v_off + 1];
  2659. ngeo[5] = mm->input.geometry[v_off - 1];
  2660. rot = (geo >> 4) & 3;
  2661. geo &= 15;
  2662. for (i=0; i < 6; ++i) {
  2663. nrot[i] = (ngeo[i] >> 4) & 3;
  2664. ngeo[i] &= 15;
  2665. }
  2666. } else {
  2667. int i;
  2668. assert(mm->input.block_geometry);
  2669. geo = mm->input.block_geometry[bt];
  2670. for (i=0; i < 6; ++i)
  2671. ngeo[i] = mm->input.block_geometry[nbt[i]];
  2672. if (mm->input.selector) {
  2673. #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING
  2674. if (mm->input.packed_compact == NULL) {
  2675. rot = (mm->input.selector[v_off ] >> 4) & 3;
  2676. nrot[0] = (mm->input.selector[v_off + ew_off] >> 4) & 3;
  2677. nrot[1] = (mm->input.selector[v_off + ns_off] >> 4) & 3;
  2678. nrot[2] = (mm->input.selector[v_off - ew_off] >> 4) & 3;
  2679. nrot[3] = (mm->input.selector[v_off - ns_off] >> 4) & 3;
  2680. nrot[4] = (mm->input.selector[v_off + 1] >> 4) & 3;
  2681. nrot[5] = (mm->input.selector[v_off - 1] >> 4) & 3;
  2682. }
  2683. #endif
  2684. } else {
  2685. #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING
  2686. if (mm->input.packed_compact == NULL) {
  2687. rot = (geo>>4)&3;
  2688. geo &= 15;
  2689. for (i=0; i < 6; ++i) {
  2690. nrot[i] = (ngeo[i]>>4)&3;
  2691. ngeo[i] &= 15;
  2692. }
  2693. }
  2694. #endif
  2695. }
  2696. }
  2697. #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING
  2698. if (mm->input.packed_compact) {
  2699. rot = mm->input.packed_compact[rot] & 3;
  2700. nrot[0] = mm->input.packed_compact[v_off + ew_off] & 3;
  2701. nrot[1] = mm->input.packed_compact[v_off + ns_off] & 3;
  2702. nrot[2] = mm->input.packed_compact[v_off - ew_off] & 3;
  2703. nrot[3] = mm->input.packed_compact[v_off - ns_off] & 3;
  2704. nrot[4] = mm->input.packed_compact[v_off + 1] & 3;
  2705. nrot[5] = mm->input.packed_compact[v_off - 1] & 3;
  2706. }
  2707. #else
  2708. rot = mm->input.lighting[v_off] & 3;
  2709. nrot[0] = (mm->input.lighting[v_off + ew_off]) & 3;
  2710. nrot[1] = (mm->input.lighting[v_off + ns_off]) & 3;
  2711. nrot[2] = (mm->input.lighting[v_off - ew_off]) & 3;
  2712. nrot[3] = (mm->input.lighting[v_off - ns_off]) & 3;
  2713. nrot[4] = (mm->input.lighting[v_off + 1]) & 3;
  2714. nrot[5] = (mm->input.lighting[v_off - 1]) & 3;
  2715. #endif
  2716. if (geo == STBVOX_GEOM_transp) {
  2717. // transparency has a special rule: if the blocktype is the same,
  2718. // and the faces are compatible, then can hide them; otherwise,
  2719. // force them on
  2720. // Note that this means we don't support any transparentshapes other
  2721. // than solid blocks, since detecting them is too complicated. If
  2722. // you wanted to do something like minecraft water, you probably
  2723. // should just do that with a separate renderer anyway. (We don't
  2724. // support transparency sorting so you need to use alpha test
  2725. // anyway)
  2726. int i;
  2727. for (i=0; i < 6; ++i)
  2728. if (nbt[i] != bt) {
  2729. nbt[i] = 0;
  2730. ngeo[i] = STBVOX_GEOM_empty;
  2731. } else
  2732. ngeo[i] = STBVOX_GEOM_solid;
  2733. geo = STBVOX_GEOM_solid;
  2734. }
  2735. // now compute the face visibility
  2736. visible_base = stbvox_hasface[geo][rot];
  2737. // @TODO: assert(visible_base != 0); // we should have early-outted earlier in this case
  2738. visible_faces = 0;
  2739. // now, for every face that might be visible, check if neighbor hides it
  2740. if (visible_base & (1 << STBVOX_FACE_east)) {
  2741. int type = stbvox_facetype[ geo ][(STBVOX_FACE_east+ rot )&3];
  2742. int ntype = stbvox_facetype[ngeo[0]][(STBVOX_FACE_west+nrot[0])&3];
  2743. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_east)) & (1 << STBVOX_FACE_east);
  2744. }
  2745. if (visible_base & (1 << STBVOX_FACE_north)) {
  2746. int type = stbvox_facetype[ geo ][(STBVOX_FACE_north+ rot )&3];
  2747. int ntype = stbvox_facetype[ngeo[1]][(STBVOX_FACE_south+nrot[1])&3];
  2748. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_north)) & (1 << STBVOX_FACE_north);
  2749. }
  2750. if (visible_base & (1 << STBVOX_FACE_west)) {
  2751. int type = stbvox_facetype[ geo ][(STBVOX_FACE_west+ rot )&3];
  2752. int ntype = stbvox_facetype[ngeo[2]][(STBVOX_FACE_east+nrot[2])&3];
  2753. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_west)) & (1 << STBVOX_FACE_west);
  2754. }
  2755. if (visible_base & (1 << STBVOX_FACE_south)) {
  2756. int type = stbvox_facetype[ geo ][(STBVOX_FACE_south+ rot )&3];
  2757. int ntype = stbvox_facetype[ngeo[3]][(STBVOX_FACE_north+nrot[3])&3];
  2758. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_south)) & (1 << STBVOX_FACE_south);
  2759. }
  2760. if (visible_base & (1 << STBVOX_FACE_up)) {
  2761. int type = stbvox_facetype[ geo ][STBVOX_FACE_up];
  2762. int ntype = stbvox_facetype[ngeo[4]][STBVOX_FACE_down];
  2763. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_up)) & (1 << STBVOX_FACE_up);
  2764. }
  2765. if (visible_base & (1 << STBVOX_FACE_down)) {
  2766. int type = stbvox_facetype[ geo ][STBVOX_FACE_down];
  2767. int ntype = stbvox_facetype[ngeo[5]][STBVOX_FACE_up];
  2768. visible_faces |= ((stbvox_face_visible[type]) >> (ntype + 5 - STBVOX_FACE_down)) & (1 << STBVOX_FACE_down);
  2769. }
  2770. if (geo == STBVOX_GEOM_force)
  2771. geo = STBVOX_GEOM_solid;
  2772. assert((geo == STBVOX_GEOM_crossed_pair) ? (visible_faces == 15) : 1);
  2773. // now we finally know for sure which faces are getting generated
  2774. if (visible_faces == 0)
  2775. return;
  2776. mesh = mm->default_mesh;
  2777. if (mm->input.selector)
  2778. mesh = mm->input.selector[v_off];
  2779. else if (mm->input.block_selector)
  2780. mesh = mm->input.block_selector[bt];
  2781. if (geo <= STBVOX_GEOM_ceil_slope_north_is_bottom) {
  2782. // this is the simple case, we can just use regular block gen with special vmesh calculated with vheight
  2783. stbvox_mesh_vertex basevert;
  2784. stbvox_mesh_vertex vmesh[6][4];
  2785. stbvox_rotate rotate = { 0,0,0,0 };
  2786. unsigned char simple_rot = rot;
  2787. int i;
  2788. // we only need to do this for the displayed faces, but it's easier
  2789. // to just do it up front; @OPTIMIZE check if it's faster to do it
  2790. // for visible faces only
  2791. for (i=0; i < 6*4; ++i) {
  2792. int vert = stbvox_vertex_selector[0][i];
  2793. vert = stbvox_rotate_vertex[vert][rot];
  2794. vmesh[0][i] = stbvox_vmesh_pre_vheight[0][i]
  2795. + stbvox_geometry_vheight[geo][vert];
  2796. }
  2797. basevert = stbvox_vertex_encode(pos.x, pos.y, pos.z << STBVOX_CONFIG_PRECISION_Z, 0,0);
  2798. if (mm->input.selector) {
  2799. mesh = mm->input.selector[v_off];
  2800. } else if (mm->input.block_selector)
  2801. mesh = mm->input.block_selector[bt];
  2802. // check if we're going off the end
  2803. if (mm->output_cur[mesh][0] + mm->output_size[mesh][0]*6 > mm->output_end[mesh][0]) {
  2804. mm->full = 1;
  2805. return;
  2806. }
  2807. if (geo >= STBVOX_GEOM_floor_slope_north_is_top) {
  2808. if (visible_faces & (1 << STBVOX_FACE_up)) {
  2809. int normal = geo == STBVOX_GEOM_floor_slope_north_is_top ? stbvox_floor_slope_for_rot[simple_rot] : STBVOX_FACE_up;
  2810. rotate.facerot = simple_rot;
  2811. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_up , v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, normal);
  2812. }
  2813. if (visible_faces & (1 << STBVOX_FACE_down)) {
  2814. int normal = geo == STBVOX_GEOM_ceil_slope_north_is_bottom ? stbvox_ceil_slope_for_rot[simple_rot] : STBVOX_FACE_down;
  2815. rotate.facerot = (-rotate.facerot) & 3;
  2816. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, normal);
  2817. }
  2818. } else {
  2819. if (visible_faces & (1 << STBVOX_FACE_up)) {
  2820. rotate.facerot = simple_rot;
  2821. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_up , v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, STBVOX_FACE_up);
  2822. }
  2823. if (visible_faces & (1 << STBVOX_FACE_down)) {
  2824. rotate.facerot = (-rotate.facerot) & 3;
  2825. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, STBVOX_FACE_down);
  2826. }
  2827. }
  2828. if (mm->input.rotate) {
  2829. unsigned char val = mm->input.rotate[v_off];
  2830. rotate.block = (val >> 0) & 3;
  2831. rotate.overlay = (val >> 2) & 3;
  2832. //rotate.tex2 = (val >> 4) & 3;
  2833. rotate.ecolor = (val >> 6) & 3;
  2834. } else {
  2835. rotate.block = rotate.overlay = rotate.ecolor = simple_rot;
  2836. }
  2837. rotate.facerot = 0;
  2838. if (visible_faces & (1 << STBVOX_FACE_north))
  2839. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_north, v_off, pos, basevert, vmesh[STBVOX_FACE_north], mesh, STBVOX_FACE_north);
  2840. if (visible_faces & (1 << STBVOX_FACE_south))
  2841. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_south, v_off, pos, basevert, vmesh[STBVOX_FACE_south], mesh, STBVOX_FACE_south);
  2842. if (visible_faces & (1 << STBVOX_FACE_east))
  2843. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_east , v_off, pos, basevert, vmesh[STBVOX_FACE_east ], mesh, STBVOX_FACE_east);
  2844. if (visible_faces & (1 << STBVOX_FACE_west))
  2845. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_west , v_off, pos, basevert, vmesh[STBVOX_FACE_west ], mesh, STBVOX_FACE_west);
  2846. }
  2847. if (geo >= STBVOX_GEOM_floor_vheight_03) {
  2848. // this case can also be generated with regular block gen with special vmesh,
  2849. // except:
  2850. // if we want to generate middle diagonal for 'weird' blocks
  2851. // it's more complicated to detect neighbor matchups
  2852. stbvox_mesh_vertex vmesh[6][4];
  2853. stbvox_mesh_vertex cube[8];
  2854. stbvox_mesh_vertex basevert;
  2855. stbvox_rotate rotate = { 0,0,0,0 };
  2856. unsigned char simple_rot = rot;
  2857. unsigned char ht[4];
  2858. int extreme;
  2859. // extract the heights
  2860. #ifdef STBVOX_CONFIG_VHEIGHT_IN_LIGHTING
  2861. ht[0] = mm->input.lighting[v_off ] & 3;
  2862. ht[1] = mm->input.lighting[v_off+ew_off ] & 3;
  2863. ht[2] = mm->input.lighting[v_off +ns_off] & 3;
  2864. ht[3] = mm->input.lighting[v_off+ew_off+ns_off] & 3;
  2865. #else
  2866. if (mm->input.vheight) {
  2867. unsigned char v = mm->input.vheight[v_off];
  2868. ht[0] = (v >> 0) & 3;
  2869. ht[1] = (v >> 2) & 3;
  2870. ht[2] = (v >> 4) & 3;
  2871. ht[3] = (v >> 6) & 3;
  2872. } else if (mm->input.block_vheight) {
  2873. unsigned char v = mm->input.block_vheight[bt];
  2874. unsigned char raw[4];
  2875. int i;
  2876. raw[0] = (v >> 0) & 3;
  2877. raw[1] = (v >> 2) & 3;
  2878. raw[2] = (v >> 4) & 3;
  2879. raw[3] = (v >> 6) & 3;
  2880. for (i=0; i < 4; ++i)
  2881. ht[i] = raw[stbvox_rotate_vertex[i][rot]];
  2882. } else if (mm->input.packed_compact) {
  2883. ht[0] = (mm->input.packed_compact[v_off ] >> 2) & 3;
  2884. ht[1] = (mm->input.packed_compact[v_off+ew_off ] >> 2) & 3;
  2885. ht[2] = (mm->input.packed_compact[v_off +ns_off] >> 2) & 3;
  2886. ht[3] = (mm->input.packed_compact[v_off+ew_off+ns_off] >> 2) & 3;
  2887. } else if (mm->input.geometry) {
  2888. ht[0] = mm->input.geometry[v_off ] >> 6;
  2889. ht[1] = mm->input.geometry[v_off+ew_off ] >> 6;
  2890. ht[2] = mm->input.geometry[v_off +ns_off] >> 6;
  2891. ht[3] = mm->input.geometry[v_off+ew_off+ns_off] >> 6;
  2892. } else {
  2893. assert(0);
  2894. }
  2895. #endif
  2896. // flag whether any sides go off the top of the block, which means
  2897. // our visible_faces test was wrong
  2898. extreme = (ht[0] == 3 || ht[1] == 3 || ht[2] == 3 || ht[3] == 3);
  2899. if (geo >= STBVOX_GEOM_ceil_vheight_03) {
  2900. cube[0] = stbvox_vertex_encode(0,0,ht[0],0,0);
  2901. cube[1] = stbvox_vertex_encode(0,0,ht[1],0,0);
  2902. cube[2] = stbvox_vertex_encode(0,0,ht[2],0,0);
  2903. cube[3] = stbvox_vertex_encode(0,0,ht[3],0,0);
  2904. cube[4] = stbvox_vertex_encode(0,0,2,0,0);
  2905. cube[5] = stbvox_vertex_encode(0,0,2,0,0);
  2906. cube[6] = stbvox_vertex_encode(0,0,2,0,0);
  2907. cube[7] = stbvox_vertex_encode(0,0,2,0,0);
  2908. } else {
  2909. cube[0] = stbvox_vertex_encode(0,0,0,0,0);
  2910. cube[1] = stbvox_vertex_encode(0,0,0,0,0);
  2911. cube[2] = stbvox_vertex_encode(0,0,0,0,0);
  2912. cube[3] = stbvox_vertex_encode(0,0,0,0,0);
  2913. cube[4] = stbvox_vertex_encode(0,0,ht[0],0,0);
  2914. cube[5] = stbvox_vertex_encode(0,0,ht[1],0,0);
  2915. cube[6] = stbvox_vertex_encode(0,0,ht[2],0,0);
  2916. cube[7] = stbvox_vertex_encode(0,0,ht[3],0,0);
  2917. }
  2918. if (!mm->input.vheight && mm->input.block_vheight) {
  2919. // @TODO: support block vheight here, I've forgotten what needs to be done specially
  2920. }
  2921. // build vertex mesh
  2922. {
  2923. int i;
  2924. for (i=0; i < 6*4; ++i) {
  2925. int vert = stbvox_vertex_selector[0][i];
  2926. vmesh[0][i] = stbvox_vmesh_pre_vheight[0][i]
  2927. + cube[vert];
  2928. }
  2929. }
  2930. basevert = stbvox_vertex_encode(pos.x, pos.y, pos.z << STBVOX_CONFIG_PRECISION_Z, 0,0);
  2931. // check if we're going off the end
  2932. if (mm->output_cur[mesh][0] + mm->output_size[mesh][0]*6 > mm->output_end[mesh][0]) {
  2933. mm->full = 1;
  2934. return;
  2935. }
  2936. // @TODO generate split faces
  2937. if (visible_faces & (1 << STBVOX_FACE_up)) {
  2938. if (geo >= STBVOX_GEOM_ceil_vheight_03)
  2939. // flat
  2940. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_up , v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, STBVOX_FACE_up);
  2941. else {
  2942. #ifndef STBVOX_CONFIG_OPTIMIZED_VHEIGHT
  2943. // check if it's non-planar
  2944. if (cube[5] + cube[6] != cube[4] + cube[7]) {
  2945. // not planar, split along diagonal and make degenerate quads
  2946. if (geo == STBVOX_GEOM_floor_vheight_03)
  2947. stbvox_make_03_split_mesh_for_face(mm, rotate, STBVOX_FACE_up, v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, ht);
  2948. else
  2949. stbvox_make_12_split_mesh_for_face(mm, rotate, STBVOX_FACE_up, v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, ht);
  2950. } else
  2951. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_up , v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, stbvox_planar_face_up_normal[ht[2]][ht[1]][ht[0]]);
  2952. #else
  2953. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_up , v_off, pos, basevert, vmesh[STBVOX_FACE_up], mesh, stbvox_optimized_face_up_normal[ht[3]][ht[2]][ht[1]][ht[0]]);
  2954. #endif
  2955. }
  2956. }
  2957. if (visible_faces & (1 << STBVOX_FACE_down)) {
  2958. if (geo < STBVOX_GEOM_ceil_vheight_03)
  2959. // flat
  2960. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, STBVOX_FACE_down);
  2961. else {
  2962. #ifndef STBVOX_CONFIG_OPTIMIZED_VHEIGHT
  2963. // check if it's non-planar
  2964. if (cube[1] + cube[2] != cube[0] + cube[3]) {
  2965. // not planar, split along diagonal and make degenerate quads
  2966. if (geo == STBVOX_GEOM_ceil_vheight_03)
  2967. stbvox_make_03_split_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, ht);
  2968. else
  2969. stbvox_make_12_split_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, ht);
  2970. } else
  2971. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, stbvox_reverse_face[stbvox_planar_face_up_normal[ht[2]][ht[1]][ht[0]]]);
  2972. #else
  2973. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_down, v_off, pos, basevert, vmesh[STBVOX_FACE_down], mesh, stbvox_reverse_face[stbvox_optimized_face_up_normal[ht[3]][ht[2]][ht[1]][ht[0]]]);
  2974. #endif
  2975. }
  2976. }
  2977. if (mm->input.rotate) {
  2978. unsigned char val = mm->input.rotate[v_off];
  2979. rotate.block = (val >> 0) & 3;
  2980. rotate.overlay = (val >> 2) & 3;
  2981. //rotate.tex2 = (val >> 4) & 3;
  2982. rotate.ecolor = (val >> 6) & 3;
  2983. } else if (mm->input.selector) {
  2984. rotate.block = rotate.overlay = rotate.ecolor = simple_rot;
  2985. }
  2986. if ((visible_faces & (1 << STBVOX_FACE_north)) || (extreme && (ht[2] == 3 || ht[3] == 3)))
  2987. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_north, v_off, pos, basevert, vmesh[STBVOX_FACE_north], mesh, STBVOX_FACE_north);
  2988. if ((visible_faces & (1 << STBVOX_FACE_south)) || (extreme && (ht[0] == 3 || ht[1] == 3)))
  2989. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_south, v_off, pos, basevert, vmesh[STBVOX_FACE_south], mesh, STBVOX_FACE_south);
  2990. if ((visible_faces & (1 << STBVOX_FACE_east)) || (extreme && (ht[1] == 3 || ht[3] == 3)))
  2991. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_east , v_off, pos, basevert, vmesh[STBVOX_FACE_east ], mesh, STBVOX_FACE_east);
  2992. if ((visible_faces & (1 << STBVOX_FACE_west)) || (extreme && (ht[0] == 3 || ht[2] == 3)))
  2993. stbvox_make_mesh_for_face(mm, rotate, STBVOX_FACE_west , v_off, pos, basevert, vmesh[STBVOX_FACE_west ], mesh, STBVOX_FACE_west);
  2994. }
  2995. if (geo == STBVOX_GEOM_crossed_pair) {
  2996. // this can be generated with a special vmesh
  2997. stbvox_mesh_vertex basevert = stbvox_vertex_encode(pos.x, pos.y, pos.z << STBVOX_CONFIG_PRECISION_Z , 0,0);
  2998. unsigned char simple_rot=0;
  2999. stbvox_rotate rot = { 0,0,0,0 };
  3000. unsigned char mesh = mm->default_mesh;
  3001. if (mm->input.selector) {
  3002. mesh = mm->input.selector[v_off];
  3003. simple_rot = mesh >> 4;
  3004. mesh &= 15;
  3005. }
  3006. if (mm->input.block_selector) {
  3007. mesh = mm->input.block_selector[bt];
  3008. }
  3009. // check if we're going off the end
  3010. if (mm->output_cur[mesh][0] + mm->output_size[mesh][0]*4 > mm->output_end[mesh][0]) {
  3011. mm->full = 1;
  3012. return;
  3013. }
  3014. if (mm->input.rotate) {
  3015. unsigned char val = mm->input.rotate[v_off];
  3016. rot.block = (val >> 0) & 3;
  3017. rot.overlay = (val >> 2) & 3;
  3018. //rot.tex2 = (val >> 4) & 3;
  3019. rot.ecolor = (val >> 6) & 3;
  3020. } else if (mm->input.selector) {
  3021. rot.block = rot.overlay = rot.ecolor = simple_rot;
  3022. }
  3023. rot.facerot = 0;
  3024. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_north, v_off, pos, basevert, stbvox_vmesh_crossed_pair[STBVOX_FACE_north], mesh, STBVF_ne_u_cross);
  3025. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_south, v_off, pos, basevert, stbvox_vmesh_crossed_pair[STBVOX_FACE_south], mesh, STBVF_sw_u_cross);
  3026. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_east , v_off, pos, basevert, stbvox_vmesh_crossed_pair[STBVOX_FACE_east ], mesh, STBVF_se_u_cross);
  3027. stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_west , v_off, pos, basevert, stbvox_vmesh_crossed_pair[STBVOX_FACE_west ], mesh, STBVF_nw_u_cross);
  3028. }
  3029. // @TODO
  3030. // STBVOX_GEOM_floor_slope_north_is_top_as_wall,
  3031. // STBVOX_GEOM_ceil_slope_north_is_bottom_as_wall,
  3032. }
  3033. static void stbvox_make_mesh_for_column(stbvox_mesh_maker *mm, int x, int y, int z0)
  3034. {
  3035. stbvox_pos pos;
  3036. int v_off = x * mm->x_stride_in_bytes + y * mm->y_stride_in_bytes;
  3037. int ns_off = mm->y_stride_in_bytes;
  3038. int ew_off = mm->x_stride_in_bytes;
  3039. pos.x = x;
  3040. pos.y = y;
  3041. pos.z = 0;
  3042. if (mm->input.geometry) {
  3043. unsigned char *bt = mm->input.blocktype + v_off;
  3044. unsigned char *geo = mm->input.geometry + v_off;
  3045. int z;
  3046. for (z=z0; z < mm->z1; ++z) {
  3047. if (bt[z] && ( !bt[z+ns_off] || !STBVOX_GET_GEO(geo[z+ns_off]) || !bt[z-ns_off] || !STBVOX_GET_GEO(geo[z-ns_off])
  3048. || !bt[z+ew_off] || !STBVOX_GET_GEO(geo[z+ew_off]) || !bt[z-ew_off] || !STBVOX_GET_GEO(geo[z-ew_off])
  3049. || !bt[z-1] || !STBVOX_GET_GEO(geo[z-1]) || !bt[z+1] || !STBVOX_GET_GEO(geo[z+1])))
  3050. { // TODO check up and down
  3051. pos.z = z;
  3052. stbvox_make_mesh_for_block_with_geo(mm, pos, v_off+z);
  3053. if (mm->full) {
  3054. mm->cur_z = z;
  3055. return;
  3056. }
  3057. }
  3058. }
  3059. } else if (mm->input.block_geometry) {
  3060. int z;
  3061. unsigned char *bt = mm->input.blocktype + v_off;
  3062. unsigned char *geo = mm->input.block_geometry;
  3063. for (z=z0; z < mm->z1; ++z) {
  3064. if (bt[z] && ( geo[bt[z+ns_off]] != STBVOX_GEOM_solid
  3065. || geo[bt[z-ns_off]] != STBVOX_GEOM_solid
  3066. || geo[bt[z+ew_off]] != STBVOX_GEOM_solid
  3067. || geo[bt[z-ew_off]] != STBVOX_GEOM_solid
  3068. || geo[bt[z-1]] != STBVOX_GEOM_solid
  3069. || geo[bt[z+1]] != STBVOX_GEOM_solid))
  3070. {
  3071. pos.z = z;
  3072. stbvox_make_mesh_for_block_with_geo(mm, pos, v_off+z);
  3073. if (mm->full) {
  3074. mm->cur_z = z;
  3075. return;
  3076. }
  3077. }
  3078. }
  3079. } else {
  3080. unsigned char *bt = mm->input.blocktype + v_off;
  3081. int z;
  3082. #if STBVOX_CONFIG_PRECISION_Z == 1
  3083. stbvox_mesh_vertex *vmesh = stbvox_vmesh_delta_half_z[0];
  3084. #else
  3085. stbvox_mesh_vertex *vmesh = stbvox_vmesh_delta_normal[0];
  3086. #endif
  3087. for (z=z0; z < mm->z1; ++z) {
  3088. // if it's solid and at least one neighbor isn't solid
  3089. if (bt[z] && (!bt[z+ns_off] || !bt[z-ns_off] || !bt[z+ew_off] || !bt[z-ew_off] || !bt[z-1] || !bt[z+1])) {
  3090. pos.z = z;
  3091. stbvox_make_mesh_for_block(mm, pos, v_off+z, vmesh);
  3092. if (mm->full) {
  3093. mm->cur_z = z;
  3094. return;
  3095. }
  3096. }
  3097. }
  3098. }
  3099. }
  3100. static void stbvox_bring_up_to_date(stbvox_mesh_maker *mm)
  3101. {
  3102. if (mm->config_dirty) {
  3103. int i;
  3104. #ifdef STBVOX_ICONFIG_FACE_ATTRIBUTE
  3105. mm->num_mesh_slots = 1;
  3106. for (i=0; i < STBVOX_MAX_MESHES; ++i) {
  3107. mm->output_size[i][0] = 32;
  3108. mm->output_step[i][0] = 8;
  3109. }
  3110. #else
  3111. mm->num_mesh_slots = 2;
  3112. for (i=0; i < STBVOX_MAX_MESHES; ++i) {
  3113. mm->output_size[i][0] = 16;
  3114. mm->output_step[i][0] = 4;
  3115. mm->output_size[i][1] = 4;
  3116. mm->output_step[i][1] = 4;
  3117. }
  3118. #endif
  3119. mm->config_dirty = 0;
  3120. }
  3121. }
  3122. int stbvox_make_mesh(stbvox_mesh_maker *mm)
  3123. {
  3124. int x,y;
  3125. stbvox_bring_up_to_date(mm);
  3126. mm->full = 0;
  3127. if (mm->cur_x > mm->x0 || mm->cur_y > mm->y0 || mm->cur_z > mm->z0) {
  3128. stbvox_make_mesh_for_column(mm, mm->cur_x, mm->cur_y, mm->cur_z);
  3129. if (mm->full)
  3130. return 0;
  3131. ++mm->cur_y;
  3132. while (mm->cur_y < mm->y1 && !mm->full) {
  3133. stbvox_make_mesh_for_column(mm, mm->cur_x, mm->cur_y, mm->z0);
  3134. if (mm->full)
  3135. return 0;
  3136. ++mm->cur_y;
  3137. }
  3138. ++mm->cur_x;
  3139. }
  3140. for (x=mm->cur_x; x < mm->x1; ++x) {
  3141. for (y=mm->y0; y < mm->y1; ++y) {
  3142. stbvox_make_mesh_for_column(mm, x, y, mm->z0);
  3143. if (mm->full) {
  3144. mm->cur_x = x;
  3145. mm->cur_y = y;
  3146. return 0;
  3147. }
  3148. }
  3149. }
  3150. return 1;
  3151. }
  3152. void stbvox_init_mesh_maker(stbvox_mesh_maker *mm)
  3153. {
  3154. memset(mm, 0, sizeof(*mm));
  3155. stbvox_build_default_palette();
  3156. mm->config_dirty = 1;
  3157. mm->default_mesh = 0;
  3158. }
  3159. int stbvox_get_buffer_count(stbvox_mesh_maker *mm)
  3160. {
  3161. stbvox_bring_up_to_date(mm);
  3162. return mm->num_mesh_slots;
  3163. }
  3164. int stbvox_get_buffer_size_per_quad(stbvox_mesh_maker *mm, int n)
  3165. {
  3166. return mm->output_size[0][n];
  3167. }
  3168. void stbvox_reset_buffers(stbvox_mesh_maker *mm)
  3169. {
  3170. int i;
  3171. for (i=0; i < STBVOX_MAX_MESHES*STBVOX_MAX_MESH_SLOTS; ++i) {
  3172. mm->output_cur[0][i] = 0;
  3173. mm->output_buffer[0][i] = 0;
  3174. }
  3175. }
  3176. void stbvox_set_buffer(stbvox_mesh_maker *mm, int mesh, int slot, void *buffer, size_t len)
  3177. {
  3178. int i;
  3179. stbvox_bring_up_to_date(mm);
  3180. mm->output_buffer[mesh][slot] = (char *) buffer;
  3181. mm->output_cur [mesh][slot] = (char *) buffer;
  3182. mm->output_len [mesh][slot] = len;
  3183. mm->output_end [mesh][slot] = (char *) buffer + len;
  3184. for (i=0; i < STBVOX_MAX_MESH_SLOTS; ++i) {
  3185. if (mm->output_buffer[mesh][i]) {
  3186. assert(mm->output_len[mesh][i] / mm->output_size[mesh][i] == mm->output_len[mesh][slot] / mm->output_size[mesh][slot]);
  3187. }
  3188. }
  3189. }
  3190. void stbvox_set_default_mesh(stbvox_mesh_maker *mm, int mesh)
  3191. {
  3192. mm->default_mesh = mesh;
  3193. }
  3194. int stbvox_get_quad_count(stbvox_mesh_maker *mm, int mesh)
  3195. {
  3196. return (mm->output_cur[mesh][0] - mm->output_buffer[mesh][0]) / mm->output_size[mesh][0];
  3197. }
  3198. stbvox_input_description *stbvox_get_input_description(stbvox_mesh_maker *mm)
  3199. {
  3200. return &mm->input;
  3201. }
  3202. void stbvox_set_input_range(stbvox_mesh_maker *mm, int x0, int y0, int z0, int x1, int y1, int z1)
  3203. {
  3204. mm->x0 = x0;
  3205. mm->y0 = y0;
  3206. mm->z0 = z0;
  3207. mm->x1 = x1;
  3208. mm->y1 = y1;
  3209. mm->z1 = z1;
  3210. mm->cur_x = x0;
  3211. mm->cur_y = y0;
  3212. mm->cur_z = z0;
  3213. // @TODO validate that this range is representable in this mode
  3214. }
  3215. void stbvox_get_transform(stbvox_mesh_maker *mm, float transform[3][3])
  3216. {
  3217. // scale
  3218. transform[0][0] = 1.0;
  3219. transform[0][1] = 1.0;
  3220. #if STBVOX_CONFIG_PRECISION_Z==1
  3221. transform[0][2] = 0.5f;
  3222. #else
  3223. transform[0][2] = 1.0f;
  3224. #endif
  3225. // translation
  3226. transform[1][0] = (float) (mm->pos_x);
  3227. transform[1][1] = (float) (mm->pos_y);
  3228. transform[1][2] = (float) (mm->pos_z);
  3229. // texture coordinate projection translation
  3230. transform[2][0] = (float) (mm->pos_x & 255); // @TODO depends on max texture scale
  3231. transform[2][1] = (float) (mm->pos_y & 255);
  3232. transform[2][2] = (float) (mm->pos_z & 255);
  3233. }
  3234. void stbvox_get_bounds(stbvox_mesh_maker *mm, float bounds[2][3])
  3235. {
  3236. bounds[0][0] = (float) (mm->pos_x + mm->x0);
  3237. bounds[0][1] = (float) (mm->pos_y + mm->y0);
  3238. bounds[0][2] = (float) (mm->pos_z + mm->z0);
  3239. bounds[1][0] = (float) (mm->pos_x + mm->x1);
  3240. bounds[1][1] = (float) (mm->pos_y + mm->y1);
  3241. bounds[1][2] = (float) (mm->pos_z + mm->z1);
  3242. }
  3243. void stbvox_set_mesh_coordinates(stbvox_mesh_maker *mm, int x, int y, int z)
  3244. {
  3245. mm->pos_x = x;
  3246. mm->pos_y = y;
  3247. mm->pos_z = z;
  3248. }
  3249. void stbvox_set_input_stride(stbvox_mesh_maker *mm, int x_stride_in_bytes, int y_stride_in_bytes)
  3250. {
  3251. int f,v;
  3252. mm->x_stride_in_bytes = x_stride_in_bytes;
  3253. mm->y_stride_in_bytes = y_stride_in_bytes;
  3254. for (f=0; f < 6; ++f) {
  3255. for (v=0; v < 4; ++v) {
  3256. mm->cube_vertex_offset[f][v] = stbvox_vertex_vector[f][v][0] * mm->x_stride_in_bytes
  3257. + stbvox_vertex_vector[f][v][1] * mm->y_stride_in_bytes
  3258. + stbvox_vertex_vector[f][v][2] ;
  3259. mm->vertex_gather_offset[f][v] = (stbvox_vertex_vector[f][v][0]-1) * mm->x_stride_in_bytes
  3260. + (stbvox_vertex_vector[f][v][1]-1) * mm->y_stride_in_bytes
  3261. + (stbvox_vertex_vector[f][v][2]-1) ;
  3262. }
  3263. }
  3264. }
  3265. /////////////////////////////////////////////////////////////////////////////
  3266. //
  3267. // offline computation of tables
  3268. //
  3269. #if 0
  3270. // compute optimized vheight table
  3271. static char *normal_names[32] =
  3272. {
  3273. 0,0,0,0,"u ",0, "eu ",0,
  3274. 0,0,0,0,"ne_u",0, "nu ",0,
  3275. 0,0,0,0,"nw_u",0, "wu ",0,
  3276. 0,0,0,0,"sw_u",0, "su ",0,
  3277. };
  3278. static char *find_best_normal(float x, float y, float z)
  3279. {
  3280. int best_slot = 4;
  3281. float best_dot = 0;
  3282. int i;
  3283. for (i=0; i < 32; ++i) {
  3284. if (normal_names[i]) {
  3285. float dot = x * stbvox_default_normals[i][0] + y * stbvox_default_normals[i][1] + z * stbvox_default_normals[i][2];
  3286. if (dot > best_dot) {
  3287. best_dot = dot;
  3288. best_slot = i;
  3289. }
  3290. }
  3291. }
  3292. return normal_names[best_slot];
  3293. }
  3294. int main(int argc, char **argv)
  3295. {
  3296. int sw,se,nw,ne;
  3297. for (ne=0; ne < 4; ++ne) {
  3298. for (nw=0; nw < 4; ++nw) {
  3299. for (se=0; se < 4; ++se) {
  3300. printf(" { ");
  3301. for (sw=0; sw < 4; ++sw) {
  3302. float x = (float) (nw + sw - ne - se);
  3303. float y = (float) (sw + se - nw - ne);
  3304. float z = 2;
  3305. printf("STBVF_%s, ", find_best_normal(x,y,z));
  3306. }
  3307. printf("},\n");
  3308. }
  3309. }
  3310. }
  3311. return 0;
  3312. }
  3313. #endif
  3314. // @TODO
  3315. //
  3316. // - test API for texture rotation on side faces
  3317. // - API for texture rotation on top & bottom
  3318. // - better culling of vheight faces with vheight neighbors
  3319. // - better culling of non-vheight faces with vheight neighbors
  3320. // - gather vertex lighting from slopes correctly
  3321. // - better support texture edge_clamp: currently if you fall
  3322. // exactly on 1.0 you get wrapped incorrectly; this is rare, but
  3323. // can avoid: compute texcoords in vertex shader, offset towards
  3324. // center before modding, need 2 bits per vertex to know offset direction)
  3325. // - other mesh modes (10,6,4-byte quads)
  3326. //
  3327. //
  3328. // With TexBuffer for the fixed vertex data, we can actually do
  3329. // minecrafty non-blocks like stairs -- we still probably only
  3330. // want 256 or so, so we can't do the equivalent of all the vheight
  3331. // combos, but that's ok. The 256 includes baked rotations, but only
  3332. // some of them need it, and lots of block types share some faces.
  3333. //
  3334. // mode 5 (6 bytes): mode 6 (6 bytes)
  3335. // x:7 x:6
  3336. // y:7 y:6
  3337. // z:6 z:6
  3338. // tex1:8 tex1:8
  3339. // tex2:8 tex2:7
  3340. // color:8 color:8
  3341. // face:4 face:7
  3342. //
  3343. //
  3344. // side faces (all x4) top&bottom faces (2x) internal faces (1x)
  3345. // 1 regular 1 regular
  3346. // 2 slabs 2
  3347. // 8 stairs 4 stairs 16
  3348. // 4 diag side 8
  3349. // 4 upper diag side 8
  3350. // 4 lower diag side 8
  3351. // 4 crossed pairs
  3352. //
  3353. // 23*4 + 5*4 + 46
  3354. // == 92 + 20 + 46 = 158
  3355. //
  3356. // Must drop 30 of them to fit in 7 bits:
  3357. // ceiling half diagonals: 16+8 = 24
  3358. // Need to get rid of 6 more.
  3359. // ceiling diagonals: 8+4 = 12
  3360. // This brings it to 122, so can add a crossed-pair variant.
  3361. // (diagonal and non-diagonal, or randomly offset)
  3362. // Or carpet, which would be 5 more.
  3363. //
  3364. //
  3365. // Mode 4 (10 bytes):
  3366. // v: z:2,light:6
  3367. // f: x:6,y:6,z:7, t1:8,t2:8,c:8,f:5
  3368. //
  3369. // Mode ? (10 bytes)
  3370. // v: xyz:5 (27 values), light:3
  3371. // f: x:7,y:7,z:6, t1:8,t2:8,c:8,f:4
  3372. // (v: x:2,y:2,z:2,light:2)
  3373. #endif // STB_VOXEL_RENDER_IMPLEMENTATION
  3374. /*
  3375. ------------------------------------------------------------------------------
  3376. This software is available under 2 licenses -- choose whichever you prefer.
  3377. ------------------------------------------------------------------------------
  3378. ALTERNATIVE A - MIT License
  3379. Copyright (c) 2017 Sean Barrett
  3380. Permission is hereby granted, free of charge, to any person obtaining a copy of
  3381. this software and associated documentation files (the "Software"), to deal in
  3382. the Software without restriction, including without limitation the rights to
  3383. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  3384. of the Software, and to permit persons to whom the Software is furnished to do
  3385. so, subject to the following conditions:
  3386. The above copyright notice and this permission notice shall be included in all
  3387. copies or substantial portions of the Software.
  3388. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3389. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3390. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3391. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3392. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  3393. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  3394. SOFTWARE.
  3395. ------------------------------------------------------------------------------
  3396. ALTERNATIVE B - Public Domain (www.unlicense.org)
  3397. This is free and unencumbered software released into the public domain.
  3398. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  3399. software, either in source code form or as a compiled binary, for any purpose,
  3400. commercial or non-commercial, and by any means.
  3401. In jurisdictions that recognize copyright laws, the author or authors of this
  3402. software dedicate any and all copyright interest in the software to the public
  3403. domain. We make this dedication for the benefit of the public at large and to
  3404. the detriment of our heirs and successors. We intend this dedication to be an
  3405. overt act of relinquishment in perpetuity of all present and future rights to
  3406. this software under copyright law.
  3407. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3408. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3409. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3410. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  3411. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3412. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3413. ------------------------------------------------------------------------------
  3414. */