terminalCube.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. Simple example of small3dlib, rendering in terminal.
  3. by drummyfish, released under CC0 1.0, public domain
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h> // for usleep
  7. // we need to define screen resolution before including the library:
  8. #define S3L_RESOLUTION_X 80
  9. #define S3L_RESOLUTION_Y 40
  10. // and a name of the function we'll be using to draw individual pixels:
  11. #define S3L_PIXEL_FUNCTION drawPixel
  12. #include "../small3dlib.h" // now include the library
  13. // we'll use a predefined geometry of a cube from the library:
  14. S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(S3L_F) };
  15. S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
  16. S3L_Model3D cubeModel; // 3D model, has a geometry, position, rotation etc.
  17. S3L_Scene scene; // scene we'll be rendring (can have multiple models)
  18. #define FRAME_OFFSET 20 // how many newlines will be printed before each frame
  19. #define SCREEN_SIZE (FRAME_OFFSET + (S3L_RESOLUTION_X + 1) * S3L_RESOLUTION_Y + 1)
  20. uint8_t screen[SCREEN_SIZE]; // ASCII screen
  21. /* This function will be called by the library to draw individual rasterized
  22. pixels to the screen. We should try to make this function as fast as possible
  23. as it tends to be the performance bottle neck. */
  24. void drawPixel(S3L_PixelInfo *p)
  25. {
  26. uint8_t c; // ASCII pixel we'll write to the screen
  27. /* We'll draw different triangles with different ASCII symbols to give the
  28. illusion of lighting. */
  29. if (p->triangleIndex == 0 || p->triangleIndex == 1 ||
  30. p->triangleIndex == 4 || p->triangleIndex == 5)
  31. c = '#';
  32. else if (p->triangleIndex == 2 || p->triangleIndex == 3 ||
  33. p->triangleIndex == 6 || p->triangleIndex == 7)
  34. c = 'x';
  35. else
  36. c = '.';
  37. // draw to ASCII screen
  38. screen[FRAME_OFFSET + p->y * (S3L_RESOLUTION_X + 1) + p->x] = c;
  39. }
  40. int main()
  41. {
  42. S3L_model3DInit(
  43. cubeVertices,
  44. S3L_CUBE_VERTEX_COUNT,
  45. cubeTriangles,
  46. S3L_CUBE_TRIANGLE_COUNT,
  47. &cubeModel);
  48. S3L_sceneInit( // Initialize the scene we'll be rendering.
  49. &cubeModel, // This is like an array with only one model in it.
  50. 1,
  51. &scene);
  52. // shift the camera a little bit backwards so that it's not inside the cube:
  53. scene.camera.transform.translation.z = -2 * S3L_F;
  54. for (int i = 0; i < 200; ++i) // render 200 frames
  55. {
  56. // clear the screen
  57. for (int j = 0; j < FRAME_OFFSET; ++j)
  58. screen[j] = '\n';
  59. for (int j = FRAME_OFFSET; j < SCREEN_SIZE; ++j)
  60. screen[j] = ((j - FRAME_OFFSET + 1) % (S3L_RESOLUTION_X + 1) ? ' ' : '\n');
  61. screen[SCREEN_SIZE - 1] = 0; // terminate the string
  62. S3L_newFrame(); // has to be called before each frame
  63. S3L_drawScene(scene); /* This starts the scene rendering. The drawPixel
  64. function will be called to draw it. */
  65. puts(screen); // display the frame
  66. usleep(100000); // wait a bit to let the user see the frame
  67. // now move and rotate the cube a little to see some movement:
  68. scene.models[0].transform.rotation.y += 10;
  69. scene.models[0].transform.rotation.x += 4;
  70. scene.models[0].transform.translation.x = S3L_sin(i * 4);
  71. scene.models[0].transform.translation.y = S3L_sin(i * 2) / 2;
  72. }
  73. return 0;
  74. }