The simultaneous effect of both disorder and crystal-lattice pinning on the equilibrium behavior of oriented elastic objects is studied using scaling arguments and a functional renormalization group technique. Our analysis applies to elastic manifolds, e.g., interfaces, as well as to periodic elastic media, e.g., charge-density waves or flux-line lattices. The competition between both pinning mechanisms leads to a continuous, disorder driven roughening transition between a flat state where the mean relative displacement saturates on large scales and a rough state with diverging relative displacement. The transition can be approached by changing the impurity concentration or, indirectly, by tuning the temperature since the pinning strengths of the random and crystal potential have in general a different temperature dependence. For D dimensional elastic manifolds interacting with either random-field or random-bond disorder a transition exists for 2<D<4, and the critical exponents are obtained to lowest order in \epsilon=4-D. At the transition, the manifolds show a superuniversal logarithmic roughness. Dipolar interactions render lattice effects relevant also in the physical case of D=2. For periodic elastic media, a roughening transition exists only if the ratio p of the periodicities of the medium and the crystal lattice exceeds the critical value p_c=6/\pi\sqrt{\epsilon}. For p<p_c the medium is always flat. Critical exponents are calculated in a double expansion in \mu=p^2/p_c^2-1 and \epsilon=4-D and fulfill the scaling relations of random field models.