Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

need to port to ps3

  • Loading branch information...
commit 50704cca665dc1b343d225df02682608b200ee4b 1 parent 2839787
@nkostelnik authored
View
65 assets/scenes/cauldron/cauldronsimple.scene
@@ -0,0 +1,65 @@
+{
+ "backgroundColor": {
+ "r": 1,
+ "g": 1,
+ "b": 1
+ },
+ "terrain": [],
+ "objects": [
+ {
+ "model": "actors/fluid_plane/fluid_plane.modelbinary",
+ "actor" : "Cauldron",
+ "position": {
+ "x": 0.0,
+ "y": -0.1,
+ "z": 0
+ }
+ }
+ ],
+ "lights": [
+ {
+ "type": "directional",
+ "direction": {
+ "x": -1,
+ "y": -1,
+ "z": -1
+ },
+ "color": {
+ "r": 0.3,
+ "g": 0.3,
+ "b": 0.3
+ }
+ },
+ {
+ "type" : "spot",
+ "innerangle" : 10,
+ "outerangle" : 20,
+ "decay" : 0.2,
+ "length" : 30,
+ "intensity" : 1.0,
+ "castsshadows" : false,
+ "position" : {
+ "x" : 0,
+ "y" : 0,
+ "z" : 5
+ },
+ "orientation" : {
+ "x" : 0,
+ "y" : 0,
+ "z" : 0
+ },
+ "color" : {
+ "r" : 1.0,
+ "g" : 1.0,
+ "b" : 1.0
+ },
+ "direction" : {
+ "x" : 0.0,
+ "y" : 0.0,
+ "z" : -1.0
+ }
+ }
+ ]
+}
+
+
View
232 src/engine/common/dynamics/NavierStokesSimulation.cpp
@@ -7,180 +7,93 @@
#include "maths/Vector2.h"
#include "maths/Point.h"
-#define SWAP(a,b) {float * tmp=a;a=b;b=tmp;}
-#define IX(i,j) ((i)+(N+2)*(j))
-#define SWAP(x0,x) {float * tmp=x0;x0=x;x=tmp;}
-#define FOR_EACH_CELL for ( i=1 ; i<=N ; i++ ) { for ( j=1 ; j<=N ; j++ ) {
-#define END_FOR }}
+#include "StamFluidSolver.h"
-void add_source ( int N, float * x, float * s, float dt )
-{
- int i, size=(N+2)*(N+2);
- for ( i=0 ; i<size ; i++ ) x[i] += dt*s[i];
-}
+typedef unsigned int u32;
-void set_bnd ( int N, int b, float * x )
+void NavierStokesSimulation::setGridSize(u32 gridSize)
{
- int i;
+ gridSize_ = gridSize;
- for ( i=1 ; i<=N ; i++ ) {
- x[IX(0 ,i)] = b==1 ? -x[IX(1,i)] : x[IX(1,i)];
- x[IX(N+1,i)] = b==1 ? -x[IX(N,i)] : x[IX(N,i)];
- x[IX(i,0 )] = b==2 ? -x[IX(i,1)] : x[IX(i,1)];
- x[IX(i,N+1)] = b==2 ? -x[IX(i,N)] : x[IX(i,N)];
- }
- x[IX(0 ,0 )] = 0.5f*(x[IX(1,0 )]+x[IX(0 ,1)]);
- x[IX(0 ,N+1)] = 0.5f*(x[IX(1,N+1)]+x[IX(0 ,N)]);
- x[IX(N+1,0 )] = 0.5f*(x[IX(N,0 )]+x[IX(N+1,1)]);
- x[IX(N+1,N+1)] = 0.5f*(x[IX(N,N+1)]+x[IX(N+1,N)]);
-}
+ userDataSize_ = gridSize * gridSize;
-void lin_solve ( int N, int b, float * x, float * x0, float a, float c, int it )
-{
- int i, j, k;
+ userDensity_ = new float[userDataSize_];
+ memset(userDensity_, 0, userDataSize_ * sizeof(float));
- for ( k=0 ; k<it ; k++ ) {
- FOR_EACH_CELL
- x[IX(i,j)] = (x0[IX(i,j)] + a*(x[IX(i-1,j)]+x[IX(i+1,j)]+x[IX(i,j-1)]+x[IX(i,j+1)]))/c;
- END_FOR
- set_bnd ( N, b, x );
- }
-}
+ userVelocityY_ = new float[userDataSize_];
+ memset(userVelocityY_, 0, userDataSize_ * sizeof(float));
-void diffuse ( int N, int b, float * x, float * x0, float diff, float dt, int it )
-{
- float a=dt*diff*N*N;
- lin_solve ( N, b, x, x0, a, 1+4*a, it );
-}
+ userVelocityX_ = new float[userDataSize_];
+ memset(userVelocityX_, 0, userDataSize_ * sizeof(float));
-void advect ( int N, int b, float * d, float * d0, float * u, float * v, float dt )
-{
- int i, j, i0, j0, i1, j1;
- float x, y, s0, t0, s1, t1, dt0;
-
- dt0 = dt*N;
- FOR_EACH_CELL
- x = i-dt0*u[IX(i,j)]; y = j-dt0*v[IX(i,j)];
- if (x<0.5f) x=0.5f; if (x>N+0.5f) x=N+0.5f; i0=(int)x; i1=i0+1;
- if (y<0.5f) y=0.5f; if (y>N+0.5f) y=N+0.5f; j0=(int)y; j1=j0+1;
- s1 = x-i0; s0 = 1-s1; t1 = y-j0; t0 = 1-t1;
- d[IX(i,j)] = s0*(t0*d0[IX(i0,j0)]+t1*d0[IX(i0,j1)])+s1*(t0*d0[IX(i1,j0)]+t1*d0[IX(i1,j1)]);
- END_FOR
-
- set_bnd ( N, b, d );
-}
-void project ( int N, float * u, float * v, float * p, float * div, int it )
-{
- int i, j;
+ dataSize_ = (gridSize + 2) * (gridSize + 2);
+
+ density_ = new float[dataSize_];
+ memset(density_, 0, dataSize_ * sizeof(float));
- FOR_EACH_CELL
- div[IX(i,j)] = -0.5f*(u[IX(i+1,j)]-u[IX(i-1,j)]+v[IX(i,j+1)]-v[IX(i,j-1)])/N;
- p[IX(i,j)] = 0;
- END_FOR
+ lastDensity_ = new float[dataSize_];
+ memset(lastDensity_, 0, dataSize_ * sizeof(float));
- set_bnd ( N, 0, div ); set_bnd ( N, 0, p );
+ userDensity_ = new float[dataSize_];
+ memset(userDensity_, 0, userDataSize_ * sizeof(float));
- lin_solve ( N, 0, p, div, 1, 4, it );
- FOR_EACH_CELL
- u[IX(i,j)] -= 0.5f*N*(p[IX(i+1,j)]-p[IX(i-1,j)]);
- v[IX(i,j)] -= 0.5f*N*(p[IX(i,j+1)]-p[IX(i,j-1)]);
- END_FOR
+ velocityX_ = new float[dataSize_];
+ memset(velocityX_, 0, dataSize_ * sizeof(float));
- set_bnd ( N, 1, u ); set_bnd ( N, 2, v );
-}
+ lastVelocityX_ = new float[dataSize_];
+ memset(lastVelocityX_, 0, dataSize_ * sizeof(float));
-void dens_step ( int N, float * x, float * x0, float * u, float * v, float diff, float dt, int it )
-{
- add_source ( N, x, x0, dt );
- SWAP ( x0, x ); diffuse ( N, 0, x, x0, diff, dt, it );
- SWAP ( x0, x ); advect ( N, 0, x, x0, u, v, dt );
+
+ velocityY_ = new float[dataSize_];
+ memset(velocityY_, 0, dataSize_ * sizeof(float));
+
+ lastVelocityY_ = new float[dataSize_];
+ memset(lastVelocityY_, 0, dataSize_ * sizeof(float));
}
-void vel_step ( int N, float * u, float * v, float * u0, float * v0, float visc, float dt, int it )
+void NavierStokesSimulation::update(float dt)
{
- add_source ( N, u, u0, dt ); add_source ( N, v, v0, dt );
- SWAP ( u0, u ); diffuse ( N, 1, u, u0, visc, dt, it );
- SWAP ( v0, v ); diffuse ( N, 2, v, v0, visc, dt, it );
- project ( N, u, v, u0, v0, it );
- SWAP ( u0, u ); SWAP ( v0, v );
- advect ( N, 1, u, u0, u0, v0, dt );
- advect ( N, 2, v, v0, u0, v0, dt );
- project ( N, u, v, u0, v0, it );
-}
+ int N = gridSize_;
+
+ float visc = viscosity_;
+ float diff = diffuseRate_;
+ int it = solveIterations_;
-void NavierStokesSimulation::update(float dt) {
- int N = gridSize_.width;
float* u = velocityX_;
- float* v = velocityY_;
float* u_prev = lastVelocityX_;
+
+ float* v = velocityY_;
float* v_prev = lastVelocityY_;
- float visc = viscosity_;
- float diff = diffuseRate_;
+
float* dens = density_;
float* dens_prev = lastDensity_;
- int it = solverIterations_;
-
- vel_step ( N, u, v, u_prev, v_prev, visc, dt, it );
- dens_step ( N, dens, dens_prev, u, v, diff, dt, it );
+ vel_step(N, u, v, u_prev, v_prev, visc, dt, it);
+ dens_step(N, dens, dens_prev, u, v, diff, dt, it);
- ClearMemory(u_prev, dataSize_);
- ClearMemory(v_prev, dataSize_);
- ClearMemory(dens_prev, dataSize_);
+ memset(u_prev, 0, dataSize_ * sizeof(float));
+ memset(v_prev, 0, dataSize_ * sizeof(float));
+ memset(dens_prev, 0, dataSize_ * sizeof(float));
}
-void NavierStokesSimulation::setGridSize(const CSize& gridSize) {
- gridSize_ = gridSize;
-
- userDataSize_ = gridSize.width * gridSize.height * sizeof(float);
-
- SAFE_DELETE_ARRAY(userDensity_);
- userDensity_ = new float[userDataSize_];
- ClearMemory(userDensity_, userDataSize_);
-
- dataSize_ = (gridSize.width + 2) * (gridSize.height + 2) * sizeof(float);
-
-
- SAFE_DELETE_ARRAY(density_);
- density_ = new float[dataSize_];
- ClearMemory(density_, dataSize_);
-
- SAFE_DELETE_ARRAY(lastDensity_);
- lastDensity_ = new float[dataSize_];
- ClearMemory(lastDensity_, dataSize_);
-
- SAFE_DELETE_ARRAY(userDensity_);
- userDensity_ = new float[userDataSize_];
- ClearMemory(userDensity_, userDataSize_);
-
-
- SAFE_DELETE_ARRAY(velocityX_);
- velocityX_ = new float[dataSize_];
- ClearMemory(velocityX_, dataSize_);
-
- SAFE_DELETE_ARRAY(lastVelocityX_);
- lastVelocityX_ = new float[dataSize_];
- ClearMemory(lastVelocityX_, dataSize_);
-
- SAFE_DELETE_ARRAY(userVelocityX_);
- userVelocityX_ = new float[dataSize_];
- ClearMemory(userVelocityX_, dataSize_);
-
-
- SAFE_DELETE_ARRAY(velocityY_);
- velocityY_ = new float[dataSize_];
- ClearMemory(velocityY_, dataSize_);
-
- SAFE_DELETE_ARRAY(lastVelocityY_);
- lastVelocityY_ = new float[dataSize_];
- ClearMemory(lastVelocityY_, dataSize_);
-
- SAFE_DELETE_ARRAY(userVelocityY_);
- userVelocityY_ = new float[dataSize_];
- ClearMemory(userVelocityY_, dataSize_);
+void NavierStokesSimulation::copyUserArray(float* in, float* out) {
+ memset(out, 0, userDataSize_ * sizeof(float));
+ unsigned int userOffset = 0;
+ unsigned int offset = 2 + gridSize_ + 1;
+ for (int y = 0; y < gridSize_; y++)
+ {
+ memcpy(userOffset + out, offset + in, gridSize_ * sizeof(float));
+ offset += gridSize_ + 2;
+ userOffset += gridSize_;
+ }
+}
+const float* NavierStokesSimulation::density()
+{
+ copyUserArray(density_, userDensity_);
+ return userDensity_;
}
void NavierStokesSimulation::addDensity(const Point& location, float densityAmount) {
@@ -189,34 +102,7 @@ void NavierStokesSimulation::addDensity(const Point& location, float densityAmou
}
void NavierStokesSimulation::setVelocity(const Point& gridPosition, const Vector2& velocity) {
- unsigned int dataIndex = gridIndex(gridPosition.x + 1, gridPosition.y + 1);
+ u32 dataIndex = gridIndex(gridPosition.x + 1, gridPosition.y + 1);
lastVelocityX_[dataIndex] = velocity.x;
lastVelocityY_[dataIndex] = velocity.y;
}
-
-const float* NavierStokesSimulation::density() {
- copyUserArray(density_, userDensity_);
- return userDensity_;
-}
-
-const float* NavierStokesSimulation::velocityX() {
- copyUserArray(velocityX_, userVelocityX_);
- return userVelocityX_;
-}
-
-const float* NavierStokesSimulation::velocityY() {
- copyUserArray(velocityY_, userVelocityY_);
- return userVelocityY_;
-}
-
-void NavierStokesSimulation::copyUserArray(float* in, float* out) {
- ClearMemory(out, userDataSize_);
- unsigned int userOffset = 0;
- unsigned int offset = 2 + gridSize_.width + 1;
- for (int y = 0; y < gridSize_.height; y++) {
- memcpy(userOffset + out, offset + in, gridSize_.width * sizeof(float));
- offset += gridSize_.width + 2;
- userOffset += gridSize_.width;
- }
-}
-
View
10 src/engine/common/dynamics/NavierStokesSimulation.h
@@ -11,7 +11,7 @@ class NavierStokesSimulation {
public:
NavierStokesSimulation()
- : solverIterations_(10)
+ : solveIterations_(10)
, dataSize_(0)
, userDataSize_(0)
, diffuseRate_(1)
@@ -33,7 +33,7 @@ class NavierStokesSimulation {
public:
- void setGridSize(const CSize& gridSize);
+ void setGridSize(unsigned int gridSize);
public:
@@ -67,8 +67,8 @@ class NavierStokesSimulation {
private:
- CSize gridSize_;
- unsigned int solverIterations_;
+ int gridSize_;
+ unsigned int solveIterations_;
unsigned int dataSize_;
unsigned int userDataSize_;
@@ -95,7 +95,7 @@ class NavierStokesSimulation {
};
inline unsigned int NavierStokesSimulation::gridIndex(unsigned int x, unsigned int y) {
- return y * (gridSize_.width + 2) + x;
+ return y * (gridSize_ + 2) + x;
}
inline void NavierStokesSimulation::setDiffuseRate(float diffuseRate) {
View
100 src/engine/common/dynamics/StamFluidSolver.cpp
@@ -0,0 +1,100 @@
+#include "StamFluidSolver.h"
+
+#define IX(i,j) ((i)+(N+2)*(j))
+#define SWAP(x0,x) {float* tmp=x0;x0=x;x=tmp;}
+
+#define FOR_EACH_CELL for (int i=1 ; i<=N ; i++ ) { for (int j=1 ; j<=N ; j++ ) {
+#define END_FOR }}
+
+void add_source( int N, float * x, float * s, float dt )
+{
+ int i, size=(N+2)*(N+2);
+ for (i=0 ; i<size ; i++) x[i] += dt*s[i];
+}
+
+void set_bnd( int N, int b, float * x )
+{
+ for (int i=1 ; i<=N ; i++ )
+ {
+ x[IX(0 ,i)] = b==1 ? -x[IX(1,i)] : x[IX(1,i)];
+ x[IX(N+1,i)] = b==1 ? -x[IX(N,i)] : x[IX(N,i)];
+ x[IX(i,0 )] = b==2 ? -x[IX(i,1)] : x[IX(i,1)];
+ x[IX(i,N+1)] = b==2 ? -x[IX(i,N)] : x[IX(i,N)];
+ }
+ x[IX(0 ,0 )] = 0.5f*(x[IX(1,0 )]+x[IX(0 ,1)]);
+ x[IX(0 ,N+1)] = 0.5f*(x[IX(1,N+1)]+x[IX(0 ,N)]);
+ x[IX(N+1,0 )] = 0.5f*(x[IX(N,0 )]+x[IX(N+1,1)]);
+ x[IX(N+1,N+1)] = 0.5f*(x[IX(N,N+1)]+x[IX(N+1,N)]);
+}
+
+void lin_solve( int N, int b, float * x, float * x0, float a, float c, int it )
+{
+ for (int k=0 ; k<it ; k++ )
+ {
+ FOR_EACH_CELL
+ x[IX(i,j)] = (x0[IX(i,j)] + a*(x[IX(i-1,j)]+x[IX(i+1,j)]+x[IX(i,j-1)]+x[IX(i,j+1)]))/c;
+ END_FOR
+ set_bnd ( N, b, x );
+ }
+}
+
+void diffuse( int N, int b, float * x, float * x0, float diff, float dt, int it )
+{
+ float a=dt*diff*N*N;
+ lin_solve ( N, b, x, x0, a, 1+4*a, it );
+}
+
+void advect (int N, int b, float * d, float * d0, float * u, float * v, float dt)
+{
+ int i0, j0, i1, j1;
+ float x, y, s0, t0, s1, t1, dt0;
+
+ dt0 = dt*N;
+ FOR_EACH_CELL
+ x = i-dt0*u[IX(i,j)]; y = j-dt0*v[IX(i,j)];
+ if (x<0.5f) x=0.5f; if (x>N+0.5f) x=N+0.5f; i0=(int)x; i1=i0+1;
+ if (y<0.5f) y=0.5f; if (y>N+0.5f) y=N+0.5f; j0=(int)y; j1=j0+1;
+ s1 = x-i0; s0 = 1-s1; t1 = y-j0; t0 = 1-t1;
+ d[IX(i,j)] = s0*(t0*d0[IX(i0,j0)]+t1*d0[IX(i0,j1)])+s1*(t0*d0[IX(i1,j0)]+t1*d0[IX(i1,j1)]);
+ END_FOR
+
+ set_bnd ( N, b, d );
+}
+
+void project(int N, float * u, float * v, float * p, float * div, int it)
+{
+ FOR_EACH_CELL
+ div[IX(i,j)] = -0.5f*(u[IX(i+1,j)]-u[IX(i-1,j)]+v[IX(i,j+1)]-v[IX(i,j-1)])/N;
+ p[IX(i,j)] = 0;
+ END_FOR
+
+ set_bnd ( N, 0, div ); set_bnd ( N, 0, p );
+
+ lin_solve ( N, 0, p, div, 1, 4, it );
+
+ FOR_EACH_CELL
+ u[IX(i,j)] -= 0.5f*N*(p[IX(i+1,j)]-p[IX(i-1,j)]);
+ v[IX(i,j)] -= 0.5f*N*(p[IX(i,j+1)]-p[IX(i,j-1)]);
+ END_FOR
+
+ set_bnd ( N, 1, u ); set_bnd ( N, 2, v );
+}
+
+void dens_step(int N, float * x, float * x0, float * u, float * v, float diff, float dt, int it)
+{
+ add_source (N, x, x0, dt);
+ SWAP (x0, x); diffuse (N, 0, x, x0, diff, dt, it);
+ SWAP (x0, x); advect (N, 0, x, x0, u, v, dt);
+}
+
+void vel_step(int N, float * u, float * v, float * u0, float * v0, float visc, float dt, int it)
+{
+ add_source (N, u, u0, dt); add_source (N, v, v0, dt);
+ SWAP (u0, u); diffuse (N, 1, u, u0, visc, dt, it);
+ SWAP (v0, v); diffuse (N, 2, v, v0, visc, dt, it);
+ project (N, u, v, u0, v0, it);
+ SWAP (u0, u); SWAP (v0, v);
+ advect (N, 1, u, u0, u0, v0, dt);
+ advect (N, 2, v, v0, u0, v0, dt);
+ project (N, u, v, u0, v0, it);
+}
View
15 src/engine/common/dynamics/StamFluidSolver.h
@@ -0,0 +1,15 @@
+#ifndef STAM_FLUID_SOLVER_H
+#define STAM_FLUID_SOLVER_H
+
+void add_source (int N, float * x, float * s, float dt);
+void set_bnd (int N, int b, float * x );
+void lin_solve (int N, int b, float * x, float * x0, float a, float c, int it);
+
+void diffuse (int N, int b, float * x, float * x0, float diff, float dt, int it);
+void advect (int N, int b, float * d, float * d0, float * u, float * v, float dt);
+void project (int N, float * u, float * v, float * p, float * div, int it);
+
+void dens_step (int N, float * x, float * x0, float * u, float * v, float diff, float dt, int it);
+void vel_step (int N, float * u, float * v, float * u0, float * v0, float visc, float dt, int it);
+
+#endif
View
145 src/game/Game.cpp
@@ -1,72 +1,73 @@
-#include "Game.h"
-
-#include "renderer/GraphicsInterface.h"
-#include "maths/Angles.h"
-
-#include "renderer/WorldLoader.h"
-
-#include "input/Pad.h"
-#include "input/Mouse.h"
-#include "input/Keyboard.h"
-
-#include "maths/Plane.h"
-
-#include "entity/ActorRegistry.h"
-
-void Game::init(const char* sceneFile) {
- Pad::init();
- Mouse::init();
- Keyboard::init();
-
- ActorRegistry::registerActors(actorFactory_);
-
- renderer_.init(GraphicsInterface::backBufferSize());
- ui_.init(&renderer_);
-
- clock_.init();
-
- camera_.translateZ(2.0f);
- camera_.translateY(5.3f);
- camera_.rotateX(-1.0f);
- //camera_.rotateX(toRadians(-90));
- camera_.setIsPlayerControlled(false);
-
- camera_.setProjection(45.0f, GraphicsInterface::aspectRatio(), 1.0f, 500);
-
- if (sceneFile) {
- WorldLoader loader;
- loader.loadFromSceneFile(sceneFile, world_, sceneContext_, actorFactory_);
- }
-
-
- Keyboard::setKeydownListener(this);
-}
-
-void Game::mainLoop() {
- float dt = clock_.delta_time();
- camera_.update(dt);
- Pad::update();
- Keyboard::update();
- ui_.update(dt);
-
- world_.update(dt);
-
- renderer_.render(&camera_, world_, sceneContext_);
-
- world_.debugRender();
-
- ui_.render();
-}
-
-void Game::keyUp(int keyCode) {
- if (keyCode > 47 && keyCode < 59) {
- int renderTargetgId = keyCode - 49;// + 5;
- renderer_.presentRenderTarget(renderTargetgId);
- }
-}
-
-void Game::destroy() {
- ui_.destroy();
- renderer_.destroy();
- world_.destroy();
-}
+#include "Game.h"
+
+#include "renderer/GraphicsInterface.h"
+#include "maths/Angles.h"
+
+#include "renderer/WorldLoader.h"
+
+#include "input/Pad.h"
+#include "input/Mouse.h"
+#include "input/Keyboard.h"
+
+#include "maths/Plane.h"
+
+#include "entity/ActorRegistry.h"
+
+void Game::init(const char* sceneFile) {
+ Pad::init();
+ Mouse::init();
+ Keyboard::init();
+
+ ActorRegistry::registerActors(actorFactory_);
+
+ renderer_.init(GraphicsInterface::backBufferSize());
+ ui_.init(&renderer_);
+
+ clock_.init();
+
+// camera_.translateZ(2.0f);
+// camera_.translateY(5.3f);
+// camera_.rotateX(-1.0f);
+ camera_.translateZ(4.0f);
+ //camera_.rotateX(toRadians(-90));
+ camera_.setIsPlayerControlled(false);
+
+ camera_.setProjection(45.0f, GraphicsInterface::aspectRatio(), 1.0f, 500);
+
+ if (sceneFile) {
+ WorldLoader loader;
+ loader.loadFromSceneFile(sceneFile, world_, sceneContext_, actorFactory_);
+ }
+
+
+ Keyboard::setKeydownListener(this);
+}
+
+void Game::mainLoop() {
+ float dt = clock_.delta_time();
+ camera_.update(dt);
+ Pad::update();
+ Keyboard::update();
+ ui_.update(dt);
+
+ world_.update(dt);
+
+ renderer_.render(&camera_, world_, sceneContext_);
+
+ world_.debugRender();
+
+ ui_.render();
+}
+
+void Game::keyUp(int keyCode) {
+ if (keyCode > 47 && keyCode < 59) {
+ int renderTargetgId = keyCode - 49;// + 5;
+ renderer_.presentRenderTarget(renderTargetgId);
+ }
+}
+
+void Game::destroy() {
+ ui_.destroy();
+ renderer_.destroy();
+ world_.destroy();
+}
View
111 src/game/entity/Cauldron.cpp
@@ -1,3 +1,5 @@
+typedef unsigned int u32;
+
#include "Cauldron.h"
#include "renderer/GraphicsInterface.h"
@@ -17,6 +19,7 @@
#include "core/Size.h"
#include <iostream>
+#include <math.h>
#include "input/Mouse.h"
@@ -25,31 +28,37 @@ void Cauldron::init() {
velocityEffect_ = EffectCache::instance()->loadEffect("shaders/compiled/line.shader");
lineVBO_ = Geometry::line();
- velocityTextureSize_ = CSize(256);
+ int size = 64;
+
+ velocityTextureSize_ = CSize(size);
model_->setMaterialCallback("fluid_material", LiquidMaterialUpdate, this);
- textureSize_ = CSize(128);
+ textureSize_ = CSize(size);
int textureSquare = textureSize_.square();
- colorDataSize_ = textureSquare * IGraphicsInterface::R32G32B32A32_SIZE;
- colorData_ = new float[colorDataSize_];
+ colorDataSize_ = textureSquare; // uint 1 for 1
+ colorData_ = new unsigned int [colorDataSize_];
ClearMemory(colorData_, colorDataSize_);
- densityTextureId_ = GraphicsInterface::createTexture(textureSize_, IGraphicsInterface::R32G32B32A32, 1, 1, colorData_, textureSize_.width * IGraphicsInterface::R32G32B32A32_SIZE, true);
+ densityTextureId_ = GraphicsInterface::createTexture(textureSize_, IGraphicsInterface::R8G8B8A8, 1, 1, colorData_, textureSize_.width * IGraphicsInterface::R8G8B8A8_SIZE, true);
velocityTextureId_ = GraphicsInterface::createTexture(velocityTextureSize_, IGraphicsInterface::R8G8B8A8);
velocityRenderTarget_ = GraphicsInterface::createRenderTarget(velocityTextureId_);
- solver_.setGridSize(textureSize_);
+ solver_.setGridSize(size);
solver_.setDiffuseRate(0.0f);
}
+inline float clamp(float x, float a, float b) {
+ return x < a ? a : (x > b ? b : x);
+}
+
void Cauldron::update(float dt) {
if (Mouse::isLeftButtonDown()) {
- solver_.setVelocity(Point(0, 0), Vector2(1000, 1000));
- solver_.addDensity(Point(0, 0), 10);
+ solver_.setVelocity(Point(1, 1), Vector2(50000, 50000));
+ solver_.addDensity(Point(1, 1), 100);
}
solver_.update(dt);
@@ -57,24 +66,21 @@ void Cauldron::update(float dt) {
{
const float* densityData = solver_.density();
- CSize solverGridSize(textureSize_.width + 2, textureSize_.height + 2);
- unsigned int textureSizeSquare = solverGridSize.width * solverGridSize.height;
+ for (unsigned int i = 0; i < colorDataSize_; i++) {
+ float density = densityData[i];
+ density = clamp(density, 0, 1);
- unsigned int densityIndex = 0;
+ u32 rgba = (u32)(255 * density);
- for (unsigned int i = 0; i < colorDataSize_; i+=4) {
- float density = densityData[densityIndex++];
-
- float a = density;
- float rgb = 1.0f * density;
-
- colorData_[i] = rgb;
- colorData_[i+1] = rgb;
- colorData_[i+2] = rgb;
- colorData_[i+3] = a;
+ u32 a = rgba << 24;
+ u32 r = rgba << 16;
+ u32 g = rgba << 8;
+ u32 b = rgba << 0;
+ u32 color = r + g + b + a;
+ colorData_[i] = color;
}
- GraphicsInterface::setTextureData(densityTextureId_, colorData_, textureSize_, textureSize_.width * IGraphicsInterface::R32G32B32A32_SIZE);
+ GraphicsInterface::setTextureData(densityTextureId_, colorData_, textureSize_, textureSize_.width * IGraphicsInterface::R8G8B8A8_SIZE);
}
}
@@ -85,64 +91,5 @@ void Cauldron::LiquidMaterialUpdate(Material* material, IEffect* effect, void* u
}
void Cauldron::debugRender() {
- if (drawVelocity_) {
- const float* velocityXData = solver_.velocityX();
- const float* velocityYData = solver_.velocityY();
-
- unsigned int textureSquare = textureSize_.square();
-
- GraphicsInterface::setRenderTarget(velocityRenderTarget_, false);
- GraphicsInterface::clearActiveColorBuffers(Color4::CORNFLOWERBLUE);
- GraphicsInterface::setRenderState(true);
- GraphicsInterface::setBlendState(IGraphicsInterface::NOBLEND);
- GraphicsInterface::setViewport(velocityTextureSize_);
-
- //Matrix4x4 projection = Matrix4x4::perspective(45.0f, 1.57f, 1.0f, 10.0f);
- Matrix4x4 projection = Matrix4x4::orthographic(0.0f, (float)velocityTextureSize_.width, 0.0f, (float)velocityTextureSize_.height, -100.0f, 100.0f);
-
- float xOffsetUnProj = 1.0f / textureSize_.width;
- float xOffset = (xOffsetUnProj * velocityTextureSize_.width) / 2.0f;
-
- float yOffsetUnProj = 1.0f / textureSize_.height;
- float yOffset = (yOffsetUnProj * velocityTextureSize_.height) / 2.0f;
-
- for (unsigned int i = 0; i < textureSquare; i++) {
- float yPositionProj = (float)(i / textureSize_.height);
- float yPositonUnProj = yPositionProj / textureSize_.height;
- float yPosition = yPositonUnProj * velocityTextureSize_.height + yOffset;
-
- float xPositonProj = i - yPositionProj * textureSize_.width;
- float xPositonUnProj = xPositonProj / textureSize_.width;
- float xPositon = xPositonUnProj * velocityTextureSize_.width + xOffset;
-
- float velocityX = velocityXData[i] * 1000;
- float velocityY = velocityYData[i] * 1000;
- float velocityZ = 1.0f;
-
- Vector3 velocity(velocityX, velocityY, velocityZ);
- velocity.normalizeIP();
-
- Vector3 line(0, 0, 1);
-
- Vector3 axis = line.cross(velocity);
- axis.normalizeIP();
-
- float lineDotVelocity = line.dot(velocity);
- float angle = acos(lineDotVelocity);
-
- Matrix4x4 rotation = Matrix4x4::rotation(axis, angle);
- Matrix4x4 translation = Matrix4x4::translation(Vector4(xPositon, yPosition, 0, 0));
- Matrix4x4 scale = Matrix4x4::scale(Vector4(10, 10, 10, 1));
- Matrix4x4 model = translation * rotation * scale;
-
- Matrix4x4 modelProjection = projection * model;
- velocityEffect_->setUniform(modelProjection, "ModelViewProjection");
-
- velocityEffect_->beginDraw();
- GraphicsInterface::drawVertexBuffer(lineVBO_, Geometry::LINE_VERTEX_COUNT, Geometry::LINE_VERTEX_FORMAT);
- velocityEffect_->endDraw();
- }
-
- GraphicsInterface::setViewport(GraphicsInterface::backBufferSize());
- }
+
}
View
2  src/game/entity/Cauldron.h
@@ -49,7 +49,7 @@ class Cauldron : public IActor {
unsigned int densityTextureId_;
unsigned int velocityTextureId_;
- float* colorData_;
+ unsigned int * colorData_;
unsigned int colorDataSize_;
CSize textureSize_;
Please sign in to comment.
Something went wrong with that request. Please try again.