Refine texture calculation c++ code.