Three years after his head was smashed by a cinderblock hurled from the roof of a building, Miami-Dade Detective Carlos Castillo dramatically stepped before a jury, pushed aside the badge draped around his neck and unbuttoned his dress shirt.

He showed jurors a long straight scar from chest to belly, the visible proof of the April 2010 attack that nearly claimed his life.

But Castillo, 40, told jurors Wednesday many of the effects of the attack are less obvious.

He lost much of the strength in his right hand, so much so that he often can’t turn the key to his car. His barber, he joked, noted that he had a dent in the back of his head.

“I lost the ability to smell,” Castillo added.

The Miami-Dade detective was the first witness Wednesday in the trial against Michael Robertson, 36, who is accused of throwing the block at Castillo, then kicking the cop before stealing his police Dodge Charger and running over his bloodied body.

The testimony of Castillo, now a detective with the department’s homeland security bureau, was as dramatic as it was heart wrenching. The father of two young girls — both with birthdays the month he was attacked — described the photo of his daughter tucked into the police badge he wore that night.

Castillo, a Miami-Dade officer for 18 years, was a member of the Robbery Intervention Detail, or RID, a proactive group of detectives that patrols high-crime areas in unmarked cars.

That night, he remembered, his squad was assigned to Northside, traditionally one of the toughest areas in Miami-Dade. They grabbed dinner, got a briefing and took to the streets.

Around Northwest 71st Street, Castillo recalled, he noticed a green Chevrolet Suburban, its tag askew and expired. Suspicious, he ran the tag. The SUV did not match the description on file with the state.

Driving alone in an unmarked Dodge Charger, Castillo flipped on his emergency blue-and-red lights and blasted his siren. The SUV, while not zooming off, didn’t stop right away, eventually pulling into a dark alleyway.

A man with long dreadlocks sat in the driver’s seat. A woman was seated on the passenger side, with two children in the back seat.

The man claimed he didn’t have his license. The name he gave turned out to be bogus, Castillo recalled. As he ran the name in his car’s computer, the man suddenly took off running. Castillo quickly handcuffed the woman so as to focus on the sudden threat of the missing man.

Two RID detectives in one car arrived moments later, then took off to find the man. His memory goes blank after that, Castillo told jurors.

“What do you remember next?” prosecutor Gail Levine asked.

“Waking up in a room with people standing around,” Castillo said, recalling awaking in a hospital with now-retired Miami-Dade police director Jim Loftus looming over him.

What unfolded, prosecutors say, was that the driver, Robertson — who lived in a building located in the alley — slipped back into the area, climbed to the roof and grabbed a 30-pound cinderblock.

“Castillo had no reason to look up at the roof,” prosecutor Rebecca DiMeglio told jurors in Wednesday’s opening statement. “This man took the cinderblock, lifted it up and threw it down with all of his force.”

Officers found Castillo laying in a pool of blood, his head so injured they believed he had been shot. Castillo was taken to Ryder Trauma Center, where doctors discovered severe brain trauma, 10 broken ribs and other internal injuries.

Eyewitnesses soon identified Robertson as the attacker who jumped down from the roof, kicked Castillo, stole the cop’s Charger and ran over his body. His handcuffed girlfriend yelled out: “I don’t know why he did it,” DiMeglio said.

Among the key evidence, she said, was Robertson’s DNA and his fingerprint found in the Charger, which was abandoned a few blocks away.

“Miracles are life’s biggest mystery,” DiMeglio said. “It’s a miracle that Detective Carlos Castillo is alive today. It’s not a mystery who tried to kill Detective Castillo. This defendant.”

She pointed at Robertson, 36, who sat at the defendant’s table sporting an all-white suit, a scruffy beard and dreadlocks.

His attorney, Charles White, did not attempt to explain away the DNA or the fingerprint in his opening statement. Instead, he suggested that eyewitnesses — including one felon who did not come forward for days — tailored their statements to fit the police’s version of events.

“You will hear a lot of evidence in this case, and you’ve been instructed to keep an open mind,” White said.

Robertson, 36, is charged with the attempted murder of law enforcement officer, burglary with assault and armed carjacking. Trial is expected to last until next week in the court of Miami-Dade Circuit Judge Ellen Sue Venzer.