highPoly.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Example program for small3dlib, testing a high-poly offline model.
  3. author: Miloslav Ciz
  4. license: CC0 1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #define S3L_STRICT_NEAR_CULLING 0
  9. #if TEXTURES
  10. #define S3L_PERSPECTIVE_CORRECTION 2
  11. #else
  12. #define S3L_PERSPECTIVE_CORRECTION 0
  13. #endif
  14. #define S3L_NEAR (S3L_F / 5)
  15. #define S3L_Z_BUFFER 1
  16. #define S3L_PIXEL_FUNCTION drawPixel
  17. #define S3L_RESOLUTION_X 800
  18. #define S3L_RESOLUTION_Y 600
  19. #include "../small3dlib.h"
  20. #include "alligatorModel.h"
  21. S3L_Unit normals[ALLIGATOR_VERTEX_COUNT * 3];
  22. uint8_t frameBuffer[S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 3];
  23. S3L_Scene scene;
  24. S3L_Vec4 teleportPoint;
  25. uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
  26. void clearScreen()
  27. {
  28. memset(frameBuffer,255,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 3 * sizeof(uint8_t));
  29. }
  30. void saveImage(char *fileName)
  31. {
  32. printf("saving image file: %s\n",fileName);
  33. FILE *f = fopen(fileName,"w");
  34. fprintf(f,"P3\n%d %d\n255\n",S3L_RESOLUTION_X,S3L_RESOLUTION_Y);
  35. for (int i = 0; i < S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 3; i += 3)
  36. fprintf(f,"%d %d %d\n",frameBuffer[i],frameBuffer[i + 1],frameBuffer[i + 2]);
  37. fclose(f);
  38. }
  39. uint32_t previousTriangle = 1000;
  40. S3L_Vec4 n0, n1, n2, toLight;
  41. void drawPixel(S3L_PixelInfo *p)
  42. {
  43. if (p->triangleID != previousTriangle)
  44. {
  45. S3L_getIndexedTriangleValues(
  46. p->triangleIndex,
  47. scene.models[p->modelIndex].triangles,
  48. normals,3,&n0,&n1,&n2);
  49. previousTriangle = p->triangleID;
  50. }
  51. S3L_Vec4 normal;
  52. normal.x = S3L_interpolateBarycentric(n0.x,n1.x,n2.x,p->barycentric);
  53. normal.y = S3L_interpolateBarycentric(n0.y,n1.y,n2.y,p->barycentric);
  54. normal.z = S3L_interpolateBarycentric(n0.z,n1.z,n2.z,p->barycentric);
  55. S3L_vec3Normalize(&normal);
  56. S3L_Unit shading =
  57. (S3L_vec3Dot(normal,toLight) + S3L_F) / 2;
  58. shading = S3L_interpolate(shading,0,p->depth,32 * S3L_F);
  59. int index = (p->y * S3L_RESOLUTION_X + p->x) * 3;
  60. frameBuffer[index] = S3L_clamp(S3L_interpolateByUnitFrom0(200,shading),0,255);
  61. frameBuffer[index + 1] = S3L_clamp(S3L_interpolateByUnitFrom0(255,shading),0,255);
  62. frameBuffer[index + 2] = S3L_clamp(S3L_interpolateByUnitFrom0(150,shading),0,255);
  63. }
  64. int main()
  65. {
  66. S3L_vec4Set(&toLight,10,-10,-10,0);
  67. S3L_vec3Normalize(&toLight);
  68. alligatorModelInit();
  69. S3L_computeModelNormals(alligatorModel,normals,0);
  70. S3L_sceneInit(&alligatorModel,1,&scene);
  71. scene.camera.transform.translation.z = -8 * S3L_F;
  72. scene.camera.transform.translation.x = 9 * S3L_F;
  73. scene.camera.transform.translation.y = 6 * S3L_F;
  74. S3L_lookAt(scene.models[0].transform.translation,&(scene.camera.transform));
  75. clearScreen();
  76. S3L_newFrame();
  77. S3L_drawScene(scene);
  78. saveImage("allligator.ppm");
  79. return 0;
  80. }