UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cl_view.cpp
Go to the documentation of this file.
1 
6 /*
7 All original material Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 Original file from Quake 2 v3.21: quake2-2.31/client/cl_view.c
10 Copyright (C) 1997-2001 Id Software, Inc.
11 
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 
21 See the GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 
27 */
28 
29 #include "../client.h"
30 #include "../cl_screen.h"
31 #include "../cgame/cl_game.h"
32 #include "cl_particle.h"
33 #include "cl_localentity.h"
34 #include "cl_actor.h"
35 #include "cl_hud.h"
36 #include "cl_spawn.h"
37 #include "cl_view.h"
38 #include "../renderer/r_main.h"
39 #include "../renderer/r_entity.h"
40 
44 
48 void CL_ViewLoadMedia (void)
49 {
51 
52  if (CL_GetConfigString(CS_TILES)[0] == '\0')
53  return; /* no map loaded */
54 
56 
57  float loadingPercent = 0;
58 
59  /* register models, pics, and skins */
60  SCR_DrawLoading(loadingPercent);
64 
65  loadingPercent += 10.0f;
66  SCR_DrawLoading(loadingPercent);
67 
68  LM_Register();
70 
71  int max = 0;
72  for (int i = 1; i < MAX_MODELS && CL_GetConfigString(CS_MODELS + i)[0] != '\0'; i++)
73  max++;
74 
75  max += csi.numODs;
76 
77  for (int i = 1; i < MAX_MODELS; i++) {
78  const char* name = CL_GetConfigString(CS_MODELS + i);
79  if (name[0] == '\0')
80  break;
81  SCR_DrawLoading(loadingPercent);
82  cl.model_draw[i] = R_FindModel(name);
83  if (!cl.model_draw[i]) {
84  Cmd_ExecuteString("fs_info");
85  Com_Error(ERR_DROP, "Could not load model '%s'\n", name);
86  }
87 
88  /* initialize clipping for bmodels */
89  if (name[0] == '*')
91  else
92  cl.model_clip[i] = nullptr;
93 
94  loadingPercent += 100.0f / (float)max;
95  }
96 
97  /* update le model references */
98  le_t* le = nullptr;
99  while ((le = LE_GetNextInUse(le))) {
100  if (le->modelnum1 > 0)
101  le->model1 = LE_GetDrawModel(le->modelnum1);
102  if (le->modelnum2 > 0)
103  le->model2 = LE_GetDrawModel(le->modelnum2);
104  }
105 
106  refdef.ready = true;
107 
108  /* waiting for EV_START */
110 }
111 
117 static float CL_PrecacheCharacterModels (float alreadyLoadedPercent)
118 {
119  if (!cl_precache->integer)
120  return 0;
121 
122  const float percent = 40.0f;
123  /* search the name */
124  for (int i = 0; i < csi.numTeamDefs; i++) {
125  teamDef_t* td = &csi.teamDef[i];
126  for (int j = NAME_NEUTRAL; j < NAME_LAST; j++) {
127  /* search one of the model definitions */
128  for (linkedList_t const* list = td->models[j]; list; list = list->next) {
129  teamDef_t::model_t const& m = *static_cast<teamDef_t::model_t const*>(list->data);
130  /* register body */
131  char model[MAX_QPATH];
132  Com_sprintf(model, sizeof(model), "%s/%s", m.path, m.body);
133  if (!R_FindModel(model))
134  Com_Printf("Com_PrecacheCharacterModels: Could not register model %s\n", model);
135  /* register head */
136  Com_sprintf(model, sizeof(model), "%s/%s", m.path, m.head);
137  if (!R_FindModel(model))
138  Com_Printf("Com_PrecacheCharacterModels: Could not register model %s\n", model);
139 
140  alreadyLoadedPercent += percent / (td->numModels[j] * csi.numTeamDefs * NAME_LAST);
141  SCR_DrawLoadingScreen(true, alreadyLoadedPercent);
142  }
143  }
144  }
145  /* some genders may not have models - ensure that we do the wanted percent step */
146  return percent;
147 }
148 
153 {
154  float percent = 30.0f;
155  float alreadyLoadedPercent = 30.0f;
156 
157  const float loaded = CL_PrecacheCharacterModels(alreadyLoadedPercent);
158  alreadyLoadedPercent += loaded;
159  if (loaded == 0)
160  percent = 100 - alreadyLoadedPercent;
161 
162  for (int i = 0; i < csi.numODs; i++) {
163  const objDef_t* od = INVSH_GetItemByIDX(i);
164 
165  alreadyLoadedPercent += percent / csi.numODs;
166  SCR_DrawLoadingScreen(true, alreadyLoadedPercent);
167 
168  if (od->type[0] == '\0' || od->isDummy)
169  continue;
170 
171  if (od->model[0] != '\0') {
172  cls.modelPool[i] = R_FindModel(od->model);
173  if (cls.modelPool[i])
174  Com_DPrintf(DEBUG_CLIENT, "CL_PrecacheModels: Registered object model: '%s' (%i)\n", od->model, i);
175  }
176  }
177 
178  /* now make sure that all the precached models are stored until we quit the game
179  * otherwise they would be freed with every map change */
181 
182  SCR_DrawLoadingScreen(false, 100.f);
183 }
184 
190 {
191  if (cl_isometric->integer) {
192  const float zoom = 3.6 * (cl.cam.zoom - cl_camzoommin->value) + 0.3 * cl_camzoommin->value;
193  refdef.fieldOfViewX = std::max(std::min(FOV / zoom, 140.0), 1.0);
194  } else {
195  refdef.fieldOfViewX = std::max(std::min(FOV / cl.cam.zoom, 95.0), 55.0);
196  }
197 }
198 
202 static inline void CL_ViewCalcFieldOfViewY (const float width, const float height)
203 {
204  refdef.fieldOfViewY = atan(tan(refdef.fieldOfViewX * (M_PI / 360.0)) * (height / width)) * (360.0 / M_PI);
205 }
206 
211 {
214 
216 
217  /* setup refdef */
218  refdef.time = cl.time * 0.001;
220 }
221 
225 void CL_ViewRender (void)
226 {
227  refdef.brushCount = 0;
228  refdef.aliasCount = 0;
229  refdef.batchCount = 0;
233 
234  if (cls.state != ca_active)
235  return;
236 
238  return;
239 
240  /* still loading */
241  if (!refdef.ready)
242  return;
243 
244  refdef.numEntities = 0;
246 
247  /* tell the bsp thread to start */
249  /* make sure we are really rendering the world */
251  /* add local models to the renderer chain */
252  LM_AddToScene();
253  /* add local entities to the renderer chain */
254  LE_AddToScene();
255 
256  /* adds pathing data */
257  if (cl_map_displayavailablecells->integer) {
259  }
260 
261  if (cl_map_debug->integer) {
262  if (cl_map_debug->integer & MAPDEBUG_PATHING)
263  CL_AddPathing();
264  /* adds floor arrows */
265  if (cl_map_debug->integer & MAPDEBUG_CELLS)
267  /* adds wall arrows */
268  if (cl_map_debug->integer & MAPDEBUG_WALLS)
270  }
271 
272 
273  /* adds target cursor */
274  CL_AddTargeting();
275 
276  /* update ref def */
278 
279  /* render the world */
280  R_RenderFrame();
281 }
282 
290 {
291  vec3_t vec;
292 
293  PosToVec(pos, vec);
294  VectorCopy(vec, cl.cam.origin);
295  Cvar_SetValue("cl_worldlevel", pos[2]);
296 }
297 
298 void CL_ViewInit (void)
299 {
300  cl_precache = Cvar_Get("cl_precache", "0", CVAR_ARCHIVE, "Precache character models at startup - more memory usage but smaller loading times in the game");
301  cl_map_displayavailablecells = Cvar_Get("cl_map_displayavailablecells", "0", 0, "Display cells where a soldier can move");
302  cl_map_draw_rescue_zone = Cvar_Get("cl_map_draw_rescue_zone", "2", 0, "Draw rescue zone: 1 - draw perimeter, 2 - draw circles on the ground, 3 - draw both");
303 }
#define CS_MAPTITLE
Definition: q_shared.h:310
void CL_AddPathing(void)
Adds a pathing marker to the current floor when we render the world.
Definition: cl_actor.cpp:2089
void CL_ViewInit(void)
Definition: cl_view.cpp:298
#define VectorCopy(src, dest)
Definition: vector.h:51
cvar_t * cl_map_debug
Definition: cl_view.cpp:41
void CL_AddTargeting(void)
Adds a target cursor when we render the world.
Definition: cl_actor.cpp:1981
int viewHeight
Definition: cl_video.h:76
void SCR_DrawLoading(int percent)
Draws the current loading pic of the map from base/pics/maps/loading.
Definition: cl_screen.cpp:140
renderer_threadstate_t r_threadstate
Definition: r_thread.cpp:34
#define FOV
Definition: cl_camera.h:42
#define MAPDEBUG_CELLS
Definition: cl_view.h:65
void CL_ViewPrecacheModels(void)
Precaches all models at game startup - for faster access.
Definition: cl_view.cpp:152
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
#define _(String)
Definition: cl_shared.h:43
char * CL_GetConfigString(int index)
static cvar_t * cl_precache
Definition: cl_view.cpp:42
void SCR_DrawLoadingScreen(bool string, int percent)
Precache and loading screen at startup.
Definition: cl_screen.cpp:100
void CL_AddActorPathing(void)
Adds an actor pathing marker to the current floor when we render the world.
Definition: cl_actor.cpp:2109
void CL_SpawnParseEntitystring(void)
Parse the map entity string and spawns those entities that are client-side only.
Definition: cl_spawn.cpp:308
csi_t csi
Definition: common.cpp:39
float fieldOfViewX
Definition: cl_renderer.h:171
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
void LM_AddToScene(void)
Add the local models to the scene.
#define CS_MAPZONE
Definition: q_shared.h:322
void R_ModBeginLoading(const char *tiles, bool day, const char *pos, const char *mapName, const char *mapZone)
Specifies the model that will be used as the world.
void CL_ViewCenterAtGridPosition(const pos3_t pos)
Centers the camera on a given grid field.
Definition: cl_view.cpp:289
void CL_DisplayObstructionArrows(void)
Useful for debugging pathfinding.
Definition: cl_actor.cpp:2180
float value
Definition: cvar.h:80
viddef_t viddef
Definition: cl_video.cpp:34
vec3_t viewAngles
Definition: cl_renderer.h:173
linkedList_t * models[NAME_LAST]
Definition: chr_shared.h:314
#define MAPDEBUG_PATHING
Definition: cl_view.h:64
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
model_t * LE_GetDrawModel(unsigned int index)
int numODs
Definition: q_shared.h:518
int CL_GetConfigStringInteger(int index)
Defines all attributes of objects used in the inventory.
Definition: inv_shared.h:264
const char * type
Definition: inv_shared.h:271
#define CS_POSITIONS
Definition: q_shared.h:326
int integer
Definition: cvar.h:81
#define CVAR_ARCHIVE
Definition: cvar.h:40
static void CL_ViewCalcFieldOfViewY(const float width, const float height)
Definition: cl_view.cpp:202
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
void Cmd_ExecuteString(const char *text,...)
A complete command line has been parsed, so try to execute it.
Definition: cmd.cpp:1007
vec3_t viewOrigin
Definition: cl_renderer.h:172
mapTiles_t * mapTiles
client_static_t cls
Definition: cl_main.cpp:83
#define CS_MODELS
Definition: q_shared.h:327
cvar_t * cl_worldlevel
Definition: cl_hud.cpp:46
#define ERR_DROP
Definition: common.h:211
#define DEBUG_CLIENT
Definition: defines.h:59
vec3_t origin
Definition: cl_camera.h:31
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition: cvar.cpp:342
void CL_ParticleRegisterArt(void)
model_t * modelPool[MAX_OBJDEFS]
Definition: client.h:96
cvar_t * cl_map_draw_rescue_zone
rendererData_t refdef
Definition: r_main.cpp:45
model_t * model_draw[MAX_MODELS]
void GAME_InitMissionBriefing(const char *title)
Definition: cl_game.cpp:1502
#define M_PI
Definition: mathlib.h:34
void SCR_EndLoadingPlaque(void)
Definition: cl_screen.cpp:277
float fieldOfViewY
Definition: cl_renderer.h:171
const char * model
Definition: inv_shared.h:269
#define MAPDEBUG_WALLS
Definition: cl_view.h:66
#define CS_NAME
Definition: q_shared.h:309
void R_SwitchModelMemPoolTag(void)
After all static models are loaded, switch the pool tag for these models to not free them everytime R...
Definition: r_model.cpp:299
#define PosToVec(p, v)
Pos boundary size is +/- 128 - to get into the positive area we add the possible max negative value a...
Definition: mathlib.h:110
model_t * model1
clientBattleScape_t cl
void CL_ViewRender(void)
Definition: cl_view.cpp:225
void CL_ViewCalcFieldOfViewX(void)
Calculates refdef's FOV_X. Should generally be called after any changes are made to the zoom level (v...
Definition: cl_view.cpp:189
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition: common.cpp:398
model_t * R_FindModel(const char *name)
Tries to load a model.
Definition: r_model.cpp:203
teamDef_t teamDef[MAX_TEAMDEFS]
Definition: q_shared.h:548
threadstate_t state
Definition: cl_renderer.h:227
pos_t pos3_t[3]
Definition: ufotypes.h:58
model_t * model2
const struct cBspModel_s * model_clip[MAX_MODELS]
cvar_t * cl_isometric
Definition: cl_input.cpp:77
int numModels[NAME_LAST]
Definition: chr_shared.h:315
#define RDF_NOWORLDMODEL
Definition: cl_renderer.h:34
void LM_Register(void)
Register misc_models.
cBspModel_t * CM_InlineModel(const mapTiles_t *mapTiles, const char *name)
Searches all inline models and return the cBspModel_t pointer for the given modelnumber or -name...
Definition: bsp.cpp:929
a local entity
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
le_t * LE_GetNextInUse(le_t *lastLE)
Iterate through the entities that are in use.
#define MAX_MODELS
Definition: defines.h:100
int numTeamDefs
Definition: q_shared.h:549
static cvar_t * cl_map_displayavailablecells
Definition: cl_view.cpp:43
#define MAX_QPATH
Definition: filesys.h:40
QGL_EXTERN GLint i
Definition: r_gl.h:113
int viewWidth
Definition: cl_video.h:76
int shaderToShaderCount
Definition: cl_renderer.h:178
unsigned int modelnum2
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
bool isDummy
Definition: inv_shared.h:290
void CL_ViewUpdateRenderData(void)
Updates the refdef.
Definition: cl_view.cpp:210
#define CS_TILES
Definition: q_shared.h:325
#define CS_LIGHTMAP
Definition: q_shared.h:321
vec3_t angles
Definition: cl_camera.h:34
HUD related routines.
void LE_AddToScene(void)
vec_t vec3_t[3]
Definition: ufotypes.h:39
linkedList_t * next
Definition: list.h:32
const objDef_t * INVSH_GetItemByIDX(int index)
Returns the item that belongs to the given index or nullptr if the index is invalid.
Definition: inv_shared.cpp:266
vec3_t camorg
Definition: cl_camera.h:32
static float CL_PrecacheCharacterModels(float alreadyLoadedPercent)
Precache all menu models for faster access.
Definition: cl_view.cpp:117
float zoom
Definition: cl_camera.h:39
connstate_t state
Definition: client.h:55
void R_RenderFrame(void)
Definition: r_main.cpp:298
void Cvar_SetValue(const char *varName, float value)
Expands value to a string and calls Cvar_Set.
Definition: cvar.cpp:671
static struct mdfour * m
Definition: md4.cpp:35
void CL_ViewLoadMedia(void)
Call before entering a new level, or after vid_restart.
Definition: cl_view.cpp:48
void CL_DisplayFloorArrows(void)
Useful for debugging pathfinding.
Definition: cl_actor.cpp:2166
mapTiles_t * mapTiles
Definition: cl_renderer.h:203
unsigned int modelnum1
cvar_t * cl_camzoommin
Definition: cl_camera.cpp:68