Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //link https://yadi.sk/d/zrApptoovqxNxQ
- #include <iostream>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <math.h>
- #include <unistd.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <time.h>
- #include <string>
- #include <cmath>
- #include <stdio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <fstream>
- #include <string>
- #include <cmath>
- #include <iostream>
- int window;
- double sphereRadius = 0.4;
- double cylinderRadius = 0.175;
- int resolution = 50;
- float depth = 8;
- float defX = 45, defY = 0;
- float downX, downY;
- bool leftButton = false, rightButton = false;
- void renderCylinder(float x1, float y1, float z1, float ax, float ay, float az, double len, float radius, GLUquadricObj* quadric)
- {
- glPushMatrix();
- glTranslatef(x1, y1, z1);
- glRotatef(ax, ay, az, 0.0f);
- gluCylinder(quadric, radius, radius, len, resolution, 1);
- glPopMatrix();
- }
- void mouseCallback(int button, int state, int x, int y)
- {
- downX = x; downY = y;
- leftButton = ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN));
- rightButton = ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN));
- }
- void motionCallback(int x, int y)
- {
- if (leftButton)
- {
- defX += (x - downX) / 4.0;
- defY += (downY - y) / 4.0;
- }
- if (rightButton)
- {
- depth += (downY - y) / 10.0;
- if (depth < 8)
- depth = 8;
- else if (depth > 50)
- depth = 50;
- }
- downX = x;
- downY = y;
- glutPostRedisplay();
- }
- void keyboardCallback(unsigned char ch, int x, int y) {
- switch (ch)
- {
- case 27:
- exit(0);
- case 114:
- defY = 0;
- defX = 0;
- case 82:
- defY = 0;
- defX = 0;
- break;
- }
- glutPostRedisplay();
- }
- void displayCallback(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, 1, 1, 100);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(-0.35, 0.3, 2, 0, 0, 0, 0, 1, 0);
- glTranslatef(0.0, 0.0, -depth);
- glRotatef(-defY, 1.0, 0.0, 0.0);
- glRotatef(defX, 0.0, 1.0, 0.0);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_COLOR_MATERIAL);
- GLUquadric* myQuad;
- myQuad = gluNewQuadric();
- std::ifstream file2("spheres.txt");
- if (!file2.fail())
- {
- double x, y, z;
- int nbElements = 0;
- char cColor;
- file2 >> nbElements;
- for (int i = 0; i < nbElements; i++)
- {
- file2 >> x;
- file2 >> y;
- file2 >> z;
- file2 >> sphereRadius;
- file2 >> cColor;
- glPushMatrix();
- if (cColor == 'R')
- glColor3f(1, 0, 0.0);
- else if (cColor == 'B')
- glColor3f(0.0, 0.0, 1);
- else if (cColor == 'G')
- glColor3f(0.0, 1, 0.0);
- else if (cColor == 'g')
- glColor3f(0.5, 0.5, 0.5);
- else if (cColor == 'o')
- glColor3f(1.0, 0.5, 0.0);
- glTranslatef(x, y, z);
- gluSphere(myQuad, sphereRadius, resolution, resolution);
- glPopMatrix();
- }
- file2.close();
- }
- std::ifstream file("cylinders.txt");
- if (!file.fail())
- {
- double x, y, z, ax, ay, az, len;
- int nbElements = 0;
- file >> nbElements;
- for (int i = 0; i < nbElements; i++)
- {
- file >> x;
- file >> y;
- file >> z;
- file >> ax;
- file >> ay;
- file >> az;
- file >> len;
- glColor3f(0.6, 0.6, 0.6);
- renderCylinder(x, y, z, ax, ay, az, len, cylinderRadius, myQuad);
- }
- file.close();
- }
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- window = glutCreateWindow("Молекула");
- glutSetWindow(window);
- int widthPos = (glutGet(GLUT_SCREEN_WIDTH)) / 2 - (glutGet(GLUT_WINDOW_WIDTH)) / 2;
- int heightPos = (glutGet(GLUT_SCREEN_HEIGHT)) / 2 - (glutGet(GLUT_WINDOW_HEIGHT)) / 2;
- glutPositionWindow(widthPos, heightPos);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glutDisplayFunc(displayCallback);
- glutMouseFunc(mouseCallback);
- glutMotionFunc(motionCallback);
- glutKeyboardFunc(keyboardCallback);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement