Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <SFML/Graphics.hpp>
- #include <SFML/System.hpp>
- #include <thread>
- #include <glm/gtc/type_ptr.hpp>
- #include <stdlib.h>
- #include <stdio.h>
- #include "heightfield.h"
- #include "Quadtree.hpp"
- #include "Object.hpp"
- bool HeightField::Init(){
- terrainMVP = engine->getGraphicsManager()->getMVP();
- return true;
- }
- void HeightField::MoveRotateScale(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale)
- {
- glm::mat4 T = glm::translate(glm::mat4(1.0f),pos);
- glm::mat4 Rx = glm::rotate(T, rot.x, glm::vec3(1.0f, 0.0f, 0.0f));
- glm::mat4 Ry = glm::rotate(Rx, rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
- glm::mat4 AlmostModel = glm::rotate(Ry, rot.z, glm::vec3(0.0f, 0.0f, 1.0f));
- glm::mat4 Model = glm::scale(AlmostModel, glm::vec3(1.0, 1.0, 1.0));
- terrainMVP = engine->getGraphicsManager()->getProjection() * engine->getGraphicsManager()->getView() * Model;
- modelM = Model;
- }
- Quadtree* tree = NULL;
- bool HeightField::CreateFromFractal( const int hWidth, const int hHeight)
- {
- hmHeight = hHeight;
- hmWidth = hWidth;
- float iValue;
- float jValue;
- for(int i = 0; i < 32; i++)
- {
- for(int j = 0; j < 32; j++)
- {
- iValue = i;
- jValue = j;
- vertices.push_back(glm::vec3(0.0+iValue, 1.0, 0.0+jValue));
- vertices.push_back(glm::vec3(0.0+iValue, 1.0, 1.0+jValue));
- vertices.push_back(glm::vec3(1.0+iValue, 1.0, 0.0+jValue));
- vertices.push_back(glm::vec3(1.0+iValue, 1.0, 1.0+jValue));
- vertices.push_back(glm::vec3(1.0+iValue, 1.0, 0.0+jValue));
- vertices.push_back(glm::vec3(0.0+iValue, 1.0, 1.0+jValue));
- }
- }
- // bool res = loadOBJ("DATA/Models/box.obj", vertices, uvs, normals);
- for(int i = 0; i < vertices.size(); i++)
- {
- uvs.push_back(glm::vec2(vertices[i].x, vertices[i].z));
- normals.push_back(vertices[i]);
- glm::vec3 oldVec = vertices[i];
- // glm::vec3 newVec = glm::normalize(oldVec);
- glm::vec3 newVec = oldVec;
- vertices[i] = newVec * glm::vec3(20, 20, 20);
- }
- for(int i = 0; i < vertices.size(); i+=3)
- {
- // get the three vertices that make the faces
- glm::vec3 p1 = vertices[i+0];
- glm::vec3 p2 = vertices[i+1];
- glm::vec3 p3 = vertices[i+2];
- glm::vec3 v1 = p2 - p1;
- glm::vec3 v2 = p3 - p1;
- glm::vec3 normal = glm::cross( v1,v2 );
- normal = glm::normalize(normal);
- normals[i+0] = normal;
- normals[i+1] = normal;
- normals[i+2] = normal;
- }
- tree = new Quadtree(0, 4000, 0, 4000, 1);
- tree->Setup();
- // MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
- std::cout << "Terrain has: " << vertices.size() * 3 << " vertices!" << std::endl;
- tID[1] = engine->getGraphicsManager()->loadTexture("Data/Textures/snow.jpg");
- tID[2] = engine->getGraphicsManager()->loadTexture("Data/Textures/stone.jpg");
- tID[3] = engine->getGraphicsManager()->loadTexture("Data/Textures/grass.jpg");
- tID[4] = engine->getGraphicsManager()->loadTexture("Data/Textures/sand.jpg");
- glGenBuffers(1, &vertexbuffer);
- glGenBuffers(1, &uvbuffer);
- glGenBuffers(1, &normalbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
- glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
- glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
- return true;
- }
- bool HeightField::Create(char *hFileName, const int hWidth, const int hHeight){
- hmHeight = hHeight;
- hmWidth = hWidth;
- FILE *fp;
- fp = fopen(hFileName, "rb");
- fread(hHeightField, 1, hWidth * hHeight, fp);
- fclose(fp);
- VertexCount = (int)(hmHeight * hHeight * 6) / (hLOD * hLOD);
- //vertices.resize(VertexCount);
- //uvs.resize(VertexCount);
- int nIndex = 0;
- float flX;
- float flZ;
- for (int hMapX = 0; hMapX < hmWidth; hMapX+=hLOD){
- for (int hMapZ = 0; hMapZ < hmHeight; hMapZ+=hLOD){
- for (int nTri = 0; nTri < 6; nTri++){
- flX = (float)hMapX + ((nTri == 1 || nTri == 2 || nTri == 5) ? hLOD : 0);
- flZ = (float)hMapZ + ((nTri == 2 || nTri == 4 || nTri == 5) ? hLOD : 0);
- vertices.push_back(glm::vec3(flX, (hHeightField[(int)flX][(int)flZ]), flZ));
- uvs.push_back(glm::vec2(flX / 64, flZ / 64));
- nIndex++;
- }
- }
- }
- for( int i = 0; i < vertices.size() ; i ++ )
- {
- vertices[i].y = vertices[i].y * 0.5;
- }
- /*
- for( int i = 0; i < vertices.size() / 3; i += 3 )
- {
- glm::vec3 p1 = vertices[i+0];
- glm::vec3 p2 = vertices[i+1];
- glm::vec3 p3 = vertices[i+2];
- glm::vec3 v1 = p2 - p1;
- glm::vec3 v2 = p3 - p1;
- glm::vec3 normal = glm::cross( v1, v2 );
- glm::normalize(normal);
- // Store the face's normal for each of the vertices that make up the face.
- normals.push_back( normal );
- normals.push_back( normal );
- normals.push_back( normal );
- }
- */
- normals.resize(vertices.size(), glm::vec3(0.0, 1.0, 0.0));
- for( int i = 0; i < vertices.size() / 3; i += 3 )
- {
- glm::vec3 v[3] = { vertices[i], vertices[i+1], vertices[i+2] };
- glm::vec3 normal = glm::cross(v[1] - v[0], v[2] - v[0]);
- for (int j = 0; j < 3; ++j)
- {
- glm::vec3 a = v[(j+1) % 3] - v[j];
- glm::vec3 b = v[(j+2) % 3] - v[j];
- float weight = acos(glm::dot(a, b) / (a.length() * b.length()));
- //normals[(i+j)] += weight * normal;
- }
- //normals[i] += normal;
- }
- for ( unsigned int i = 0; i < normals.size(); ++i )
- {
- normals[i] = glm::normalize( normals[i] );
- }
- /*
- // Now loop through each vertex vector, and avarage out all the normals stored.
- for( int i = 0; i < normals.size(); i+=3 )
- {
- glm::vec3 normal = (normals[i+0] + normals[i+1] + normals[i+2]);
- glm::normalize(normal);
- normals[i+0] = normal;
- normals[i+1] = normal;
- normals[i+2] = normal;
- }
- */
- tID[0] = engine->getGraphicsManager()->loadTexture("Data/Textures/grass.jpg");
- tID[1] = engine->getGraphicsManager()->loadTexture("Data/Textures/sand.jpg");
- glGenBuffers(1, &vertexbuffer);
- glGenBuffers(1, &uvbuffer);
- glGenBuffers(1, &normalbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
- glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
- glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
- MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
- //std::cout << "DONE" << std::endl;
- return true;
- }
- int timeTaken = 0;
- float size = 4;
- void HeightField::Update()
- {
- // Object camera( 0.001, 0.002 , 0.001, 0.002);
- //vector<Object*> getObjects;
- //getObjects = tree->GetObjectsAt( engine->getInputManager()->position.x, engine->getInputManager()->position.z );
- // printf( "Objects at %lf %lf \n", engine->getInputManager()->position.x, engine->getInputManager()->position.z );
- if(timeTaken > 500)
- {
- tree->CheckDistance(engine->getInputManager()->getPosition());
- timeTaken = 0;
- }
- timeTaken++;
- Object camera( engine->getInputManager()->getPosition().x - size, engine->getInputManager()->getPosition().x + size, engine->getInputManager()->getPosition().z - size , engine->getInputManager()->getPosition().z + size);
- tree->AddObject(&camera);
- tree->Clear();
- }
- void HeightField::Render(void){
- glActiveTexture(GL_TEXTURE0 + 1);
- glBindTexture(GL_TEXTURE_2D, tID[1]);
- glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(2), 1);
- glActiveTexture(GL_TEXTURE0 + 2);
- glBindTexture(GL_TEXTURE_2D, tID[2]);
- glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(3), 2);
- glActiveTexture(GL_TEXTURE0 + 3);
- glBindTexture(GL_TEXTURE_2D, tID[3]);
- glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(4), 3);
- glActiveTexture(GL_TEXTURE0 + 4);
- glBindTexture(GL_TEXTURE_2D, tID[4]);
- glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(5), 4);
- glActiveTexture(GL_TEXTURE0 + 5);
- glBindTexture(GL_TEXTURE_2D, tID[5]);
- glUniform1i(engine->getGraphicsManager()->getTerrainTextureID(6), 5);
- MoveRotateScale(position, glm::vec3(0, 0, 0), glm::vec3(1,1,1));
- glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainModelMatrixID(), 1, GL_FALSE, &modelM[0][0]);
- glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainViewMatrixID(), 1, GL_FALSE, &engine->getGraphicsManager()->getView()[0][0]);
- glUniformMatrix4fv(engine->getGraphicsManager()->getTerrainMatrixID(), 1, GL_FALSE, &terrainMVP[0][0]);
- glUniform3f(engine->getGraphicsManager()->getTerrainLightID(), 4200, 4400, 4200);
- //glUniform1f(engine->getGraphicsManager()->getTerrainTimeID(), engine->getWindow()->getTime() * 0.001);
- //glUniform1f(engine->getGraphicsManager()->getTerrainTimeID(), 10 + position.x *0.0001);
- //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);
- tree->Render();
- /*if(timeTaken > 1000)
- {
- tree->deleteLeaves();
- timeTaken = 0;
- }*/
- /*
- //vertices
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glVertexAttribPointer(
- 0,
- 3,
- GL_FLOAT,
- GL_FALSE,
- 0,
- (char*)0
- );
- //UVs
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
- glVertexAttribPointer(
- 1, // attribute
- 2, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (char*)0 // array buffer offset
- );
- //Normal
- glEnableVertexAttribArray(2);
- glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
- glVertexAttribPointer(
- 2, // attribute
- 3, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- glDrawArrays(GL_TRIANGLES, 0, vertices.size() );
- //glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- */
- glBindTexture(GL_TEXTURE_2D, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment