Skip to content

Commit

Permalink
refacor simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
Su Tongkui committed Feb 3, 2018
0 parents commit 520d0c2
Show file tree
Hide file tree
Showing 43 changed files with 8,579 additions and 0 deletions.
Binary file added .vs/simulator/v14/.suo
Binary file not shown.
15 changes: 15 additions & 0 deletions Cloth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "VAOMesh.h"

enum cloth_type { SINGLE_LAYER_NOB, SINGLE_LAYER_BOUNDARY };
class Cloth : public VAOMesh
{
public:
Cloth(cloth_type type = SINGLE_LAYER_BOUNDARY) : _type(type) { }
cloth_type get_obj_type() const { return _type; }
private:
cloth_type _type;
};


20 changes: 20 additions & 0 deletions Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <vector>

#include <glm\glm.hpp>

typedef char sint8;
typedef short sint16;
typedef int sint32;
typedef long long sint64;

typedef sint8 uint8;
typedef sint16 uint16;
typedef sint32 uint32;
typedef sint64 uint64;

typedef std::vector<glm::vec4> Vec4s;
typedef std::vector<glm::vec3> Vec3s;
typedef std::vector<glm::vec2> Vec2s;

119 changes: 119 additions & 0 deletions GLSLShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//A simple class for handling GLSL shader compilation
//Author: Movania Muhammad Mobeen
//Last Modified: February 2, 2011
#include "GLSLShader.h"
#include <iostream>

GLSLShader::GLSLShader(void)
{
_totalShaders=0;
_shaders[VERTEX_SHADER]=0;
_shaders[FRAGMENT_SHADER]=0;
_shaders[GEOMETRY_SHADER]=0;
_attributeList.clear();
_uniformLocationList.clear();
}

GLSLShader::~GLSLShader(void)
{
_attributeList.clear();
_uniformLocationList.clear();
}

void GLSLShader::LoadFromString(GLenum type, const string& source) {
unsigned int shader = glCreateShader (type);

const char * ptmp = source.c_str();
glShaderSource (shader, 1, &ptmp, NULL);

//check whether the shader loads fine
GLint status;
glCompileShader (shader);
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint infoLogLength;
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *infoLog= new GLchar[infoLogLength];
glGetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
cerr<<"Compile log: "<<infoLog<<endl;
delete [] infoLog;
}
_shaders[_totalShaders++]=shader;
}


void GLSLShader::CreateAndLinkProgram() {
_program = glCreateProgram ();
if (_shaders[VERTEX_SHADER] != 0) {
glAttachShader (_program, _shaders[VERTEX_SHADER]);
}
if (_shaders[FRAGMENT_SHADER] != 0) {
glAttachShader (_program, _shaders[FRAGMENT_SHADER]);
}
if (_shaders[GEOMETRY_SHADER] != 0) {
glAttachShader (_program, _shaders[GEOMETRY_SHADER]);
}

//link and check whether the program links fine
GLint status;
glLinkProgram (_program);
glGetProgramiv (_program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
GLint infoLogLength;

glGetProgramiv (_program, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *infoLog= new GLchar[infoLogLength];
glGetProgramInfoLog (_program, infoLogLength, NULL, infoLog);
cerr<<"Link log: "<<infoLog<<endl;
delete [] infoLog;
}

glDeleteShader(_shaders[VERTEX_SHADER]);
glDeleteShader(_shaders[FRAGMENT_SHADER]);
glDeleteShader(_shaders[GEOMETRY_SHADER]);
}

void GLSLShader::Use() {
glUseProgram(_program);
}

void GLSLShader::UnUse() {
glUseProgram(0);
}

void GLSLShader::AddAttribute(const string& attribute) {
_attributeList[attribute]= glGetAttribLocation(_program, attribute.c_str());
}

//An indexer that returns the location of the attribute
unsigned int GLSLShader::operator [](const string& attribute) {
return _attributeList[attribute];
}

void GLSLShader::AddUniform(const string& uniform) {
_uniformLocationList[uniform] = glGetUniformLocation(_program, uniform.c_str());
}

unsigned int GLSLShader::operator()(const string& uniform){
return _uniformLocationList[uniform];
}
unsigned int GLSLShader::GetProgram() const {
return _program;
}
#include <fstream>
void GLSLShader::LoadFromFile(GLenum whichShader, const string& filename){
ifstream fp;
fp.open(filename.c_str(), ios_base::in);
if(fp) {
/*string line, buffer;
while(getline(fp, line)) {
buffer.append(line);
buffer.append("\r\n");
} */
string buffer(std::istreambuf_iterator<char>(fp), (std::istreambuf_iterator<char>()));
//copy to source
LoadFromString(whichShader, buffer);
} else {
cerr<<"Error loading shader: "<<filename<<endl;
}
}
41 changes: 41 additions & 0 deletions GLSLShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//A simple class for handling GLSL shader compilation
//Auhtor: Movania Muhammad Mobeen
#pragma once
//#define GLEW_STATIC
#include <map>
#include <string>

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>


using namespace std;


class GLSLShader
{
public:
GLSLShader(void);
~GLSLShader(void);
void LoadFromString(GLenum whichShader, const string& source);
void LoadFromFile(GLenum whichShader, const string& filename);
void CreateAndLinkProgram();
void Use();
void UnUse();
void AddAttribute(const string& attribute);
void AddUniform(const string& uniform);
unsigned int GetProgram() const;
//An indexer that returns the location of the attribute/uniform
unsigned int operator[](const string& attribute);
unsigned int operator()(const string& uniform);
//Program deletion
void DeleteProgram() { glDeleteProgram(_program); _program = -1; }
private:
enum ShaderType { VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER };
unsigned int _program;
int _totalShaders;
unsigned int _shaders[3];//0->vertexshader, 1->fragmentshader, 2->geometryshader
map<string, unsigned int> _attributeList;
map<string, unsigned int> _uniformLocationList;
};
189 changes: 189 additions & 0 deletions KinectJointFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//--------------------------------------------------------------------------------------
// KinectJointFilter.cpp
//
// This file contains Holt Double Exponential Smoothing filter for filtering Joints
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

//#include "stdafx.h"
#include "KinectJointFilter.h"

using namespace Sample;
using namespace DirectX;

//-------------------------------------------------------------------------------------
// Name: Lerp()
// Desc: Linear interpolation between two floats
//-------------------------------------------------------------------------------------
inline FLOAT Lerp( FLOAT f1, FLOAT f2, FLOAT fBlend )
{
return f1 + ( f2 - f1 ) * fBlend;
}

//--------------------------------------------------------------------------------------
// if joint is 0 it is not valid.
//--------------------------------------------------------------------------------------
inline BOOL JointPositionIsValid( XMVECTOR vJointPosition )
{
return ( XMVectorGetX( vJointPosition ) != 0.0f ||
XMVectorGetY( vJointPosition ) != 0.0f ||
XMVectorGetZ( vJointPosition ) != 0.0f );
}

//--------------------------------------------------------------------------------------
// Implementation of a Holt Double Exponential Smoothing filter. The double exponential
// smooths the curve and predicts. There is also noise jitter removal. And maximum
// prediction bounds. The paramaters are commented in the init function.
//--------------------------------------------------------------------------------------
void FilterDoubleExponential::Update( IBody *const pBody )
{
assert( pBody );

// Check for divide by zero. Use an epsilon of a 10th of a millimeter
m_fJitterRadius = XMMax( 0.0001f, m_fJitterRadius );

TRANSFORM_SMOOTH_PARAMETERS SmoothingParams;

UINT jointCapacity = 0;
Joint joints[JointType_Count];

pBody->GetJoints( jointCapacity, joints );
for( INT i = 0; i < JointType_Count; i++ )
{
SmoothingParams.fSmoothing = m_fSmoothing;
SmoothingParams.fCorrection = m_fCorrection;
SmoothingParams.fPrediction = m_fPrediction;
SmoothingParams.fJitterRadius = m_fJitterRadius;
SmoothingParams.fMaxDeviationRadius = m_fMaxDeviationRadius;

// If inferred, we smooth a bit more by using a bigger jitter radius
Joint joint = joints[i];
if( joint.TrackingState == TrackingState::TrackingState_Inferred )
{
SmoothingParams.fJitterRadius *= 2.0f;
SmoothingParams.fMaxDeviationRadius *= 2.0f;
}

Update( joints, i, SmoothingParams );
}
}

void FilterDoubleExponential::Update( Joint joints[] )
{
// Check for divide by zero. Use an epsilon of a 10th of a millimeter
m_fJitterRadius = XMMax( 0.0001f, m_fJitterRadius );

TRANSFORM_SMOOTH_PARAMETERS SmoothingParams;
for( INT i = 0; i < JointType_Count; i++ )
{
SmoothingParams.fSmoothing = m_fSmoothing;
SmoothingParams.fCorrection = m_fCorrection;
SmoothingParams.fPrediction = m_fPrediction;
SmoothingParams.fJitterRadius = m_fJitterRadius;
SmoothingParams.fMaxDeviationRadius = m_fMaxDeviationRadius;

// If inferred, we smooth a bit more by using a bigger jitter radius
Joint joint = joints[i];
if( joint.TrackingState == TrackingState::TrackingState_Inferred )
{
SmoothingParams.fJitterRadius *= 2.0f;
SmoothingParams.fMaxDeviationRadius *= 2.0f;
}

Update( joints, i, SmoothingParams );
}

}

void FilterDoubleExponential::Update( Joint joints[], UINT JointID, TRANSFORM_SMOOTH_PARAMETERS smoothingParams )
{
XMVECTOR vPrevRawPosition;
XMVECTOR vPrevFilteredPosition;
XMVECTOR vPrevTrend;
XMVECTOR vRawPosition;
XMVECTOR vFilteredPosition;
XMVECTOR vPredictedPosition;
XMVECTOR vDiff;
XMVECTOR vTrend;
XMVECTOR vLength;
FLOAT fDiff;
BOOL bJointIsValid;

const Joint joint = joints[JointID];

vRawPosition = XMVectorSet( joint.Position.X, joint.Position.Y, joint.Position.Z, 0.0f );
vPrevFilteredPosition = m_pHistory[JointID].m_vFilteredPosition;
vPrevTrend = m_pHistory[JointID].m_vTrend;
vPrevRawPosition = m_pHistory[JointID].m_vRawPosition;
bJointIsValid = JointPositionIsValid( vRawPosition );

// If joint is invalid, reset the filter
if( !bJointIsValid )
{
m_pHistory[JointID].m_dwFrameCount = 0;
}

// Initial start values
if( m_pHistory[JointID].m_dwFrameCount == 0 )
{
vFilteredPosition = vRawPosition;
vTrend = XMVectorZero();
m_pHistory[JointID].m_dwFrameCount++;
}
else if( m_pHistory[JointID].m_dwFrameCount == 1 )
{
vFilteredPosition = XMVectorScale( XMVectorAdd( vRawPosition, vPrevRawPosition ), 0.5f );
vDiff = XMVectorSubtract( vFilteredPosition, vPrevFilteredPosition );
vTrend = XMVectorAdd( XMVectorScale( vDiff, smoothingParams.fCorrection ), XMVectorScale( vPrevTrend, 1.0f - smoothingParams.fCorrection ) );
m_pHistory[JointID].m_dwFrameCount++;
}
else
{
// First apply jitter filter
vDiff = XMVectorSubtract( vRawPosition, vPrevFilteredPosition );
vLength = XMVector3Length( vDiff );
fDiff = fabs( XMVectorGetX( vLength ) );

if( fDiff <= smoothingParams.fJitterRadius )
{
vFilteredPosition = XMVectorAdd( XMVectorScale( vRawPosition, fDiff / smoothingParams.fJitterRadius ),
XMVectorScale( vPrevFilteredPosition, 1.0f - fDiff / smoothingParams.fJitterRadius ) );
}
else
{
vFilteredPosition = vRawPosition;
}

// Now the double exponential smoothing filter
vFilteredPosition = XMVectorAdd( XMVectorScale( vFilteredPosition, 1.0f - smoothingParams.fSmoothing ),
XMVectorScale( XMVectorAdd( vPrevFilteredPosition, vPrevTrend ), smoothingParams.fSmoothing ) );


vDiff = XMVectorSubtract( vFilteredPosition, vPrevFilteredPosition );
vTrend = XMVectorAdd( XMVectorScale( vDiff, smoothingParams.fCorrection ), XMVectorScale( vPrevTrend, 1.0f - smoothingParams.fCorrection ) );
}

// Predict into the future to reduce latency
vPredictedPosition = XMVectorAdd( vFilteredPosition, XMVectorScale( vTrend, smoothingParams.fPrediction ) );

// Check that we are not too far away from raw data
vDiff = XMVectorSubtract( vPredictedPosition, vRawPosition );
vLength = XMVector3Length( vDiff );
fDiff = fabs( XMVectorGetX( vLength ) );

if( fDiff > smoothingParams.fMaxDeviationRadius )
{
vPredictedPosition = XMVectorAdd( XMVectorScale( vPredictedPosition, smoothingParams.fMaxDeviationRadius / fDiff ),
XMVectorScale( vRawPosition, 1.0f - smoothingParams.fMaxDeviationRadius / fDiff ) );
}

// Save the data from this frame
m_pHistory[JointID].m_vRawPosition = vRawPosition;
m_pHistory[JointID].m_vFilteredPosition = vFilteredPosition;
m_pHistory[JointID].m_vTrend = vTrend;

// Output the data
m_pFilteredJoints[JointID] = vPredictedPosition;
m_pFilteredJoints[JointID] = XMVectorSetW( m_pFilteredJoints[JointID], 1.0f );
}
Loading

0 comments on commit 520d0c2

Please sign in to comment.