Advertisement
_Jacques

Untitled

Feb 4th, 2021
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <conio.h>
  5.  
  6. LPCWSTR shading = L" .-:=+%*@#";
  7. DWORD _ = 1;
  8. HANDLE conOut = GetStdHandle(STD_OUTPUT_HANDLE);
  9. HANDLE conIn = GetStdHandle(STD_INPUT_HANDLE);
  10. CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo;
  11. float lightVector[3] = {25.0f, 25.0f, 25.0f};
  12. /*
  13. #define INPUT_SIZE 64
  14. INPUT_RECORD* inputBuffer[INPUT_SIZE];
  15. */
  16.  
  17.  
  18.  
  19. void draw(LPCWSTR character, short x, short y) {
  20.     COORD coordinates;
  21.     coordinates.X = x;
  22.     coordinates.Y = y;
  23.     WriteConsoleOutputCharacter(conOut, character, 1, coordinates, &_);
  24. };
  25.  
  26. float magnitude(float vector[]) {
  27.     return sqrtf(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
  28. };
  29.  
  30. float normalizedDotProduct(float vector1[], float vector2[]) {
  31.     return (vector1[0] * vector2[0] + vector1[1] * vector2[1] + vector1[2] * vector2[2]) / (magnitude(vector1) * magnitude(vector2));
  32. };
  33.  
  34. void drawCircle(float radius, float stretchX, float stretchY, float offsetX, float offsetY) {
  35.     for (int x = 1; x <= 2*radius*stretchX; x++) {
  36.         for (int y = 1; y <= 2*radius*stretchY; y++) {
  37.             float X = x - radius*stretchX; // The displacement of x by relation of the center of the circle's x coordinate
  38.             float Y = y - radius*stretchY;
  39.             if ((X * X/stretchX + Y * Y/stretchY) <= radius*radius) {
  40.                 draw(L"X", (x + offsetX), (y + offsetY));
  41.             };
  42.         };
  43.     };
  44. };
  45.  
  46. void drawLitSphere(float radius, float stretchX, float stretchY, float offsetX, float offsetY, float lightVector[]) {
  47.     for (int x = 1; x <= 2 * radius * stretchX; x++) {
  48.         for (int y = 1; y <= 2 * radius * stretchY; y++) {
  49.             float X = x - radius * stretchX; // The displacement of x by relation of the center of the circle's x coordinate
  50.             float Y = y - radius * stretchY;
  51.             float Z = sqrtf(radius * radius - X * X - Y * Y);
  52.             float vector[3] = { X, Y, Z };
  53.             int lightingLevel = roundf(9.0f * normalizedDotProduct(vector, lightVector));
  54.             if (lightingLevel >= 0) {
  55.                 draw(&shading[lightingLevel], x + offsetX, y + offsetY);
  56.             };
  57.         };
  58.     };
  59. };
  60.  
  61.  
  62. int main()
  63. {  
  64.     SetConsoleMode(conIn, ENABLE_WINDOW_INPUT|ENABLE_MOUSE_INPUT);
  65.     BOOL pressedKey = FALSE;
  66.     int INPUT_SIZE = 1;
  67.     INPUT_RECORD inputBuffer;
  68.     while (!pressedKey) {
  69.         ReadConsoleInput(conIn, &inputBuffer, INPUT_SIZE, &_);
  70.         if (inputBuffer.EventType == KEY_EVENT) {
  71.             float lightMagnitude = magnitude(lightVector);
  72.             float tempX = lightVector[0];
  73.             float tempY = lightVector[1];
  74.             WORD input = inputBuffer.Event.KeyEvent.wVirtualKeyCode;
  75.             if (input == VK_LEFT) {
  76.                 lightVector[0] += -lightVector[2] / lightMagnitude;
  77.                 lightVector[2] += tempX / lightMagnitude;
  78.             }
  79.             else if(input == VK_RIGHT) {
  80.                 lightVector[0] += lightVector[2] / lightMagnitude;
  81.                 lightVector[2] += -tempX / lightMagnitude;
  82.             }
  83.             else if (input == VK_UP) {
  84.                 lightVector[1] += -lightVector[2] / lightMagnitude;
  85.                 lightVector[2] += tempY / lightMagnitude;
  86.             }
  87.             else if (input == VK_DOWN) {
  88.                 lightVector[1] += lightVector[2] / lightMagnitude;
  89.                 lightVector[2] += -tempY / lightMagnitude;
  90.             }
  91.             else if (input == 0x57) {// W key
  92.                 lightVector[0] *= 1.05f;
  93.                 lightVector[1] *= 1.05f;
  94.                 lightVector[2] *= 1.05f;
  95.             }
  96.             else if (input == 0x53) {// S key
  97.                 lightVector[0] *= 0.95f;
  98.                 lightVector[1] *= 0.95f;
  99.                 lightVector[2] *= 0.95f;
  100.             }
  101.             else if(input == VK_ESCAPE) {
  102.                 pressedKey = TRUE;
  103.             };
  104.             drawLitSphere(30.0, 1, 1, 10, 10, lightVector);
  105.         };
  106.     };
  107.     return 0;
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement