Advertisement
melnikovmaxim

BKV_molecule

Dec 16th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. //link https://yadi.sk/d/zrApptoovqxNxQ
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <math.h>
  6. #include <unistd.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9. #include <GL/glut.h>
  10. #include <time.h>
  11. #include <string>
  12. #include <cmath>
  13. #include <stdio.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <fstream>
  18. #include <string>
  19. #include <cmath>
  20. #include <iostream>
  21. int window;
  22. double sphereRadius = 0.4;
  23. double cylinderRadius = 0.175;
  24. int resolution = 50;
  25. float depth = 8;
  26. float defX = 45, defY = 0;
  27. float downX, downY;
  28. bool leftButton = false, rightButton = false;
  29.  
  30. void renderCylinder(float x1, float y1, float z1, float ax, float ay, float az, double len, float radius, GLUquadricObj* quadric)
  31. {
  32.     glPushMatrix();
  33.     glTranslatef(x1, y1, z1);
  34.     glRotatef(ax, ay, az, 0.0f);
  35.     gluCylinder(quadric, radius, radius, len, resolution, 1);
  36.     glPopMatrix();
  37. }
  38. void mouseCallback(int button, int state, int x, int y)
  39. {
  40.     downX = x; downY = y;
  41.     leftButton = ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN));
  42.     rightButton = ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN));
  43. }
  44. void motionCallback(int x, int y)
  45. {
  46.     if (leftButton)
  47.     {
  48.         defX += (x - downX) / 4.0;
  49.         defY += (downY - y) / 4.0;
  50.     }
  51.  
  52.     if (rightButton)
  53.     {
  54.         depth += (downY - y) / 10.0;
  55.         if (depth < 8)
  56.             depth = 8;
  57.         else if (depth > 50)
  58.             depth = 50;
  59.     }
  60.     downX = x;
  61.     downY = y;
  62.     glutPostRedisplay();
  63. }
  64.  
  65. void keyboardCallback(unsigned char ch, int x, int y) {
  66.     switch (ch)
  67.     {
  68.     case 27:
  69.         exit(0);
  70.     case 114:
  71.         defY = 0;
  72.         defX = 0;
  73.     case 82:
  74.         defY = 0;
  75.         defX = 0;
  76.         break;
  77.     }
  78.     glutPostRedisplay();
  79. }
  80. void displayCallback(void)
  81. {
  82.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  83.     glMatrixMode(GL_PROJECTION);
  84.     glLoadIdentity();
  85.     gluPerspective(60, 1, 1, 100);
  86.     glMatrixMode(GL_MODELVIEW);
  87.     glLoadIdentity();
  88.     gluLookAt(-0.35, 0.3, 2, 0, 0, 0, 0, 1, 0);
  89.     glTranslatef(0.0, 0.0, -depth);
  90.     glRotatef(-defY, 1.0, 0.0, 0.0);
  91.     glRotatef(defX, 0.0, 1.0, 0.0);
  92.     glEnable(GL_LIGHTING);
  93.     glEnable(GL_LIGHT0);
  94.     glEnable(GL_COLOR_MATERIAL);
  95.     GLUquadric* myQuad;
  96.     myQuad = gluNewQuadric();
  97.     std::ifstream file2("spheres.txt");
  98.  
  99.     if (!file2.fail())
  100.     {
  101.         double x, y, z;
  102.         int nbElements = 0;
  103.         char cColor;
  104.         file2 >> nbElements;
  105.         for (int i = 0; i < nbElements; i++)
  106.         {
  107.             file2 >> x;
  108.             file2 >> y;
  109.             file2 >> z;
  110.             file2 >> sphereRadius;
  111.             file2 >> cColor;
  112.             glPushMatrix();
  113.             if (cColor == 'R')
  114.                 glColor3f(1, 0, 0.0);
  115.             else if (cColor == 'B')
  116.                 glColor3f(0.0, 0.0, 1);
  117.             else if (cColor == 'G')
  118.                 glColor3f(0.0, 1, 0.0);
  119.             else if (cColor == 'g')
  120.                 glColor3f(0.5, 0.5, 0.5);
  121.             else if (cColor == 'o')
  122.                 glColor3f(1.0, 0.5, 0.0);
  123.             glTranslatef(x, y, z);
  124.             gluSphere(myQuad, sphereRadius, resolution, resolution);
  125.             glPopMatrix();
  126.         }
  127.         file2.close();
  128.     }
  129.     std::ifstream file("cylinders.txt");
  130.     if (!file.fail())
  131.     {
  132.         double x, y, z, ax, ay, az, len;
  133.         int nbElements = 0;
  134.         file >> nbElements;
  135.         for (int i = 0; i < nbElements; i++)
  136.         {
  137.             file >> x;
  138.             file >> y;
  139.             file >> z;
  140.             file >> ax;
  141.             file >> ay;
  142.             file >> az;
  143.             file >> len;
  144.             glColor3f(0.6, 0.6, 0.6);
  145.             renderCylinder(x, y, z, ax, ay, az, len, cylinderRadius, myQuad);
  146.         }
  147.         file.close();
  148.     }
  149.     glFlush();
  150. }
  151.  
  152. int main(int argc, char* argv[])
  153. {
  154.     glutInit(&argc, argv);
  155.     glutInitDisplayMode(GLUT_DEPTH);
  156.     glutInitWindowSize(800, 600);
  157.     window = glutCreateWindow("Молекула");
  158.     glutSetWindow(window);
  159.     int widthPos = (glutGet(GLUT_SCREEN_WIDTH)) / 2 - (glutGet(GLUT_WINDOW_WIDTH)) / 2;
  160.     int heightPos = (glutGet(GLUT_SCREEN_HEIGHT)) / 2 - (glutGet(GLUT_WINDOW_HEIGHT)) / 2;
  161.     glutPositionWindow(widthPos, heightPos);
  162.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  163.     glEnable(GL_DEPTH_TEST);
  164.     glClearColor(0.0, 0.0, 0.0, 0.0);
  165.     glutDisplayFunc(displayCallback);
  166.     glutMouseFunc(mouseCallback);
  167.     glutMotionFunc(motionCallback);
  168.     glutKeyboardFunc(keyboardCallback);
  169.     glutMainLoop();
  170.  
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement