WIP on fluid medium traversal

This commit is contained in:
Michaël Lemaire 2013-11-09 18:46:34 +01:00
parent 5c2bc4b31e
commit e9751a0e0b
15 changed files with 442 additions and 192 deletions

View file

@ -0,0 +1 @@
#include "SpaceCoordinates.h"

View file

@ -0,0 +1,25 @@
#ifndef SPACECOORDINATES_H
#define SPACECOORDINATES_H
#include "basics_global.h"
#include "Vector3.h"
namespace paysages {
namespace basics {
/*!
* \brief Space 3D coordinates.
*
* These coordinates allow to work on very large scenes, regardless of the 'double' type precision.
*/
class SpaceCoordinates:public Vector3
{
/*public:
SpaceCoordinates();*/
};
}
}
#endif // SPACECOORDINATES_H

156
src/basics/Vector3.cpp Normal file
View file

@ -0,0 +1,156 @@
#include "Vector3.h"
#include <math.h>
#include "PackStream.h"
Vector3 VECTOR_ZERO = {0.0, 0.0, 0.0};
Vector3 VECTOR_DOWN = {0.0, -1.0, 0.0};
Vector3 VECTOR_UP = {0.0, 1.0, 0.0};
Vector3 VECTOR_NORTH = {0.0, 0.0, -1.0};
Vector3 VECTOR_SOUTH = {0.0, 0.0, 1.0};
Vector3 VECTOR_WEST = {-1.0, 0.0, 0.0};
Vector3 VECTOR_EAST = {1.0, 0.0, 0.0};
static inline double _euclidGet2DAngle(double x, double y)
{
// TEMP Copy of old euclid.c
double nx, ny, d, ret;
if (x == 0.0)
{
if (y == 0.0)
{
return 0.0;
}
else if (y < 0.0)
{
return 3.0 * M_PI_2;
}
else
{
return M_PI_2;
}
}
d = sqrt(x * x + y * y);
nx = x / d;
ny = y / d;
ret = asin(ny);
if (nx < 0.0)
{
ret = M_PI - ret;
}
return ret < 0.0 ? ret + 2.0 * M_PI : ret;
}
void v3Save(PackStream* stream, Vector3* v)
{
stream->write(&v->x);
stream->write(&v->y);
stream->write(&v->z);
}
void v3Load(PackStream* stream, Vector3* v)
{
stream->read(&v->x);
stream->read(&v->y);
stream->read(&v->z);
}
Vector3 v3Translate(Vector3 v1, double x, double y, double z)
{
Vector3 result;
result.x = v1.x + x;
result.y = v1.y + y;
result.z = v1.z + z;
return result;
}
Vector3 v3Add(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
Vector3 v3Sub(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
Vector3 v3Neg(Vector3 v)
{
Vector3 result;
result.x = -v.x;
result.y = -v.y;
result.z = -v.z;
return result;
}
Vector3 v3Scale(Vector3 v, double scale)
{
Vector3 result;
result.x = v.x * scale;
result.y = v.y * scale;
result.z = v.z * scale;
return result;
}
double v3Norm(Vector3 v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
Vector3 v3Normalize(Vector3 v)
{
double norm = v3Norm(v);
if (norm == 0.0)
{
return VECTOR_ZERO;
}
else
{
return v3Scale(v, 1.0 / norm);
}
}
double v3Dot(Vector3 v1, Vector3 v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vector3 v3Cross(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.y * v2.z - v1.z * v2.y;
result.y = v1.z * v2.x - v1.x * v2.z;
result.z = v1.x * v2.y - v1.y * v2.x;
return result;
}
VectorSpherical v3ToSpherical(Vector3 v)
{
VectorSpherical result;
result.phi = _euclidGet2DAngle(v.x, -v.z);
result.theta = _euclidGet2DAngle(sqrt(v.x * v.x + v.z * v.z), v.y);
if (v.y < 0.0)
{
result.theta -= 2.0 * M_PI;
}
result.r = v3Norm(v);
return result;
}
Vector3 v3FromSpherical(VectorSpherical v)
{
Vector3 result = {v.r * cos(v.phi) * cos(v.theta), v.r * sin(v.theta), -v.r * sin(v.phi) * cos(v.theta)};
return result;
}

78
src/basics/Vector3.h Normal file
View file

@ -0,0 +1,78 @@
#ifndef VECTOR3_H
#define VECTOR3_H
#include "basics_global.h"
/*
* Cartesian coordinates (X, Y, Z) - right handed :
*
* Y (up)
* |
* |
* +----X
* /
* /
* Z
*
* Spherical coordinates (R, PHI, THETA) :
*
* R is the distance to origin
* PHI is typical azimuth
* THETA is elevation angle (not polar angle!)
*
* X=1 Y=0 Z=0 => PHI=0
* X=0 Y=0 Z=-1 => PHI=PI/2
* X=-1 Y=0 Z=0 => PHI=PI
* X=0 Y=0 Z=1 => PHI=3*PI/2
*
* X=1 Y=0 Z=0 => THETA=0
* X=0 Y=1 Z=0 => THETA=PI/2
* X=0 Y=-1 Z=0 => THETA=-PI/2
*/
namespace paysages {
namespace system {class PackStream;}
}
typedef struct
{
double x;
double y;
double z;
} Vector3;
typedef struct
{
double r;
double theta;
double phi;
} VectorSpherical;
BASICSSHARED_EXPORT extern Vector3 VECTOR_ZERO;
BASICSSHARED_EXPORT extern Vector3 VECTOR_DOWN;
BASICSSHARED_EXPORT extern Vector3 VECTOR_UP;
BASICSSHARED_EXPORT extern Vector3 VECTOR_NORTH;
BASICSSHARED_EXPORT extern Vector3 VECTOR_SOUTH;
BASICSSHARED_EXPORT extern Vector3 VECTOR_EAST;
BASICSSHARED_EXPORT extern Vector3 VECTOR_WEST;
static inline Vector3 v3(double x, double y, double z)
{
Vector3 result = {x, y, z};
return result;
}
BASICSSHARED_EXPORT void v3Save(PackStream* stream, Vector3* v);
BASICSSHARED_EXPORT void v3Load(PackStream* stream, Vector3* v);
BASICSSHARED_EXPORT Vector3 v3Translate(Vector3 v1, double x, double y, double z);
BASICSSHARED_EXPORT Vector3 v3Add(Vector3 v1, Vector3 v2);
BASICSSHARED_EXPORT Vector3 v3Sub(Vector3 v1, Vector3 v2);
BASICSSHARED_EXPORT Vector3 v3Neg(Vector3 v);
BASICSSHARED_EXPORT Vector3 v3Scale(Vector3 v, double scale);
BASICSSHARED_EXPORT double v3Norm(Vector3 v);
BASICSSHARED_EXPORT Vector3 v3Normalize(Vector3 v);
BASICSSHARED_EXPORT double v3Dot(Vector3 v1, Vector3 v2);
BASICSSHARED_EXPORT Vector3 v3Cross(Vector3 v1, Vector3 v2);
BASICSSHARED_EXPORT VectorSpherical v3ToSpherical(Vector3 v);
BASICSSHARED_EXPORT Vector3 v3FromSpherical(VectorSpherical v);
#endif // VECTOR3_H

View file

@ -16,7 +16,9 @@ SOURCES += \
NoiseFunctionNaive.cpp \
NoiseFunctionPerlin.cpp \
NoiseFunctionSimplex.cpp \
Interpolation.cpp
Interpolation.cpp \
SpaceCoordinates.cpp \
Vector3.cpp
HEADERS +=\
basics_global.h \
@ -24,7 +26,9 @@ HEADERS +=\
NoiseFunctionNaive.h \
NoiseFunctionPerlin.h \
NoiseFunctionSimplex.h \
Interpolation.h
Interpolation.h \
SpaceCoordinates.h \
Vector3.h
unix:!symbian {
maemo5 {

View file

@ -3,7 +3,7 @@
/* Shared object helpers */
#include <QtCore/qglobal.h>
#if defined(DEFINITION_LIBRARY)
#if defined(BASICS_LIBRARY)
# define BASICSSHARED_EXPORT Q_DECL_EXPORT
#else
# define BASICSSHARED_EXPORT Q_DECL_IMPORT

View file

@ -6,6 +6,7 @@ SUBDIRS = \
basics \
definition \
rendering \
render/software \
render/opengl \
editing \
controlling

View file

@ -0,0 +1,14 @@
#include "SoftwareRenderer.h"
#include "global/FluidMediumTraversal.h"
SoftwareRenderer::SoftwareRenderer(Scenery* scenery):
scenery(scenery)
{
fluid_medium = new FluidMediumTraversal(this);
}
SoftwareRenderer::~SoftwareRenderer()
{
delete fluid_medium;
}

View file

@ -0,0 +1,35 @@
#ifndef SOFTWARERENDERER_H
#define SOFTWARERENDERER_H
#include "software_global.h"
#include "renderer.h"
class Scenery;
namespace paysages {
namespace software {
class FluidMediumTraversal;
/*!
* \brief This class renders a defined scenery in sotware mode (using only standard CPU computations).
*/
class SOFTWARESHARED_EXPORT SoftwareRenderer: public Renderer
{
public:
SoftwareRenderer(Scenery* scenery);
virtual ~SoftwareRenderer();
inline Scenery* getScenery() const {return scenery;}
private:
Scenery* scenery;
FluidMediumTraversal* fluid_medium;
};
}
}
#endif // SOFTWARERENDERER_H

View file

@ -0,0 +1,6 @@
#include "FluidMediumTraversal.h"
FluidMediumTraversal::FluidMediumTraversal(SoftwareRenderer* renderer):
renderer(renderer)
{
}

View file

@ -0,0 +1,42 @@
#ifndef FLUIDMEDIUMTRAVERSAL_H
#define FLUIDMEDIUMTRAVERSAL_H
#include "software_global.h"
#include <vector>
#include "SpaceCoordinates.h"
namespace paysages {
namespace software {
class SoftwareRenderer;
class FluidMediumCollector;
class FluidMediumInterface;
/*!
* \brief Global object to interact with fluid medium (air, water, clouds...)
*
* This object handles the traversal of fluid medium and the collecting of
* medium density and properties.
* It is mainly used to compute the alteration made by such media on light.
*/
class SOFTWARESHARED_EXPORT FluidMediumTraversal
{
public:
FluidMediumTraversal(SoftwareRenderer *renderer);
void setCollector(FluidMediumCollector *collector);
virtual void getTraversedMedia(std::vector<FluidMediumInterface> &media);
void collectHalfLine(const SpaceCoordinates &start, const Vector3 &direction);
void collectSegment(const SpaceCoordinates &start, const SpaceCoordinates &end);
private:
SoftwareRenderer* renderer;
};
}
}
#endif // FLUIDMEDIUMTRAVERSAL_H

View file

@ -0,0 +1,52 @@
#-------------------------------------------------
#
# Project created by QtCreator 2013-11-09T15:13:23
#
#-------------------------------------------------
QT -= gui
TARGET = software
TEMPLATE = lib
DEFINES += SOFTWARE_LIBRARY
SOURCES += SoftwareRenderer.cpp \
global/FluidMediumTraversal.cpp
HEADERS += SoftwareRenderer.h\
software_global.h \
global/FluidMediumTraversal.h
unix:!symbian {
maemo5 {
target.path = /opt/usr/lib
} else {
target.path = /usr/lib
}
INSTALLS += target
}
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../system/release/ -lpaysages_system
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../system/debug/ -lpaysages_system
else:unix: LIBS += -L$$OUT_PWD/../../system/ -lpaysages_system
INCLUDEPATH += $$PWD/../../system
DEPENDPATH += $$PWD/../../system
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../basics/release/ -lpaysages_basics
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../basics/debug/ -lpaysages_basics
else:unix: LIBS += -L$$OUT_PWD/../../basics/ -lpaysages_basics
INCLUDEPATH += $$PWD/../../basics
DEPENDPATH += $$PWD/../../basics
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../definition/release/ -lpaysages_definition
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../definition/debug/ -lpaysages_definition
else:unix: LIBS += -L$$OUT_PWD/../../definition/ -lpaysages_definition
INCLUDEPATH += $$PWD/../../definition
DEPENDPATH += $$PWD/../../definition
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../rendering/release/ -lpaysages_rendering
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../rendering/debug/ -lpaysages_rendering
else:unix: LIBS += -L$$OUT_PWD/../../rendering/ -lpaysages_rendering
INCLUDEPATH += $$PWD/../../rendering
DEPENDPATH += $$PWD/../../rendering

View file

@ -0,0 +1,22 @@
#ifndef SOFTWARE_GLOBAL_H
#define SOFTWARE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(SOFTWARE_LIBRARY)
# define SOFTWARESHARED_EXPORT Q_DECL_EXPORT
#else
# define SOFTWARESHARED_EXPORT Q_DECL_IMPORT
#endif
namespace paysages {
namespace basics {}
namespace definition {}
namespace software {}
}
using namespace paysages::basics;
using namespace paysages::definition;
using namespace paysages::software;
#endif // SOFTWARE_GLOBAL_H

View file

@ -1,129 +1,8 @@
#include "euclid.h"
#include <math.h>
#include "../tools.h"
#include "PackStream.h"
Vector3 VECTOR_ZERO = {0.0, 0.0, 0.0};
Vector3 VECTOR_DOWN = {0.0, -1.0, 0.0};
Vector3 VECTOR_UP = {0.0, 1.0, 0.0};
Vector3 VECTOR_NORTH = {0.0, 0.0, -1.0};
Vector3 VECTOR_SOUTH = {0.0, 0.0, 1.0};
Vector3 VECTOR_WEST = {-1.0, 0.0, 0.0};
Vector3 VECTOR_EAST = {1.0, 0.0, 0.0};
void v3Save(PackStream* stream, Vector3* v)
{
stream->write(&v->x);
stream->write(&v->y);
stream->write(&v->z);
}
void v3Load(PackStream* stream, Vector3* v)
{
stream->read(&v->x);
stream->read(&v->y);
stream->read(&v->z);
}
Vector3 v3Translate(Vector3 v1, double x, double y, double z)
{
Vector3 result;
result.x = v1.x + x;
result.y = v1.y + y;
result.z = v1.z + z;
return result;
}
Vector3 v3Add(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
Vector3 v3Sub(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
Vector3 v3Neg(Vector3 v)
{
Vector3 result;
result.x = -v.x;
result.y = -v.y;
result.z = -v.z;
return result;
}
Vector3 v3Scale(Vector3 v, double scale)
{
Vector3 result;
result.x = v.x * scale;
result.y = v.y * scale;
result.z = v.z * scale;
return result;
}
double v3Norm(Vector3 v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
Vector3 v3Normalize(Vector3 v)
{
double norm = v3Norm(v);
if (norm == 0.0)
{
return VECTOR_ZERO;
}
else
{
return v3Scale(v, 1.0 / norm);
}
}
double v3Dot(Vector3 v1, Vector3 v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vector3 v3Cross(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.y * v2.z - v1.z * v2.y;
result.y = v1.z * v2.x - v1.x * v2.z;
result.z = v1.x * v2.y - v1.y * v2.x;
return result;
}
VectorSpherical v3ToSpherical(Vector3 v)
{
VectorSpherical result;
result.phi = euclidGet2DAngle(v.x, -v.z);
result.theta = euclidGet2DAngle(sqrt(v.x * v.x + v.z * v.z), v.y);
if (v.y < 0.0)
{
result.theta -= 2.0 * M_PI;
}
result.r = v3Norm(v);
return result;
}
Vector3 v3FromSpherical(VectorSpherical v)
{
Vector3 result = {v.r * cos(v.phi) * cos(v.theta), v.r * sin(v.theta), -v.r * sin(v.phi) * cos(v.theta)};
return result;
}
void m4Save(PackStream* stream, Matrix4* m)
{
stream->write(&m->a);

View file

@ -3,51 +3,13 @@
#include "../rendering_global.h"
// TEMP
#include "Vector3.h"
namespace paysages {
namespace system {class PackStream;}
}
/*
* Cartesian coordinates (X, Y, Z) - right handed :
*
* Y (up)
* |
* |
* +----X
* /
* /
* Z
*
* Spherical coordinates (R, PHI, THETA) :
*
* R is the distance to origin
* PHI is typical azimuth
* THETA is elevation angle (not polar angle!)
*
* X=1 Y=0 Z=0 => PHI=0
* X=0 Y=0 Z=-1 => PHI=PI/2
* X=-1 Y=0 Z=0 => PHI=PI
* X=0 Y=0 Z=1 => PHI=3*PI/2
*
* X=1 Y=0 Z=0 => THETA=0
* X=0 Y=1 Z=0 => THETA=PI/2
* X=0 Y=-1 Z=0 => THETA=-PI/2
*/
typedef struct
{
double x;
double y;
double z;
} Vector3;
typedef struct
{
double r;
double theta;
double phi;
} VectorSpherical;
typedef struct
{
double a;
@ -68,33 +30,6 @@ typedef struct
double p;
} Matrix4;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_ZERO;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_DOWN;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_UP;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_NORTH;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_SOUTH;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_EAST;
RENDERINGSHARED_EXPORT extern Vector3 VECTOR_WEST;
static inline Vector3 v3(double x, double y, double z)
{
Vector3 result = {x, y, z};
return result;
};
RENDERINGSHARED_EXPORT void v3Save(PackStream* stream, Vector3* v);
RENDERINGSHARED_EXPORT void v3Load(PackStream* stream, Vector3* v);
RENDERINGSHARED_EXPORT Vector3 v3Translate(Vector3 v1, double x, double y, double z);
RENDERINGSHARED_EXPORT Vector3 v3Add(Vector3 v1, Vector3 v2);
RENDERINGSHARED_EXPORT Vector3 v3Sub(Vector3 v1, Vector3 v2);
RENDERINGSHARED_EXPORT Vector3 v3Neg(Vector3 v);
RENDERINGSHARED_EXPORT Vector3 v3Scale(Vector3 v, double scale);
RENDERINGSHARED_EXPORT double v3Norm(Vector3 v);
RENDERINGSHARED_EXPORT Vector3 v3Normalize(Vector3 v);
RENDERINGSHARED_EXPORT double v3Dot(Vector3 v1, Vector3 v2);
RENDERINGSHARED_EXPORT Vector3 v3Cross(Vector3 v1, Vector3 v2);
RENDERINGSHARED_EXPORT VectorSpherical v3ToSpherical(Vector3 v);
RENDERINGSHARED_EXPORT Vector3 v3FromSpherical(VectorSpherical v);
RENDERINGSHARED_EXPORT void m4Save(PackStream* stream, Matrix4* m);
RENDERINGSHARED_EXPORT void m4Load(PackStream* stream, Matrix4* m);
RENDERINGSHARED_EXPORT Matrix4 m4NewIdentity();