Skip to content

Commit

Permalink
changes since march
Browse files Browse the repository at this point in the history
  • Loading branch information
iszihan committed Jul 22, 2020
1 parent def023e commit 240070e
Show file tree
Hide file tree
Showing 23 changed files with 1,135 additions and 79 deletions.
1 change: 1 addition & 0 deletions ReplicaSDK/include/DepthMeshLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DepthMesh {
void SetSaturation(const float& val);

void SetBaseline(const float& val);

static std::shared_ptr<Shape> GenerateMeshData(int width, int height, bool renderSpherical);

private:
Expand Down
20 changes: 7 additions & 13 deletions ReplicaSDK/ptex/DepthMeshLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ DepthMesh::DepthMesh(const std::shared_ptr<Shape> &in_mesh,
const std::string& meshColor, const std::string& meshDepth,
const std::string& meshAlpha,
bool rL, bool rS, bool rO, bool fp, bool rJ) {

// Check everything exists
ASSERT(pangolin::FileExists(meshColor));
// std::cout<<meshDepth<<std::endl;

ASSERT(pangolin::FileExists(meshDepth));

if(rL && !fp) {
ASSERT(pangolin::FileExists(meshAlpha));
}

// Set rendering mdoe
// Set rendering mode
renderLayered = rL;
renderSpherical = rS;
renderODS = rO;
Expand All @@ -41,34 +39,31 @@ DepthMesh::DepthMesh(const std::shared_ptr<Shape> &in_mesh,
mesh = in_mesh;
LoadMeshColor(meshColor);
LoadMeshDepth(meshDepth);

if(renderLayered && !firstPass) {
LoadMeshAlpha(meshAlpha);
}

// Load shader
const std::string shadir = STR(SHADER_DIR);
ASSERT(pangolin::FileExists(shadir), "Shader directory not found!");

if(renderSpherical){
shader.AddShaderFromFile(pangolin::GlSlVertexShader, shadir + "/depth-mesh-spherical.vert", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlGeometryShader, shadir + "/depth-mesh-spherical.geom", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlFragmentShader, shadir + "/depth-mesh.frag", {}, {shadir});
shader.Link();

}
else{
shader.AddShaderFromFile(pangolin::GlSlVertexShader, shadir + "/depth-mesh.vert", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlFragmentShader, shadir + "/depth-mesh.frag", {}, {shadir});
shader.Link();

}
}

DepthMesh::DepthMesh(const std::shared_ptr<Shape> &in_mesh,
pangolin::GlTexture &mct,
pangolin::GlTexture &mdt,
bool rS) {

// Set rendering mdoe
renderLayered = false;
renderSpherical = rS;
Expand All @@ -84,19 +79,16 @@ DepthMesh::DepthMesh(const std::shared_ptr<Shape> &in_mesh,
// Load shader
const std::string shadir = STR(SHADER_DIR);
ASSERT(pangolin::FileExists(shadir), "Shader directory not found!");

if(renderSpherical){
shader.AddShaderFromFile(pangolin::GlSlVertexShader, shadir + "/depth-mesh-spherical.vert", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlGeometryShader, shadir + "/depth-mesh-spherical.geom", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlFragmentShader, shadir + "/depth-mesh.frag", {}, {shadir});
shader.Link();

}
else{
shader.AddShaderFromFile(pangolin::GlSlVertexShader, shadir + "/depth-mesh.vert", {}, {shadir});
shader.AddShaderFromFile(pangolin::GlSlFragmentShader, shadir + "/depth-mesh.frag", {}, {shadir});
shader.Link();

}
}

Expand Down Expand Up @@ -142,10 +134,10 @@ void DepthMesh::Render(
shader.SetUniform("gamma", 1.0f / gamma);
shader.SetUniform("saturation", saturation);
shader.SetUniform("clipPlane", clipPlane(0), clipPlane(1), clipPlane(2), clipPlane(3));
shader.SetUniform("baseline", baseline);

if(renderSpherical) {
shader.SetUniform("MV", cam.GetModelViewMatrix());
shader.SetUniform("baseline", baseline);
}

if(renderODS) {
Expand Down Expand Up @@ -193,16 +185,19 @@ void DepthMesh::Render(
}

std::shared_ptr<Shape> DepthMesh::GenerateMeshData(int width, int height, bool renderSpherical) {

std::shared_ptr<Shape> mesh = std::make_shared<Shape>(VBO::GEOMETRY_LAYOUT::LAYOUT_TRIANGLE_STRIP);

for(int row = 0; row < height; row++) {
for(int col = 0; col < width; col++) {

float x = ((float)col) / width;
float x_next = ((float)col + 1.f) / width;
float y = ((float)row) / height;
float y_next = ((float)row + 1.f) / height;

if(renderSpherical) {
//different order to do with GL_CULL_FACE
mesh->addPosition(x, y_next, 0.f);
mesh->addNormal(0.f, 0.f, 0.f);
mesh->addTextureCoordinate(x, y_next);
Expand Down Expand Up @@ -238,7 +233,6 @@ std::shared_ptr<Shape> DepthMesh::GenerateMeshData(int width, int height, bool r
}
}
}

return mesh;
}

Expand Down
1 change: 0 additions & 1 deletion ReplicaSDK/ptex/PLYParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ void PLYParse(MeshData& meshData, const std::string& filename) {
size_t positionOffsetBytes = 0;
size_t normalOffsetBytes = 0;
size_t colorOffsetBytes = 0;

size_t offsetSoFarBytes = 0;

for (size_t i = 0; i < vertexLayout.size(); i++) {
Expand Down
4 changes: 3 additions & 1 deletion ReplicaSDK/ptex/PTexLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void PTexMesh::RenderSubMeshDepth(
const float depthScale,
const Eigen::Vector4f& clipPlane,
int lrC ) {

ASSERT(subMesh < meshes.size());
Mesh& mesh = *meshes[subMesh];

Expand Down Expand Up @@ -233,7 +234,9 @@ void PTexMesh::RenderSubMeshDepth(
mesh.vbo.Unbind();

mesh.ibo.Bind();

glDrawElements(GL_QUADS, mesh.ibo.num_elements, mesh.ibo.datatype, 0);

mesh.ibo.Unbind();

// mesh.ibo.Bind();
Expand All @@ -244,7 +247,6 @@ void PTexMesh::RenderSubMeshDepth(
glDisableVertexAttribArray(0);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
depthShader.Unbind();

glPopAttrib();


Expand Down
13 changes: 7 additions & 6 deletions ReplicaSDK/shaders/depth-mesh-spherical.vert
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main()
{
// Get position on the sphere from uv coordinates
vec2 uv = vec2(1 - in_uv.x, in_uv.y);

vec3 p = vec3(sin(M_PI * uv.y) * cos(2 * M_PI * uv.x),
cos(M_PI * uv.y),
sin(M_PI * uv.y) * sin(2 * M_PI * uv.x));
Expand Down Expand Up @@ -56,13 +57,13 @@ void main()
p = (MV * vec4(p, 1)).xyz;

// Angles from direction vector
float theta = - atan(p.z, p.x);
float phi = - atan(p.y, sqrt(p.x * p.x + p.z * p.z));
float theta = atan(p.z, p.x);
float phi = atan(p.y, sqrt(p.x * p.x + p.z * p.z));

// Normalize to clip space
vec4 pos;
pos.x = (-theta / (M_PI) + 0.0) * 1;
pos.y = (-phi / (M_PI / 2) + 0.0) * 1;
pos.x = (theta / (M_PI) + 0.0) * 1; // -1 to 1
pos.y = (phi / (M_PI / 2) + 0.0) * 1; // -1 to 1
pos.z = abs(length(p.xyz)) / 20;
pos.w = 1;

Expand All @@ -73,6 +74,6 @@ void main()
gl_ClipDistance[0] = 1;

// Frag uv
vs_out.uv = in_uv;
vs_out.uv_screen = (gl_Position.xy / gl_Position.w + 1) / 2;
vs_out.uv = in_uv; // 0 to 1
vs_out.uv_screen = (gl_Position.xy / gl_Position.w + 1) / 2; // turns [-1,1] to [0,1]
}
3 changes: 1 addition & 2 deletions ReplicaSDK/shaders/depth-mesh.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 in_uv;
layout(binding = 1) uniform sampler2D depthTex;
uniform mat4 MVP;
uniform float baseline;
uniform vec4 clipPlane;

uniform int render_jump;
uniform int render_ods;

Expand All @@ -27,7 +27,6 @@ void main()
// Offset by depth
vec4 texel;
float depth;

if(render_jump == 1) {
depth = textureLod(depthTex, uv, 0.0).x;
depth = 0.299999999999999999f / (depth + 0.001f);
Expand Down
6 changes: 3 additions & 3 deletions ReplicaSDK/shaders/mesh-depth-spherical.vert
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ void main()

// Angles from direction vector
float theta = - atan(d.z, d.x);
float phi = - atan(d.y, sqrt(d.x * d.x + d.z * d.z));
float phi = atan(d.y, sqrt(d.x * d.x + d.z * d.z));

// Normalize to clip space
vec4 pos;
pos.x = (-theta / (M_PI) + 0.0) * 1;
pos.y = (-phi / (M_PI / 2) + 0.0) * 1;
pos.x = (theta / (M_PI) + 0.0) * 1;
pos.y = (phi / (M_PI / 2) + 0.0) * 1;
pos.z = abs(length(p.xyz)) / 20;
pos.w = 1;

Expand Down
2 changes: 1 addition & 1 deletion ReplicaSDK/shaders/mesh-depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#version 430 core

layout(location = 0) in vec4 position;

layout(location = 1) in vec4 normal;
uniform mat4 MV, MVP;
uniform vec4 clipPlane;

Expand Down
6 changes: 3 additions & 3 deletions ReplicaSDK/shaders/mesh-ptex-spherical.vert
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ void main()

// Angles from direction vector
float theta = - atan(d.z, d.x);
float phi = - atan(d.y, sqrt(d.x * d.x + d.z * d.z));
float phi = atan(d.y, sqrt(d.x * d.x + d.z * d.z));

// Normalize to clip space
vec4 pos;

pos.x = (-theta / (M_PI) + 0.0) * 1;
pos.y = (-phi / (M_PI / 2) - 0.0002) * 1;
pos.x = ( theta / (M_PI) + 0.0) * 1;
pos.y = ( phi / (M_PI / 2) - 0.0002) * 1;

pos.z = abs(length(p.xyz)) / 20;
pos.w = 1;
Expand Down
1 change: 0 additions & 1 deletion ReplicaSDK/src/depth_mesh_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,3 @@ int main(int argc, char* argv[]) {

return 0;
}

23 changes: 18 additions & 5 deletions ReplicaSDK/src/depth_mesh_render_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ int main(int argc, char* argv[]) {
std::vector<std::vector<float>> camera_poses;

int c = 0;

while(std::getline(in_cam,line)){
float value;
std::stringstream ss(line);
Expand Down Expand Up @@ -108,13 +107,23 @@ int main(int argc, char* argv[]) {
pangolin::ProjectionMatrixRDF_TopLeft(
width,
height,
width / 2.0f,
width / 2.0f,
width / 4.0f,
width / 4.0f,
(width - 1.0f) / 2.0f,
(height - 1.0f) / 2.0f,
0.1f,
100.0f),
pangolin::ModelViewLookAtRDF(0, 0, 0, 0, 0, 1, 0, 1, 0));
pangolin::ModelViewLookAtRDF(0, 0, 0, 0, 0, 1, 0, -1, 0));

if(!spherical){
//rotate camera by 90 degree
Eigen::Transform<double,3,Eigen::Affine> t(Eigen::AngleAxis<double>(1.5*M_PI,Eigen::Vector3d::UnitY()));
Eigen::Matrix4d R_side=Eigen::Matrix4d::Identity();
R_side=t.matrix();
Eigen::Matrix4d curr_spot_cam_to_world = s_cam.GetModelViewMatrix();
Eigen::Matrix4d T_camera_world = R_side.inverse() * curr_spot_cam_to_world ;
s_cam.GetModelViewMatrix() = T_camera_world;
}

const std::string shadir = STR(SHADER_DIR);

Expand Down Expand Up @@ -171,7 +180,11 @@ int main(int argc, char* argv[]) {
int status = system(dir);

char filename[1000];
snprintf(filename, 1000, "%s/%04d/output_tgt_%04d.png", out_dir.c_str(), i, i);
if(spherical){
snprintf(filename, 1000, "%s/%04d/output_tgt_%04d.png", out_dir.c_str(), i, i);
}else{
snprintf(filename, 1000, "%s/%04d/output_ptgt_%04d.png", out_dir.c_str(), i, i);
}

pangolin::SaveImage(
image.UnsafeReinterpret<uint8_t>(),
Expand Down
4 changes: 3 additions & 1 deletion ReplicaSDK/src/depth_mesh_render_layered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ int main(int argc, char* argv[]) {
const std::string colorFile = prefix + ".png";
const std::string depthFile = prefix + "D.png";
const std::string alphaFile = prefix + "A.png";

const std::string bgColorFile = prefix + "_BG.png";
const std::string bgDepthFile = prefix + "_BGD.png";
const std::string bgAlphaFile = prefix + "_BGA.png";

const std::string inpColorFile = prefix + "_BG_inp.png";
const std::string inpDepthFile = prefix + "_BGD_inp.png";

const std::string sphericalArg = std::string(argv[3]);
bool spherical = sphericalArg.compare(std::string("y")) == 0;

Expand Down Expand Up @@ -151,4 +154,3 @@ int main(int argc, char* argv[]) {

return 0;
}

Loading

0 comments on commit 240070e

Please sign in to comment.