bool SPARSE_SDF::absoluteNearestSurfacePoint(const VEC3F& position, VEC3F& surface) { MyVec3 vecNormal; Real sdf; MyVec3 positionVec(position[0], position[1], position[2]); MyVec3 tmpSurface = positionVec; bool inNarrowBand = _grid->lookupDistanceAndNormalFast(positionVec, sdf, vecNormal); if(!inNarrowBand){ cout << "lookup point inside the mesh but not captured by the narrow band!!!" << endl; // Core::throwDefaultException(std::string("lookup point inside the mesh but not captured by the narrow band!!!"), __FILE__, __LINE__); return false; } Real initSdf = sdf; int iter = 0; while(abs(sdf) > 1e-9 && iter < 100){ tmpSurface += (-sdf) * vecNormal; sdf = (*_grid)(tmpSurface); iter++; } Real dist = (tmpSurface - positionVec).length(); if(dist > abs(initSdf * 1.5)){ return false; } surface[0] = tmpSurface[0]; surface[1] = tmpSurface[1]; surface[2] = tmpSurface[2]; return true; }
void Material::bindMaterial(Transform &T, Camera &camera) { glUseProgram(shaderProgram); // MATRICES FROM TRANSFORM GLint loc = glGetUniformLocation(shaderProgram, "uObjectWorldM"); if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.transform)); // loc = glGetUniformLocation(shaderProgram, "uObjectWorldInverseM"); //printMat(T.invTransform); if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.invTransform)); // glm::mat4x4 objectWorldViewPerspect = camera.worldViewProject * T.transform; loc = glGetUniformLocation(shaderProgram, "uObjectPerpsectM"); if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(objectWorldViewPerspect)); // MATERIAL COLORS for (int i = 0; i < (int) colors.size(); i++) { if (colors[i].id == -1) { loc = glGetUniformLocation(shaderProgram, colors[i].name.c_str()); colors[i].id = loc; } if (colors[i].id >= 0) { glUniform4fv(colors[i].id, 1, &colors[i].val[0]); } } loc = glGetUniformLocation(shaderProgram, "uViewPosition"); if(loc != -1) { glm::vec4 positionVec(camera.center, 0); glUniform4fv(loc, 1, &positionVec[0]); } loc = glGetUniformLocation(shaderProgram, "uViewDirection"); if(loc != -1) { glm::vec4 directionVec(glm::normalize(camera.eye - camera.center), 0); glUniform4fv(loc, 1, &directionVec[0]); } // MATERIAL TEXTURES for (int i = 0; i < (int) textures.size(); i++) { if (textures[i].id == -1) { loc = glGetUniformLocation(shaderProgram, textures[i].name.c_str()); textures[i].id = loc; } if (textures[i].id >= 0) { //printf("\n%d %d\n", textures[i].id, textures[i].val->samplerId); glActiveTexture(GL_TEXTURE0 + i); glUniform1i(textures[i].id, i); glBindTexture(GL_TEXTURE_2D, textures[i].val->textureId); glBindSampler(textures[i].id, textures[i].val->samplerId); } } }