-
-/*
-==============================================================================
-
- SCREEN SHOTS
-
-==============================================================================
-*/
-
-qboolean SCR_ScreenShot(char *filename, unsigned char *buffer1, unsigned char *buffer2, unsigned char *buffer3, int x, int y, int width, int height, qboolean flipx, qboolean flipy, qboolean flipdiagonal, qboolean jpeg, qboolean gammacorrect)
-{
- int indices[3] = {0,1,2};
- qboolean ret;
-
- if (!r_render.integer)
- return false;
-
- qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer1);
- CHECKGLERROR
-
- if (scr_screenshot_gamma.value != 1 && gammacorrect)
- {
- int i;
- double igamma = 1.0 / scr_screenshot_gamma.value;
- unsigned char ramp[256];
- for (i = 0;i < 256;i++)
- ramp[i] = (unsigned char) (pow(i * (1.0 / 255.0), igamma) * 255.0);
- for (i = 0;i < width*height*3;i++)
- buffer1[i] = ramp[buffer1[i]];
- }
-
- Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
-
- if (jpeg)
- ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
- else
- ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer2, buffer3);
-
- return ret;
-}
-
-//=============================================================================
-
-void R_ClearScreen(void)
-{
- if (r_render.integer)
- {
- // clear to black
- if (fogenabled)
- qglClearColor(fogcolor[0],fogcolor[1],fogcolor[2],0);
- else
- qglClearColor(0,0,0,0);
- CHECKGLERROR
- qglClearDepth(1);CHECKGLERROR
- if (gl_stencil)
- {
- // LordHavoc: we use a stencil centered around 128 instead of 0,
- // to avoid clamping interfering with strange shadow volume
- // drawing orders
- qglClearStencil(128);CHECKGLERROR
- }
- // clear the screen
- GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));
- // set dithering mode
- if (gl_dither.integer)
- {
- qglEnable(GL_DITHER);CHECKGLERROR
- }
- else
- {
- qglDisable(GL_DITHER);CHECKGLERROR
- }
- }
-}
-
-qboolean CL_VM_UpdateView (void);
-void SCR_DrawConsole (void);
-
-int r_stereo_side;
-
-void SCR_DrawScreen (void)
-{
- R_Mesh_Start();
-
- R_TimeReport("setup");
-
- if (cls.signon == SIGNONS)
- {
- float size;
-
- size = scr_viewsize.value * (1.0 / 100.0);
- size = min(size, 1);
-
- if (r_stereo_sidebyside.integer)
- {
- r_refdef.width = vid.width * size / 2.5;
- r_refdef.height = vid.height * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100);
- r_refdef.x = (vid.width - r_refdef.width * 2.5) * 0.5;
- r_refdef.y = (vid.height - r_refdef.height)/2;
- if (r_stereo_side)
- r_refdef.x += r_refdef.width * 1.5;
- }
- else
- {
- r_refdef.width = vid.width * size;
- r_refdef.height = vid.height * size * (1 - bound(0, r_letterbox.value, 100) / 100);
- r_refdef.x = (vid.width - r_refdef.width)/2;
- r_refdef.y = (vid.height - r_refdef.height)/2;
- }
-
- // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
- // LordHavoc: this is designed to produce widescreen fov values
- // when the screen is wider than 4/3 width/height aspect, to do
- // this it simply assumes the requested fov is the vertical fov
- // for a 4x3 display, if the ratio is not 4x3 this makes the fov
- // higher/lower according to the ratio
- r_refdef.frustum_y = tan(scr_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
- r_refdef.frustum_x = r_refdef.frustum_y * (float)r_refdef.width / (float)r_refdef.height / vid_pixelheight.value;
-
- r_refdef.frustum_x *= r_refdef.frustumscale_x;
- r_refdef.frustum_y *= r_refdef.frustumscale_y;
-
- if(!CL_VM_UpdateView())
- R_RenderView();
- else
- SCR_DrawConsole();
-
- if (scr_zoomwindow.integer)
- {
- float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
- float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
- r_refdef.width = vid.width * sizex;
- r_refdef.height = vid.height * sizey;
- r_refdef.x = (vid.width - r_refdef.width)/2;
- r_refdef.y = 0;
-
- r_refdef.frustum_y = tan(scr_zoomwindow_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
- r_refdef.frustum_x = r_refdef.frustum_y * vid_pixelheight.value * (float)r_refdef.width / (float)r_refdef.height;
-
- r_refdef.frustum_x *= r_refdef.frustumscale_x;
- r_refdef.frustum_y *= r_refdef.frustumscale_y;
-
- if(!CL_VM_UpdateView())
- R_RenderView();
- }
- }
-
- if (!r_stereo_sidebyside.integer)
- {
- r_refdef.width = vid.width;
- r_refdef.height = vid.height;
- r_refdef.x = 0;
- r_refdef.y = 0;
- }
-
- // draw 2D stuff
- R_DrawQueue();
-
- R_Mesh_Finish();
-
- R_TimeReport("meshfinish");
-}
-
-void SCR_UpdateLoadingScreen (void)
-{
- float x, y;
- cachepic_t *pic;
- rmeshstate_t m;
- // don't do anything if not initialized yet
- if (vid_hidden)
- return;
- r_showtrispass = 0;
- VID_UpdateGamma(false);
- qglViewport(0, 0, vid.width, vid.height);
- //qglDisable(GL_SCISSOR_TEST);
- //qglDepthMask(1);
- qglColorMask(1,1,1,1);
- //qglClearColor(0,0,0,0);
- //qglClear(GL_COLOR_BUFFER_BIT);
- //qglCullFace(GL_FRONT);
- //qglDisable(GL_CULL_FACE);
- //R_ClearScreen();
- R_Textures_Frame();
- GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
- R_Mesh_Start();
- R_Mesh_Matrix(&r_identitymatrix);
- // draw the loading plaque
- pic = Draw_CachePic("gfx/loading", false);
- x = (vid_conwidth.integer - pic->width)/2;
- y = (vid_conheight.integer - pic->height)/2;
- GL_Color(1,1,1,1);
- GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- GL_DepthTest(false);
- memset(&m, 0, sizeof(m));
- m.pointer_vertex = varray_vertex3f;
- m.pointer_texcoord[0] = varray_texcoord2f[0];
- m.tex[0] = R_GetTexture(pic->tex);
- R_Mesh_State(&m);
- varray_vertex3f[0] = varray_vertex3f[9] = x;
- varray_vertex3f[1] = varray_vertex3f[4] = y;
- varray_vertex3f[3] = varray_vertex3f[6] = x + pic->width;
- varray_vertex3f[7] = varray_vertex3f[10] = y + pic->height;
- varray_texcoord2f[0][0] = 0;varray_texcoord2f[0][1] = 0;
- varray_texcoord2f[0][2] = 1;varray_texcoord2f[0][3] = 0;
- varray_texcoord2f[0][4] = 1;varray_texcoord2f[0][5] = 1;
- varray_texcoord2f[0][6] = 0;varray_texcoord2f[0][7] = 1;
- GL_LockArrays(0, 4);
- R_Mesh_Draw(0, 4, 2, polygonelements);
- GL_LockArrays(0, 0);
- R_Mesh_Finish();
- // refresh
- VID_Finish();
-}
-
-/*
-==================
-SCR_UpdateScreen
-
-This is called every frame, and can also be called explicitly to flush
-text to the screen.
-==================
-*/
-void SCR_UpdateScreen (void)
-{
- if (vid_hidden)
- return;
-
- if (r_textureunits.integer > gl_textureunits)
- Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
- if (r_textureunits.integer < 1)
- Cvar_SetValueQuick(&r_textureunits, 1);
-
- if (gl_combine.integer && !gl_combine_extension)
- Cvar_SetValueQuick(&gl_combine, 0);
-
- r_showtrispass = 0;
-
- CHECKGLERROR
- qglViewport(0, 0, vid.width, vid.height);
- qglDisable(GL_SCISSOR_TEST);
- qglDepthMask(1);
- qglColorMask(1,1,1,1);
- qglClearColor(0,0,0,0);
- qglClear(GL_COLOR_BUFFER_BIT);
- CHECKGLERROR
-
- R_TimeReport("clear");
-
- if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
- {
- matrix4x4_t originalmatrix = r_refdef.viewentitymatrix;
- r_refdef.viewentitymatrix.m[0][3] = originalmatrix.m[0][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[0][1];
- r_refdef.viewentitymatrix.m[1][3] = originalmatrix.m[1][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[1][1];
- r_refdef.viewentitymatrix.m[2][3] = originalmatrix.m[2][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[2][1];
-
- if (r_stereo_sidebyside.integer)
- r_stereo_side = 0;
-
- if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
- {
- r_refdef.colormask[0] = 1;
- r_refdef.colormask[1] = 0;
- r_refdef.colormask[2] = 0;
- }
-
- SCR_DrawScreen();
-
- r_refdef.viewentitymatrix.m[0][3] = originalmatrix.m[0][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[0][1];
- r_refdef.viewentitymatrix.m[1][3] = originalmatrix.m[1][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[1][1];
- r_refdef.viewentitymatrix.m[2][3] = originalmatrix.m[2][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[2][1];
-
- if (r_stereo_sidebyside.integer)
- r_stereo_side = 1;
-
- if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
- {
- r_refdef.colormask[0] = 0;
- r_refdef.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
- r_refdef.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
- }
-
- SCR_DrawScreen();
-
- r_refdef.viewentitymatrix = originalmatrix;
- }
- else
- SCR_DrawScreen();
-
- VID_Finish();
- R_TimeReport("finish");
-}
-
-
-//===========================================================================
-// dynamic vertex array buffer subsystem
-//===========================================================================
-
-// FIXME: someday this should be dynamically allocated and resized?
-float varray_vertex3f[65536*3];
-float varray_svector3f[65536*3];
-float varray_tvector3f[65536*3];
-float varray_normal3f[65536*3];
-float varray_color4f[65536*4];
-float varray_texcoord2f[4][65536*2];
-float varray_texcoord3f[4][65536*3];
-int earray_element3i[65536];
-float varray_vertex3f2[65536*3];
-
-//===========================================================================
-// vertex array caching subsystem
-//===========================================================================
-
-typedef struct rcachearraylink_s
-{
- struct rcachearraylink_s *next, *prev;
- struct rcachearrayitem_s *data;
-}
-rcachearraylink_t;
-
-typedef struct rcachearrayitem_s
-{
- // the original request structure
- rcachearrayrequest_t request;
- // active
- int active;
- // offset into r_mesh_rcachedata
- int offset;
- // for linking this into the sequential list
- rcachearraylink_t sequentiallink;
- // for linking this into the lookup list
- rcachearraylink_t hashlink;
-}
-rcachearrayitem_t;
-
-#define RCACHEARRAY_HASHSIZE 65536
-#define RCACHEARRAY_ITEMS 4096
-#define RCACHEARRAY_DEFAULTSIZE (4 << 20)
-
-// all active items are linked into this chain in sorted order
-static rcachearraylink_t r_mesh_rcachesequentialchain;
-// all inactive items are linked into this chain in unknown order
-static rcachearraylink_t r_mesh_rcachefreechain;
-// all active items are also linked into these chains (using their hashlink)
-static rcachearraylink_t r_mesh_rcachechain[RCACHEARRAY_HASHSIZE];
-
-// all items are stored here, whether active or inactive
-static rcachearrayitem_t r_mesh_rcacheitems[RCACHEARRAY_ITEMS];
-
-// size of data buffer
-static int r_mesh_rcachedata_size = RCACHEARRAY_DEFAULTSIZE;
-// data buffer
-static unsigned char r_mesh_rcachedata[RCACHEARRAY_DEFAULTSIZE];
-
-// current state
-static int r_mesh_rcachedata_offset;
-static rcachearraylink_t *r_mesh_rcachesequentialchain_current;
-
-static void R_Mesh_CacheArray_Startup(void)
-{
- int i;
- rcachearraylink_t *l;
- // prepare all the linked lists
- l = &r_mesh_rcachesequentialchain;l->next = l->prev = l;l->data = NULL;
- l = &r_mesh_rcachefreechain;l->next = l->prev = l;l->data = NULL;
- memset(&r_mesh_rcachechain, 0, sizeof(r_mesh_rcachechain));
- for (i = 0;i < RCACHEARRAY_HASHSIZE;i++)
- {
- l = &r_mesh_rcachechain[i];
- l->next = l->prev = l;
- l->data = NULL;
- }
- memset(&r_mesh_rcacheitems, 0, sizeof(r_mesh_rcacheitems));
- for (i = 0;i < RCACHEARRAY_ITEMS;i++)
- {
- r_mesh_rcacheitems[i].hashlink.data = r_mesh_rcacheitems[i].sequentiallink.data = &r_mesh_rcacheitems[i];
- l = &r_mesh_rcacheitems[i].sequentiallink;
- l->next = &r_mesh_rcachefreechain;
- l->prev = l->next->prev;
- l->next->prev = l->prev->next = l;
- }
- // clear other state
- r_mesh_rcachedata_offset = 0;
- r_mesh_rcachesequentialchain_current = &r_mesh_rcachesequentialchain;
-}
-
-static void R_Mesh_CacheArray_Shutdown(void)
-{
-}
-
-/*
-static void R_Mesh_CacheArray_ValidateState(int num)
-{
- rcachearraylink_t *l, *lhead;
- lhead = &r_mesh_rcachesequentialchain;
- if (r_mesh_rcachesequentialchain_current == lhead)
- return;
- for (l = lhead->next;l != lhead;l = l->next)
- if (r_mesh_rcachesequentialchain_current == l)
- return;
- Sys_Error("%i", num);
-}
-*/
-
-int R_Mesh_CacheArray(rcachearrayrequest_t *r)
-{
- rcachearraylink_t *l, *lhead, *lnext;
- rcachearrayitem_t *d;
- int hashindex, offset, offsetend;
-
- //R_Mesh_CacheArray_ValidateState(3);
- // calculate a hashindex to choose a cache chain
- r->data = NULL;
- hashindex = CRC_Block((unsigned char *)r, sizeof(*r)) % RCACHEARRAY_HASHSIZE;
-
- // is it already cached?
- for (lhead = &r_mesh_rcachechain[hashindex], l = lhead->next;l != lhead;l = l->next)
- {
- if (!memcmp(&l->data->request, r, sizeof(l->data->request)))
- {
- // we have it cached already
- r->data = r_mesh_rcachedata + l->data->offset;
- return false;
- }
- }
-
- // we need to add a new cache item, this means finding a place for the new
- // data and making sure we have a free item available, lots of work...
-
- // check if buffer needs to wrap
- if (r_mesh_rcachedata_offset + r->data_size > r_mesh_rcachedata_size)
- {
- /*
- if (r->data_size * 10 > r_mesh_rcachedata_size)
- {
- // realloc whole cache
- }
- */
- // reset back to start
- r_mesh_rcachedata_offset = 0;
- r_mesh_rcachesequentialchain_current = &r_mesh_rcachesequentialchain;
- }
- offset = r_mesh_rcachedata_offset;
- r_mesh_rcachedata_offset += r->data_size;
- offsetend = r_mesh_rcachedata_offset;
- //R_Mesh_CacheArray_ValidateState(4);
-
- /*
- {
- int n;
- for (lhead = &r_mesh_rcachesequentialchain, l = lhead->next, n = 0;l != lhead;l = l->next, n++);
- Con_Printf("R_Mesh_CacheArray: new data range %i:%i, %i items are already linked\n", offset, offsetend, n);
- }
- */
-
- // make room for the new data (remove old items)
- lhead = &r_mesh_rcachesequentialchain;
- l = r_mesh_rcachesequentialchain_current;
- if (l == lhead)
- l = l->next;
- while (l != lhead && l->data->offset < offsetend && l->data->offset + l->data->request.data_size > offset)
- {
- //r_mesh_rcachesequentialchain_current = l;
- //R_Mesh_CacheArray_ValidateState(8);
- lnext = l->next;
- // if at the end of the chain, wrap around
- if (lnext == lhead)
- lnext = lnext->next;
- //r_mesh_rcachesequentialchain_current = lnext;
- //R_Mesh_CacheArray_ValidateState(10);
-
- // unlink from sequential chain
- l->next->prev = l->prev;
- l->prev->next = l->next;
- //R_Mesh_CacheArray_ValidateState(11);
- // link into free chain
- l->next = &r_mesh_rcachefreechain;
- l->prev = l->next->prev;
- l->next->prev = l->prev->next = l;
- //R_Mesh_CacheArray_ValidateState(12);
-
- l = &l->data->hashlink;
- // unlink from hash chain
- l->next->prev = l->prev;
- l->prev->next = l->next;
-
- l = lnext;
- //r_mesh_rcachesequentialchain_current = l;
- //R_Mesh_CacheArray_ValidateState(9);
- }
- //r_mesh_rcachesequentialchain_current = l;
- //R_Mesh_CacheArray_ValidateState(5);
- // gobble an extra item if we have no free items available
- if (r_mesh_rcachefreechain.next == &r_mesh_rcachefreechain)
- {
- lnext = l->next;
-
- // unlink from sequential chain
- l->next->prev = l->prev;
- l->prev->next = l->next;
- // link into free chain
- l->next = &r_mesh_rcachefreechain;
- l->prev = l->next->prev;
- l->next->prev = l->prev->next = l;
-
- l = &l->data->hashlink;
- // unlink from hash chain
- l->next->prev = l->prev;
- l->prev->next = l->next;
-
- l = lnext;
- }
- r_mesh_rcachesequentialchain_current = l;
- //R_Mesh_CacheArray_ValidateState(6);
-
- // now take an item from the free chain
- l = r_mesh_rcachefreechain.next;
- // set it up
- d = l->data;
- d->request = *r;
- d->offset = offset;
- // unlink
- l->next->prev = l->prev;
- l->prev->next = l->next;
- // relink to sequential
- l->next = r_mesh_rcachesequentialchain_current->prev;
- l->prev = l->next->prev;
- while (l->next->data && l->data && l->next->data->offset <= d->offset)
- {
- //Con_Print(">\n");
- l->next = l->next->next;
- l->prev = l->prev->next;
- }
- while (l->prev->data && l->data && l->prev->data->offset >= d->offset)
- {
- //Con_Print("<\n");
- l->prev = l->prev->prev;
- l->next = l->next->prev;
- }
- l->next->prev = l->prev->next = l;
- // also link into hash chain
- l = &l->data->hashlink;
- l->next = &r_mesh_rcachechain[hashindex];
- l->prev = l->next->prev;
- l->prev->next = l;
- l->next->prev = l->prev->next = l;
-
-
- //r_mesh_rcachesequentialchain_current = d->sequentiallink.next;
-
- //R_Mesh_CacheArray_ValidateState(7);
- // and finally set the data pointer
- r->data = r_mesh_rcachedata + d->offset;
- // and tell the caller to fill the array
- return true;
-}
-