Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.19 KB | None | 0 0
  1.  
  2. //#define GLM_FORCE_RADIANS
  3.  
  4. #include "Game.h"
  5. #include "3rd\glm\gtc\matrix_transform.hpp"
  6. #include "3rd\glm\glm.hpp"
  7. #include "Content.h"
  8. #include "Keyboard.h"
  9.  
  10.  
  11. Game::Game(void)
  12. {
  13.  
  14. }
  15.  
  16. Game::~Game(void)
  17. {
  18. }
  19.  
  20. // inicjalizacja parametrow renderingu
  21. void Game::Init()
  22. {
  23.  
  24.     // parametry polozenia i ruchu gracza
  25.     sceneWidth = 10;
  26.     sceneHeight = 15;
  27.     rotate = -90;
  28.     speed = 0.5f; // predkosc ruchu czajnika
  29.     posX = 1.0f;
  30.     posY = 1.0f;
  31.  
  32.     theta = 1.0f;
  33.     Effect::ShadersDirectory = "";
  34.     glClearColor(0.6f, 0.5f, 1.0f, 1.0f);
  35.  
  36.     //stworzenie figury (quada) do wyświetlania render targetów
  37.     quad = new Quad();
  38.  
  39.     // wczytanie tekstury
  40.     tex = Content::LoadTexture("../resources/test.png");
  41.  
  42.     // wczytanie modelu
  43.     testModel = Content::LoadModel("../resources/teaPot.ASE");
  44.  
  45.     //ustawienie materiału
  46.     glm::mat4x4 mm;
  47.     for (int i = 0; i < testModel->Elements(); i++)
  48.     {
  49.         //ustawienie koloru materiału dla pobranego modelu
  50.         testModel->GetMesh(i)->GetMaterial().Color = glm::vec3(1, 1, 0);
  51.         //podmiana tekstury w modelu
  52.         testModel->GetMesh(i)->GetMaterial().texture = tex;
  53.  
  54.         mm = testModel->GetMesh(i)->getLocalWorld();
  55.     }
  56.  
  57.     // drugi model (sciany na scenie)
  58.     sceneModelBox = Content::LoadModel("../resources/cube.ASE");
  59.     for (int i = 0; i < sceneModelBox->Elements(); i++) {
  60.         sceneModelBox->GetMesh(i)->GetMaterial().Color = glm::vec3(0.5f, 0.5f, 0.5f);
  61.     }
  62.  
  63.     // wczytanie tekstury
  64.     textura = Content::LoadTexture("../resources/car.jpg");
  65.  
  66.     // wczytanie modelu
  67.     myModel = Content::LoadModel("../resources/car.3ds");
  68.  
  69.     for (int i = 0; i < testModel->Elements(); i++)
  70.     {
  71.         //ustawienie koloru materiału dla pobranego modelu
  72.         myModel->GetMesh(i)->GetMaterial().Color = glm::vec3(1, 1, 1);
  73.         //podmiana tekstury w modelu
  74.         myModel->GetMesh(i)->GetMaterial().texture = textura;
  75.  
  76.     }
  77.     //////////////////////////////////////// RENDER TARGETS
  78.     rtTMO = new RenderTarget2D(800, 600, GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT, GL_DEPTH24_STENCIL8);
  79.     rt = new RenderTarget2D(800, 600, GL_RGBA32F, GL_RGBA, GL_FLOAT, GL_DEPTH24_STENCIL8);
  80.  
  81.  
  82.     /////////////////////////////////////// SHADERS
  83.     // wczytanie i inicjalizacja shadera obliczajacego kolor powierzchni obiektu
  84.     // wczytuje shadery (wspiera jedynie fragment (.ps) i vertex shadery (.vs), wystarczy podać nazwę pliku bez rozszerzenia)
  85.     shaderColor = new Effect("shaders/color");
  86.     // kompilacja shadera, alteratywnie można wykorzystać CreateShaderInfo, ktory wypisze w konsoli dane nt. shadera oraz ewnetualne błędy.
  87.     shaderColor->CreateShader();
  88.  
  89.     // wczytanie i inicjalizacja shadera obliczajacego mapowanie tonow
  90.     shaderTMO = new Effect("shaders/tmo");
  91.     shaderTMO->CreateShader();
  92.  
  93.     // wczytanie i inicjalizacja shadera renderujacego obraz do framebuffera
  94.     shaderTextureDrawer = new Effect("shaders/texture");
  95.     shaderTextureDrawer->CreateShader();
  96.  
  97.  
  98.     /////////////////////////////////////// KAMERA
  99.     // ustawienie parametrów kamery (konstruktor przyjmuje rozdzielczość viewportu),
  100.     // domyślny field of view to 45*, można zmienić metodą SetFOV(float)
  101.     camera = Camera(800, 600);
  102.     camera.setPosition(glm::vec3(0, 0, 15));
  103.     camera.setTarget(glm::vec3(0, 0, 0));
  104.     //camera.setFOV( 60.0f );
  105.  
  106.     // polozenie zrodla swiatla
  107.     LightPosition = glm::vec3(10, 10, 10);
  108. }
  109.  
  110. // obsluga klawiatury, zmiana parametrow renderingu
  111. void Game::Update()
  112. {
  113.     theta += 0.02f;
  114.     if (Keyboard::isPressed('W'))
  115.     {
  116.         LightPosition.z += 0.1f;
  117.     }
  118.     if (Keyboard::isPressed('S'))
  119.     {
  120.         LightPosition.z -= 0.1f;
  121.     }
  122.     if (Keyboard::isPressed('A'))
  123.     {
  124.         LightPosition.x -= 0.1f;
  125.     }
  126.     if (Keyboard::isPressed('D'))
  127.     {
  128.         LightPosition.x += 0.1f;
  129.     }
  130.     if (Keyboard::isPressed(GLFW_KEY_T)) {
  131.         go = 1;
  132.     }
  133.     if (Keyboard::isPressed(GLFW_KEY_Y)) {
  134.         go = 0;
  135.     }
  136.     if (go == 1) {
  137.         posX = posX + speed * (float)cos(rotate * 3.14 / 180.0f);
  138.         posY = posY - speed * (float)sin(rotate * 3.14 / 180.0f);
  139.     }
  140.     if (go == 0) {
  141.         posX = posX - speed * (float)cos(rotate * 3.14 / 180.0f);
  142.         posY = posY + speed * (float)sin(rotate * 3.14 / 180.0f);
  143.     }
  144.     if (Keyboard::isPressed(GLFW_KEY_UP)) {
  145.         posX = posX + speed * (float)cos(rotate * 3.14 / 180.0f);
  146.         posY = posY - speed * (float)sin(rotate * 3.14 / 180.0f);
  147.     }
  148.     if (Keyboard::isPressed(GLFW_KEY_DOWN)) {
  149.         posX = posX - speed * (float)cos(rotate * 3.14 / 180.0f);
  150.         posY = posY + speed * (float)sin(rotate * 3.14 / 180.0f);
  151.     }
  152.  
  153.     if (Keyboard::isPressed(GLFW_KEY_RIGHT)) {
  154.         rotate += 0.4f;
  155.     }
  156.     if (Keyboard::isPressed(GLFW_KEY_LEFT)) {
  157.         rotate -= 0.4f;
  158.     }
  159.  
  160.     if (Keyboard::isPressed(GLFW_KEY_ESCAPE))
  161.     {
  162.         glfwSetWindowShouldClose(glfwGetCurrentContext(), GL_TRUE);
  163.     }
  164.  
  165.     // zmiana parametrow kamery w czasie ruchu
  166.     camera.setPosition(glm::vec3(posX, posY, 0.6));
  167.  
  168.     glm::mat4 camRot = glm::rotate(glm::mat4(1.0f), (rotate + 90), glm::vec3(0.0f, 0.0f, 1.0f));
  169.     glm::vec4 up = camRot * glm::vec4(0, 1, 0, 1);
  170.  
  171.     float upX = (float)cos((rotate) * 3.14 / 180);
  172.     float upY = (float)sin((rotate) * 3.14 / 180);
  173.     camera.setUp(glm::vec3(upX, -upY, 0));
  174.  
  175.     float camTZ = 0;
  176.     float camTY = posY - 3 * upY;
  177.     float camTX = posX + 3 * upX;
  178.     camera.setTarget(glm::vec3(camTX, camTY, camTZ));
  179.  
  180. }
  181.  
  182.  
  183. // rysowanie planszy gry skladajacej sie z obiektow sceneModelBox
  184. void Game::drawScene(void) {
  185.  
  186.     int i = 0;
  187.  
  188.     shaderColor->GetParameter("matColor")->SetValue(sceneModelBox->GetMesh(i)->GetMaterial().Color);
  189.     shaderColor->GetParameter("mode")->SetValue(-1.0f);
  190.  
  191.     for (int yy = 0; yy < this->sceneHeight; yy++)
  192.         for (int xx = 0; xx < this->sceneWidth; xx++) {
  193.  
  194.             if (xx != 0 && xx != (this->sceneWidth - 1) && yy > 0 && yy < (this->sceneHeight - 1))
  195.                 continue;
  196.  
  197.             this->shaderColor->GetParameter("World")->SetValue(
  198.                 sceneModelBox->GetMesh(i)->getLocalWorld() *
  199.                 glm::translate(glm::mat4(1.0f), glm::vec3(-(float)this->sceneWidth / 2.0f, -(float)this->sceneHeight / 1.0f, 0.0f)) *
  200.                 glm::translate(glm::mat4(1.0f), glm::vec3((float)xx, 0.0f, 0.0f)) *
  201.                 glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, (float)yy, 0.0f)) *
  202.                 glm::rotate(glm::mat4(1.0f), 90.0f, glm::vec3(1.0f, 0.0f, 0.0f)) *
  203.                 glm::rotate(glm::mat4(1.0f), 90.0f, glm::vec3(0.0f, 1.0f, 0.0f)) *
  204.                 glm::scale(glm::mat4(1.0f), glm::vec3(0.01f, 0.01f, 0.01f))
  205.             );
  206.  
  207.             // rysowanie modelu
  208.             sceneModelBox->GetMesh(i)->Draw();
  209.  
  210.         }
  211. }
  212.  
  213.  
  214. // rysowanie sceny (glowna petla)
  215. void Game::Redraw()
  216. {
  217.     //ustawienie textury do ktorej chcemy renderować
  218.     rt->SetRenderTarget();
  219.     glClearColor(0.6f, 0.5f, 1.0f, 1.0f);
  220.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  221.  
  222.     //ustawienie shadera e jako aktywnego
  223.     shaderColor->Apply();
  224.     //ustawienie macierzy widoku i projekcji
  225.     shaderColor->GetParameter("View")->SetValue(camera.getView());
  226.     shaderColor->GetParameter("Projection")->SetValue(camera.getProjection());
  227.     //ustawienie pozycji światła i camery
  228.     shaderColor->GetParameter("LightPosition")->SetValue(LightPosition);
  229.     shaderColor->GetParameter("EyePosition")->SetValue(camera.getPosition());
  230.  
  231.     int i = 0;
  232.  
  233.     //ustawienie koloru materiału
  234.     shaderColor->GetParameter("matColor")->SetValue(testModel->GetMesh(i)->GetMaterial().Color);
  235.  
  236.     //if(testModel->GetMesh(i)->GetMaterial().texture != NULL)
  237.     //{
  238.     //  //jeśli mamy teksturę to ją bindujemy i ustawiamy tryb teskturowania w shaderze
  239.     //  shaderColor->GetParameter("mode")->SetValue(1.0f);
  240.     //  shaderColor->GetParameter("tex")->SetValue(*testModel->GetMesh(i)->GetMaterial().texture);
  241.     //}
  242.     //else
  243.     //{
  244.     //  //włączenie trybu bez tekstury
  245.     //  shaderColor->GetParameter("mode")->SetValue(-1.0f);
  246.     //}
  247.  
  248.     //// ustawienie transformacji obiektu
  249.     //// model może się składać z kilku części (meshy) które znajdują się w przestrzeni lokalnej modelu,
  250.     //// musimy skorygować ich pozycję i przekształcić do przestrzeni naszego świata,
  251.     //// w tym celu wyciągamy macierz transformacji mesha testModel->GetMesh(i)->getLocalWorld(),
  252.     //// a następnie przekształcamy w podobny sposób jak w starszych wersjach OpenGL
  253.  
  254.     //for(int i = 0; i < testModel->Elements(); i++) {
  255.  
  256.     //  shaderColor->GetParameter("World")->SetValue(
  257.     //     
  258.     //      testModel->GetMesh(i)->getLocalWorld() *
  259.     //          glm::translate(glm::mat4(1.0f), glm::vec3( posX, posY, 0 )) *
  260.     //          glm::rotate(glm::mat4(1.0f), -rotate, glm::vec3( 0.0f, 0.0f, 1.0f )) *
  261.     //          glm::rotate(glm::mat4(1.0f), 90.0f, glm::vec3( 1.0f, 0.0f, 0.0f )) *
  262.     // 
  263.     //          glm::scale(glm::mat4(1.0f), glm::vec3( 0.1, 0.1, 0.1 ))
  264.     //          );
  265.  
  266.     //  // rysowanie modelu
  267.     //  testModel->GetMesh(i)->Draw();
  268.     //}
  269.  
  270.     // rysowanie sceny
  271.     drawScene();
  272.  
  273.     //ustawienie koloru materiału
  274.     shaderColor->GetParameter("matColor")->SetValue(myModel->GetMesh(i)->GetMaterial().Color);
  275.  
  276.     if (myModel->GetMesh(i)->GetMaterial().texture != NULL)
  277.     {
  278.         //jeśli mamy teksturę to ją bindujemy i ustawiamy tryb teskturowania w shaderze
  279.         shaderColor->GetParameter("mode")->SetValue(1.0f);
  280.         shaderColor->GetParameter("tex")->SetValue(*myModel->GetMesh(i)->GetMaterial().texture);
  281.     }
  282.     else
  283.     {
  284.         //włączenie trybu bez tekstury
  285.         shaderColor->GetParameter("mode")->SetValue(-1.0f);
  286.     }
  287.  
  288.     for (int i = 0; i < myModel->Elements(); i++) {
  289.  
  290.         shaderColor->GetParameter("World")->SetValue(
  291.  
  292.             myModel->GetMesh(i)->getLocalWorld() *
  293.             //glm::translate(glm::mat4(0.2f), glm::vec3(posX, posY, 1)) *
  294.             glm::rotate(glm::mat4(1.0f), -rotate, glm::vec3(0.0f, 0.0f, 1.0f)) *
  295.             glm::rotate(glm::mat4(1.0f), 90.0f, glm::vec3(1.0f, 0.0f, 0.0f)) *
  296.  
  297.             glm::scale(glm::mat4(0.2f), glm::vec3(1.0, 1.0, 1.0))
  298.         );
  299.  
  300.         // rysowanie modelu
  301.         myModel->GetMesh(i)->Draw();
  302.     }
  303.  
  304.  
  305.     // mapowanie tonow za pomoca drugiego shadera, wynik renderowany jest do drugiego render targetu (rtTMO)
  306.     rtTMO->SetRenderTarget();
  307.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  308.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  309.     shaderTMO->Apply();
  310.     shaderTMO->GetParameter("tex")->SetValue(*rt);
  311.     shaderTMO->GetParameter("gamma")->SetValue(1.6f);
  312.     quad->Draw(0, 0, 800, 600, shaderTMO->GetParameter("World"));
  313.  
  314.     //rendering na ekran (do framebuffera)
  315.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  316.     glViewport(0, 0, 800, 600);
  317.     glClearColor(0.0, 0.0, 0.0, 0.0); //zmiana glClearColor by sprawdzić czy faktycznie wyrenderowaliśmy coś do tekstury
  318.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  319.     shaderTextureDrawer->Apply();
  320.     quad->Draw(0, 0, 800, 600, shaderTextureDrawer->GetParameter("World"));
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement