War changes people. That fact is evident in real life, yet rarely expressed in movies until the 1970s, when films about Vietnam began to emerge. The perceived unrighteousness of that war made the fact that it screwed up its veterans all the more poignant: At least, Hollywood seemed to say, the soldiers who were scarred and damaged in World War II had been fighting for a just cause.

The Persian Gulf War of 1990 was neither lengthy, devastating nor controversial enough to inspire films the way Vietnam did, and it came at a time when a new attitude was sweeping the nation: ironic detachment. Hence films like “Three Kings,” and now “Jarhead,” a humor-tinged but ultimately haunting drama that examines the psychology of a Gulf War veteran as carefully and insightfully as any film has done.

Based on LCpl. Anthony Swofford’s autobiography detailing his stint in the Gulf, “Jarhead” stars Jake Gyllenhaal as Swofford, depicted as a directionless but highly intelligent 20-year-old from a broken home who joins the Marines in 1989 because he has nothing else to do. The military is in Swofford’s blood, perhaps literally: He was conceived in 1969 while his Marine father was on a weekend pass in Hawaii.

Swofford is recruited to a team of snipers, led by Staff Sgt. Siek (Jamie Foxx) as a raucous band of brothers who participate in the usual ultra-macho boys-will-be-boys jackassery while learning how to kill their enemies.

When Saddam invades Kuwait and the troops are sent to the Middle East to see what happens next, Swofford and his fellow soldiers are itching to fight, bursting with equal parts testosterone and patriotism. One soldier, a cynical Texan troublemaker named Kruger (Lucas Black), says the war is all about oil and that the U.S. gave Saddam his weapons in the first place. Kruger is shouted down and the matter isn’t discussed again. (Indeed, modern viewers looking for “Jarhead” to make comments on the current situation in Iraq will be stymied: This is an intentionally non-political film.)

The soldiers follow the line of thinking promoted by Lt. Col. Kazinski (Chris Cooper), who rallies them with talk of kicking some Iraqi behind. These are men who, in one scene, watch “Apocalypse Now” the way some people watch “Rocky Horror Picture Show,” reciting the lines and singing along with “Ride of the Valkyries.”

The Vietnam War plays a major part in the way these men think. Swofford’s father was a veteran, and while we don’t know the parentage of the other snipers, we know they were born at a time when Vietnam overshadowed most of American life. Movies like “Apocalypse Now” have shown them how thrilling war can be (which means they kind of missed the point), and now it’s their chance to create their own history. When a helicopter flies over their heads blasting music by The Doors, Swofford says bitterly, “That’s Vietnam music. Can’t we get our own [expletive] music?”

The answer is no. Even when, after 175 days of idling, Desert Storm begins, it’s over quickly, and ground troops like Swofford’s crew play a comparatively small part. Most of their days are spent training, sitting around and, um, pleasuring themselves — a clear metaphor for these soldiers’ military experience: putting forth a lot of effort without actually accomplishing anything.

The kicker is that, even though most of the danger he faces is in the form of “friendly fire” (i.e., stupid accidents), Swofford, along with his embittered sniping partner Troy (Peter Sarsgaard), winds up with a psyche almost as scarred as if he’d seen heavy action. Played by Jake Gyllenhaal in his first truly grown-up, completely three-dimensional role, Swofford sees pointlessness and futility at every turn, always training but never getting to experience the catharsis of putting his training to work. He warms the bench for the whole game, yet winds up bruised as if he’d played.

This is director Sam Mendes’ third film, after “American Beauty” and “Road to Perdition,” two films also marked by grace and introspection amid depressing, deadly circumstances. It’s noteworthy that Swofford’s post-war life gets almost no screen time, yet his wartime experience is portrayed so vividly and efficiently — not a scene wasted in William Broyles Jr.’s screenplay — that we know just how it’s going to be when he gets home. He’ll be a changed man, and he’ll being asking himself why.

A- (2 hrs., 3 min.; R, non-stop F-bombs, a lot of crude dialogue, some strong sexuality, a lot of nudity (a little of it sexual), some violence and grisly images.)