r_misc.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 1997-2001 Id Software, Inc.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #include "r_local.h"
00026 #include "r_misc.h"
00027 #include "r_error.h"
00028 #include "../../shared/images.h"
00029 
00030 static const byte gridtexture[8][8] = {
00031     {1, 1, 1, 1, 1, 1, 1, 1},
00032     {1, 0, 0, 0, 0, 0, 0, 1},
00033     {1, 0, 0, 0, 0, 0, 0, 1},
00034     {1, 0, 0, 0, 0, 0, 0, 1},
00035     {1, 0, 0, 0, 0, 0, 0, 1},
00036     {1, 0, 0, 0, 0, 0, 0, 1},
00037     {1, 0, 0, 0, 0, 0, 0, 1},
00038     {1, 1, 1, 1, 1, 1, 1, 1},
00039 };
00040 
00041 #define MISC_TEXTURE_SIZE 16
00042 void R_InitMiscTexture (void)
00043 {
00044     int x, y;
00045     byte data[MISC_TEXTURE_SIZE][MISC_TEXTURE_SIZE][4];
00046 
00047     memset(data, 0, sizeof(data));
00048 
00049     /* also use this for bad textures, but without alpha */
00050     for (x = 0; x < 8; x++) {
00051         for (y = 0; y < 8; y++) {
00052             data[y][x][0] = gridtexture[x][y] * 255;
00053             data[y][x][3] = 255;
00054         }
00055     }
00056     r_noTexture = R_LoadImageData("***r_notexture***", (byte *) data, 8, 8, it_effect);
00057 
00058     for (x = 0; x < MISC_TEXTURE_SIZE; x++) {
00059         for (y = 0; y < MISC_TEXTURE_SIZE; y++) {
00060             data[y][x][0] = rand() % 255;
00061             data[y][x][1] = rand() % 255;
00062             data[y][x][2] = rand() % 48;
00063             data[y][x][3] = rand() % 48;
00064         }
00065     }
00066     r_warpTexture = R_LoadImageData("***r_warptexture***", (byte *)data, MISC_TEXTURE_SIZE, MISC_TEXTURE_SIZE, it_effect);
00067 
00068     /* empty pic in the texture chain for cinematic frames */
00069     R_LoadImageData("***cinematic***", NULL, VID_NORM_WIDTH, VID_NORM_HEIGHT, it_effect);
00070 }
00071 
00072 /*
00073 ==============================================================================
00074 SCREEN SHOTS
00075 ==============================================================================
00076 */
00077 
00078 enum {
00079     SSHOTTYPE_JPG,
00080     SSHOTTYPE_PNG,
00081     SSHOTTYPE_TGA,
00082     SSHOTTYPE_TGA_COMP,
00083 };
00084 
00094 void R_ScreenShot (int x, int y, int width, int height, const char *filename, const char *ext)
00095 {
00096     char    checkName[MAX_OSPATH];
00097     int     type, shotNum, quality = 100;
00098     byte    *buffer;
00099     qFILE   f;
00100     int rowPack;
00101 
00102     glGetIntegerv(GL_PACK_ALIGNMENT, &rowPack);
00103     glPixelStorei(GL_PACK_ALIGNMENT, 1);
00104 
00105     /* Find out what format to save in */
00106     if (ext == NULL)
00107         ext = r_screenshot_format->string;
00108 
00109     if (!Q_strcasecmp(ext, "png"))
00110         type = SSHOTTYPE_PNG;
00111     else if (!Q_strcasecmp(ext, "jpg"))
00112         type = SSHOTTYPE_JPG;
00113     else
00114         type = SSHOTTYPE_TGA_COMP;
00115 
00116     /* Set necessary values */
00117     switch (type) {
00118     case SSHOTTYPE_TGA_COMP:
00119         Com_Printf("Taking TGA screenshot...\n");
00120         ext = "tga";
00121         break;
00122 
00123     case SSHOTTYPE_PNG:
00124         Com_Printf("Taking PNG screenshot...\n");
00125         ext = "png";
00126         break;
00127 
00128     case SSHOTTYPE_JPG:
00129         if (Cmd_Argc() == 3)
00130             quality = atoi(Cmd_Argv(2));
00131         else
00132             quality = r_screenshot_jpeg_quality->integer;
00133         if (quality > 100 || quality <= 0)
00134             quality = 100;
00135 
00136         Com_Printf("Taking JPG screenshot (at %i%% quality)...\n", quality);
00137         ext = "jpg";
00138         break;
00139     }
00140 
00141     /* Find a file name to save it to */
00142     if (filename) {
00143         Com_sprintf(checkName, sizeof(checkName), "scrnshot/%s.%s", filename, ext);
00144     } else {
00145         for (shotNum = 0; shotNum < 1000; shotNum++) {
00146             Com_sprintf(checkName, sizeof(checkName), "scrnshot/ufo%i%i.%s", shotNum / 10, shotNum % 10, ext);
00147             if (FS_CheckFile("%s", checkName) == -1)
00148                 break;
00149         }
00150         if (shotNum == 1000) {
00151             Com_Printf("R_ScreenShot_f: screenshot limit (of 1000) exceeded!\n");
00152             return;
00153         }
00154     }
00155 
00156     /* Open it */
00157     FS_OpenFile(checkName, &f, FILE_WRITE);
00158     if (!f.f) {
00159         Com_Printf("R_ScreenShot_f: Couldn't create file: %s\n", checkName);
00160         return;
00161     }
00162 
00163     /* Allocate room for a copy of the framebuffer */
00164     buffer = Mem_PoolAlloc(width * height * 3, vid_imagePool, 0);
00165     if (!buffer) {
00166         Com_Printf("R_ScreenShot_f: Could not allocate %i bytes for screenshot!\n", width * height * 3);
00167         FS_CloseFile(&f);
00168         return;
00169     }
00170 
00171     /* Read the framebuffer into our storage */
00172     glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
00173     R_CheckError();
00174 
00175     /* Write */
00176     switch (type) {
00177     case SSHOTTYPE_TGA_COMP:
00178         R_WriteCompressedTGA(&f, buffer, width, height);
00179         break;
00180 
00181     case SSHOTTYPE_PNG:
00182         R_WritePNG(&f, buffer, width, height);
00183         break;
00184 
00185     case SSHOTTYPE_JPG:
00186         R_WriteJPG(&f, buffer, width, height, quality);
00187         break;
00188     }
00189 
00190     /* Finish */
00191     FS_CloseFile(&f);
00192     Mem_Free(buffer);
00193 
00194     Com_Printf("Wrote %s to %s\n", checkName, FS_Gamedir());
00195     glPixelStorei(GL_PACK_ALIGNMENT, rowPack);
00196 }
00197 
00198 void R_ScreenShot_f (void)
00199 {
00200     const char *ext = NULL;
00201     if (Cmd_Argc() > 1)
00202         ext = Cmd_Argv(1);
00203     R_ScreenShot(0, 0, viddef.width, viddef.height, NULL, ext);
00204 }
00205 
00215 void R_Transform (const vec3_t transform, const vec3_t rotate, const vec3_t scale)
00216 {
00217     if (transform != NULL) {
00218         glTranslatef(transform[0], transform[1], transform[2]);
00219     }
00220 
00221     if (rotate != NULL) {
00222         glRotatef(rotate[0], 0, 0, 1);
00223         glRotatef(rotate[1], 0, 1, 0);
00224         glRotatef(rotate[2], 1, 0, 0);
00225     }
00226 
00227     if (scale != NULL) {
00228         glScalef(scale[0], scale[1], scale[2]);
00229     }
00230 }
00231 
00235 void R_PushMatrix (void)
00236 {
00237     glPushMatrix();
00238 }
00239 
00243 void R_PopMatrix (void)
00244 {
00245     glPopMatrix();
00246 }

Generated by  doxygen 1.6.2