Advertisement
Guest User

Simple 3D

a guest
Dec 14th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.00 KB | None | 0 0
  1. #include <tchar.h>
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. char buffer[480][640] = {0};
  9. double buffer_z[480][640] = {0};
  10. COORD Cursor;
  11. HANDLE output;
  12. struct Point;
  13. double lenght(Point, Point);
  14.  
  15. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  16. TCHAR szClassName[ ] = _T("GraphNeo");
  17. HWND hwnd_window;
  18. HBRUSH brush,brush1;
  19. HDC hwnd_dc,mem_dc;
  20. RECT rect;
  21. PAINTSTRUCT pnt;
  22. PDWORD thread;
  23.  
  24.  
  25. struct Point{
  26.     double x,y,z;
  27.     void Set(double xa, double ya, double za){
  28.         x = xa; y = ya; z = za;
  29.     }
  30.     Point(): x(0), y(0), z(0){}
  31.  
  32.     Point(double xa, double ya, double za){
  33.         x = xa; y = ya; z = za;
  34.     }
  35.  
  36.     friend double lenght(Point temp2, Point temp3 );
  37.  
  38.     bool operator != ( Point p ){
  39.         if (( this->x == p.x ) && ( this->y == p.y ) && ( this->z == p.z ))
  40.             return 0;
  41.         return 1;
  42.  
  43.     }
  44.  
  45.  
  46.     Point& operator = (Point temp1)
  47.     {
  48.         x = temp1.x; y = temp1.y; z = temp1.z;
  49.         return *this;
  50.     }
  51.  
  52.     Point operator - (Point temp1){
  53.         Point temp;
  54.         temp.x = this->x - temp1.x; temp.y = this->y - temp1.y; temp.z = this->z - temp1.z;
  55.         return temp;
  56.     }
  57.  
  58.     Point operator + (Point temp1){
  59.         Point temp;
  60.         temp.x = this->x + temp1.x; temp.y = this->y + temp1.y; temp.z = this->z + temp1.z;
  61.         return temp;
  62.     }
  63. } NullPoint;
  64.  
  65. class Triangle{
  66. private:
  67.     double lenght = 0; double GlobalLenght = 0;
  68.     Point center,new_p1,new_p2,new_p3;
  69.  
  70.     Point step( Point temp1, Point temp2, Point temp3 ){
  71.         Point temp = temp1;
  72.         lenght = round(sqrt(abs(pow(temp2.x - temp3.x, 2)+pow(temp2.y - temp3.y, 2)+pow(temp2.z - temp3.z, 2))));
  73.         lenght *= 2.5;
  74.         temp.x = temp.x / lenght; temp.y = temp.y / lenght; temp.z = temp.z / lenght;
  75.         return temp;
  76.     }
  77.  
  78.     void draw_line( Point temp2 ){
  79.         Point temp1,temp3;
  80.         temp1 = temp2 - new_p1;
  81.         temp1 = step(temp1, temp2,new_p1);
  82.         temp3 = new_p1;
  83.         for(int i = 0; i < lenght; i++ ){
  84.                 if ( temp3.z >= 0 ){
  85.                     if( (int)temp3.y < 480 && (int)temp3.x < 640 ){
  86.                         if ( buffer_z[(int)temp3.y][(int)temp3.x] > temp3.z ){
  87.                         SetPixel(mem_dc,(int)temp3.x,(int)temp3.y,RGB(t,0,100));
  88.                         buffer_z[(int)temp3.y][(int)temp3.x] = temp3.z;
  89.                         }
  90.                     }
  91.                 }
  92.                 temp3 = temp3 + temp1;
  93.         }
  94.     }
  95.  
  96. public:
  97.     char t = '#';
  98.     Point p1,p2,p3;
  99.     Triangle(){ t = '#'; }
  100.  
  101.     Triangle(char ch){
  102.         t = ch;
  103.     }
  104.  
  105.     Triangle(Point A, Point B, Point C){
  106.         Set(A,B,C);
  107.     }
  108.  
  109.     void Set( Point A, Point B, Point C ){
  110.  
  111.         p1 = A; p2 = B; p3 = C;
  112.         double l1,l2;
  113.         l1 = ::lenght(A,C);
  114.         l2 = ::lenght(B,C);
  115.         if ( l1 > l2 ){
  116.             p1= B; p2 = A; p3 = C;
  117.         }
  118.         else if ( ::lenght(A,B) > l2) {
  119.             p1 = C; p2 = A; p3 = B;
  120.         }
  121.     }
  122.  
  123.     void draw_to_buffer()
  124.     {
  125.  
  126.          Point temp1, temp2;
  127.          perspective();
  128.          temp1 = new_p3 - new_p2;
  129.          temp1 = step(temp1,new_p3,new_p2);
  130.          GlobalLenght = lenght;
  131.          temp2 = new_p2;
  132.          for(int ai = 0; ai <= GlobalLenght; ai++ )
  133.          {
  134.            draw_line(temp2);
  135.            temp2 = temp2 + temp1;
  136.          }
  137.     }
  138.  
  139.     void perspective(){
  140.         center.Set(640/2,480/2,0);
  141.         new_p1 = p1;
  142.         new_p2 = p2;
  143.         new_p3 = p3;
  144.         new_p3 = new_p3 - center;
  145.         new_p3.x = new_p3.x / new_p3.z;
  146.         new_p3.y = new_p3.y / new_p3.z;
  147.         new_p2 = new_p2 - center;
  148.         new_p2.x = new_p2.x / new_p2.z;
  149.         new_p2.y = new_p2.y / new_p2.z;
  150.         new_p1 = new_p1 - center;
  151.         new_p1.x = new_p1.x / new_p1.z;
  152.         new_p1.y = new_p1.y / new_p1.z;
  153.         new_p1 = new_p1 + center;
  154.         new_p2 = new_p2 + center;
  155.         new_p3 = new_p3 + center;
  156.     }
  157.  
  158. };
  159.  
  160.  
  161. class Rectangl{
  162. private:
  163.     Triangle t1,t2;
  164.     Point p[4];
  165.  
  166. public:
  167.     Rectangl(){}
  168.  
  169.     Rectangl(Point A, Point B, Point C, Point D){
  170.     p[0] = A; p[1] = B; p[2] = C; p[3] = D;
  171.     t1.Set(A,B,C); t2.Set(A,C,D);
  172.     }
  173.  
  174.     void Set(Point A, Point B, Point C, Point D){
  175.     p[0] = A; p[1] = B; p[2] = C; p[3] = D;
  176.  
  177.     t1.Set(A,B,C); t2.Set(A,C,D);
  178.     }
  179.  
  180.     void SetColor(char ch){
  181.         t1.t = ch; t2.t = ch;
  182.     }
  183.  
  184.     void Draw(){
  185.         t1.draw_to_buffer();
  186.         t2.draw_to_buffer();
  187.     }
  188. };
  189.  
  190. class Box{
  191. public:
  192.     Rectangl r[6];
  193.     Box(){}
  194.  
  195.     Box(double x, double y, double z, Point position ){
  196.         Point A(0,y,0),B(0,y,z),C(x,y,z),D(x,y,0),A1(0,0,0),B1(0,0,z),C1(x,0,z),D1(x,0,0);
  197.         A = A + position; B = B + position; C = C + position; D = D + position;
  198.         A1 = A1 + position; B1 = B1 + position; C1 = C1 + position; D1 = D1 + position;
  199.         r[0].Set(A,A1,D,D1);
  200.         r[1].Set(A,A1,B,B1);
  201.         r[2].Set(D,D1,C,C1);
  202.         r[3].Set(B,B1,C,C1);
  203.         r[4].Set(A,B,C,D);
  204.         r[5].Set(A1,B1,C1,D1);
  205.     }
  206.  
  207.     void Set(double x, double y, double z, Point position ){
  208.         Point A(0,y,0),B(0,y,z),C(x,y,z),D(x,y,0),A1(0,0,0),B1(0,0,z),C1(x,0,z),D1(x,0,0);
  209.         A = A + position; B = B + position; C = C + position; D = D + position;
  210.         A1 = A1 + position; B1 = B1 + position; C1 = C1 + position; D1 = D1 + position;
  211.         r[0].Set(A1,A,D,D1); r[0].SetColor('1');
  212.         r[1].Set(A1,A,B,B1); r[1].SetColor('2');
  213.         r[2].Set(D1,D,C,C1); r[2].SetColor('5');
  214.         r[3].Set(B1,B,C,C1); r[3].SetColor('4');
  215.         r[4].Set(A,B,C,D);   r[4].SetColor('5');
  216.         r[5].Set(A1,B1,C1,D1); r[5].SetColor('6');
  217.     }
  218.  
  219.     void SetColor(char ch){
  220.         for(int i = 0; i < 6; i++ )
  221.             r[i].SetColor(ch);
  222.     }
  223.  
  224.     void Draw(){
  225.         for(int i = 0; i < 6; i++ )
  226.             r[i].Draw();
  227.     }
  228. };
  229.  
  230. double lenght(Point temp2, Point temp3 ){
  231.     return round(sqrt(abs(pow(temp2.x - temp3.x, 2)+pow(temp2.y - temp3.y, 2)+pow(temp2.z - temp3.z, 2))));
  232. }
  233.  
  234. void draw_display(){
  235.             GetWindowRect(hwnd_window,&rect);
  236.             GetClientRect(hwnd_window,&rect);
  237.             InvalidateRect(hwnd_window,&rect,0);
  238.  for(int y = 0; y < 480; y++ )
  239.      for( int x = 0; x < 640; x++ )
  240.         buffer_z[y][x] = 999999;
  241. }
  242.  
  243. void GraphInit(){
  244.    Cursor.X = 0; Cursor.Y = 0;
  245.    output = GetStdHandle(STD_OUTPUT_HANDLE);
  246.    draw_display();
  247. }
  248.  
  249.  
  250.  
  251.  
  252. int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
  253. {
  254.     MSG messages;
  255.     WNDCLASSEX wincl;
  256.     wincl.hInstance = hThisInstance;
  257.     wincl.lpszClassName = szClassName;
  258.     wincl.lpfnWndProc = WindowProcedure;
  259.     wincl.style = CS_DBLCLKS;
  260.     wincl.cbSize = sizeof (WNDCLASSEX);
  261.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  262.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  263.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  264.     wincl.lpszMenuName = NULL;
  265.     wincl.cbClsExtra = 0;
  266.     wincl.cbWndExtra = 0;
  267.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  268.     if (!RegisterClassEx (&wincl))
  269.         return 0;
  270.     hwnd_window = CreateWindowEx(0,szClassName,_T("Test"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,HWND_DESKTOP,NULL,hThisInstance,NULL);
  271.     ShowWindow (hwnd_window, nCmdShow);
  272.     while (GetMessage (&messages, NULL, 0, 0))
  273.     {
  274.         TranslateMessage(&messages);
  275.         DispatchMessage(&messages);
  276.     }
  277.     return messages.wParam;
  278. }
  279. DWORD WINAPI ThreadProc(CONST LPVOID lpParam);
  280.  
  281. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  282. {
  283.     switch (message)
  284.     {
  285.         case WM_DESTROY:
  286.             PostQuitMessage (0);
  287.             break;
  288.         case WM_CREATE:
  289.             hwnd_dc = GetDC(hwnd_window);
  290.             mem_dc = CreateCompatibleDC(hwnd_dc);
  291.             SelectObject(mem_dc,CreateCompatibleBitmap(hwnd_dc,640,480));
  292.             brush = CreateSolidBrush(0x0);
  293.             brush1 = CreateSolidBrush(0x100);
  294.             SelectObject(mem_dc,brush);
  295.             PatBlt(mem_dc,0,0,640,480,PATCOPY);
  296.             ReleaseDC(hwnd,hwnd_dc);
  297.             CreateThread(0,0,&ThreadProc,0,0,thread);
  298.             SetThreadPriority(thread,HIGH_PRIORITY_CLASS);
  299.             break;
  300.  
  301.         case WM_PAINT:
  302.             hwnd_dc = BeginPaint(hwnd_window,&pnt);
  303.             BitBlt(hwnd_dc,0,0,640,480,mem_dc,0,0,SRCCOPY);
  304.             EndPaint(hwnd,&pnt);
  305.             SelectObject(mem_dc,brush);
  306.             PatBlt(mem_dc,0,0,640,480,PATCOPY);
  307.             break;
  308.  
  309.         case WM_LBUTTONDOWN:
  310.             break;
  311.  
  312.         default:
  313.             return DefWindowProc (hwnd, message, wParam, lParam);
  314.     }
  315.     return 0;
  316. }
  317.  
  318. DWORD WINAPI ThreadProc(CONST LPVOID lpParam){
  319.     GraphInit();
  320.     Box box1;
  321.     Point pos1(200,10,1);
  322.     int x = 0;
  323.     double turn = 2;
  324.     while(true){
  325.     pos1.Set((double)x,10.0,1.0);
  326.     box1.Set(100,15*10,1.4,pos1);
  327.     box1.Draw();
  328.     draw_display();
  329.     x++;
  330.     }
  331. return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement