Advertisement
Guest User

jajko

a guest
Dec 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.28 KB | None | 0 0
  1. // Jajko2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <gl/gl.h>
  7. #include <gl/glut.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <cstdlib>
  11. #include <time.h>
  12. #include <vector>
  13.  
  14. enum DisplayMode {
  15.     Vertex = 1,
  16.     Net = 2,
  17.     Solid = 3
  18. };
  19.  
  20. enum MouseStatus
  21. {
  22.     NoAction = 0,
  23.     LeftButtonPressed = 1,
  24.     RightButtonPressed = 2,
  25.     MiddleButtonPressed = 3
  26. };
  27.  
  28. enum LightStatus
  29. {
  30.     Light0 = 0,
  31.     Light1 = 1
  32. };
  33.  
  34. static GLfloat y = 1.0, x = 1.0;
  35. using namespace std;
  36. typedef float point3[3];
  37. typedef float point2[2];
  38. typedef float color3[3];
  39. const float pi = acos(-1.0);
  40. int N = 20;
  41. static GLfloat viewer[] = { 0.0, 0.0, 10.0 };
  42. static GLfloat light0[] = { 0.0, 0.0, 5.0, 1.0 };
  43. static GLfloat light1[] = { 5.0, 0.0, 10.0, 1.0 };
  44.  
  45. point3** model;
  46. color3** colors;
  47. point3** lights;
  48. point2** textures;
  49.  
  50. static GLfloat theta_zoom = 20.0;
  51. static GLfloat theta = 0.0;   // kąt obrotu obiektu
  52. static GLfloat theta_y = 0.0;
  53. static GLfloat theta_x_light0 = 0.0;
  54. static GLfloat theta_y_light0 = 0.0;
  55. static GLfloat theta_x_light1 = 0.0;
  56. static GLfloat theta_y_light1 = 0.0;
  57. static GLfloat pix2angle;     // przelicznik pikseli na stopnie
  58. static GLfloat pix2angleVert;
  59.  
  60. static MouseStatus status = NoAction;
  61. static LightStatus lightNo = Light0;
  62.  
  63. static int x_pos_old = 0;
  64. static int delta_x = 0;
  65.  
  66. static int y_pos_old = 0;
  67. static int delta_y = 0;
  68.  
  69. static int zoom_old = 0;
  70. static int delta_zoom = 0;
  71.  
  72. static int light_x_pos_old0 = 0;
  73. static int delta_x_light0 = 0;
  74. static int light_y_pos_old0 = 0;
  75. static int delta_y_light0 = 0;
  76. static int light_x_pos_old1 = 0;
  77. static int delta_x_light1 = 0;
  78. static int light_y_pos_old1 = 0;
  79. static int delta_y_light1 = 0;
  80.  
  81. void PrepareModel();
  82. void DrawEgg();
  83.  
  84. DisplayMode displayMode = Solid;
  85.  
  86. GLbyte *LoadTGAImage(const char *FileName, GLint *ImWidth, GLint *ImHeight, GLint *ImComponents, GLenum *ImFormat)
  87. {
  88.     /*************************************************************************************/
  89.     // Struktura dla nagłówka pliku  TGA
  90.  
  91. #pragma pack(1)            
  92.     typedef struct
  93.     {
  94.         GLbyte    idlength;
  95.         GLbyte    colormaptype;
  96.         GLbyte    datatypecode;
  97.         unsigned short    colormapstart;
  98.         unsigned short    colormaplength;
  99.         unsigned char     colormapdepth;
  100.         unsigned short    x_orgin;
  101.         unsigned short    y_orgin;
  102.         unsigned short    width;
  103.         unsigned short    height;
  104.         GLbyte    bitsperpixel;
  105.         GLbyte    descriptor;
  106.     }TGAHEADER;
  107. #pragma pack(8)
  108.  
  109.     FILE *pFile;
  110.     TGAHEADER tgaHeader;
  111.     unsigned long lImageSize;
  112.     short sDepth;
  113.     GLbyte    *pbitsperpixel = NULL;
  114.  
  115.     /*************************************************************************************/
  116.     // Wartości domyślne zwracane w przypadku błędu
  117.  
  118.     *ImWidth = 0;
  119.     *ImHeight = 0;
  120.     *ImFormat = GL_BGR_EXT;
  121.     *ImComponents = GL_RGB8;
  122.  
  123.     fopen_s(&pFile, FileName, "rb");
  124.     if (pFile == NULL)
  125.         return NULL;
  126.     /*************************************************************************************/
  127.     // Przeczytanie nagłówka pliku
  128.  
  129.     fread(&tgaHeader, sizeof(TGAHEADER), 1, pFile);
  130.  
  131.     /*************************************************************************************/
  132.     // Odczytanie szerokości, wysokości i głębi obrazu
  133.  
  134.     *ImWidth = tgaHeader.width;
  135.     *ImHeight = tgaHeader.height;
  136.     sDepth = tgaHeader.bitsperpixel / 8;
  137.  
  138.     /*************************************************************************************/
  139.     // Sprawdzenie, czy głębia spełnia założone warunki (8, 24, lub 32 bity)
  140.  
  141.     if (tgaHeader.bitsperpixel != 8 && tgaHeader.bitsperpixel != 24 && tgaHeader.bitsperpixel != 32)
  142.         return NULL;
  143.  
  144.     /*************************************************************************************/
  145.     // Obliczenie rozmiaru bufora w pamięci
  146.  
  147.     lImageSize = tgaHeader.width * tgaHeader.height * sDepth;
  148.  
  149.     /*************************************************************************************/
  150.     // Alokacja pamięci dla danych obrazu
  151.  
  152.     pbitsperpixel = (GLbyte*)malloc(lImageSize * sizeof(GLbyte));
  153.  
  154.     if (pbitsperpixel == NULL)
  155.         return NULL;
  156.  
  157.     if (fread(pbitsperpixel, lImageSize, 1, pFile) != 1)
  158.     {
  159.         free(pbitsperpixel);
  160.         return NULL;
  161.     }
  162.  
  163.     /*************************************************************************************/
  164.     // Ustawienie formatu OpenGL
  165.  
  166.     switch (sDepth)
  167.     {
  168.     case 3:
  169.         *ImFormat = GL_BGR_EXT;
  170.         *ImComponents = GL_RGB8;
  171.         break;
  172.     case 4:
  173.         *ImFormat = GL_BGRA_EXT;
  174.         *ImComponents = GL_RGBA8;
  175.         break;
  176.     case 1:
  177.         *ImFormat = GL_LUMINANCE;
  178.         *ImComponents = GL_LUMINANCE8;
  179.         break;
  180.     };
  181.  
  182.     fclose(pFile);
  183.  
  184.     return pbitsperpixel;
  185. }
  186.  
  187. auto FunX = [](float u, float v)
  188. {
  189.     return (-90 * pow(u, 5) + 225 * pow(u, 4) - 270 * pow(u, 3) + 180 * pow(u, 2) - 45 * u) * cos(pi * v);
  190. };
  191. auto FunY = [](float u, float v)
  192. {
  193.     return 160 * pow(u, 4) - 320 * pow(u, 3) + 160 * pow(u, 2);
  194. };
  195. auto FunZ = [](float u, float v)
  196. {
  197.     return (-90 * pow(u, 5) + 225 * pow(u, 4) - 270 * pow(u, 3) + 180 * pow(u, 2) - 45 * u) * sin(pi * v);
  198. };
  199. float SpectX(int rotX, int rotY)
  200. {
  201.     return theta_zoom * cos(rotX) * cos(rotY);
  202. };
  203. float SpectY(int rotY)
  204. {
  205.     return theta_zoom * sin(rotY);
  206. };
  207. float SpectZ(int rotX, int rotY)
  208. {
  209.     return theta_zoom * sin(rotX) * cos(rotY);
  210. };
  211. GLfloat* GetNormalVector(float u, float v)
  212. {
  213.     float lxu = (-450 * pow(u, 4) + 900 * pow(u, 3) - 810 * pow(u, 2) + 360 * u - 45) * cos(pi * v);
  214.     float lxv = pi * (90 * pow(u, 5) - 225 * pow(u, 4) + 270 * pow(u, 3) - 180 * pow(u, 2) + 45 * u) * sin(pi * v);
  215.     float lyu = 640 * pow(u, 3) - 960 * pow(u, 2) + 320 * u;
  216.     float lyv = 0;
  217.     float lzu = (-450 * pow(u, 4) + 900 * pow(u, 3) - 810 * pow(u, 2) + 360 * u - 45) * sin(pi * v);
  218.     float lzv = -pi * (90 * pow(u, 5) - 225 * pow(u, 4) + 270 * pow(u, 3) - 180 * pow(u, 2) + 45 * u) * cos(pi * v);
  219.  
  220.  
  221.  
  222.     GLfloat* res = new GLfloat[2];
  223.     res[0] = lyu * lzv - lzu * lyv;
  224.     res[1] = lzu * lxv - lxu * lzv;
  225.     res[2] = lxu * lyv - lyu * lxv;
  226.  
  227.     float vectorLength = sqrtf(powf(res[0], 2) + powf(res[1], 2) + powf(res[2], 2));
  228.  
  229.     for (auto i = 0; i < 3; i++)
  230.         res[i] = res[i] / vectorLength;
  231.  
  232.     return res;
  233. };
  234.  
  235.  
  236. void Axes(void)
  237. {
  238.     point3  x_min = { -5.0, 0.0, 0.0 };
  239.     point3  x_max = { 5.0, 0.0, 0.0 };
  240.  
  241.     point3  y_min = { 0.0, -5.0, 0.0 };
  242.     point3  y_max = { 0.0, 5.0, 0.0 };
  243.  
  244.     point3  z_min = { 0.0, 0.0, -5.0 };
  245.     point3  z_max = { 0.0, 0.0, 5.0 };
  246.  
  247.     glColor3f(1.0f, 0.0f, 0.0f);  // kolor rysowania osi - czerwony
  248.     glBegin(GL_LINES); // rysowanie osi x
  249.     glVertex3fv(x_min);
  250.     glVertex3fv(x_max);
  251.     glEnd();
  252.  
  253.     glColor3f(0.0f, 1.0f, 0.0f);  // kolor rysowania - zielony
  254.     glBegin(GL_LINES);  // rysowanie osi y
  255.     glVertex3fv(y_min);
  256.     glVertex3fv(y_max);
  257.     glEnd();
  258.  
  259.     glColor3f(0.0f, 0.0f, 1.0f);  // kolor rysowania - niebieski
  260.     glBegin(GL_LINES); // rysowanie osi z
  261.     glVertex3fv(z_min);
  262.     glVertex3fv(z_max);
  263.     glEnd();
  264. }
  265.  
  266. void RenderScene(void)
  267. {
  268.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  269.     // Czyszczenie okna aktualnym kolorem czyszcz�cym
  270.  
  271.     glLoadIdentity();
  272.     // Czyszczenie macierzy bie��cej
  273.  
  274.     Axes();
  275.     glColor3f(1.0f, 1.0f, 1.0f);
  276.     if (status == LeftButtonPressed)
  277.     {
  278.         if (delta_x != 0)
  279.             theta += delta_x * pix2angle * 0.01;
  280.         if (delta_y != 0)
  281.             theta_y += delta_y * pix2angleVert * 0.01;
  282.     }                                  // do różnicy położeń kursora myszy
  283.     if (status == RightButtonPressed)
  284.     {
  285.         theta_zoom += delta_zoom * 0.1;
  286.     }
  287.     if (status == MiddleButtonPressed)
  288.     {
  289.         if (lightNo == Light0)
  290.         {
  291.             if (delta_x_light0 != 0)
  292.                 theta_x_light0 += delta_x_light0 * pix2angle * 0.01;
  293.             if (delta_y_light0 != 0)
  294.                 theta_y_light0 += delta_y_light0 * pix2angleVert * 0.01;
  295.         }
  296.         else if (lightNo == Light1)
  297.         {
  298.             if (delta_x_light1 != 0)
  299.                 theta_x_light1 += delta_x_light1 * pix2angle * 0.01;
  300.             if (delta_y_light1 != 0)
  301.                 theta_y_light1 += delta_y_light1 * pix2angleVert * 0.01;
  302.         }
  303.     }
  304.  
  305.     if (theta_y > 2 * pi) theta_y -= 2 * pi;
  306.     else if (theta_y <= -2 * pi) theta_y += 2 * pi;
  307.  
  308.     if (theta_y < pi / 2 || theta_y > 2 * pi - pi / 2) {
  309.         y = -1.0;
  310.     }
  311.     else y = 1.0;
  312.  
  313.     if (theta_zoom < 10) theta_zoom = 10;
  314.     if (theta_zoom > 30) theta_zoom = 30;
  315.  
  316.     //viewer[0] = SpectX(theta, theta_y);
  317.     viewer[0] = theta_zoom * cos(theta) * cos(theta_y);
  318.     //viewer[1] = SpectY(theta_y);
  319.     viewer[1] = theta_zoom * sin(theta_y);
  320.     //viewer[2] = SpectZ(theta, theta_y);
  321.     viewer[2] = theta_zoom * sin(theta) * cos(theta_y);
  322.  
  323.  
  324.     if (lightNo == Light0) {
  325.         light0[0] = theta_zoom * cos(theta_x_light0) * cos(theta_y_light0);
  326.         light0[1] = theta_zoom * sin(theta_y_light0);
  327.         light0[2] = theta_zoom * sin(theta_x_light0) * cos(theta_y_light0);
  328.         glLightfv(GL_LIGHT0, GL_POSITION, light0);
  329.     }
  330.     else if (lightNo == Light1)
  331.     {
  332.         light1[0] = theta_zoom * cos(theta_x_light1) * cos(theta_y_light1);
  333.         light1[1] = theta_zoom * sin(theta_y_light1);
  334.         light1[2] = theta_zoom * sin(theta_x_light1) * cos(theta_y_light1);
  335.         glLightfv(GL_LIGHT1, GL_POSITION, light1);
  336.     }
  337.  
  338.     gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, y, 0.0);
  339.     // Zdefiniowanie położenia obserwatora
  340.  
  341.     // Narysowanie osi przy pomocy funkcji zdefiniowanej wy�ej
  342.  
  343.     DrawEgg();
  344.     glFlush();
  345.     // Przekazanie polece� rysuj�cych do wykonania
  346.  
  347.     glutSwapBuffers();
  348.     //
  349. }
  350.  
  351. void ChangeSize(GLsizei horizontal, GLsizei vertical)
  352. {
  353.     pix2angle = 360.0 / static_cast<float>(horizontal);  // przeliczenie pikseli na stopnie
  354.     pix2angleVert = 360.0 / static_cast<float>(vertical);
  355.     glMatrixMode(GL_PROJECTION);
  356.     // Przełączenie macierzy bieżącej na macierz projekcji
  357.  
  358.     glLoadIdentity();
  359.     // Czyszcznie macierzy bieżącej
  360.  
  361.     gluPerspective(70, 1.0, 1.0, 30.0);
  362.     // Ustawienie parametrów dla rzutu perspektywicznego
  363.  
  364.  
  365.     if (horizontal <= vertical)
  366.         glViewport(0, (vertical - horizontal) / 2, horizontal, horizontal);
  367.  
  368.     else
  369.         glViewport((horizontal - vertical) / 2, 0, vertical, vertical);
  370.     // Ustawienie wielkości okna okna widoku (viewport) w zależności
  371.     // relacji pomiędzy wysokością i szerokością okna
  372.  
  373.     glMatrixMode(GL_MODELVIEW);
  374.     // Przełączenie macierzy bieżącej na macierz widoku modelu  
  375.  
  376.     glLoadIdentity();
  377.     // Czyszczenie macierzy bieżącej
  378.  
  379. }
  380.  
  381. void PrepareModel() {
  382.     model = new point3*[N];
  383.     lights = new point3*[N];
  384.     for (auto i = 0; i < N; i++) {
  385.         model[i] = new point3[N];
  386.         lights[i] = new point3[N];
  387.     }
  388.  
  389.     for (auto i = 0; i < N; i++) {
  390.         for (auto j = 0; j < N; j++) {
  391.             auto vect = GetNormalVector(float(i) / float(N - 1), float(j) / float(N - 1));
  392.             if (i < N / 2 && j < N / 2)
  393.             {
  394.                 lights[i][j][0] = vect[0];
  395.                 lights[i][j][1] = vect[1];
  396.                 lights[i][j][2] = vect[2];
  397.             }
  398.             else if (j < N / 2)
  399.             {
  400.                 lights[i][j][0] = -vect[0];
  401.                 lights[i][j][1] = -vect[1];
  402.                 lights[i][j][2] = -vect[2];
  403.             }
  404.             else if (i < N / 2)
  405.             {
  406.                 lights[i][j][0] = vect[0];
  407.                 lights[i][j][1] = vect[1];
  408.                 lights[i][j][2] = vect[2];
  409.             }
  410.             else // i >= N/2 && j >= N/2
  411.             {
  412.                 lights[i][j][0] = -vect[0];
  413.                 lights[i][j][1] = -vect[1];
  414.                 lights[i][j][2] = -vect[2];
  415.             }
  416.  
  417.             model[i][j][0] = FunX(float(i) / float(N - 1), float(j) / float(N - 1));
  418.             model[i][j][1] = FunY(float(i) / float(N - 1), float(j) / float(N - 1)) - 5;
  419.             model[i][j][2] = FunZ(float(i) / float(N - 1), float(j) / float(N - 1));
  420.         }
  421.     }
  422.  
  423.     colors = new color3*[N];
  424.     textures = new point2*[N];
  425.  
  426.     for (auto i = 0; i < N; i++) {
  427.         colors[i] = new color3[N];
  428.         textures[i] = new point2[N];
  429.         for (auto j = 0; j < N; j++) {
  430.             colors[i][j][0] = static_cast<float>(rand()) / RAND_MAX;
  431.             colors[i][j][1] = static_cast<float>(rand()) / RAND_MAX;
  432.             colors[i][j][2] = static_cast<float>(rand()) / RAND_MAX;
  433.  
  434.            
  435.         }
  436.     }
  437.  
  438.     for(int i = 0; i < N / 2; i++)
  439.     {
  440.         for(int j = 0; j < N; j++)
  441.         {
  442.             textures[i][j][0] = static_cast<float>(j) / static_cast<float>(N - 1);
  443.             textures[i][j][1] = static_cast<float>(i) / static_cast<float>(N - 1);
  444.         }
  445.     }
  446.     for (int i = N / 2; i < N; i++)
  447.     {
  448.         for (int j = 0; j < N; j++)
  449.         {
  450.             textures[i][j][0] = 1-static_cast<float>(j) / static_cast<float>(N - 1);
  451.             textures[i][j][1] = 1-static_cast<float>(i) / static_cast<float>(N - 1);
  452.         }
  453.     }
  454. }
  455.  
  456. void DrawEgg() {
  457.     switch (displayMode) {
  458.     case Vertex:
  459.         glBegin(GL_POINTS);
  460.         for (auto i = 0; i < N; i++) {
  461.             for (auto j = 0; j < N; j++) {
  462.                 glVertex3fv(model[i][j]);
  463.             }
  464.         }
  465.         glEnd();
  466.         break;
  467.  
  468.     case Net:
  469.         glBegin(GL_LINES);
  470.         for (auto i = 0; i < N - 1; i++) {
  471.             for (auto j = 0; j < N - 1; j++) {
  472.                 auto x = i, y = j;
  473.                 glVertex3fv(model[i][j]);
  474.                 glVertex3fv(model[i + 1][j]);
  475.                 glVertex3fv(model[i][j + 1]);
  476.                 //
  477.                 glVertex3fv(model[i][j]);
  478.                 glVertex3fv(model[i][j + 1]);
  479.                 glVertex3fv(model[i > 0 ? i - 1 : N - 1][j + 1]);
  480.             }
  481.         }
  482.         glEnd();
  483.         break;
  484.     case Solid:
  485.         glBegin(GL_TRIANGLES);
  486.         for (auto i = 0; i < N / 2; i++) {
  487.             for (auto j = 0; j < N - 1; j++) {
  488.                 glNormal3fv(lights[i][j]);
  489.                 glTexCoord2fv(textures[i][j]);
  490.                 glVertex3fv(model[i][j]);
  491.  
  492.                 glNormal3fv(lights[i + 1][j + 1]);
  493.                 glTexCoord2fv(textures[i + 1][j + 1]);
  494.                 glVertex3fv(model[i + 1][j + 1]);
  495.  
  496.                 glNormal3fv(lights[i][j + 1]);
  497.                 glTexCoord2fv(textures[i][j + 1]);
  498.                 glVertex3fv(model[i][j + 1]);
  499.  
  500.                 glNormal3fv(lights[i][j]);
  501.                 glTexCoord2fv(textures[i][j]);
  502.                 glVertex3fv(model[i][j]);
  503.  
  504.                 glNormal3fv(lights[i + 1][j]);
  505.                 glTexCoord2fv(textures[i + 1][j]);
  506.                 glVertex3fv(model[i + 1][j]);
  507.  
  508.                 glNormal3fv(lights[i + 1][j + 1]);
  509.                 glTexCoord2fv(textures[i + 1][j + 1]);
  510.                 glVertex3fv(model[i + 1][j + 1]);
  511.  
  512.             }
  513.         }
  514.         for (auto i = N / 2; i < N - 1; i++) {
  515.             for (auto j = 0; j < N - 1; j++) {
  516.                 glNormal3fv(lights[i][j]);
  517.                 glTexCoord2fv(textures[i][j]);
  518.                 glVertex3fv(model[i][j]);
  519.  
  520.                 glNormal3fv(lights[i][j + 1]);
  521.                 glTexCoord2fv(textures[i][j + 1]);
  522.                 glVertex3fv(model[i][j + 1]);
  523.  
  524.                 glNormal3fv(lights[i + 1][j + 1]);
  525.                 glTexCoord2fv(textures[i + 1][j + 1]);
  526.                 glVertex3fv(model[i + 1][j + 1]);
  527.  
  528.                 glNormal3fv(lights[i][j]);
  529.                 glTexCoord2fv(textures[i][j]);
  530.                 glVertex3fv(model[i][j]);
  531.  
  532.                 glNormal3fv(lights[i + 1][j + 1]);
  533.                 glTexCoord2fv(textures[i + 1][j + 1]);
  534.                 glVertex3fv(model[i + 1][j + 1]);
  535.  
  536.                 glNormal3fv(lights[i + 1][j]);
  537.                 glTexCoord2fv(textures[i + 1][j]);
  538.                 glVertex3fv(model[i + 1][j]);
  539.  
  540.             }
  541.         }
  542.         glEnd();
  543.         break;
  544.     default: break;
  545.     }
  546. }
  547.  
  548. void Keys(unsigned char key, int x, int y) {
  549.     if (key == 'p') displayMode = Vertex;
  550.     if (key == 'w') displayMode = Net;
  551.     if (key == 's') displayMode = Solid;
  552.     if (key == '1') lightNo = Light0;
  553.     if (key == '2') lightNo = Light1;
  554.     RenderScene();
  555. }
  556.  
  557. void Mouse(int btn, int state, int x, int y)
  558. {
  559.     if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  560.     {
  561.         x_pos_old = x;
  562.         y_pos_old = y;
  563.         status = LeftButtonPressed;
  564.     }
  565.     else if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  566.     {
  567.         zoom_old = x;
  568.         status = RightButtonPressed;
  569.     }
  570.     else if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
  571.     {
  572.         light_x_pos_old0 = x;
  573.         light_y_pos_old0 = y;
  574.         light_x_pos_old1 = x;
  575.         light_y_pos_old1 = y;
  576.         status = MiddleButtonPressed;
  577.     }
  578.     else status = NoAction;
  579. }
  580.  
  581. void Motion(GLsizei x, GLsizei y)
  582. {
  583.     delta_x = x - x_pos_old;
  584.     x_pos_old = x;
  585.  
  586.     delta_y = y - y_pos_old;
  587.     y_pos_old = y;
  588.  
  589.     delta_zoom = y - zoom_old;
  590.     zoom_old = y;
  591.  
  592.     delta_x_light0 = delta_x;
  593.     delta_y_light0 = delta_y;
  594.     light_x_pos_old0 = x;
  595.     light_y_pos_old0 = y;
  596.  
  597.     delta_x_light1 = delta_x;
  598.     delta_y_light1 = delta_y;
  599.     light_x_pos_old1 = x;
  600.     light_y_pos_old1 = y;
  601.  
  602.     glutPostRedisplay();
  603. }
  604.  
  605. void TurnOnTheLight()
  606. {
  607.     GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  608.     GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  609.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  610.     GLfloat mat_shininess = { 40.0 };
  611.  
  612.     // Definicja źródła światła
  613.     GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 0.4 };
  614.     GLfloat light_ambient2[] = { 1.0, 1.0, 1.0, 0.4 };
  615.  
  616.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0.4  };
  617.     GLfloat light_diffuse2[] = { 1.0, 1.0, 1.0, 0.4 };
  618.  
  619.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  620.     GLfloat att_constant = { 1.0 };
  621.     double att_linear = { 0.05 };
  622.     double att_quadratic = { 0.001 };
  623.  
  624.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  625.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  626.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  627.     glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
  628.  
  629.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  630.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  631.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  632.     glLightfv(GL_LIGHT0, GL_POSITION, light0);
  633.  
  634.     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, att_constant);
  635.     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, att_linear);
  636.     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, att_quadratic);
  637.  
  638.     glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient2);
  639.     glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse2);
  640.     glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
  641.     glLightfv(GL_LIGHT1, GL_POSITION, light1);
  642.  
  643.     glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, att_constant);
  644.     glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, att_linear);
  645.     glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, att_quadratic);
  646.  
  647.     glShadeModel(GL_SMOOTH); // właczenie łagodnego cieniowania
  648.     glEnable(GL_LIGHTING);   // właczenie systemu oświetlenia sceny
  649.     glEnable(GL_LIGHT0);     // włączenie źródła o numerze 0
  650.     glEnable(GL_LIGHT1);
  651.     glEnable(GL_DEPTH_TEST); // włączenie mechanizmu z-bufora
  652.     glEnable(GL_COLOR_MATERIAL);
  653. }
  654.  
  655. void MyInit(void)
  656. {
  657.     GLbyte *pBytes;
  658.     GLint ImWidth, ImHeight, ImComponents;
  659.     GLenum ImFormat;
  660.  
  661.     // ..................................      
  662.     //       Pozostała część funkcji MyInit()
  663.     // ..................................
  664.  
  665.     /*************************************************************************************/
  666.     // Teksturowanie będzie prowadzone tyko po jednej stronie ściany
  667.  
  668.     glEnable(GL_CULL_FACE);
  669.  
  670.     /*************************************************************************************/
  671.     //  Przeczytanie obrazu tekstury z pliku o nazwie tekstura.tga
  672.  
  673.     pBytes = LoadTGAImage("jajko.tga", &ImWidth, &ImHeight, &ImComponents, &ImFormat);
  674.  
  675.     /*************************************************************************************/
  676.     // Zdefiniowanie tekstury 2-D
  677.  
  678.     glTexImage2D(GL_TEXTURE_2D, 0, ImComponents, ImWidth, ImHeight, 0, ImFormat, GL_UNSIGNED_BYTE, pBytes);
  679.  
  680.     /*************************************************************************************/
  681.     // Zwolnienie pamięci
  682.  
  683.     free(pBytes);
  684.  
  685.     /*************************************************************************************/
  686.     // Włączenie mechanizmu teksturowania
  687.  
  688.     glEnable(GL_TEXTURE_2D);
  689.  
  690.     /*************************************************************************************/
  691.     // Ustalenie trybu teksturowania
  692.  
  693.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  694.  
  695.     /*************************************************************************************/
  696.     // Określenie sposobu nakładania tekstur
  697.  
  698.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  699.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  700.  
  701.     //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  702.  
  703.     TurnOnTheLight();
  704. }
  705.  
  706.  
  707.  
  708. void main(void)
  709. {
  710.     srand(time(NULL));
  711.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  712.  
  713.     glutInitWindowSize(700, 700);
  714.  
  715.     glutCreateWindow("Tekstury");
  716.  
  717.     glutDisplayFunc(RenderScene);
  718.     glutReshapeFunc(ChangeSize);
  719.     glutMouseFunc(Mouse);
  720.     glutMotionFunc(Motion);
  721.     MyInit();
  722.     glEnable(GL_DEPTH_TEST);
  723.     PrepareModel();
  724.  
  725.     glutKeyboardFunc(Keys);
  726.     glutMainLoop();
  727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement