Forward and Inverse Rendering with Gradient-based Optimization