Author Topic: Breaking up game_render_frame  (Read 1142 times)

0 Members and 1 Guest are viewing this topic.

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Breaking up game_render_frame
I figured this would be a good first step to enable further work with the rendering system. It's nothing specific enough that it would prevent certain options, once it comes time to go further with it, but hopefully it will make future tasks easier.

As far as I can tell, what I've done with it in this patch doesn't actually change what it does, it just makes it more modular. Some of that modularity might not be quite necessary... I'd like to hear some opinions from the coders on this.

Edit 1.3: pastebin link for the actual functions code (for readability):

Code: [Select]
// Functions called within game_render_frame ( code moved out of game_render_frame by Aardwolf )
void game_render_frame_place_camera( camid cid, matrix &eye_no_jitter );
void game_render_frame_prerender(camid cid);
void game_render_frame_background();
void game_render_frame_scene_objects(bool *draw_viewer_last); // by pointer or by reference?
void game_render_frame_foreground(bool draw_viewer_last);
void game_render_frame_debug_or_release();
void game_render_frame_cleanup();

// Does everything needed to render a frame
void game_render_frame( camid cid )
{
g3_start_frame(game_zbuffer);

matrix eye_no_jitter;
game_render_frame_place_camera(cid, eye_no_jitter); // Put the camera in the right spot
game_render_frame_prerender(cid); // Get final pre-render state of env map (and shields, in multi)

game_render_frame_background(); // Draw the background (possibly including sun bitmaps ?)

game_sunspot_process(flFrametime); // Calculations for suns as light sources, as well as the whiteout from looking directly at them

bool draw_viewer_last = false; // Value may be modified... depending on the ship class you're flying, the Show Ship flag may affect this
game_render_frame_scene_objects(&draw_viewer_last); // Drawing the dynamic 3d stuff (e.g. ships, lazorz, explody things, etc.)
game_render_frame_foreground(draw_viewer_last); // Now for the Show Ship cockpit (as opposed to a separate cockpit pof) and local neb poofs

game_render_frame_debug_or_release();

if(!Cmdline_nohtl)
{
gr_end_proj_matrix();
gr_end_view_matrix();
}

// Draw viewer cockpit (i.e. separate-pof-style) ... the coordinate system is camera coords, so we don't need much work for that
if(Viewer_obj != NULL && Viewer_mode != VM_TOPDOWN)
{
gr_zbuffer_clear(TRUE);
ship_render_cockpit(Viewer_obj);
}
//================ END OF 3D RENDERING STUFF ====================

game_render_frame_cleanup();

g3_end_frame();
}

// Camera placement/orientation setup function, moved from game_render_frame
// Note, this provides an eye matrix to which camera jitter (from shudder, etc.) has not been applied
// It also is responsible for some other HUD drawing-related preparations
void game_render_frame_place_camera( camid cid, matrix &eye_no_jitter )
{
camera *cam = cid.getCamera();
eye_no_jitter = vmd_identity_matrix;
if(cam != NULL)
{
vec3d eye_pos;
matrix eye_orient;

//Get current camera info
cam->get_info(&eye_pos, &eye_orient);

//Handle jitter if not cutscene camera
eye_no_jitter = eye_orient;
if( !(Viewer_mode & VM_FREECAMERA) ) {
apply_view_shake(&eye_orient);
cam->set_rotation(&eye_orient);
}

//Maybe override FOV from SEXP
if(Sexp_fov <= 0.0f)
g3_set_view_matrix(&eye_pos, &eye_orient, cam->get_fov());
else
g3_set_view_matrix(&eye_pos, &eye_orient, Sexp_fov);
}
else
{
g3_set_view_matrix(&vmd_zero_vector, &vmd_identity_matrix, VIEWER_ZOOM_DEFAULT);
}

// maybe offset the HUD (jitter stuff) and measure the 2D displacement between the player's view and ship vector
int dont_offset = ((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER));
HUD_set_offsets(Viewer_obj, !dont_offset, &eye_no_jitter);
}

// Certain things (notably the env map, also shield in multiplayer) must be updated here instead of in the normal update code
void game_render_frame_prerender( camid cid)
{
// for multiplayer clients, call code in Shield.cpp to set up the Shield_hit array.  Have to
// do this becaues of the disjointed nature of this system (in terms of setup and execution).
// must be done before ships are rendered
if ( MULTIPLAYER_CLIENT ) {
shield_point_multi_setup();
}

// this needs to happen after g3_start_frame() and before the primary projection and view matrix is setup
if ( Cmdline_env && !Env_cubemap_drawn ) {
setup_environment_mapping(cid);

if ( !Dynamic_environment ) {
Env_cubemap_drawn = true;
}
}

#ifndef DYN_CLIP_DIST
if (!Cmdline_nohtl) {
gr_set_proj_matrix(Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
#endif
}

// Not sure if this draws the suns, but if definitely does NOT do lighting/lens flare from suns
void game_render_frame_background()
{
if ( Game_subspace_effect ) {
stars_draw(0,0,0,1,0);
} else {
stars_draw(1,1,1,0,0);
}
}

// Draws all of the things actually in the scene (i.e. not background, and not the player ship (if it is showing))
void game_render_frame_scene_objects(bool *draw_viewer_last)
{
obj_render_all(obj_render, draw_viewer_last);

//mflash_render_all(); // render all muzzle flashes
// Why do we not show the shield effect in these modes?  Seems ok.
//if (!(Viewer_mode & (VM_EXTERNAL | VM_SLEWED | VM_CHASE | VM_DEAD_VIEW))) {
render_shields();
//}
particle_render_all(); // render particles after everything else.
#ifdef DYN_CLIP_DIST
if(!Cmdline_nohtl)
{
gr_end_proj_matrix();
gr_end_view_matrix();
gr_set_proj_matrix(Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
#endif

beam_render_all(); // render all beam weapons
trail_render_all(); // render missilie trails after everything else.

// render nebula lightning
nebl_render_all();
}
// Draws stuff in front of everything else
// Stuff like the cockpit of your ship, if you have the show ship flag (not the same as a separate pof cockpit)
// Also local nebula poofs
void game_render_frame_foreground(bool draw_viewer_last)
{
// render local player nebula
neb2_render_player();

//Draw the viewer 'cause we didn't before.
//This is so we can change the minimum clipping distance without messing everything up.
if(draw_viewer_last && Viewer_obj)
{
gr_zbuffer_clear(TRUE);
ship_render(Viewer_obj);
}
}

void game_render_frame_cleanup()
{
extern int Multi_display_netinfo;
if(Multi_display_netinfo){
extern void multi_display_netinfo();
multi_display_netinfo();
}

game_tst_frame_pre();

#ifndef NDEBUG
do_timing_test(flFrametime);

extern int OO_update_index;
multi_rate_display(OO_update_index, 375, 0);

// test
extern void oo_display();
oo_display();
#endif
}

void game_render_frame_debug_or_release()
{
#ifndef NDEBUG
ai_debug_render_stuff();
#endif

#ifndef RELEASE_REAL
// game_framerate_check();
#endif

#ifndef NDEBUG
extern void snd_spew_debug_info();
snd_spew_debug_info();
#endif
}


Edit 2.1: Trying again to get the .patch file up properly...

Code: [Select]
Index: freespace.cpp
===================================================================
--- freespace.cpp (revision 5595)
+++ freespace.cpp (working copy)
@@ -3841,13 +3841,60 @@
 int Game_subspace_effect = 0;
 DCF_BOOL( subspace, Game_subspace_effect )
 
+// Functions called within game_render_frame ( code moved out of game_render_frame by Aardwolf )
+void game_render_frame_place_camera( camid cid, matrix &eye_no_jitter );
+void game_render_frame_prerender(camid cid);
+void game_render_frame_background();
+void game_render_frame_scene_objects(bool *draw_viewer_last); // by pointer or by reference?
+void game_render_frame_foreground(bool draw_viewer_last);
+void game_render_frame_debug_or_release();
+void game_render_frame_cleanup();
+
 // Does everything needed to render a frame
 void game_render_frame( camid cid )
 {
  g3_start_frame(game_zbuffer);
 
+ matrix eye_no_jitter;
+ game_render_frame_place_camera(cid, eye_no_jitter); // Put the camera in the right spot
+ game_render_frame_prerender(cid); // Get final pre-render state of env map (and shields, in multi)
+
+ game_render_frame_background(); // Draw the background (possibly including sun bitmaps ?)
+
+ game_sunspot_process(flFrametime); // Calculations for suns as light sources, as well as the whiteout from looking directly at them
+
+ bool draw_viewer_last = false; // Value may be modified... depending on the ship class you're flying, the Show Ship flag may affect this
+ game_render_frame_scene_objects(&draw_viewer_last); // Drawing the dynamic 3d stuff (e.g. ships, lazorz, explody things, etc.)
+ game_render_frame_foreground(draw_viewer_last); // Now for the Show Ship cockpit (as opposed to a separate cockpit pof) and local neb poofs
+
+ game_render_frame_debug_or_release();
+
+ if(!Cmdline_nohtl)
+ {
+ gr_end_proj_matrix();
+ gr_end_view_matrix();
+ }
+
+ // Draw viewer cockpit (i.e. separate-pof-style) ... the coordinate system is camera coords, so we don't need much work for that
+ if(Viewer_obj != NULL && Viewer_mode != VM_TOPDOWN)
+ {
+ gr_zbuffer_clear(TRUE);
+ ship_render_cockpit(Viewer_obj);
+ }
+ //================ END OF 3D RENDERING STUFF ====================
+
+ game_render_frame_cleanup();
+
+ g3_end_frame();
+}
+
+// Camera placement/orientation setup function, moved from game_render_frame
+// Note, this provides an eye matrix to which camera jitter (from shudder, etc.) has not been applied
+// It also is responsible for some other HUD drawing-related preparations
+void game_render_frame_place_camera( camid cid, matrix &eye_no_jitter )
+{
  camera *cam = cid.getCamera();
- matrix eye_no_jitter = vmd_identity_matrix;
+ eye_no_jitter = vmd_identity_matrix;
  if(cam != NULL)
  {
  vec3d eye_pos;
@@ -3877,7 +3924,11 @@
  // maybe offset the HUD (jitter stuff) and measure the 2D displacement between the player's view and ship vector
  int dont_offset = ((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER));
  HUD_set_offsets(Viewer_obj, !dont_offset, &eye_no_jitter);
+}
 
+// Certain things (notably the env map, also shield in multiplayer) must be updated here instead of in the normal update code
+void game_render_frame_prerender( camid cid)
+{
  // for multiplayer clients, call code in Shield.cpp to set up the Shield_hit array.  Have to
  // do this becaues of the disjointed nature of this system (in terms of setup and execution).
  // must be done before ships are rendered
@@ -3894,25 +3945,29 @@
  }
  }
 
-#ifndef DYN_CLIP_DIST
+ #ifndef DYN_CLIP_DIST
  if (!Cmdline_nohtl) {
  gr_set_proj_matrix(Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
  gr_set_view_matrix(&Eye_position, &Eye_matrix);
  }
-#endif
+ #endif
+}
 
+// Not sure if this draws the suns, but if definitely does NOT do lighting/lens flare from suns
+void game_render_frame_background()
+{
  if ( Game_subspace_effect ) {
  stars_draw(0,0,0,1,0);
  } else {
  stars_draw(1,1,1,0,0);
  }
+}
 
- // Do the sunspot
- game_sunspot_process(flFrametime);
+// Draws all of the things actually in the scene (i.e. not background, and not the player ship (if it is showing))
+void game_render_frame_scene_objects(bool *draw_viewer_last)
+{
+ obj_render_all(obj_render, draw_viewer_last);
 
- bool draw_viewer_last = false;
- obj_render_all(obj_render, &draw_viewer_last);
-
  //mflash_render_all(); // render all muzzle flashes
  // Why do we not show the shield effect in these modes?  Seems ok.
  //if (!(Viewer_mode & (VM_EXTERNAL | VM_SLEWED | VM_CHASE | VM_DEAD_VIEW))) {
@@ -3934,7 +3989,12 @@
 
  // render nebula lightning
  nebl_render_all();
-
+}
+// Draws stuff in front of everything else
+// Stuff like the cockpit of your ship, if you have the show ship flag (not the same as a separate pof cockpit)
+// Also local nebula poofs
+void game_render_frame_foreground(bool draw_viewer_last)
+{
  // render local player nebula
  neb2_render_player();
 
@@ -3945,35 +4005,10 @@
  gr_zbuffer_clear(TRUE);
  ship_render(Viewer_obj);
  }
+}
 
-
-#ifndef NDEBUG
- ai_debug_render_stuff();
-#endif
-
-#ifndef RELEASE_REAL
-// game_framerate_check();
-#endif
-
-#ifndef NDEBUG
- extern void snd_spew_debug_info();
- snd_spew_debug_info();
-#endif
-
- if(!Cmdline_nohtl)
- {
- gr_end_proj_matrix();
- gr_end_view_matrix();
- }
-
- //Draw viewer cockpit
- if(Viewer_obj != NULL && Viewer_mode != VM_TOPDOWN)
- {
- gr_zbuffer_clear(TRUE);
- ship_render_cockpit(Viewer_obj);
- }
- //================ END OF 3D RENDERING STUFF ====================
-
+void game_render_frame_cleanup()
+{
  extern int Multi_display_netinfo;
  if(Multi_display_netinfo){
  extern void multi_display_netinfo();
@@ -3992,10 +4027,24 @@
  extern void oo_display();
  oo_display();
 #endif
-
- g3_end_frame();
 }
 
+void game_render_frame_debug_or_release()
+{
+#ifndef NDEBUG
+ ai_debug_render_stuff();
+#endif
+
+#ifndef RELEASE_REAL
+// game_framerate_check();
+#endif
+
+#ifndef NDEBUG
+ extern void snd_spew_debug_info();
+ snd_spew_debug_info();
+#endif
+}
+
 //#define JOHNS_DEBUG_CODE 1
 
 #ifdef JOHNS_DEBUG_CODE

« Last Edit: September 14, 2009, 01:05:16 pm by Aardwolf »