Skip to content

Commit

Permalink
fix shader
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 18, 2019
1 parent 8e90b4d commit e4917e0
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 53 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@

This project is the face render of Photo-Realistic Facial Details Synthesis from Single Image(ICCV 2019 Oral)
### Dependency library
all dependent libs are included in folder **thirds**

## Run executable
```cpp
./exe/hmrenderer.exe /root/to/obj /root/to/norm /root/to/shader

example: ./
```

## build
If you want to build the source code, you can follow the following introduce. All dependents and libs are included.

### Project structure
```
Expand Down Expand Up @@ -31,13 +40,14 @@ all dependent libs are included in folder **thirds**
cd to the root of this project
mkdir build
cd build
cmake -A X64 -D CMAKE_PREFIX_PATH=/root/to/thirds ../src
cmake -A X64 -D CMAKE_PREFIX_PATH=../thirds ../src
```

### Run executable
```cpp
./hmrenderer.exe /root/to/obj /root/to/norm /root/to/shader

eg: (unzip ./exe/face_rendering.rar file to get the executable file.)
./hmrenderer.exe ../assets/face.obj ../assets/nrm.png ../src/shaders/
```

### Some screenshots

<div align="left">
<img src="https://github.com/gg-z/face_rendering/blob/master/imgs/left.png" width = "430" height = "430" alt="demo_0" align=center />
<img src="https://github.com/gg-z/face_rendering/blob/master/imgs/right.png" width = "430" height = "430" alt="demo_1" align=center />
</div>
Binary file modified exe/hmrenderer.exe
Binary file not shown.
10 changes: 10 additions & 0 deletions exe/imgui.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0

[Window][Tweak]
Pos=24,28
Size=246,355
Collapsed=0

Binary file added imgs/left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed imgs/screen_shot0.png
Binary file not shown.
Binary file removed imgs/screen_shot1.png
Binary file not shown.
3 changes: 2 additions & 1 deletion src/includes/ind/loaders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void GenNormal(tinyobj::attrib_t &shape, tinyobj::mesh_t &mesh, float angle) {
v[2] = shape.vertices[indexZ + 2] -
shape.vertices[indexX + 2];

n = glm::cross(u, v); n = glm::normalize(n); count[int(indexX / 3)] += 1;
n = glm::cross(u, v); n = glm::normalize(n);
count[int(indexX / 3)] += 1; count[int(indexY / 3)] += 1; count[int(indexZ / 3)] += 1;
shape.normals[indexX] += n.x; shape.normals[indexX + 1] += n.y; shape.normals[indexX + 2] += n.z;
shape.normals[indexY] += n.x; shape.normals[indexY + 1] += n.y; shape.normals[indexY + 2] += n.z;
shape.normals[indexZ] += n.x; shape.normals[indexZ + 1] += n.y; shape.normals[indexZ + 2] += n.z;
Expand Down
7 changes: 4 additions & 3 deletions src/includes/ind/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ void Renderer::render_hm(const std::vector<MeshDesc> &meshes) {
// render scene
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, view_->Width(), view_->Height());
glClearColor(1.f, 1.f, 1.f, 1.f);
glClearColor(0.164f, 0.164f, 0.164f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shad_hm_.Get());
for (auto &mesh : meshes) {
AssignUniform(shad_hm_.Get(), "projection", view_->Proj());
AssignUniform(shad_hm_.Get(), "view", view_->View());
AssignUniform(shad_hm_.Get(), "model", mesh.Model);
AssignUniform(shad_hm_.Get(), "cam_pos", view_->Pos());
AssignUniform(shad_hm_.Get(), "view", mesh.Model);
AssignUniform(shad_hm_.Get(), "model", view_->View());
AssignUniform(shad_hm_.Get(), "lht_pos", lights_->at(0).pos);
AssignUniform(shad_hm_.Get(), "albedo", albedo);
AssignUniform(shad_hm_.Get(), "lht_color", glm::make_vec3(lht_color));
Expand Down
4 changes: 2 additions & 2 deletions src/includes/ind/view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ IndView::IndView(unsigned width, unsigned height, float FoVy, Bounds scene_box)

void IndView::Zoom(float dist) {
if (near_ + dist <= 0.f) return;
near_ += dist;
far_ += dist;
near_ += dist * 5;
far_ += dist * 5;
}

glm::vec3 IndView::Pos() const {
Expand Down
5 changes: 1 addition & 4 deletions src/renderer_hm/face.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ void LoadRes(World* world, std::string obj_path, std::string nrm_path ) {

// load quad aligned to screen
world->quad = std::make_unique<QuadDesc>();
//ReadObjFile(std::string(kAssetDir_) + "/1/agi/quad.obj", &world->quad->mesh);
world->quad.get()->mesh.txtr_path = "";
PrepGLDataNTgt(&world->quad.get()->mesh);
PrepGLQuad(world->quad.get());

// load quad_irradiance to render irridance texture
world->quad_irrid = std::make_unique<QuadDesc>();
//ReadObjFile(std::string(kAssetDir_) + "/1/agi/quad.obj", &world->quad_irrid->mesh);
world->quad_irrid.get()->mesh.txtr_path = "";
PrepGLDataNTgt(&world->quad_irrid.get()->mesh);
PrepGLQuad(world->quad_irrid.get());
Expand All @@ -57,15 +55,14 @@ void LoadRes(World* world, std::string obj_path, std::string nrm_path ) {

world->lights = std::make_unique<std::vector<DirLightDesc>>(1);
for (size_t i = 0; i < world->lights.get()->size(); i++) {
world->lights->at(i).dir = glm::vec3(1, 1, 0.4);
world->lights->at(i).dir = glm::vec3(0.27, -0.66, 0.60);
world->lights->at(i).color = glm::vec3(1.0f);
world->lights->at(i).shadow_w = kWindowWidth*4;
world->lights->at(i).shadow_h = kWindowHeight*4;
}

// prepare the light's geometry
PrepGLLights(world->lights.get());
//ReadObjFile(std::string(kAssetDir_) + "/1/agi/quad.obj", &world->lights.get()->at(0).mesh);
world->lights.get()->at(0).mesh.type = MeshType::Tri;
PrepGLDataNTgt(&world->lights.get()->at(0).mesh);

Expand Down
31 changes: 12 additions & 19 deletions src/renderer_hm/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ int main(int argc, char** argv){


void parseArgs(int argc, char** argv) {
if (argc != 4) {
std::cout << "wrong args, please check it." << std::endl;
exit(0);
}

kShaderDir_ = "../src/shaders";
kObjPath = std::string(argv[1]);
kNrmPath = std::string(argv[2]);
kShaderDir_ = argv[3];
if(argc>3)
kShaderDir_ = argv[3];
}


Expand Down Expand Up @@ -74,7 +73,7 @@ void Rotate(std::vector<T> *ts) {

void GameLoop(World &world, GLFWWindowDesc &window) {

glClearColor(1.f, 1.f, 1.f, 1.f);
glClearColor(0.164f, 0.164f, 0.164f, 1.f);
glEnable(GL_DEPTH_TEST);
unsigned frame_cnt = 0;

Expand All @@ -99,26 +98,18 @@ void GameLoop(World &world, GLFWWindowDesc &window) {
// render irridance map
// world.renderer->renderLgtIrrid(*world.meshes.get(), *world.quad_irrid.get());
while (!window.ShouldClose()) {
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.f, 1.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Rotate(world.meshes.get());

//Rotate(world.meshes.get());

#if 0
auto milli = TimeMeasure([&world]() {world.gloomy->Render(*world.meshes.get()); }, 500);
printf("render profile %.2f ms per frame\n", milli);
break;
#endif
//world.renderer->render(*world.meshes.get(), *world.quad.get(), 1);
//world.renderer->render(*world.meshes.get());
//world.renderer->renderBSSRDF(*world.meshes.get(), *world.quad_irrid.get(), *world.quad.get());
//world.renderer->renderBSSRDF(*world.meshes.get(), *world.quad_irrid.get(), *world.quad.get(), *world.fbos.get());
//std::vector<float> sh_cof(27, 0.f);
//world.renderer->render_sh(*world.meshes.get(), *world.quad_irrid.get(), *world.fbos.get(), sh_cof);

world.renderer->render_hm(*world.meshes.get());
//world.renderer->render_lattice(*world.meshes.get());



ImGui_ImplOpenGL3_NewFrame();
Expand Down Expand Up @@ -220,4 +211,6 @@ void HandleInput(GLFWWindowDesc *window, World *world) {
if (ImGui::GetIO().WantCaptureMouse) return;
world->view->Zoom(yoffset);
});
}
}


25 changes: 14 additions & 11 deletions src/shaders/render_hm.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ uniform sampler2D hm;
uniform vec3 lht_pos;
uniform vec3 lht_color;
uniform float albedo;



uniform vec3 cam_pos;

void main()
{
Expand All @@ -24,28 +22,33 @@ void main()
// ambient
float abnt = 0.1;
vec3 ambient = abnt * lht_clr;
ambient = ambient * obj_clr;
ambient = ambient;


// diffuse
vec3 T = fs_in.tgt;
vec3 N = normalize(fs_in.normal);
vec3 T = normalize(fs_in.tgt- dot(fs_in.normal, fs_in.tgt)*fs_in.normal);
vec3 B = cross(N,T);

mat3 TBN= mat3(T,B,N);
T = normalize(T - dot(T, N) * N);
vec3 B = cross(T, N);
mat3 TBN = mat3(T, B, N);

vec2 uv = vec2(fs_in.tex_cord.x, 1-fs_in.tex_cord.y);
vec3 map_normal = texture2D(hm, uv).xyz;
vec3 normal = 2.0*map_normal-vec3(1.0,1.0,1.0);
normal = normalize(TBN*normal);

vec3 lht_dir = normalize(lht_pos - fs_in.pos);

float diff = max(dot(normal, lht_dir), 0.0);
vec3 diffuse = diff*lht_clr;

vec3 res = (ambient + diffuse)*obj_clr;
// Specular
vec3 viewDir = normalize(cam_pos - fs_in.pos);
vec3 reflectDir = reflect(-lht_dir, normal);
vec3 halfwayDir = normalize(lht_dir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
vec3 specular = vec3(0.3) * spec;

frag_color = vec4(res, 1.0f);
vec3 res = (ambient + diffuse + specular)*obj_clr;

frag_color = vec4(res, 1.0f);
}
9 changes: 5 additions & 4 deletions src/shaders/render_hm.vs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ uniform mat4 model;
void main()
{
vs_out.tex_cord = tex_cord;
vs_out.normal = vec3(normalize(model*vec4(normal,0.0)));
vs_out.tgt = vec3(normalize(model*vec4(tgt,0.0)));
vs_out.pos = vec3(normalize(model*vec4(pos,0.0)));

vs_out.pos = vec3(model*vec4(pos,1.0));
vs_out.tgt = normalize(vec3(model * vec4(tgt, 0.0)));

mat3 normalMatrix = transpose(inverse(mat3(model)));
vs_out.normal = normalize(normalMatrix*normal);

gl_Position = projection * view * model * vec4(pos, 1.0);

}

0 comments on commit e4917e0

Please sign in to comment.