Merge branch 'master' into pathfinding

This commit is contained in:
Lendemor 2016-03-08 23:48:49 +01:00
commit c2f500d3b5
7 changed files with 288 additions and 149 deletions

View File

@ -9,20 +9,9 @@ set(EXEC_SRC_LIST src/main.cpp)
#set compilation option
set(IS_LIBRARY True)
set(USE_OPENGL True)
set(USE_SFML True)
set(USE_RENDERER True)
set(USE_SFML True)
set(USE_INPUT True)
set(USE_BULLET True)
list(APPEND INCLUDE_PATHS_EXTENSION
/bullet
)
list(APPEND EXTRA_INCLUDE
${PROJECT_SOURCE_DIR}/../sparrowinput
${PROJECT_SOURCE_DIR}/../SparrowInput
${PROJECT_SOURCE_DIR}/../sparrowrenderer/src
)
include(template.cmake)
include(../cmaketemplate/template.cmake)

View File

@ -1,16 +1,15 @@
/*#include "engine.h"
#include "engine.h"
#include <input.h>
#include "scene.h"
#include "cameranode.h"
#include "resourcemanager.h"
#include "sparrowrenderer.h"
*/
#include "tools/graph.h"
#include "tools/pathfinder.h"
int main(){
/* CameraNode camera;
CameraNode camera;
RESOURCE_ADD(&camera, SceneNode, "camera");
Scene scene("testScene");
@ -20,7 +19,7 @@ int main(){
engine.getRenderer()->setCamera(&camera);
engine.createWindow("test");
engine.setScene("testScene");
engine.start();*/
engine.start();
GraphNode n1 = GraphNode();
n1.setValue(1);

41
src/tools/loader.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "loader.h"
#include <fstream>
#include <streambuf>
#include <image.h>
#include <SFML/Graphics/Image.hpp>
std::string* Loader::loadTextFile(const std::string &filename)
{
std::ifstream t(filename);
std::string *str = new std::string();
t.seekg(0, std::ios::end);
str->reserve(t.tellg());
t.seekg(0, std::ios::beg);
str->assign((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
Image* Loader::loadImage(const std::string &filename, bool hasAlpha)
{
sf::Image sfImg;
sfImg.loadFromFile(filename);
Image* img = new Image();
img->depth = hasAlpha ? 32 : 24;
img->width = sfImg.getSize().x;
img->height = sfImg.getSize().y;
int size = img->width*img->height*(img->depth/8);
img->allocate(size);
const sf::Uint8 *pixels = sfImg.getPixelsPtr();
if(hasAlpha)
memcpy(img->pixels, pixels, size);
else
{
sf::Uint8 *ptr = (sf::Uint8*)img->pixels;
for(int i=0; i<img->width*img->height; ++i)
memcpy(ptr + i*3, pixels + i*4, 3);
}
return img;
}

15
src/tools/loader.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef LOADER_H
#define LOADER_H
#include <string>
class Image;
class Loader
{
public:
static std::string* loadTextFile(const std::string &filename);
static Image* loadImage(const std::string &filename, bool hasAlpha = true);
};
#endif // LOADER_H

197
src/tools/noise.cpp Normal file
View File

@ -0,0 +1,197 @@
#include "noise.h"
#include <iostream>
#include <glm/ext.hpp>
Noise::Noise()
{
}
Noise::~Noise()
{
}
void Noise::initPerlinNoise(int seed)
{
int i,j,k;
srand(seed);
//INFO: permutation array
for (i = 0 ; i < ARBITRARY_VALUE ; i++) permut[i] = i;
for (i = ARBITRARY_VALUE ; i > 0 ; i -= 2)
{
k = permut[i];
permut[i] = permut[j = std::rand() % ARBITRARY_VALUE];
permut[j] = k;
};
}
float Noise::PerlinNoise2D(float x, float z)
{
int x0,x1,z0,z1;
x0 = fastfloor(x);
x1 = x0 + 1;
z0 = fastfloor(z);
z1 = z0 + 1;
float dx,dz;
dx = x - x0;
dz = z - z0;
glm::vec2 grad[4];
int idx[4];
idx[0] = calculIdx2D(x0,z0);
idx[1] = calculIdx2D(x1,z0);
idx[2] = calculIdx2D(x0,z1);
idx[3] = calculIdx2D(x1,z1);
for(int i=0; i < 4; i++)
grad[i] = gradient2[idx[i]];
glm::vec2 vect[4];
vect[0] = glm::vec2(dx+1, dz+1);
vect[1] = glm::vec2(dx-1, dz+1);
vect[2] = glm::vec2(dx+1, dz-1);
vect[3] = glm::vec2(dx-1, dz-1);
float val[4];
for(int i=0; i < 4; i++)
val[i] = glm::dot(grad[i], vect[i]);
float sx, sz;
sx = (3.f * dx * dx) - (2.f * dx * dx * dx);
sz = (3.f * dz * dz) - (2.f * dz * dz * dz);
// interpolation on axis x
float u[2];
for (int i = 0;i <2; i++)
u[i]= lerp(val[i*2],val[(i*2)+1], sx);
// interpolation on axis z
return lerp(u[0],u[1],sz);
}
float Noise::PerlinNoise3D(float x, float y, float z)
{
int x0, x1, y0, y1, z0, z1;
x0 = fastfloor(x);
x1 = x0 + 1;
y0 = fastfloor(y);
y1 = y0 + 1;
z0 = fastfloor(z);
z1 = z0 + 1;
float dx,dy,dz;
dx = x - x0;
dy = y - y0;
dz = z - z0;
glm::vec3 grad[8];
grad[0] = gradient3[(permut[(permut[x0%ARBITRARY_VALUE] + y0)%ARBITRARY_VALUE] + z0)];
grad[1] = gradient3[(permut[(permut[x1%ARBITRARY_VALUE] + y0)%ARBITRARY_VALUE] + z0)];
grad[2] = gradient3[(permut[(permut[x0%ARBITRARY_VALUE] + y1)%ARBITRARY_VALUE] + z0)];
grad[3] = gradient3[(permut[(permut[x1%ARBITRARY_VALUE] + y1)%ARBITRARY_VALUE] + z0)];
grad[4] = gradient3[(permut[(permut[x0%ARBITRARY_VALUE] + y0)%ARBITRARY_VALUE] + z1)];
grad[5] = gradient3[(permut[(permut[x1%ARBITRARY_VALUE] + y0)%ARBITRARY_VALUE] + z1)];
grad[6] = gradient3[(permut[(permut[x0%ARBITRARY_VALUE] + y1)%ARBITRARY_VALUE] + z1)];
grad[7] = gradient3[(permut[(permut[x1%ARBITRARY_VALUE] + y1)%ARBITRARY_VALUE] + z1)];
glm::vec3 vect[8];
vect[0] = glm::vec3(dx, dy, dz);
vect[1] = glm::vec3(dx-1, dy, dz);
vect[2] = glm::vec3(dx, dy-1, dz);
vect[3] = glm::vec3(dx-1, dy-1, dz);
vect[4] = glm::vec3(dx, dy, dz-1);
vect[5] = glm::vec3(dx-1, dy, dz-1);
vect[6] = glm::vec3(dx, dy-1, dz-1);
vect[7] = glm::vec3(dx-1, dy-1, dz-1);
float val[8];
for(int i=0; i < 8; i++)
val[i] = glm::dot(grad[i], vect[i]);
// process weight for interpolation
float sx, sy, sz;
sx = (3.f * dx * dx) - (2.f * dx * dx * dx);
sy = (3.f * dy * dy) - (2.f * dy * dy * dy);
sz = (3.f * dz * dz) - (2.f * dz * dz * dz);
// interpolation on axis x
float u[4];
for (int i = 0;i <4; i++){
u[i]= lerp(val[i*2],val[(i*2)+1], sx);
}
// interpolation on axis y
float v[2];
for (int i = 0;i <2; i++)
v[i]= lerp(u[i*2],u[(i*2)+1],sy);
// interpolation on axis z
return lerp(v[0],v[1],sz);
}
//shitty function
float Noise::OctavePerlinNoise(float nb_octave, float persistence, float x, float y, float z)
{
float total = 0;
float frequency = 1;
float amplitude = 100;
float maxAmplitude = 0;
for(int i=0; i < nb_octave; i++){
total += PerlinNoise3D(x/frequency, y/frequency, z/frequency)*amplitude;
frequency *= 2;
maxAmplitude += amplitude;
amplitude *= persistence;
}
return total/maxAmplitude;
}
float Noise::SimplexNoise(float x, float y, float z){
int x0, x1, y0, y1, z0, z1;
return 1.f;
}
int Noise::calculIdx2D(int x, int z)
{
int u = x%ARBITRARY_VALUE;
u = u < 0 ? u + ARBITRARY_VALUE : u;
int v = (permut[u] + z) % ARBITRARY_VALUE;
v = v < 0 ? v + ARBITRARY_VALUE : v;
return permut[v];
}
int Noise::fastfloor(float v)
{
int res = (int) v;
return res > v ? res -1 : res;
}
//linear interpolation
float Noise::lerp(float a, float b, float w){
return (1-w)*a + w*b;
}
glm::vec2 Noise::gradient2[] = {
glm::vec2(1,0), glm::vec2(-1,0), glm::vec2(0,1), glm::vec2(0,-1),
glm::vec2(1,1), glm::vec2(-1,1), glm::vec2(1,-1), glm::vec2(-1,-1),
glm::vec2(0,1), glm::vec2(0,-1), glm::vec2(1,0), glm::vec2(-1,0)
};
glm::vec3 Noise::gradient3[] = {
// irr::core::vector3df(1,1,0), irr::core::vector3df(-1,1,0), irr::core::vector3df(1,-1,0), irr::core::vector3df(-1,-1,0),
// irr::core::vector3df(1,0,1), irr::core::vector3df(-1,0,1), irr::core::vector3df(1,0,-1), irr::core::vector3df(-1,0,-1),
// irr::core::vector3df(0,1,1), irr::core::vector3df(0,-1,1), irr::core::vector3df(0,1,-1), irr::core::vector3df(0,-1,-1)
};
int Noise::permut[] = {0};

30
src/tools/noise.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef NOISE_H
#define NOISE_H
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#define ARBITRARY_VALUE 12 // TODO : find an appropriate name
class Noise
{
private:
static glm::vec2 gradient2[ARBITRARY_VALUE];
static glm::vec3 gradient3[ARBITRARY_VALUE];
static int permut[ARBITRARY_VALUE];
//private function
static int calculIdx2D(int x, int z); // TODO : remove french namings
static int fastfloor(float v);
static float lerp(float a, float b, float w);
public:
Noise();
~Noise();
static void initPerlinNoise(int seed);
static float PerlinNoise2D(float x, float z);
static float PerlinNoise3D(float x, float y, float z);
static float OctavePerlinNoise(float nb_octave, float persistence, float x, float y, float z);
static float SimplexNoise(float x, float y, float z);
};
#endif // NOISE_H

View File

@ -1,132 +0,0 @@
# Variable that you need to define to use this template
# IS_LIBRARY, USE_SFML, USE_RENDERER, USE_INPUT, USE_BULLET,USE_OPENGL
#
# Container for list of file to be compiled :
# LIB_SRC_LIST, LIB_HEAD_LIST, EXEC_SRC_LIST, EXEC_HEAD_LIST
#
# Variable available to extend include_dir locations
# INCLUDE_PATHS_EXTENSION
# EXTRA_INCLUDE
#detect system
if(WIN32)
set(SYSTEM_LIB_PATH "win32")
else(WIN32)
set(SYSTEM_LIB_PATH "linux")
endif(WIN32)
#set dependencies paths
set(DEPENDENCIES_ROOT ${PROJECT_SOURCE_DIR}/../cpp_dependencies)
set(INCLUDE_ROOT ${DEPENDENCIES_ROOT}/include)
set(LIB_ROOT ${DEPENDENCIES_ROOT}/lib/${SYSTEM_LIB_PATH})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB_ROOT}) #for SHARED
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB_ROOT}) #for STATIC
#create library and executable
if(IS_LIBRARY)
set(LIBRARY_NAME ${PROJECT_NAME})
set(EXECUTABLE_NAME "test${PROJECT_NAME}")
add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST} ${LIB_HEAD_LIST})
else()
set(EXECUTABLE_NAME "${PROJECT_NAME}")
endif()
add_executable(${EXECUTABLE_NAME} ${EXEC_SRC_LIST})
add_definitions(-std=c++11)
LIST(APPEND INCLUDE_PATHS ${INCLUDE_ROOT})
foreach(EXTENSION ${INCLUDE_PATHS_EXTENSION})
LIST(APPEND INCLUDE_PATHS ${INCLUDE_ROOT}${EXTENSION} " ")
endforeach()
include_directories(
${INCLUDE_PATHS}
${EXTRA_INCLUDE}
)
#find libraries
set(DEP_LIST "")
if(USE_SFML)
find_library(SFML_LIBRARY_WINDOW
NAMES
sfml-window
PATHS
${LIB_ROOT}
)
find_library(SFML_LIBRARY_SYSTEM
NAMES
sfml-system
PATHS
${LIB_ROOT}
)
LIST(APPEND DEP_LIST ${SFML_LIBRARY_WINDOW} ${SFML_LIBRARY_SYSTEM})
endif()
if(USE_RENDERER)
find_library(SPARROW_RENDERER_LIBRARY
NAMES
sparrowrenderer
PATHS
${LIB_ROOT}
)
LIST(APPEND DEP_LIST ${SPARROW_RENDERER_LIBRARY})
endif()
if(USE_INPUT)
find_library(SPARROW_INPUT_LIBRARY
NAMES
SparrowInput
PATHS
${LIB_ROOT}
)
LIST(APPEND DEP_LIST ${SPARROW_INPUT_LIBRARY})
endif()
if(USE_BULLET)
find_library(BULLET_COLLISION_LIBRARY
NAMES
BulletCollision
PATHS
${LIB_ROOT}
)
find_library(BULLET_DYNAMICS_LIBRARY
NAMES
BulletDynamics
PATHS
${LIB_ROOT}
)
find_library(LINEAR_MATH_LIBRARY
NAMES
LinearMath
PATHS
${LIB_ROOT}
)
LIST(APPEND DEP_LIST ${BULLET_COLLISION_LIBRARY} ${BULLET_DYNAMICS_LIBRARY} ${LINEAR_MATH_LIBRARY})
endif()
if(USE_OPENGL)
find_package(OpenGL REQUIRED)
LIST(APPEND DEP_LIST ${OPENGL_LIBRARIES})
endif()
if(IS_LIBRARY)
target_link_libraries(
${LIBRARY_NAME}
${DEP_LIST}
)
target_link_libraries(
${EXECUTABLE_NAME}
${LIBRARY_NAME}
)
else()
target_link_libraries(
${EXECUTABLE_NAME}
${DEP_LIST}
)
endif()