Tutorial: Adding r_viewmodeloffset to GLQuake/WinQuake
The r_viewmodeloffset console variable allows you to position the gun to the left or right by any amount you choose. One example is using r_viewmodeloffset 5 or -5 to shift the gun to the left or right.
This code isn't renderer specific so both GLQuake and WinQuake receive the functionality.
[It can also position higher or farther in front if you want via r_viewmodeloffset "0 5 6". But that doesn't appear to be commonly used.]
Instuctions adding it to stock GLQuake/WinQuake
1. First we need 1 extra addition to mathlib so open mathlib.h
//Add this definition to very bottom
int ParseFloats(char *s, float *f, int *f_size);
2. Now open mathlib.c and add the function itself
- Code:
// Add this to the bottom if you prefer.
int ParseFloats(char *s, float *f, int *f_size) {
int i, argc;
if (!s || !f || !f_size)
Sys_Error("ParseFloats() wrong params");
if (f_size[0] <= 0)
return (f_size[0] = 0); // array have no size, unusual but no crime
Cmd_TokenizeString(s);
argc = min(Cmd_Argc(), f_size[0]);
for(i = 0; i < argc; i++)
f[i] = Q_atof(Cmd_Argv(i));
for( ; i < f_size[0]; i++)
f[i] = 0; // zeroing unused elements
return (f_size[0] = argc);
}
3. Open view.c - Near the top, find "cvar_t crosshair" (about 60 lines from top).
//Add this after crosshair cvar declaration
// the "true" means it saves to config
cvar_t r_viewmodeloffset = {"r_viewmodeloffset", "0", true};
4. Still in view.c, find void V_CalcRefdef (void) around line 850-860
Locate this rotten code:
Replace it with this ...
- Code:
#if 0
for (i=0 ; i<3 ; i++)
{
view->origin[i] += forward[i]*bob*0.4;
// view->origin[i] += right[i]*bob*0.4;
// view->origin[i] += up[i]*bob*0.8;
}
view->origin[2] += bob;
#endif
#if 1
VectorCopy (r_refdef.vieworg, view->origin);
VectorMA (view->origin, bob * 0.4, forward, view->origin);
if (r_viewmodeloffset.string[0]) {
float offset[3];
int size = sizeof(offset)/sizeof(offset[0]);
ParseFloats(r_viewmodeloffset.string, offset, &size);
VectorMA (view->origin, offset[0], right, view->origin);
VectorMA (view->origin, -offset[1], up, view->origin);
VectorMA (view->origin, offset[2], forward, view->origin);
}
#endif
5. In view.c, find the section where Cvar_RegisterVariable is. Add this one in there as such ...
You are done. Compile + start up Quake and type r_viewmodeloffset 5 in the console. Or type r_viewmodeloffset -5 in the console.
I'm pretty sure this code made it's first appearance in FTEQW back in 2005 maybe.
