Advertisement
Guest User

Untitled

a guest
May 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. //
  2. //  Application.cpp
  3. //  ogl4
  4. //
  5. //  Created by Philipp Lensing on 16.09.16.
  6. //  Copyright © 2016 Philipp Lensing. All rights reserved.
  7. //
  8.  
  9. #include "Application.h"
  10. #ifdef WIN32
  11. #include <GL/glew.h>
  12. #include <glfw/glfw3.h>
  13. #define _USE_MATH_DEFINES
  14. #include <math.h>
  15. #else
  16. #define GLFW_INCLUDE_GLCOREARB
  17. #define GLFW_INCLUDE_GLEXT
  18. #include <glfw/glfw3.h>
  19. #endif
  20. #include "lineplanemodel.h"
  21. #include "triangleplanemodel.h"
  22. #include "trianglespheremodel.h"
  23. #include "lineboxmodel.h"
  24. #include "triangleboxmodel.h"
  25. #include "model.h"
  26. #include "terrainshader.h"
  27.  
  28.  
  29. #ifdef WIN32
  30. #define ASSET_DIRECTORY "../../assets/"
  31. #else
  32. #define ASSET_DIRECTORY "../assets/"
  33. #endif
  34.  
  35.  
  36. Application::Application(GLFWwindow* pWin) : pWindow(pWin), Cam(pWin)
  37. {
  38.     BaseModel* pModel;
  39.    
  40.     // create LineGrid model with constant color shader
  41.     pModel = new LinePlaneModel(10, 10, 10, 10);
  42.     ConstantShader* pConstShader = new ConstantShader();
  43.     pConstShader->color( Color(1,0,0));
  44.     pModel->shader(pConstShader, true);
  45.     // add to render list
  46.     Models.push_back( pModel );
  47.    
  48.     // Exercise 1
  49.     // uncomment the following lines for testing
  50.    
  51.    
  52.     pModel = new Model(ASSET_DIRECTORY "skybox.obj", false);
  53.     pModel->shader(new PhongShader(), true);
  54.     Models.push_back(pModel);
  55.    
  56.     pTerrain = new Terrain();
  57.     TerrainShader* pTerrainShader = new TerrainShader(ASSET_DIRECTORY);
  58.     pTerrainShader->diffuseTexture(Texture::LoadShared(ASSET_DIRECTORY "grass.bmp"));
  59.     pTerrain->shader(pTerrainShader, true);
  60.     pTerrain->load(ASSET_DIRECTORY "heightmap2.png", ASSET_DIRECTORY"grass.bmp", ASSET_DIRECTORY"rock.bmp");
  61.     pTerrain->width(150);
  62.     pTerrain->depth(150);
  63.     pTerrain->height(15);
  64.     Models.push_back(pTerrain);
  65.    
  66.    
  67. }
  68. void Application::start()
  69. {
  70.     glEnable (GL_DEPTH_TEST); // enable depth-testing
  71.     glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
  72.     glEnable(GL_CULL_FACE);
  73.     glCullFace(GL_BACK);
  74.     glEnable(GL_BLEND);
  75.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  76. }
  77.  
  78. void Application::update(float dtime)
  79. {
  80.     // Exercise 1
  81.     // TODO: Add keyboard & mouse input queries for terrain scaling ..
  82.  
  83.     double xpos, ypos;
  84.     static double oldxpos, oldypos;
  85.  
  86.     glfwGetCursorPos(pWindow, &xpos, &ypos);
  87.  
  88.    
  89.     if (glfwGetKey(pWindow, GLFW_KEY_S) == GLFW_PRESS) {
  90.        
  91.  
  92.  
  93.         if (xpos < oldxpos) {
  94.             std::cout << "Mach x kleiner"<< std::endl;
  95.         }
  96.         if (xpos > oldxpos) {
  97.             std::cout << "Mach x groesser" << std::endl;
  98.         }
  99.  
  100.         if (ypos > oldypos) {
  101.             std::cout << "Mach y kleiner" << std::endl;
  102.         }
  103.         if (ypos < oldypos) {
  104.             std::cout << "Mach y groesser" << std::endl;
  105.         }
  106.  
  107.        
  108.     }
  109.  
  110.     oldxpos = xpos;
  111.     oldypos = ypos;
  112.    
  113.     Cam.update();
  114. }
  115.  
  116. void Application::draw()
  117. {
  118.     // 1. clear screen
  119.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120.  
  121.     // 2. setup shaders and draw models
  122.     for( ModelList::iterator it = Models.begin(); it != Models.end(); ++it )
  123.     {
  124.         (*it)->draw(Cam);
  125.     }
  126.    
  127.     // 3. check once per frame for opengl errors
  128.     GLenum Error = glGetError();
  129.     assert(Error==0);
  130. }
  131. void Application::end()
  132. {
  133.     for( ModelList::iterator it = Models.begin(); it != Models.end(); ++it )
  134.         delete *it;
  135.    
  136.     Models.clear();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement