Advertisement
HigerSkill

OpenGL Lab 3

Apr 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.01 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  1
  4. //
  5. //  Created by Александр on 14.04.17.
  6. //  Copyright © 2017 Александр. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <math.h>
  11.  
  12. #include <OpenGL/gl.h>
  13. #include <OpenGL/glu.h>
  14. #include <GLUT/glut.h>
  15.  
  16.  
  17. using namespace std;
  18.  
  19. bool createMas = false;
  20. float **masF, **masFF;
  21. int masSize;
  22.  
  23. // Control camera
  24.  
  25. int i,j;
  26. //  Угол поворота камеры
  27. float angle = 0.0f;
  28. // Вектор направления
  29. float lx=0.0f,lz=-1.0f;
  30. // XZ позиция
  31. float x=0.0f, z=5.0f;
  32.  
  33. float deltaAngle = 0.0f;
  34. float deltaMove = 0;
  35. int xOrigin = -1;
  36.  
  37.  
  38. void computePos(float deltaMove) {
  39.     x += deltaMove * lx * 0.1f;
  40.     z += deltaMove * lz * 0.1f;
  41. }
  42.  
  43.  
  44. void processNormalKeys(unsigned char key, int xx, int yy) {
  45.     if (key == 107)
  46.         exit(0);
  47. }
  48.  
  49. void pressKey(int key, int xx, int yy) {
  50.     switch (key) {
  51.         case GLUT_KEY_UP : deltaMove = 0.5f; break;
  52.         case GLUT_KEY_DOWN : deltaMove = -0.5f; break;
  53.     }
  54. }
  55.  
  56. void releaseKey(int key, int x, int y) {
  57.     switch (key) {
  58.         case GLUT_KEY_UP :
  59.         case GLUT_KEY_DOWN : deltaMove = 0;break;
  60.     }
  61. }
  62.  
  63. void mouseMove(int x, int y) {
  64.     // Когда нажата левая кнопка мыши
  65.     if (xOrigin >= 0) {
  66.         // Обновление deltaAngle
  67.         deltaAngle = (x - xOrigin) * 0.001f;
  68.         // Обновление направления камеры
  69.         lx = sin(angle + deltaAngle);
  70.         lz = -cos(angle + deltaAngle);
  71.     }
  72. }
  73.  
  74. void mouseButton(int button, int state, int x, int y) {
  75.     // Работает, когда нажата левая кнопка мыши
  76.     if (button == GLUT_LEFT_BUTTON) {
  77.         if (state == GLUT_UP) {
  78.             angle += deltaAngle;
  79.             xOrigin = -1;
  80.         }
  81.         else  {
  82.             xOrigin = x;
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. void DrawLine(float x1, float y1, float z1,
  89.               float x2, float y2, float z2,
  90.               float r, float g, float b)
  91. {
  92.     glBegin(GL_LINES);
  93.         glColor3d(r, g, b);
  94.         glVertex3d(x1, y1, z1);
  95.         glVertex3d(x2, y2, z2);
  96.     glEnd();
  97. }
  98.  
  99. void DrawAxis(){
  100.    
  101.     glPushMatrix();
  102.     double dx = 2;
  103.    
  104.     //Оси координат
  105.     DrawLine(-10 * dx, 0, 0, 10 * dx, 0, 0, 1, 1, 1);
  106.     DrawLine(0, -10 * dx, 0, 0, 10 * dx, 0, 1, 1, 1);
  107.    
  108.     //Деления
  109.     for (int i = -10; i <= 10; i++){
  110.         DrawLine(i * dx, 0.1 * dx, 0, i * dx, -0.1 * dx, 0, 1, 1, 1);
  111.         DrawLine(0.1 * dx, i * dx, 0, -0.1 * dx, i * dx, 0, 1, 1, 1);
  112.     }
  113.     glPopMatrix();
  114. }
  115.  
  116. float f(float x){
  117.    
  118.     return (sin(2*x));
  119.    
  120. }
  121.  
  122. void DrawFunc(){
  123.    
  124.     float x, x1 = -10, x2 = 10;
  125.     int n = 100;
  126.     float dx = (x2 - x1)/n;
  127.    
  128.     glBegin(GL_LINE_STRIP);
  129.         glColor3f(1, 0, 0);
  130.    
  131.         for (x = x1; x <= x2; x += dx)
  132.             glVertex3f(x, f(x), 0);
  133.     glEnd();
  134. }
  135.  
  136. // Сгенерировать массив
  137. void GenerateMasByFunc(float **mas, float x1, float x2, float dx){
  138.    
  139.     for (int i = 0; i < masSize; i++){
  140.         mas[0][i] = x1 + dx * i;
  141.         mas[1][i] = f(x1 + dx * i);
  142.     }
  143. }
  144.  
  145. // Нарисовать функцию по массиву
  146. void DrawFuncMas(float **mas, float xScale, float yScale, float r, float g, float b){
  147.    
  148.     glBegin(GL_LINE_STRIP);
  149.         glColor3f(r, g, b);
  150.         for (int i = 0; i < masSize; i++)
  151.             glVertex3f(mas[0][i] * xScale, mas[1][i] * yScale, 0);
  152.     glEnd();
  153. }
  154.  
  155. // Производная функции
  156. void CalcDFuncMas(float **mas, float **dmas, float dx){
  157.    
  158.     for (int i = 0; i < masSize; i++){
  159.         dmas[0][i] = mas[0][i];
  160.        
  161.         if (i < masSize - 1)
  162.             dmas[1][i] = (mas[1][i+1] - mas[1][i]) / dx;
  163.         else
  164.             dmas[1][i] = (mas[1][i] - mas[1][i-1]) / dx;
  165.     }
  166. }
  167.  
  168. void DrawMax(float **mas, int n){
  169.         float resy = mas[1][0];
  170.         float resx = 0.0;
  171.         for (int i = 0; i < n; i++)
  172.             if (mas[1][i] > resy){
  173.                 resy = mas[1][i];
  174.                 resx = mas[0][i];
  175.             }
  176.    
  177.     glPointSize(5);
  178.    
  179.     glBegin(GL_POINTS);
  180.         glColor3f(1, 0, 0);
  181.         glVertex3f(resx, resy, 0);
  182.     glEnd();
  183.    
  184.     glPointSize(2);
  185. }
  186.  
  187. void DrawMin(float **mas, int n){
  188.     float resy = mas[1][0];
  189.     float resx = 0.0;
  190.     for (int i = 0; i < n; i++)
  191.         if (mas[1][i] < resy){
  192.             resy = mas[1][i];
  193.             resx = mas[0][i];
  194.         }
  195.    
  196.     glPointSize(5);
  197.    
  198.     glBegin(GL_POINTS);
  199.         glColor3f(1, 0, 0);
  200.         glVertex3f(resx, resy, 0);
  201.     glEnd();
  202.    
  203.     glPointSize(2);
  204. }
  205.  
  206. void DrawFunction(){
  207.    
  208.     float xScale = 1, yScale = 1, x1 = -10, x2 = 10,
  209.     dx = 0.01;
  210.     masSize = int((x2 - x1)/dx);
  211.    
  212.     if (!createMas){
  213.        
  214.         masF = new float *[2];
  215.         masF[0] = new float [masSize];
  216.         masF[1] = new float [masSize];
  217.        
  218.         GenerateMasByFunc(masF, x1, x2, dx);
  219.         createMas = true;
  220.        
  221.         masFF = new float *[2];
  222.         masFF[0] = new float [masSize];
  223.         masFF[1] = new float [masSize];
  224.        
  225.         CalcDFuncMas(masF, masFF, dx);
  226.     }
  227.    
  228.    //DrawFuncMas(masF, xScale, yScale, 1, 0, 0);
  229.    
  230.     //Точки максимума/минимума
  231.     DrawMax(masF, masSize);
  232.     DrawMin(masF, masSize);
  233.    
  234.     for (int i = 0; i < masSize; i++){
  235.         float eps = 0.1;
  236.        
  237.         //Точки экстремума
  238.         if (masFF[1][i] <= eps && masFF[1][i] >= -eps){
  239.             glPointSize(6);
  240.             glBegin(GL_POINTS);
  241.                 glColor3f(1, 0, 1);
  242.                 glVertex3f(masF[0][i], masF[1][i], 0);
  243.                 glEnd();
  244.             glPointSize(2);
  245.         }
  246.         //Ракскраска убывания/возрастания
  247.         if (masFF[1][i] < 0){
  248.             glBegin(GL_POINTS);
  249.                 glColor3f(0, 0, 1);
  250.                 glVertex3f(masF[0][i], masF[1][i], 0);
  251.             glEnd();
  252.         }
  253.         if (masFF[1][i] > 0){
  254.             glBegin(GL_POINTS);
  255.                 glColor3f(0.7f, 0.5f, 0.5f);
  256.                 glVertex3f(masF[0][i], masF[1][i], 0);
  257.             glEnd();
  258.         }
  259.        
  260.     }
  261.  
  262.      DrawFuncMas(masFF, xScale, yScale, 0, 1, 0);
  263.    
  264. }
  265.  
  266. void DeleteMas(){
  267.    
  268.     if (createMas){
  269.        
  270.         delete masF[0];
  271.         delete masF[1];
  272.         delete masF;
  273.     }
  274. }
  275.  
  276. void resize(int width,int height){
  277.    
  278.     glViewport(0,0,width,height);
  279.     glMatrixMode( GL_PROJECTION );
  280.     glLoadIdentity();
  281.    
  282.     gluPerspective(45.0, (GLfloat)width / height, 1.0, 10000.0);
  283.  
  284.     gluLookAt( 0,0,5, 0,0,0, 0,1,0 );
  285.     glFrustum ( -1 , 1 , -1 , 1 , 1.25 , 10.0 );
  286.     glMatrixMode( GL_MODELVIEW );
  287.    
  288.         glEnable(GL_DEPTH_TEST);
  289. }
  290.  
  291. void Draw(){
  292.    
  293.     float dx = 1;
  294.    
  295.     for (int i = -10; i <= 10; i++)
  296.         DrawLine(float(i), -10, 0, i * dx, 10, 0, 1, 1, 1);
  297. }
  298.  
  299. void renderScene(void) {
  300.    
  301.     if (deltaMove)
  302.         computePos(deltaMove);
  303.    
  304.  
  305.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  306.     // Сброс трансформаций
  307.     glLoadIdentity();
  308.     // Установка камеры
  309.     gluLookAt(  x, 1.0f, z,
  310.               x+lx, 1.0f,  z+lz,
  311.               0.0f, 1.0f,  0.0f);
  312.    
  313.     DrawAxis();
  314.     DrawFunction();
  315.    
  316.     glutSwapBuffers();
  317. }
  318.  
  319.  
  320.  
  321. int RunOpenGL(){
  322.    
  323.     glutInitWindowPosition(400, 0);
  324.     glutInitWindowSize(600, 600);
  325.    
  326.     glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  327.    
  328.     glutCreateWindow("Lab");
  329.    
  330.     glutIdleFunc(renderScene);
  331.     glutReshapeFunc(resize);
  332.     glutDisplayFunc(renderScene);
  333.    
  334.     glutIgnoreKeyRepeat(1);
  335.     glutKeyboardFunc(processNormalKeys);
  336.     glutSpecialFunc(pressKey);
  337.     glutSpecialUpFunc(releaseKey);
  338.     glutMouseFunc(mouseButton);
  339.     glutMotionFunc(mouseMove);
  340.    
  341.     glEnable(GL_DEPTH_TEST);
  342.     glutMainLoop();
  343.    
  344.     return  1;
  345. }
  346.  
  347. int main(int argc,char ** argv){
  348.    
  349.     glutInit(&argc,argv);
  350.     RunOpenGL();
  351.    
  352.     return 1;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement