Spawn objects from file

This commit is contained in:
2016-05-27 10:56:24 +02:00
parent 1c59a3bc6f
commit d83cc6403c
8 changed files with 64 additions and 6 deletions
+19 -2
View File
@@ -2,12 +2,19 @@
#include "stb_image.h"
#include "vector.h"
#include "LevelObject.h"
#include <GL/freeglut.h>
#include <iostream>
#include <string>
#include "World.h"
#define RED 0
#define GREEN 1
#define BLUE 2
#define ALPHA 3
HeightMap::HeightMap(const std::string &file, float scale)
HeightMap::HeightMap(const std::string &file, float scale, World* world)
{
this->scale = scale;
@@ -16,7 +23,12 @@ HeightMap::HeightMap(const std::string &file, float scale)
auto heightAt = [&](int x, int y)
{
return (imgData[(x + y * width) * 4] / 256.0f) * 100.0f;
return (imgData[(x + y * width) * 4 ] / 256.0f) * 100.0f;
};
auto valueAt = [&](int x, int y, int offset = 0)
{
return imgData[(x + y * width) * 4 + offset];
};
for (int y = 0; y < height-1; y++)
@@ -27,6 +39,11 @@ HeightMap::HeightMap(const std::string &file, float scale)
Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
if (valueAt(x, y, GREEN) > 0)
{
world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x*scale, heightAt(x, y), y*scale), Vec3f(0, rand()%360, 0), 1, true));
}
Vec3f normal = ca.cross(ba);
normal.Normalize();