  We investigate the magnetic resonant x-ray scattering spectra around the $K$
edge of Ni in antiferromagnetic NiO using an \textit{ab-initio} band-structure
calculation based on the density-functional theory. By taking account of
orbital polarization through the spin-orbit interaction, we reproduce well the
spectra obtained experimentally, thus demonstrating the usefulness of the
\textit{ab-initio} calculation. It is shown that the main-edge peak, which
mainly comes from the dipolar ($1s \to 4p$) transition, is a direct reflection
of the orbital polarization of the $4p$ states. It is clarified that the $4p$
orbital polarization is mainly induced from the spin polarization on the $4p$
states by the spin-orbit interaction. The 3d orbital polarization at
neighboring Ni sites gives rise to only a minor contribution to the $4p$
orbital polarization through a $p$-$d$ mixing. It is also shown that the
pre-edge peak, which mainly originates from the quadrupolar ($1s \to 3d$)
transition, is a direct reflection of the orbital polarization of the
unoccupied 3d states. It shows a Fano-type antiresonant dip due to interference
with the nonresonant contribution, in agreement with the experimental result.
