level.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Example program of small3dlib for Pokitto -- Quake-like level.
  3. author: Miloslav Ciz
  4. license: CC0 1.0
  5. */
  6. #include "Pokitto.h"
  7. #define SUBSAMPLE 3
  8. #if 1 // This can switch between a textured and flat mode.
  9. #define S3L_Z_BUFFER 2
  10. #define S3L_SORT 0
  11. #define S3L_STENCIL_BUFFER 0
  12. #define S3L_FLAT 0
  13. #define S3L_PERSPECTIVE_CORRECTION 2
  14. #else
  15. #define S3L_Z_BUFFER 2
  16. #define S3L_SORT 0
  17. #define S3L_STENCIL_BUFFER 0
  18. #define S3L_FLAT 1
  19. #define S3L_MAX_TRIANGES_DRAWN 200
  20. #endif
  21. #define S3L_PIXEL_FUNCTION pixelFunc
  22. // Because we'll be writing pixels as 2x2, define the resolution one smaller.
  23. #define BASE_W 109
  24. #define BASE_H 87
  25. #define S3L_RESOLUTION_X (BASE_W - BASE_W / SUBSAMPLE)
  26. #define S3L_RESOLUTION_Y (BASE_H - BASE_H / SUBSAMPLE)
  27. #define S3L_STRICT_NEAR_CULLING 0
  28. #define S3L_COMPUTE_DEPTH 1 // for fog
  29. #define S3L_REDUCED_Z_BUFFER_GRANULARITY 6
  30. #include "small3dlib.h"
  31. #include "levelTexture1Pal.h"
  32. #include "levelModel.h"
  33. Pokitto::Core pokitto;
  34. #if S3L_FLAT
  35. uint8_t triangleColors[LEVEL_TRIANGLE_COUNT];
  36. #endif
  37. static inline uint8_t texture(int32_t u, int32_t v)
  38. {
  39. u = S3L_wrap(u,LEVEL1_TEXTURE_WIDTH);
  40. v = S3L_wrap(v,LEVEL1_TEXTURE_HEIGHT);
  41. uint32_t index = v * LEVEL1_TEXTURE_WIDTH + u;
  42. return level1Texture[index];
  43. }
  44. S3L_ScreenCoord subsampleMap[BASE_W + SUBSAMPLE];
  45. uint32_t previousTriangle = 100;
  46. static inline uint8_t addIntensity(uint8_t color, uint8_t intensity)
  47. {
  48. uint8_t newValue = color + intensity; // value as in HSV
  49. if (color >> 4 == newValue >> 4)
  50. return newValue;
  51. return color | 0x0F;
  52. }
  53. static inline uint8_t substractIntensity(uint8_t color, uint8_t intensity)
  54. {
  55. uint8_t newValue = color - intensity; // value as in HSV
  56. if (color >> 4 == newValue >> 4)
  57. return newValue;
  58. return 0;
  59. }
  60. uint8_t c = 0;
  61. S3L_Vec4 uv0, uv1, uv2;
  62. S3L_Index material = 0;
  63. void pixelFunc(S3L_PixelInfo *p)
  64. {
  65. uint8_t val;
  66. uint8_t *buf = pokitto.display.screenbuffer;
  67. #if S3L_FLAT
  68. val = triangleColors[p->triangleIndex];
  69. #else
  70. if (p->triangleIndex != previousTriangle)
  71. {
  72. material = levelMaterials[p->triangleIndex];
  73. if (material == 1)
  74. c = 135;
  75. else if (material == 2)
  76. c = 213;
  77. else
  78. S3L_getIndexedTriangleValues(p->triangleIndex,levelUVIndices,levelUVs,2,&uv0,&uv1,&uv2);
  79. previousTriangle = p->triangleID;
  80. }
  81. S3L_Unit fog = p->depth >> 9;
  82. if (material == 0)
  83. {
  84. S3L_Unit uv[2];
  85. uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
  86. uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
  87. c = texture(uv[0] / 32,uv[1] / 32);
  88. }
  89. val = substractIntensity(c,fog);
  90. #endif
  91. buf += subsampleMap[p->y] * 110;
  92. buf += subsampleMap[p->x];
  93. *buf = val;
  94. buf++;
  95. *buf = val;
  96. buf += 109;
  97. *buf = val;
  98. buf++;
  99. *buf = val;
  100. }
  101. S3L_Scene scene;
  102. void draw()
  103. {
  104. S3L_newFrame();
  105. S3L_drawScene(scene);
  106. }
  107. unsigned short palette[256];
  108. int main()
  109. {
  110. for (uint16_t i = 0; i < BASE_W + SUBSAMPLE; ++i)
  111. subsampleMap[i] = i + i / SUBSAMPLE;
  112. #if S3L_FLAT
  113. S3L_Vec4 toLight;
  114. S3L_setVec4(&toLight,10,5,7,0);
  115. S3L_normalizeVec3(&toLight);
  116. for (uint16_t i = 0; i < LEVEL_TRIANGLE_COUNT; ++i)
  117. {
  118. uint8_t c;
  119. S3L_Vec4 v0, v1, v2;
  120. S3L_getIndexedTriangleValues(
  121. i,
  122. levelTriangleIndices,
  123. levelVertices,3,&v0,&v1,&v2);
  124. material = levelMaterials[i];
  125. if (material == 1)
  126. c = 38;
  127. else if (material == 2)
  128. c = 53;
  129. else
  130. c = 24;
  131. S3L_Vec4 normal;
  132. S3L_triangleNormal(v0,v1,v2,&normal);
  133. triangleColors[i] = addIntensity(c,
  134. S3L_max(0,(S3L_dotProductVec3(normal,toLight) + S3L_FRACTIONS_PER_UNIT) / 64));
  135. }
  136. #endif
  137. pokitto.begin();
  138. // pokitto.display.persistence = 1;
  139. pokitto.setFrameRate(60);
  140. pokitto.display.load565Palette(level1Palette);
  141. S3L_initCamera(&scene.camera);
  142. levelModelInit();
  143. S3L_initScene(&levelModel,1,&scene);
  144. while (pokitto.isRunning())
  145. {
  146. if (pokitto.update())
  147. {
  148. S3L_Vec4 camF, camR, camU;
  149. int step = 300;
  150. int step2 = 8;
  151. S3L_rotationToDirections(
  152. scene.camera.transform.rotation,
  153. step,
  154. &camF,
  155. &camR,
  156. &camU);
  157. if (pokitto.aBtn())
  158. {
  159. if (pokitto.upBtn())
  160. scene.camera.transform.rotation.x += 8;
  161. else if (pokitto.downBtn())
  162. scene.camera.transform.rotation.x -= 8;
  163. else if (pokitto.rightBtn())
  164. scene.camera.transform.rotation.y += 8;
  165. else if (pokitto.leftBtn())
  166. scene.camera.transform.rotation.y -= 8;
  167. }
  168. else
  169. {
  170. if (pokitto.upBtn())
  171. S3L_vec3Add(&(scene.camera.transform.translation),camF);
  172. else if (pokitto.downBtn())
  173. S3L_vec3Sub(&scene.camera.transform.translation,camF);
  174. else if (pokitto.rightBtn())
  175. S3L_vec3Add(&scene.camera.transform.translation,camR);
  176. else if (pokitto.leftBtn())
  177. S3L_vec3Sub(&scene.camera.transform.translation,camR);
  178. }
  179. draw();
  180. }
  181. }
  182. }