Guest User

Heightfield

a guest
Aug 17th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/System.hpp>
  5. #include <thread>
  6.  
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9. #include <stdlib.h>
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "heightfield.h"
  14. #include "Quadtree.hpp"
  15. #include "Object.hpp"
  16.  
  17.  
  18. bool HeightField::Init(){
  19.    
  20.     terrainMVP = engine->getGraphicsManager()->getMVP();
  21.     return true;
  22. }
  23.  
  24. void HeightField::MoveRotateScale(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale)
  25. {
  26.    
  27.    
  28.     glm::mat4 T = glm::translate(glm::mat4(1.0f),pos);
  29.     glm::mat4 Rx = glm::rotate(T, rot.x, glm::vec3(1.0f, 0.0f, 0.0f));
  30.     glm::mat4 Ry = glm::rotate(Rx, rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
  31.     glm::mat4 AlmostModel = glm::rotate(Ry, rot.z, glm::vec3(0.0f, 0.0f, 1.0f));
  32.     glm::mat4 Model = glm::scale(AlmostModel, glm::vec3(1.0, 1.0, 1.0));
  33.  
  34.     terrainMVP = engine->getGraphicsManager()->getProjection() * engine->getGraphicsManager()->getView() * Model;
  35.     modelM = Model;
  36.  
  37. }
  38.  Quadtree* tree = NULL;
  39.  
  40. bool HeightField::CreateFromFractal( const int hWidth, const int hHeight)
  41. {
  42.  
  43.  
  44.       hmHeight = hHeight;
  45.       hmWidth = hWidth;
  46.       float iValue;
  47.       float jValue;
  48.  
  49.      
  50.  
  51.  
  52.       for(int i = 0; i < 32; i++)
  53.       {
  54.           for(int j = 0; j < 32; j++)
  55.           {
  56.               iValue = i;
  57.               jValue = j;
  58.               vertices.push_back(glm::vec3(0.0+iValue, 1.0, 0.0+jValue));
  59.               vertices.push_back(glm::vec3(0.0+iValue, 1.0, 1.0+jValue));
  60.               vertices.push_back(glm::vec3(1.0+iValue, 1.0, 0.0+jValue));
  61.  
  62.               vertices.push_back(glm::vec3(1.0+iValue, 1.0, 1.0+jValue));
  63.               vertices.push_back(glm::vec3(1.0+iValue, 1.0, 0.0+jValue));
  64.               vertices.push_back(glm::vec3(0.0+iValue, 1.0, 1.0+jValue));
  65.           }
  66.       }
  67.  
  68. //  bool res = loadOBJ("DATA/Models/box.obj", vertices, uvs, normals);
  69.  
  70.     for(int i = 0; i < vertices.size(); i++)
  71.     {
  72.         uvs.push_back(glm::vec2(vertices[i].x, vertices[i].z));
  73.         normals.push_back(vertices[i]);
  74.         glm::vec3 oldVec = vertices[i];
  75.     //  glm::vec3 newVec = glm::normalize(oldVec);
  76.         glm::vec3 newVec = oldVec;
  77.         vertices[i] = newVec * glm::vec3(20, 20, 20);
  78.  
  79.     }
  80.     for(int i = 0; i < vertices.size(); i+=3)
  81.     {
  82.  
  83.           // get the three vertices that make the faces
  84.           glm::vec3 p1 = vertices[i+0];
  85.           glm::vec3 p2 = vertices[i+1];
  86.           glm::vec3 p3 = vertices[i+2];
  87.  
  88.           glm::vec3 v1 = p2 - p1;
  89.           glm::vec3 v2 = p3 - p1;
  90.           glm::vec3 normal = glm::cross( v1,v2 );
  91.  
  92.           normal = glm::normalize(normal);
  93.  
  94.           normals[i+0] = normal;
  95.           normals[i+1] = normal;
  96.           normals[i+2] = normal;
  97.        
  98.     }
  99.    
  100.  
  101.       tree = new Quadtree(0, 4000, 0, 4000, 1);
  102.       tree->Setup();
  103.  
  104.  
  105.      
  106.  
  107. //    MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
  108.     std::cout << "Terrain has: " << vertices.size() * 3 << " vertices!" << std::endl;
  109.  
  110.  
  111.     tID[1] = engine->getGraphicsManager()->loadTexture("Data/Textures/snow.jpg");
  112.     tID[2] = engine->getGraphicsManager()->loadTexture("Data/Textures/stone.jpg");
  113.     tID[3] = engine->getGraphicsManager()->loadTexture("Data/Textures/grass.jpg");
  114.     tID[4] = engine->getGraphicsManager()->loadTexture("Data/Textures/sand.jpg");
  115.  
  116.  
  117.     glGenBuffers(1, &vertexbuffer);
  118.     glGenBuffers(1, &uvbuffer);
  119.     glGenBuffers(1, &normalbuffer);
  120.  
  121.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  122.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  123.  
  124.     glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
  125.     glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
  126.  
  127.     glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  128.     glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
  129.  
  130.  
  131.    
  132.  
  133.     return true;
  134. }
  135.  
  136.  
  137. bool HeightField::Create(char *hFileName, const int hWidth, const int hHeight){
  138.    
  139.     hmHeight = hHeight;
  140.     hmWidth = hWidth;
  141.    
  142.     FILE *fp;
  143.  
  144.     fp = fopen(hFileName, "rb");
  145.  
  146.     fread(hHeightField, 1, hWidth * hHeight, fp);
  147.  
  148.     fclose(fp);
  149.    
  150.     VertexCount = (int)(hmHeight * hHeight * 6) / (hLOD * hLOD);
  151.     //vertices.resize(VertexCount);
  152.     //uvs.resize(VertexCount);
  153.  
  154.     int nIndex = 0;
  155.     float flX;
  156.     float flZ;
  157.  
  158.     for (int hMapX = 0; hMapX < hmWidth; hMapX+=hLOD){
  159.         for (int hMapZ = 0; hMapZ < hmHeight; hMapZ+=hLOD){
  160.             for (int nTri = 0; nTri < 6; nTri++){
  161.                 flX = (float)hMapX + ((nTri == 1 || nTri == 2 || nTri == 5) ? hLOD : 0);
  162.                 flZ = (float)hMapZ + ((nTri == 2 || nTri == 4 || nTri == 5) ? hLOD : 0);
  163.                
  164.  
  165.                 vertices.push_back(glm::vec3(flX, (hHeightField[(int)flX][(int)flZ]), flZ));
  166.                 uvs.push_back(glm::vec2(flX / 64, flZ / 64));
  167.                    
  168.  
  169.                 nIndex++;
  170.             }
  171.         }
  172.     }
  173.    
  174.     for( int i = 0; i < vertices.size() ; i ++ )
  175.     {
  176.         vertices[i].y = vertices[i].y * 0.5;
  177.     }
  178.  
  179. /*
  180.     for( int i = 0; i < vertices.size() / 3; i += 3 )
  181.     {
  182.      
  183.       glm::vec3 p1 = vertices[i+0];
  184.       glm::vec3 p2 = vertices[i+1];
  185.       glm::vec3 p3 = vertices[i+2];
  186.  
  187.       glm::vec3 v1 = p2 - p1;
  188.       glm::vec3 v2 = p3 - p1;
  189.       glm::vec3 normal = glm::cross( v1, v2 );
  190.  
  191.       glm::normalize(normal);
  192.  
  193.       // Store the face's normal for each of the vertices that make up the face.
  194.  
  195.      
  196.       normals.push_back( normal );
  197.       normals.push_back( normal );
  198.       normals.push_back( normal );
  199.     }
  200. */
  201.  
  202.  
  203. normals.resize(vertices.size(), glm::vec3(0.0, 1.0, 0.0));
  204.  
  205. for( int i = 0; i < vertices.size() / 3; i += 3 )
  206. {
  207.     glm::vec3 v[3] = { vertices[i], vertices[i+1], vertices[i+2] };
  208.     glm::vec3 normal = glm::cross(v[1] - v[0], v[2] - v[0]);
  209.    
  210.    
  211.       for (int j = 0; j < 3; ++j)
  212.       {
  213.         glm::vec3 a = v[(j+1) % 3] - v[j];
  214.         glm::vec3 b = v[(j+2) % 3] - v[j];
  215.         float weight = acos(glm::dot(a, b) / (a.length() * b.length()));
  216.         //normals[(i+j)] += weight * normal;
  217.       }
  218.    
  219.     //normals[i] += normal;
  220. }
  221.  
  222. for ( unsigned int i = 0; i < normals.size(); ++i )
  223. {
  224.    normals[i] = glm::normalize( normals[i] );
  225. }
  226.  
  227.  
  228.  
  229.  
  230. /*
  231.     // Now loop through each vertex vector, and avarage out all the normals stored.
  232.     for( int i = 0; i < normals.size(); i+=3 )
  233.     {
  234.         glm::vec3 normal = (normals[i+0] + normals[i+1] + normals[i+2]);
  235.         glm::normalize(normal);
  236.         normals[i+0] = normal;
  237.         normals[i+1] = normal;
  238.         normals[i+2] = normal;
  239.     }
  240. */
  241.  
  242.  
  243.     tID[0] = engine->getGraphicsManager()->loadTexture("Data/Textures/grass.jpg");
  244.     tID[1] = engine->getGraphicsManager()->loadTexture("Data/Textures/sand.jpg");
  245.  
  246.  
  247.     glGenBuffers(1, &vertexbuffer);
  248.     glGenBuffers(1, &uvbuffer);
  249.     glGenBuffers(1, &normalbuffer);
  250.  
  251.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  252.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  253.  
  254.     glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
  255.     glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
  256.  
  257.     glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  258.     glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
  259.  
  260.  
  261.     MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
  262.  
  263.     //std::cout << "DONE" << std::endl;
  264.    
  265.     return true;
  266. }
  267.  
  268. int timeTaken = 0;
  269. float size = 4;
  270. void HeightField::Update()
  271. {
  272. //  Object camera( 0.001, 0.002 , 0.001, 0.002);
  273.  
  274.  
  275.  
  276.     //vector<Object*> getObjects;
  277.     //getObjects = tree->GetObjectsAt( engine->getInputManager()->position.x, engine->getInputManager()->position.z );
  278. //  printf( "Objects at %lf %lf \n", engine->getInputManager()->position.x, engine->getInputManager()->position.z );
  279.    
  280.     if(timeTaken > 500)
  281.     {
  282.  
  283.         tree->CheckDistance(engine->getInputManager()->getPosition()); 
  284.         timeTaken = 0;
  285.        
  286.     }
  287.     timeTaken++;
  288.  
  289.     Object camera( engine->getInputManager()->getPosition().x - size, engine->getInputManager()->getPosition().x + size, engine->getInputManager()->getPosition().z - size , engine->getInputManager()->getPosition().z + size);
  290.     tree->AddObject(&camera);
  291.     tree->Clear();
  292.    
  293. }
  294.  
  295. void HeightField::Render(void){
  296.  
  297.  
  298.     glActiveTexture(GL_TEXTURE0 + 1);
  299.     glBindTexture(GL_TEXTURE_2D, tID[1]);
  300.     glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(2), 1);
  301.  
  302.     glActiveTexture(GL_TEXTURE0 + 2);
  303.     glBindTexture(GL_TEXTURE_2D, tID[2]);
  304.     glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(3), 2);
  305.  
  306.     glActiveTexture(GL_TEXTURE0 + 3);
  307.     glBindTexture(GL_TEXTURE_2D, tID[3]);
  308.     glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(4), 3);  
  309.  
  310.     glActiveTexture(GL_TEXTURE0 + 4);
  311.     glBindTexture(GL_TEXTURE_2D, tID[4]);
  312.     glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(5), 4);  
  313.  
  314.     glActiveTexture(GL_TEXTURE0 + 5);
  315.     glBindTexture(GL_TEXTURE_2D, tID[5]);
  316.     glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(6), 5);  
  317.  
  318.  
  319.     MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
  320.  
  321.  
  322.  
  323.     glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainModelMatrixID(), 1, GL_FALSE, &modelM[0][0]);
  324.     glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainViewMatrixID(), 1, GL_FALSE, &engine->getGraphicsManager()->getView()[0][0]);
  325.     glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainMatrixID(), 1, GL_FALSE, &terrainMVP[0][0]);
  326.  
  327.     glUniform3f(engine->getGraphicsManager()->getTerrainLightID(), 4200, 4400, 4200);
  328.     //glUniform1f(engine->getGraphicsManager()->getTerrainTimeID(), engine->getWindow()->getTime() * 0.001);
  329.     //glUniform1f(engine->getGraphicsManager()->getTerrainTimeID(), 10 + position.x *0.0001);
  330.     //Object camera( engine->getInputManager()->position.x - 0.1, engine->getInputManager()->position.x + 0.1, engine->getInputManager()->position.z - 0.1 , engine->getInputManager()->position.z + 0.1);
  331.  
  332.     tree->Render();
  333.    
  334.    
  335.     /*if(timeTaken > 1000)
  336.     {
  337.         tree->deleteLeaves();
  338.         timeTaken = 0;
  339.     }*/
  340.    
  341.    
  342.  
  343.    
  344.  
  345.  
  346.    
  347.     /*
  348.     //vertices
  349.     glEnableVertexAttribArray(0);
  350.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  351.     glVertexAttribPointer(
  352.        0,                  
  353.        3,                  
  354.        GL_FLOAT,          
  355.        GL_FALSE,          
  356.        0,                  
  357.        (char*)0          
  358.     );
  359.  
  360.  
  361.  
  362.      //UVs
  363.      glEnableVertexAttribArray(1);
  364.      glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  365.      glVertexAttribPointer(
  366.        1,                                // attribute
  367.        2,                                // size
  368.        GL_FLOAT,                         // type
  369.        GL_FALSE,                         // normalized?
  370.        0,                                // stride
  371.        (char*)0                          // array buffer offset
  372.      );
  373.      
  374.      //Normal
  375.      glEnableVertexAttribArray(2);
  376.      glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
  377.      glVertexAttribPointer(
  378.        2,                                // attribute
  379.        3,                                // size
  380.        GL_FLOAT,                         // type
  381.        GL_FALSE,                         // normalized?
  382.        0,                                // stride
  383.        (void*)0                          // array buffer offset
  384.      );
  385.      
  386.  
  387.     glDrawArrays(GL_TRIANGLES, 0, vertices.size() );
  388.     //glDrawArrays(GL_TRIANGLE_STRIP, 0,  vertices.size());
  389.     glDisableVertexAttribArray(0);
  390.     glDisableVertexAttribArray(1);
  391.     glDisableVertexAttribArray(2);
  392.  
  393.  */
  394.     glBindTexture(GL_TEXTURE_2D, 0);
  395.  
  396. }
Advertisement
Add Comment
Please, Sign In to add comment