Przeglądaj źródła

Added height at location

Kenneth van Ewijk 9 lat temu
rodzic
commit
2b0e22d4c9
10 zmienionych plików z 54 dodań i 29 usunięć
  1. 5 3
      CrystalPoint.cpp
  2. 6 4
      HeightMap.cpp
  3. 3 3
      HeightMap.h
  4. 10 1
      World.cpp
  5. 1 0
      World.h
  6. 8 0
      WorldHandler.cpp
  7. 1 0
      WorldHandler.h
  8. 14 13
      worlds/fire.json
  9. 4 3
      worlds/ice.json
  10. 2 2
      worlds/worlds.json

+ 5 - 3
CrystalPoint.cpp

@@ -78,19 +78,21 @@ void CrystalPoint::update()
 	if (player->rotation.x < -90)
 		player->rotation.x = -90;
 
-	float speed = 20;
+	float speed = 2;
 
 	Vec3f oldPosition = player->position;
 	if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
 	if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
 	if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
 	if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
-	if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
-	if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
+	//if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
+	//if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
 
 	if (!worldhandler->isPlayerPositionValid())
 		player->position = oldPosition;
 
+	player->position.y = worldhandler->getHeight(player->position.x, player->position.z);
+
 	worldhandler->update(deltaTime);
 
 	mousePosition = mousePosition + mouseOffset;

+ 6 - 4
HeightMap.cpp

@@ -7,8 +7,10 @@
 #include <string>
 
 
-HeightMap::HeightMap(const std::string &file)
+HeightMap::HeightMap(const std::string &file, float scale)
 {
+	this->scale = scale;
+
 	int bpp;
 	unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
 
@@ -90,7 +92,7 @@ void HeightMap::Draw()
 	glDisableClientState(GL_NORMAL_ARRAY);
 }
 
-float HeightMap::GetHeigth(float x, float y)
+float HeightMap::GetHeight(float x, float y)
 {
 	x /= scale;
 	y /= scale;
@@ -105,14 +107,14 @@ float HeightMap::GetHeigth(float x, float y)
 
 	float lowervalue = ((b.y - c.y)*(a.x - c.x) + (c.x - b.x)*(a.y - c.y));
 
-	float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y))/ lowervalue;
+	float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y)) / lowervalue;
 
 	float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
 
 	float labda3 = 1 - labda1 - labda2;
 
 	Vec3f z = Vec3f(a.x, a.y, a.z) * labda1 + Vec3f(b.x, b.y, b.z) * labda2 + Vec3f(c.x, c.y, c.z) * labda3;
-	
+
 	return z.y;
 }
 

+ 3 - 3
HeightMap.h

@@ -12,13 +12,13 @@ private:
 	int width;
 
 	GLuint imageIndex;
-	int scale = 1;
+	int scale;
 public:
-	HeightMap(const std::string &file);
+	HeightMap(const std::string &file, float scale);
 	~HeightMap();
 
 	void Draw();
-	float GetHeigth(float x, float y);
+	float GetHeight(float x, float y);
 	void SetTexture(const std::string &file);
 
 	std::vector<Vertex> vertices;

+ 10 - 1
World.cpp

@@ -25,7 +25,11 @@ World::World(const std::string &fileName)
 	if (v["objects"].isNull())
 		std::cout << "Invalid world file: objects - " << fileName << "\n";
 
-	heightmap = new HeightMap(v["world"]["heightmap"].asString());
+	float scale = 1.0f;
+	if (!v["world"]["scale"].isNull())
+		scale = v["world"]["scale"].asFloat();
+
+	heightmap = new HeightMap(v["world"]["heightmap"].asString(), scale);
 
 	if(!v["world"]["texture"].isNull())
 		heightmap->SetTexture(v["world"]["texture"].asString());
@@ -86,6 +90,11 @@ World::~World()
 	delete heightmap;
 }
 
+float World::getHeight(float x, float y)
+{
+	return heightmap->GetHeight(x, y);
+}
+
 void World::draw()
 {
 	player->setCamera();

+ 1 - 0
World.h

@@ -22,5 +22,6 @@ public:
 	void draw();
 	void update(float elapsedTime);
 	bool isPlayerPositionValid();
+	float getHeight(float x, float y);
 };
 

+ 8 - 0
WorldHandler.cpp

@@ -99,6 +99,14 @@ bool WorldHandler::isPlayerPositionValid(void)
 		return world->isPlayerPositionValid();
 }
 
+float WorldHandler::getHeight(float x, float y)
+{
+	if (!loadingWorld)
+		return world->getHeight(x, y);
+	else
+		return 0.0f;
+}
+
 
 void WorldHandler::Navigate(const std::string &fileName)
 {

+ 1 - 0
WorldHandler.h

@@ -25,6 +25,7 @@ public:
 	void update(float deltaTime);
 
 	bool isPlayerPositionValid(void);
+	float getHeight(float x, float y);
 
 	void Navigate(const std::string &fileName);
 	void NextWorld();

+ 14 - 13
worlds/fire.json

@@ -1,20 +1,21 @@
 {
    "world": {
-      "heightmap": "worlds/hell.png",
-	  "texture": "worlds/helltexture.png",
-	  "object-templates": [
-			{
-				"color": 25,
-				"file": "models/boom/Boom.obj"
-			},
-			{
-				"color": 23,
-				"file": "models/boom/Boom.obj"
-			}
-	  ]
+    "heightmap": "worlds/hell.png",
+    "texture": "worlds/helltexture.png",
+    "scale": 1,
+	    "object-templates": [
+			  {
+				  "color": 25,
+				  "file": "models/boom/Boom.obj"
+			  },
+			  {
+				  "color": 23,
+				  "file": "models/boom/Boom.obj"
+			  }
+	    ]
    },
   "player": {
-    "startposition": [ 100, 1.7, 100 ]
+    "startposition": [ 200, 40, 200 ]
   },
   "objects": [
     {

+ 4 - 3
worlds/ice.json

@@ -1,10 +1,11 @@
 {
    "world": {
-      "heightmap": "worlds/hmcs.png",
-	  "texture": "worlds/hmcstexture.png"
+    "heightmap": "worlds/hmcs.png",
+    "texture": "worlds/hmcstexture.png",
+    "scale": 2
    },
   "player": {
-    "startposition": [ -100, 1.7, -100 ]
+    "startposition": [ 100, 20, 100 ]
   },
   "objects": [
     {

+ 2 - 2
worlds/worlds.json

@@ -1,6 +1,6 @@
 {
 	"worlds": [
-			"worlds/fire.json",
-			"worlds/ice.json"
+			"worlds/ice.json",
+			"worlds/fire.json"
 	]
 }