opengl_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "../wolfiphone.h"
  16. viddef_t viddef;
  17. float gldepthmin, gldepthmax;
  18. glconfig_t gl_config;
  19. glstate_t gl_state;
  20. //
  21. // view origin
  22. //
  23. vec3_t vup;
  24. vec3_t vpn;
  25. vec3_t vright;
  26. vec3_t r_origin;
  27. cvar_t *r_norefresh;
  28. cvar_t *r_speeds;
  29. cvar_t *r_novis;
  30. cvar_t *r_nocull;
  31. cvar_t *r_lefthand;
  32. cvar_t *gl_nosubimage;
  33. cvar_t *gl_vertex_arrays;
  34. cvar_t *gl_ext_swapinterval;
  35. cvar_t *gl_ext_palettedtexture;
  36. cvar_t *gl_ext_multitexture;
  37. cvar_t *gl_ext_pointparameters;
  38. cvar_t *gl_ext_compiled_vertex_array;
  39. //cvar_t *gl_ext_TextureCompressionS3TC;
  40. cvar_t *gl_bitdepth;
  41. cvar_t *gl_drawbuffer;
  42. cvar_t *gl_driver;
  43. cvar_t *gl_lightmap;
  44. cvar_t *gl_shadows;
  45. cvar_t *gl_mode;
  46. cvar_t *gl_dynamic;
  47. cvar_t *gl_modulate;
  48. cvar_t *gl_nobind;
  49. cvar_t *gl_round_down;
  50. cvar_t *gl_picmip;
  51. cvar_t *gl_skymip;
  52. cvar_t *gl_showtris;
  53. cvar_t *gl_ztrick;
  54. cvar_t *gl_finish;
  55. cvar_t *gl_clear;
  56. cvar_t *gl_cull;
  57. cvar_t *gl_polyblend;
  58. cvar_t *gl_flashblend;
  59. cvar_t *gl_playermip;
  60. cvar_t *gl_saturatelighting;
  61. cvar_t *gl_swapinterval;
  62. cvar_t *gl_texturemode;
  63. cvar_t *r_fullscreen;
  64. cvar_t *vid_gamma;
  65. cvar_t *r_ref;
  66. /*
  67. -----------------------------------------------------------------------------
  68. Function:
  69. Parameters:
  70. Returns:
  71. Notes:
  72. -----------------------------------------------------------------------------
  73. */
  74. PUBLIC void MYgluPerspective( GLdouble fovy, GLdouble aspect,
  75. GLdouble zNear, GLdouble zFar )
  76. {
  77. GLdouble xmin, xmax, ymin, ymax;
  78. ymax = zNear * tan( fovy * M_PI / 360.0 );
  79. ymin = -ymax;
  80. xmin = ymin * aspect;
  81. xmax = ymax * aspect;
  82. xmin += -( 2 * 0 ) / zNear;
  83. xmax += -( 2 * 0 ) / zNear;
  84. pfglFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
  85. }
  86. /*
  87. -----------------------------------------------------------------------------
  88. Function:
  89. Parameters:
  90. Returns:
  91. Notes:
  92. -----------------------------------------------------------------------------
  93. */
  94. PRIVATE void R_ScreenShot_f( void )
  95. {
  96. W8 *buffer;
  97. char picname[ 80 ];
  98. char checkname[ MAX_OSPATH ];
  99. int i;
  100. FILE *f;
  101. // create the scrnshots directory if it doesn't exist
  102. my_snprintf( checkname, sizeof( checkname ), "%s/scrnshot", FS_Gamedir() );
  103. FS_CreateDirectory( checkname );
  104. //
  105. // find a file name to save it to
  106. //
  107. my_strlcpy( picname, "scrn00.tga", sizeof( picname ) );
  108. for( i = 0 ; i <= 99 ; ++i )
  109. {
  110. picname[ 4 ] = i / 10 + '0';
  111. picname[ 5 ] = i % 10 + '0';
  112. my_snprintf( checkname, sizeof( checkname ), "%s/scrnshot/%s", FS_Gamedir(), picname );
  113. f = fopen( checkname, "rb" );
  114. if( ! f )
  115. {
  116. break; // file doesn't exist
  117. }
  118. fclose( f );
  119. }
  120. if( i == 100 )
  121. {
  122. Com_Printf( "R_ScreenShot_f: Couldn't create a file\n" );
  123. return;
  124. }
  125. buffer = MM_MALLOC( viddef.width * viddef.height * 3 );
  126. pfglReadPixels( 0, 0, viddef.width, viddef.height, GL_RGB, GL_UNSIGNED_BYTE, buffer );
  127. WriteTGA( checkname, 24, viddef.width, viddef.height, buffer, 1, 1 );
  128. MM_FREE( buffer );
  129. Com_Printf( "Wrote %s\n", picname );
  130. }
  131. /*
  132. -----------------------------------------------------------------------------
  133. Function:
  134. Parameters:
  135. Returns:
  136. Notes:
  137. -----------------------------------------------------------------------------
  138. */
  139. PRIVATE void R_Strings_f( void )
  140. {
  141. Com_Printf( "GL_VENDOR: %s\n", gl_config.vendor_string );
  142. Com_Printf( "GL_RENDERER: %s\n", gl_config.renderer_string );
  143. Com_Printf( "GL_VERSION: %s\n", gl_config.version_string );
  144. Com_Printf( "GL_EXTENSIONS: %s\n", gl_config.extensions_string );
  145. }
  146. /*
  147. -----------------------------------------------------------------------------
  148. Function:
  149. Parameters:
  150. Returns:
  151. Notes:
  152. -----------------------------------------------------------------------------
  153. */
  154. PRIVATE void R_Register( void )
  155. {
  156. gl_round_down = Cvar_Get ("gl_round_down", "1", CVAR_INIT);
  157. r_lefthand = Cvar_Get( "hand", "0", CVAR_USERINFO | CVAR_ARCHIVE );
  158. r_norefresh = Cvar_Get ("r_norefresh", "0", CVAR_INIT);
  159. r_novis = Cvar_Get ("r_novis", "0", CVAR_INIT);
  160. r_nocull = Cvar_Get ("r_nocull", "0", CVAR_INIT);
  161. r_speeds = Cvar_Get ("r_speeds", "0", CVAR_INIT);
  162. gl_nosubimage = Cvar_Get( "gl_nosubimage", "0", CVAR_INIT );
  163. gl_modulate = Cvar_Get ("gl_modulate", "1", CVAR_ARCHIVE );
  164. gl_bitdepth = Cvar_Get( "gl_bitdepth", "0", CVAR_INIT );
  165. gl_mode = Cvar_Get( "gl_mode", "0", CVAR_ARCHIVE );
  166. gl_lightmap = Cvar_Get ("gl_lightmap", "0", CVAR_INIT);
  167. gl_dynamic = Cvar_Get ("gl_dynamic", "1", CVAR_INIT);
  168. gl_nobind = Cvar_Get ("gl_nobind", "0", CVAR_INIT);
  169. gl_picmip = Cvar_Get ("gl_picmip", "0", CVAR_INIT);
  170. gl_skymip = Cvar_Get ("gl_skymip", "0", CVAR_INIT);
  171. gl_showtris = Cvar_Get( "gl_showtris", "0", CVAR_INIT );
  172. gl_ztrick = Cvar_Get( "gl_ztrick", "0", CVAR_INIT );
  173. gl_finish = Cvar_Get( "gl_finish", "0", CVAR_ARCHIVE );
  174. gl_clear = Cvar_Get( "gl_clear", "0", CVAR_INIT );
  175. gl_cull = Cvar_Get( "gl_cull", "1", CVAR_INIT );
  176. gl_polyblend = Cvar_Get( "gl_polyblend", "1", CVAR_INIT );
  177. gl_flashblend = Cvar_Get( "gl_flashblend", "0", CVAR_INIT );
  178. gl_playermip = Cvar_Get( "gl_playermip", "0", CVAR_INIT );
  179. gl_driver = Cvar_Get( "gl_driver", OPENGL_DLL_NAME, CVAR_ARCHIVE );
  180. gl_vertex_arrays = Cvar_Get( "gl_vertex_arrays", "0", CVAR_ARCHIVE );
  181. gl_ext_swapinterval = Cvar_Get( "gl_ext_swapinterval", "1", CVAR_ARCHIVE );
  182. gl_ext_palettedtexture = Cvar_Get( "gl_ext_palettedtexture", "1", CVAR_ARCHIVE );
  183. gl_ext_multitexture = Cvar_Get( "gl_ext_multitexture", "1", CVAR_ARCHIVE );
  184. gl_ext_pointparameters = Cvar_Get( "gl_ext_pointparameters", "1", CVAR_ARCHIVE );
  185. gl_ext_compiled_vertex_array = Cvar_Get( "gl_ext_compiled_vertex_array", "1", CVAR_ARCHIVE );
  186. gl_drawbuffer = Cvar_Get( "gl_drawbuffer", "GL_BACK", CVAR_INIT );
  187. gl_swapinterval = Cvar_Get( "gl_swapinterval", "1", CVAR_ARCHIVE );
  188. // gl_saturatelighting = Cvar_Get( "gl_saturatelighting", "0", CVAR_INIT );
  189. r_fullscreen = Cvar_Get( "r_fullscreen", "0", CVAR_ARCHIVE );
  190. vid_gamma = Cvar_Get( "vid_gamma", "1.0", CVAR_ARCHIVE );
  191. r_ref = Cvar_Get( "r_ref", "gl", CVAR_ARCHIVE );
  192. Cmd_AddCommand( "screenshot", R_ScreenShot_f );
  193. Cmd_AddCommand( "r_strings", R_Strings_f );
  194. }
  195. /*
  196. -----------------------------------------------------------------------------
  197. Function:
  198. Parameters:
  199. Returns:
  200. Notes:
  201. -----------------------------------------------------------------------------
  202. */
  203. PUBLIC void R_Init()
  204. {
  205. char renderer_buffer[ 1000 ];
  206. char vendor_buffer[ 1000 ];
  207. int err;
  208. int a, b;
  209. Com_Printf( "\n------ Display Initialization ------\n" );
  210. Com_Printf( "Initializing OpenGL Subsystem\n" );
  211. R_Register();
  212. // set our "safe" modes
  213. gl_state.prev_mode = 0;
  214. viddef.width = 480;
  215. viddef.height = 320;
  216. // get various GL strings
  217. gl_config.vendor_string = (char *)pfglGetString( GL_VENDOR );
  218. Com_Printf( "GL_VENDOR: %s\n", gl_config.vendor_string );
  219. gl_config.renderer_string = (char *)pfglGetString( GL_RENDERER );
  220. Com_Printf( "GL_RENDERER: %s\n", gl_config.renderer_string );
  221. gl_config.version_string = (char *)pfglGetString( GL_VERSION );
  222. Com_Printf( "GL_VERSION: %s\n", gl_config.version_string );
  223. gl_config.extensions_string = (char *)pfglGetString( GL_EXTENSIONS );
  224. Com_Printf( "GL_EXTENSIONS: %s\n", gl_config.extensions_string );
  225. my_strlcpy( renderer_buffer, gl_config.renderer_string, sizeof( renderer_buffer ) );
  226. (void)my_strlwr( renderer_buffer );
  227. my_strlcpy( vendor_buffer, gl_config.vendor_string, sizeof( vendor_buffer ) );
  228. (void)my_strlwr( vendor_buffer );
  229. sscanf( gl_config.version_string, "%d.%d", &a, &b );
  230. if( a >= 1 && b >= 2 )
  231. {
  232. gl_config.Version_1_2 = true;
  233. }
  234. pfglGetIntegerv( GL_MAX_TEXTURE_SIZE, &glMaxTexSize );
  235. Com_Printf( "GL_MAX_TEXTURE_SIZE: %d\n", glMaxTexSize);
  236. GL_SetDefaultState();
  237. TM_Init();
  238. Font_Init();
  239. err = pfglGetError();
  240. if( err != GL_NO_ERROR )
  241. {
  242. Com_Printf( "glGetError() = 0x%x\n", err );
  243. }
  244. }
  245. /*
  246. -----------------------------------------------------------------------------
  247. Function:
  248. Parameters:
  249. Returns:
  250. Notes:
  251. -----------------------------------------------------------------------------
  252. */
  253. PUBLIC void R_Shutdown( void )
  254. {
  255. // Cmd_RemoveCommand ("modellist");
  256. // Cmd_RemoveCommand ("screenshot");
  257. // Cmd_RemoveCommand ("imagelist");
  258. // Cmd_RemoveCommand ("gl_strings");
  259. // Mod_FreeAll ();
  260. TM_Shutdown();
  261. /*
  262. ** shut down OS specific OpenGL stuff like contexts, etc.
  263. */
  264. GLimp_Shutdown();
  265. /*
  266. ** shutdown our OpenGL subsystem
  267. */
  268. #ifndef IPHONE
  269. OpenGL_Shutdown();
  270. #endif
  271. }
  272. /*
  273. -----------------------------------------------------------------------------
  274. Function:
  275. Parameters:
  276. Returns:
  277. Notes:
  278. -----------------------------------------------------------------------------
  279. */
  280. PUBLIC void R_EndFrame( void )
  281. {
  282. GLimp_EndFrame();
  283. }
  284. /*
  285. -----------------------------------------------------------------------------
  286. Function:
  287. Parameters:
  288. Returns:
  289. Notes:
  290. -----------------------------------------------------------------------------
  291. */
  292. PUBLIC void R_AppActivate( _boolean active )
  293. {
  294. GLimp_AppActivate( active );
  295. }
  296. /*
  297. -----------------------------------------------------------------------------
  298. Function:
  299. Parameters:
  300. Returns:
  301. Notes:
  302. -----------------------------------------------------------------------------
  303. */
  304. PUBLIC void GL_UpdateSwapInterval( void )
  305. {
  306. if ( gl_swapinterval->modified )
  307. {
  308. gl_swapinterval->modified = false;
  309. #ifdef _WIN32
  310. if ( pfwglSwapIntervalEXT )
  311. {
  312. pfwglSwapIntervalEXT( FloatToInt( gl_swapinterval->value ) );
  313. }
  314. #endif
  315. }
  316. }
  317. /*
  318. -----------------------------------------------------------------------------
  319. Function: PrintGLError -Print OpenGL error message.
  320. Parameters: err -[in] Error code.
  321. from -[in] function name that produced the error.
  322. Returns: Nothing.
  323. Notes:
  324. -----------------------------------------------------------------------------
  325. */
  326. PUBLIC void PrintGLError( W32 err, const char *from )
  327. {
  328. if( err == GL_NO_ERROR )
  329. {
  330. return;
  331. }
  332. if( from != "" )
  333. {
  334. Com_Printf( "\n\n\nGL Error: %s\n", from );
  335. }
  336. switch( err )
  337. {
  338. case GL_NO_ERROR:
  339. Com_Printf( "GL_NO_ERROR:\nNo error has been recorded. The value of this symbolic constant is guaranteed to be zero.\n" );
  340. break;
  341. case GL_INVALID_ENUM:
  342. Com_Printf( "GL_INVALID_ENUM:\nAn unacceptable value is specified for an enumerated argument. The offending function is ignored, having no side effect other than to set the error flag.\n" );
  343. break;
  344. case GL_INVALID_VALUE:
  345. Com_Printf( "GL_INVALID_VALUE:\nA numeric argument is out of range. The offending function is ignored, having no side effect other than to set the error flag.\n" );
  346. break;
  347. case GL_INVALID_OPERATION:
  348. Com_Printf( "GL_INVALID_OPERATION:\nThe specified operation is not allowed in the current state. The offending function is ignored, having no side effect other than to set the error flag.\n" );
  349. break;
  350. case GL_STACK_OVERFLOW:
  351. Com_Printf( "GL_STACK_OVERFLOW:\nThis function would cause a stack overflow. The offending function is ignored, having no side effect other than to set the error flag.\n" );
  352. break;
  353. case GL_STACK_UNDERFLOW:
  354. Com_Printf( "GL_STACK_UNDERFLOW:\nThis function would cause a stack underflow. The offending function is ignored, having no side effect other than to set the error flag.\n" );
  355. break;
  356. case GL_OUT_OF_MEMORY:
  357. Com_Printf( "GL_OUT_OF_MEMORY:\nThere is not enough memory left to execute the function. The state of OpenGL is undefined, except for the state of the error flags, after this error is recorded.\n" );
  358. break;
  359. default:
  360. Com_Printf( "Unknown GL error flag 0x%x\n", err );
  361. }
  362. }