Skip to content

Commit

Permalink
fragment disp
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcToussaint committed Feb 18, 2025
1 parent 2ce4fb5 commit 0d28d41
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/Core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ struct String : public std::iostream {
};
stdPipes(String)

inline String operator+(const String& a, const char* b) { String s=a; s <<b; return s; }
template<class T> String operator+(const String& a, const T& b) { String s=a; s <<b; return s; }

} //namespace

Expand Down
22 changes: 13 additions & 9 deletions src/Geo/assimpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
bool loadTextures = true;

AssimpLoader::AssimpLoader(const std::string& path, bool flipYZ, bool relativeMeshPoses, int _verbose)
:verbose(_verbose){
: verbose(_verbose){

Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
Expand All @@ -33,7 +33,12 @@ AssimpLoader::AssimpLoader(const std::string& path, bool flipYZ, bool relativeMe
HALT("ERROR::ASSIMP:: " <<importer.GetErrorString());
}

directory = path.substr(0, path.find_last_of('/'));
size_t slash = path.find_last_of('/');
if(slash<std::string::npos){
directory = path.substr(0, slash);
}else{
directory = ".";
}

if(verbose>0) {
LOG(0) <<"loading " <<path <<" from directory " <<directory;
Expand Down Expand Up @@ -82,7 +87,7 @@ void AssimpLoader::loadNode(const aiNode* node, const aiScene* scene, arr T, boo
X.rot.setMatrix((R%scales).p);

if(verbose>0) {
LOG(0) <<" loading node '" <<node->mName.C_Str() <<"' of parent '" <<(node->mParent?node->mParent->mName.C_Str():"<nil>");
LOG(0) <<" loading node '" <<node->mName.C_Str() <<"' of parent '" <<(node->mParent?node->mParent->mName.C_Str():"<nil>") <<"'";

cout <<"Transform: T=\n" <<T <<"\n<" <<X <<'>' <<endl;
cout <<"Trans scaling: " <<scales <<"ortho: ";
Expand Down Expand Up @@ -170,20 +175,19 @@ rai::Mesh AssimpLoader::loadMesh(const aiMesh* mesh, const aiScene* scene) {
for(int type = aiTextureType_NONE; type<aiTextureType_UNKNOWN; type++){
uint nTex = material->GetTextureCount(aiTextureType(type));
if(verbose>0) {
cout <<"material: #textures=" <<nTex <<endl;
cout <<"material: texture-type: " <<type <<" #textures: " <<nTex <<endl;
}
if(loadTextures && nTex) {
CHECK_EQ(nTex, 1, "");
aiString str;
material->GetTexture(aiTextureType(type), 0, &str);

std::string filename = this->directory + '/' + std::string(str.C_Str());

if(verbose>0) {
cout <<"loading texture image: " <<str.C_Str() <<endl;
cout <<"loading texture image: " <<filename <<endl;
}

std::string filename = this->directory + '/' + std::string(str.C_Str());
// std::string filename = std::string(str.C_Str());

int width, height, nrComponents;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
if(data) {
Expand All @@ -198,7 +202,7 @@ rai::Mesh AssimpLoader::loadMesh(const aiMesh* mesh, const aiScene* scene) {
}
}

CHECK_EQ(M.texImg.nd, 3, "no texture image could be loaded");
CHECK_EQ(M.texImg.nd, 3, "no texture could be loaded");

return M;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Geo/assimpInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct AssimpLoader {
std::string directory;
int verbose=0;

AssimpLoader(std::string const& path, bool flipYZ=true, bool relativeMeshPoses=false, int _verbose=0);
AssimpLoader(const std::string& path, bool flipYZ=true, bool relativeMeshPoses=false, int _verbose=0);
AssimpLoader(const struct aiScene* scene);

rai::Mesh getSingleMesh();
Expand Down
2 changes: 1 addition & 1 deletion src/Geo/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ void Mesh::box() {
}

void Mesh::addMesh(const Mesh& mesh2, const Transformation& X) {
uint n=V.d0, tn=texCoords.d0, t=T.d0;
uint n=V.d0, t=T.d0;
if(V.d0==C.d0 && (C.N || mesh2.C.N)) {
if(mesh2.V.d0==mesh2.C.d0) C.append(mesh2.C);
else if(mesh2.C.N==3) C.append(replicate(mesh2.C, mesh2.V.d0));
Expand Down
50 changes: 31 additions & 19 deletions src/Gui/RenderData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,18 @@ void RenderData::ensureInitialized(OpenGL &gl){
id.progTensor = LoadShadersFile("shaderTensor.vs", "shaderTensor.fs");
}else{
id.progTensor = LoadShaders( tensorVS, tensorFS );
//id.progTensor = LoadShadersFile(rai::raiPath("src/Gui/shaderTensor.vs"), rai::raiPath("src/Gui/shaderTensor.fs") );
// id.progTensor = LoadShadersFile(rai::raiPath("src/Gui/shaderTensor.vs"), rai::raiPath("src/Gui/shaderTensor.fs") );
}
id.progTensor_Projection_W = glGetUniformLocation(id.progTensor, "Projection_W");
id.progTensor_ModelT_WM = glGetUniformLocation(id.progTensor, "ModelT_WM");
id.progTensor_ModelScale = glGetUniformLocation(id.progTensor, "ModelScale");
id.progTensor_eyePosition_W = glGetUniformLocation(id.progTensor, "eyePosition_W");
id.progTensor_tensorTexture = glGetUniformLocation(id.progTensor, "tensorTexture");

id.progMarker = LoadShaders( markerVS, markerFS ); //rai::raiPath("src/Gui/shaderMarker.vs"), rai::raiPath("src/Gui/shaderMarker.fs") );
{
id.progMarker = LoadShaders( markerVS, markerFS );
// id.progMarker = LoadShadersFile(rai::raiPath("src/Gui/shaderMarker.vs"), rai::raiPath("src/Gui/shaderMarker.fs") );
}
id.progMarker_Projection_W = glGetUniformLocation(id.progMarker, "Projection_W");
id.progMarker_ModelT_WM = glGetUniformLocation(id.progMarker, "ModelT_WM");

Expand Down Expand Up @@ -406,6 +409,7 @@ void RenderData::glDraw(OpenGL& gl){

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_PROGRAM_POINT_SIZE);
glCullFace(GL_BACK);
// if(renderUntil>=_transparent){
glEnable(GL_BLEND);
Expand Down Expand Up @@ -532,8 +536,7 @@ void RenderAsset::mesh(rai::Mesh& mesh, double avgNormalsThreshold){
colors = rai::convert<float>(mesh.texCoords);
colors.insColumns(-1, 2);
for(uint i=0;i<colors.d0;i++) colors(i,2) = colors(i,3) = 1.;
texture = rai::convert<float>(mesh.texImg);
for(float& f:texture) f /= 255.f;
texture = mesh.texImg;
textureDim = 2;
}
}
Expand All @@ -545,12 +548,17 @@ void RenderAsset::pointCloud(const arr& points, const arr& color){
vertices = rai::convert<float>(points);
arr c = reshapeColor(color);

if(color.nd==1){
if(!color.N){
colors.resize(vertices.d0, 4).setZero();
for(uint i=0;i<vertices.d0;i++) colors(i,-1) = 1.;
}else if(color.nd==1){
colors.resize(vertices.d0, 4);
for(uint i=0;i<vertices.d0;i++) for(uint k=0;k<4;k++) colors(i,k) = c(k);
}else{
CHECK_EQ(color.d0, vertices.d0, "");
colors = rai::convert<float>(c);
if(color.N){
CHECK_EQ(color.d0, vertices.d0, "");
colors = rai::convert<float>(c);
}
}
}

Expand All @@ -570,7 +578,11 @@ void RenderAsset::lines(const arr& lines, const arr& color){

void RenderAsset::tensor(const floatA& vol, const arr& size){
CHECK_EQ(size.N, 3, "");
texture = vol;
texture.resize(vol.d0, vol.d1, vol.d2);
for(uint i=0;i<vol.N;i++){
float& f = vol.p[i];
texture.p[i] = (f<0.f? byte(0) : (f>1.f ? byte(255) : byte(f*255.f) ));
}
rai::Mesh mesh;
mesh.setBox();
mesh.scale(size);
Expand Down Expand Up @@ -644,24 +656,24 @@ void RenderAsset::glInitialize(){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(texture.d2==1) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, texture.d1, texture.d0, 0, GL_LUMINANCE, GL_FLOAT, texture.p);
else if(texture.d2==2) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, texture.d1, texture.d0, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, texture.p);
else if(texture.d2==3) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.d1, texture.d0, 0, GL_RGB, GL_FLOAT, texture.p);
else if(texture.d2==4) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.d1, texture.d0, 0, GL_RGBA, GL_FLOAT, texture.p);
if(texture.d2==1) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, texture.d1, texture.d0, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texture.p);
else if(texture.d2==2) glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, texture.d1, texture.d0, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texture.p);
else if(texture.d2==3) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.d1, texture.d0, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.p);
else if(texture.d2==4) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.d1, texture.d0, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.p);
else NIY;
glBindTexture(GL_TEXTURE_2D, 0);
}
else if(textureDim==3){
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, textureBuffer);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage3D(GL_TEXTURE_3D, 0, GL_R16F, texture.d2, texture.d1, texture.d0, 0, GL_RED, GL_FLOAT, texture.p);
glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, texture.d2, texture.d1, texture.d0, 0, GL_RED, GL_UNSIGNED_BYTE, texture.p);
glBindTexture(GL_TEXTURE_3D, 0);
}
else NIY;
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/RenderData.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct Render_Options {
enum RenderType { _solid, _shadow, _tensor, _text, _marker, _transparent, _all };

struct RenderAsset{
floatA vertices, colors, normals, texture; //for 2D textures: colors are vertex texture UV coordinates
floatA vertices, colors, normals; //for 2D textures: colors are vertex-wise texture coordinates
byteA texture;
GLuint vao, vertexBuffer, colorBuffer, normalBuffer, textureBuffer;
GLenum mode=GL_TRIANGLES;
uint textureDim=0;
Expand Down
25 changes: 15 additions & 10 deletions src/Gui/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,13 @@ void OpenGL::project(double& x, double& y, double& z, bool resetCamera, int subV
// callbacks
//

void getSphereVector(rai::Vector& vec, double x, double y, int le, int ri, int bo, int to) {
rai::Vector getMouseVector(double x, double y, int le, int ri, int bo, int to) {
int w=ri-le, h=to-bo;
int minwh = w<h?w:h;
x=x-le-.5*w; x*= 2./minwh;
y=y-bo-.5*h; y*= -2./minwh;
vec.set(x, y, .5-(x*x+y*y));
if(vec.z<0.) vec.z=0.;
vec.isZero=false;
rai::Vector vec(x, y, .5-(x*x+y*y));
return vec;
}

bool OpenGL::modifiersNone() { return _NONE(modifiers); }
Expand Down Expand Up @@ -812,14 +811,14 @@ void OpenGL::MouseButton(int button, int buttonIsUp, int _x, int _y, int mods) {
for(mouseView=views.N; mouseView--;) {
v=&views(mouseView);
if(_x<v->ri*w && _x>v->le*w && _y<v->to*h && _y>v->bo*h) {
getSphereVector(vec, _x, _y, v->le*w, v->ri*w, v->bo*h, v->to*h);
vec = getMouseVector(_x, _y, v->le*w, v->ri*w, v->bo*h, v->to*h);
cam=&views(mouseView).camera;
break;
}
}

if(mouseView==-1) {
getSphereVector(vec, _x, _y, 0, w, 0, h);
vec = getMouseVector(_x, _y, 0, w, 0, h);
v=0;
}
CALLBACK_DEBUG(this, "associated to view " <<mouseView <<" x=" <<vec.x <<" y=" <<vec.y <<endl);
Expand All @@ -831,6 +830,12 @@ void OpenGL::MouseButton(int button, int buttonIsUp, int _x, int _y, int mods) {
} else {
if(!mouseIsDown) return; //the button is already up (another button was pressed...)
//CHECK(mouseIsDown, "mouse-up event although the mouse is not down???");
if(downVec.z<=.0 && mouse_button==-2 && _NONE(downModifiers) && !downVec.isZero) {
rai::Vector cz = -cam->X.rot.getY();
if(fabs(cz.x)>fabs(cz.y) && fabs(cz.x)>fabs(cz.z)) cam->upright(rai::sign(cz.x)*Vector_x);
if(fabs(cz.y)>fabs(cz.x) && fabs(cz.y)>fabs(cz.z)) cam->upright(rai::sign(cz.y)*Vector_y);
if(fabs(cz.z)>fabs(cz.x) && fabs(cz.z)>fabs(cz.y)) cam->upright(rai::sign(cz.z)*Vector_z);
}
mouseIsDown=false;
needsUpdate=true;
}
Expand Down Expand Up @@ -962,10 +967,10 @@ void OpenGL::MouseMotion(double _x, double _y) {
rai::Vector vec;
if(mouseView==-1) {
cam=&camera;
getSphereVector(vec, _x, _y, 0, w, 0, h);
vec = getMouseVector(_x, _y, 0, w, 0, h);
} else {
cam=&views(mouseView).camera;
getSphereVector(vec, _x, _y, views(mouseView).le*w, views(mouseView).ri*w, views(mouseView).bo*h, views(mouseView).to*h);
vec = getMouseVector(_x, _y, views(mouseView).le*w, views(mouseView).ri*w, views(mouseView).bo*h, views(mouseView).to*h);
}
CALLBACK_DEBUG(this, "associated to view " <<mouseView <<" x=" <<vec.x <<" y=" <<vec.y <<endl);

Expand All @@ -974,11 +979,11 @@ void OpenGL::MouseMotion(double _x, double _y) {
//-- LEFT -> rotation
if(mouse_button==1 && _NONE(downModifiers) && !downVec.isZero) {
rai::Quaternion rot;
if(downVec.z<.01) {
if(downVec.z<=.0) {
//at the margin:
downVec.z=0; downVec.normalize();
vec.z = 0.; vec.normalize();
rot.setDiff(vec, downVec); //consider imagined sphere rotation of mouse-move
rot.setDiff(vec, downVec);
} else {
//not at margin: use starndard xy to rotate
rai::Vector diff = vec - downVec;
Expand Down
1 change: 1 addition & 0 deletions src/Gui/shaderMarker.vs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ uniform mat4 ModelT_WM;

void main() {
gl_Position = Projection_W * ModelT_WM * vec4(vertexPosition_M, 1);
gl_PointSize = 2.;
objColor = vertexColor;
}

6 changes: 3 additions & 3 deletions src/Gui/shaderTensor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ void main(){
float density = 0.;
vec3 normal = vec3(0,0,0);
for(int i=0;i<=1000;i++){
step_pos += step_delta;
vec3 tensor_coord = step_pos/ModelScale;
if(tensor_coord.x>-.5 && tensor_coord.x<.5 && tensor_coord.y>-.5 && tensor_coord.y<.5 && tensor_coord.z>-.5 && tensor_coord.z<.5){
if(tensor_coord.x>=-.5 && tensor_coord.x<=.5 && tensor_coord.y>=-.5 && tensor_coord.y<=.5 && tensor_coord.z>=-.5 && tensor_coord.z<=.5){
float d = texture(tensorTexture, tensor_coord+.5).r;
vec3 n = calculate_normal(tensor_coord+.5);
if(d<0.) d=0;
Expand All @@ -57,8 +56,9 @@ void main(){
normal += d * n;
if(density>=1.) break;
}else{
break;
if(i>10) break; //might need several steps to be inside
}
step_pos += step_delta;
}

//vec3 eyeDirection_M = normalize(eyePosition_M - surfacePoint_M);
Expand Down
7 changes: 4 additions & 3 deletions src/Gui/shaders.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ void main(){
float density = 0.;
vec3 normal = vec3(0,0,0);
for(int i=0;i<=1000;i++){
step_pos += step_delta;
vec3 tensor_coord = step_pos/ModelScale;
if(tensor_coord.x>-.5 && tensor_coord.x<.5 && tensor_coord.y>-.5 && tensor_coord.y<.5 && tensor_coord.z>-.5 && tensor_coord.z<.5){
if(tensor_coord.x>=-.5 && tensor_coord.x<=.5 && tensor_coord.y>=-.5 && tensor_coord.y<=.5 && tensor_coord.z>=-.5 && tensor_coord.z<=.5){
float d = texture(tensorTexture, tensor_coord+.5).r;
vec3 n = calculate_normal(tensor_coord+.5);
if(d<0.) d=0;
Expand All @@ -193,8 +192,9 @@ void main(){
normal += d * n;
if(density>=1.) break;
}else{
break;
if(i>10) break; //might need several steps to be inside
}
step_pos += step_delta;
}
//vec3 eyeDirection_M = normalize(eyePosition_M - surfacePoint_M);
Expand Down Expand Up @@ -253,6 +253,7 @@ uniform mat4 ModelT_WM;
void main() {
gl_Position = Projection_W * ModelT_WM * vec4(vertexPosition_M, 1);
gl_PointSize = 2.;
objColor = vertexColor;
}
Expand Down
1 change: 1 addition & 0 deletions src/KOMO/komo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,7 @@ void KOMO::addForceExchangeDofs(const arr& times, const char* onto, const char*
rai::Frame *a = timeSlices(k_order+s, ontoId);
rai::Frame *b = timeSlices(k_order+s, fromId);
rai::ForceExchangeDof* ex = new ForceExchangeDof(*a, *b, type);
// ex->scale=2.;
if(initPoa.N) ex->poa = initPoa;
if(initForce.N) ex->force = initForce;
ex->q0 = ex->calcDofsFromConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/Kin/F_pose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ arr F_Position::phi(const FrameL& F) {
CHECK_EQ(F.N, 1, "Position feature only takes one frame argument");
rai::Frame* f = F.elem(0);
arr y;
f->C.kinematicsPos(y, y.J(), f);
f->C.kinematicsPos(y, y.J(), f, relPos);
return y;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Kin/F_pose.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct F_Zeros : Feature {
//===========================================================================

struct F_Position : Feature {
rai::Vector relPos;
F_Position(const rai::Vector& _relPos=0) : relPos(_relPos) {}
virtual arr phi(const FrameL& F);
virtual uint dim_phi(const FrameL& F) { return 3; }
};
Expand Down
Loading

0 comments on commit 0d28d41

Please sign in to comment.