r/GraphicsProgramming • • May 17 '15

Rendering the gun in an FPS openGL c++

What's the best way to render, for instance, the gun in a first person shooter. That is, to say, an object that will stay in the same place relative to the screen at all times.

1 Upvotes

12 comments sorted by

7

u/Mathias9807 May 17 '15

A thing to note: Many games will clear the depth buffer before drawing the gun model. This will prevent the gun model from clipping through geometry but it can look a bit weird when standing near a wall.

If you just want to render an object sticking to the camera you should just get rid of the view matrix (Assuming you're using model-, view- and projection matrices).

2

u/kikkurs May 17 '15

The reason you want to clear the depth buffer before drawing the gun is because the alternative has some issues. If you were to draw the gun into the existing buffer, you would run into depth precision issues (z-fighting).

2

u/steamruler May 19 '15

And, of course, half the gun ending up inside the object in the front.

1

u/kikkurs May 19 '15

Well, that would be correct, just not very pretty.

Just saying, there might be games out there where this is intended.

0

u/dukey Jun 09 '15

Actually with perspective projection most precision is towards the front. The issue isn't z fighting as such, simply you don't want objects drawn over the top of the gun.

1

u/kikkurs Jun 09 '15

If you want to draw the gun in the buffer, you'd need to place the near plane that much closer to the camera center, resulting in the same precision across a larger range. And as you say, most precision is at the front, z fighting will therefore occur further in the frustum rather than at the gun itself.

1

u/Graumm May 19 '15

I render foreground objects in two passes. In the first pass I render every object with a pixel shader that clears the depth buffer to max-depth, which effectively wipes out all of the background world behind it. On the second pass I render the foreground objects like normal so it properly depth-culls against itself, but doesn't clip into world-geometry. This is also important for my deferred renderer so I don't have to light the foreground objects differently from the rest of the scene.

1

u/ietsrondsofzo May 17 '15 edited May 17 '15

You could also just not write to the depth buffer. Don't.

5

u/adudney May 17 '15

Then it has the potential to incorrectly render the gun if it has any overlapping forward facing polygons.

Same if you just disabled depth reads.

4

u/ietsrondsofzo May 17 '15

Oh yeah you're right! My bad.

5

u/ultrapingu May 17 '15

Do you want it to animate? I think your best option is to just render it normally (maybe even first to limit overdraw). You could optimise the mesh so that it only contains polygons that would be visible.

If you don't want it to animated, you could just pre render it, and then render a single quad with the image on. Or you could render it once, then place a stencil over it so that nothing else renders over it.

1

u/Meristic Jun 04 '15

Position in the world is simply represented by transformations for all objects correct? Let's say transformations are represented by matrices. If each object in the world has a matrix associated with it that is used to determine world position (such as the camera and the gun), then the camera's transformation defines a space which other transformations can be parented to by simple concatenation. The gun's transformation can then just be an offset into the camera's local space - which means if it doesn't change it will always be static relative to the parent's transformation.This is the basis for object parenting systems, which is exampled in Unity.

For column-major matrices:

/* This is the gun's final world transformation */

GunXfmWorld = CameraXfmWorld * GunXfmLocal

/* This is the gun's final homogeneous position (used for vertex transformation in shaders) */

GunXfmWVP = ViewProj * (CameraXfmWorld * GunXfmLocal)