Gavin Long from Kansas City opened fire on police officers in Baton Rouge, La., killing three. (Erin Patrick O'Connor/The Washington Post)

In the early moments of a brutal ambush that took the lives of three officers, a man dressed in black and filled with contempt crept toward a Baton Rouge police vehicle and pointed his assault rifle at the window.

When Gavin Long realized no one was inside, he retreated from the gas station parking lot and got back into his rented Chevrolet Malibu, according to Louisiana investigators who at a Monday afternoon news conference offered an extraordinary account of Sunday morning’s violence. The 29-year-old from Kansas City, Mo., soon parked a few hundred feet north and sneaked back behind a series of buildings to the gas station.

It was around then that the calls to 911 began pouring in. “A dude with a rifle,” one person reported. As police and deputies arrived, Long methodically worked his way around the side of a beauty salon, where he saw two Baton Rouge police officers.

He shot both of them, killing one and badly wounding the other, before injuring a third who had approached him from behind. As the officer in front of him crawled around outside the building, East Baton Rouge Parish Sheriff’s Deputy Brad Garafola hid behind a nearby trash bin and drew his gun. But when the 45-year-old noticed that his comrade was still alive, he left his cover and approached the officer.

From left, Baton Rouge Police Officer Montrell Jackson, 32; Baton Rouge Police officer Matthew Gerald, 41; and East Baton Rouge Sheriff's deputy Brad Garafola, 45. All three were killed Sunday. (From left: courtesy of Trenisha Jackson via AP; EPA/Baton Rouge Police Department’; East Baton Rouge Sheriff's Office via AP)

It was then that Long turned the corner.

“He sees the deputy,” said Col. Michael Edmonson, the superintendent of the Louisiana State Police. “He shoots the deputy.”

Seconds later, the killer spotted the wounded officer on the ground and executed him.

Throughout the 10-minute rampage, Long walked past bystanders as if they didn’t exist — a clear indication to investigators that he was stalking those in uniform.

“There is no doubt whatsoever that these officers were intentionally targeted and assassinated,” Edmonson said. “It was a calculated act.”

In a video that aired on local TV, a terrified woman pointed her shaking camera toward the gas station and described what she was witnessing.

In this 2016 photo provided by Montrell Jackson’s wife, the 32-year-old Baton Rouge officer holds his son Mason at a Father's Day event. Mason turned 4 months the day after his father was killed.

“This man is shooting at the police,” she shouted. “He has a mask on, looking like a ninja, baby.

“He’s about to start popping again, baby. . . . Oh my God!”

Sounds of the chaos streamed through police scanners. In one recording, an officer requested additional units. A series of devastating words soon followed: “Shots fired, officer down. Shots fired, officer down. . . . I don’t know where the subject is shooting from.”

With three officers dead, Long ran back toward his car. He had left it near a carwash where he had caught sight of another officer vacuuming his vehicle. Just before Long had reached him, though, the officer pulled away, narrowly avoiding a likely firefight.

As Long approached his Chevrolet, he eyed a deputy who had just taken down his license plate and returned to his vehicle to call in the numbers.

Long shot him through the glass, leaving him in what was described on Monday as “very, very critical condition.”

He then wounded another officer.

Long, investigators believe, intended to continue his murderous rampage at the nearby police headquarters, but as he returned to the car, a member of Baton Rouge’s SWAT team trained his rifle on the killer.

He pulled the trigger.

The fatal shot, Edmonson said, was taken from more than 100 yards away.

What remains unknown is why Long — a former Marine who in online videos had called for violence against law enforcement — chose Baton Rouge or whether anyone else worked with him.

The killings came 10 days after five Dallas police officers were shot to death in an ambush at the end of a Black Lives Matter protest, and two weeks after Baton Rouge police fatally shot a black man named Alton Sterling , who had been selling CDs outside a grocery store.

The carnage on Sunday came at a time of extraordinary tension nationwide over race and policing, and it struck officers in a city that has seen some of the most heated protests against law enforcement, which were triggered by Sterling’s death.

Long, whose birthday was Sunday, once had a bright future. He served five years in the military before leaving in 2010 and later attended the University of Alabama for a semester.

In recent years, though, he began espousing unconventional and sometimes bizarre views.

Under the pen name Cosmo Setepenra, he wrote of having a “spiritual revelation” while in college that led him to Africa — his “ancestral homeland.” He described himself on one site as a “nutritionist, life coach, dietitian, personal trainer, author and spiritual advisor.”

According to law enforcement officials, Long was carrying a Washitaw Nation membership card during the shooting on Sunday. Washitaw Nation is a black nationalist movement that was once targeted by the FBI.

The founder’s son told The Washington Post that he didn’t know Long and that the group doesn’t advocate violence.

Long said in a YouTube video posted earlier this month that he had traveled to Dallas shortly after the five officers were killed there.

“I’m not gonna harp on that, you know, with a brother killing the police. You get what I’m saying?” Long said as jazz music played in the background. He called it “justice.”

Edmonson said the gunman had been in the city for “several days” before the attack. He did not say whether Long participated in any of the protests that erupted after Sterling’s death.

On Monday, President Obama called the families of each officer and ordered flags in the United States flown at half-staff until sunset on Friday to honor the officers killed in Baton Rouge, echoing an order he gave in the wake of the Dallas killings.

Among those killed were Garafola, 45, who worked as a sheriff’s deputy for 24 years. The father of four was just ending a shift at the B-Quik convenience store, where he was working extra duty, when he encountered Long.

The two Baton Rouge officers killed were Matthew Gerald and Montrell Jackson. Gerald, a 41-year-old father of two, served in the Marines and the Army before joining the police force a year ago. His friends said he served three tours in Iraq.

Jackson was a 10-year veteran who, in a recent Facebook post, had pleaded with his city to be better.

“These are trying times,” the 32-year-old wrote. “Please don’t let hate infect your heart.”

On the day after he died, his son turned 4 months old.

Theresa Vargas and Julie Tate in Washington and Bill Lodge and Amy Brittain in Baton Rouge contributed to this report.