Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

texenv.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2. /**
  3. * (c) Copyright 1993, Silicon Graphics, Inc.
  4. * ALL RIGHTS RESERVED
  5. * Permission to use, copy, modify, and distribute this software for
  6. * any purpose and without fee is hereby granted, provided that the above
  7. * copyright notice appear in all copies and that both the copyright notice
  8. * and this permission notice appear in supporting documentation, and that
  9. * the name of Silicon Graphics, Inc. not be used in advertising
  10. * or publicity pertaining to distribution of the software without specific,
  11. * written prior permission.
  12. *
  13. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  22. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * US Government Users Restricted Rights
  27. * Use, duplication, or disclosure by the Government is subject to
  28. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30. * clause at DFARS 252.227-7013 and/or in similar or successor
  31. * clauses in the FAR or the DOD or NASA FAR Supplement.
  32. * Unpublished-- rights reserved under the copyright laws of the
  33. * United States. Contractor/manufacturer is Silicon Graphics,
  34. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  35. *
  36. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37. */
  38. /*
  39. * Demonstrates texture environment modes and internal image formats.
  40. */
  41. /*
  42. * Hacked on, updated by Gareth Hughes <gareth@valinux.com>
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <GL/glut.h>
  48. #undef max
  49. #undef min
  50. #define max( a, b ) ((a) >= (b) ? (a) : (b))
  51. #define min( a, b ) ((a) <= (b) ? (a) : (b))
  52. GLfloat lightCheck[4] = { 0.7, 0.7, 0.7, 1.0 };
  53. GLfloat darkCheck[4] = { 0.3, 0.3, 0.3, 1.0 };
  54. GLfloat labelColor0[4] = { 1.0, 1.0, 1.0, 1.0 };
  55. GLfloat labelColor1[4] = { 1.0, 1.0, 0.4, 1.0 };
  56. GLfloat *labelInfoColor = labelColor0;
  57. GLfloat labelLevelColor0[4] = { 0.8, 0.8, 0.1, 1.0 };
  58. GLfloat labelLevelColor1[4] = { 0.0, 0.0, 0.0, 1.0 };
  59. GLboolean doubleBuffered = GL_TRUE;
  60. GLboolean drawBackground = GL_FALSE;
  61. GLboolean drawBlended = GL_TRUE;
  62. GLboolean drawSmooth = GL_FALSE;
  63. GLboolean drawTextured = GL_TRUE;
  64. GLboolean displayLevelInfo = GL_FALSE;
  65. int textureWidth = 64;
  66. int textureHeight = 64;
  67. int winWidth = 580, winHeight = 720;
  68. static int Win;
  69. struct formatInfo {
  70. GLenum baseFormat;
  71. GLenum internalFormat;
  72. char *name;
  73. };
  74. #define NUM_LUMINANCE_FORMATS (sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
  75. struct formatInfo luminanceFormats[] =
  76. {
  77. { GL_LUMINANCE, GL_LUMINANCE, "LUMINANCE" },
  78. { GL_LUMINANCE, GL_LUMINANCE4, "LUMINANCE4" },
  79. { GL_LUMINANCE, GL_LUMINANCE8, "LUMINANCE8" },
  80. { GL_LUMINANCE, GL_LUMINANCE12, "LUMINANCE12" },
  81. { GL_LUMINANCE, GL_LUMINANCE16, "LUMINANCE16" },
  82. };
  83. #define NUM_ALPHA_FORMATS (sizeof(alphaFormats) / sizeof(alphaFormats[0]))
  84. struct formatInfo alphaFormats[] =
  85. {
  86. { GL_ALPHA, GL_ALPHA, "ALPHA" },
  87. { GL_ALPHA, GL_ALPHA4, "ALPHA4" },
  88. { GL_ALPHA, GL_ALPHA8, "ALPHA8" },
  89. { GL_ALPHA, GL_ALPHA12, "ALPHA12" },
  90. { GL_ALPHA, GL_ALPHA16, "ALPHA16" },
  91. };
  92. #define NUM_INTENSITY_FORMATS (sizeof(intensityFormats) / sizeof(intensityFormats[0]))
  93. struct formatInfo intensityFormats[] =
  94. {
  95. { GL_INTENSITY, GL_INTENSITY, "INTENSITY" },
  96. { GL_INTENSITY, GL_INTENSITY4, "INTENSITY4" },
  97. { GL_INTENSITY, GL_INTENSITY8, "INTENSITY8" },
  98. { GL_INTENSITY, GL_INTENSITY12, "INTENSITY12" },
  99. { GL_INTENSITY, GL_INTENSITY16, "INTENSITY16" },
  100. };
  101. #define NUM_LUMINANCE_ALPHA_FORMATS (sizeof(luminanceAlphaFormats) / sizeof(luminanceAlphaFormats[0]))
  102. struct formatInfo luminanceAlphaFormats[] =
  103. {
  104. { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, "LUMINANCE_ALPHA" },
  105. { GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, "LUMINANCE4_ALPHA4" },
  106. { GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, "LUMINANCE6_ALPHA2" },
  107. { GL_LUMINANCE_ALPHA, GL_LUMINANCE8_ALPHA8, "LUMINANCE8_ALPHA8" },
  108. { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA4, "LUMINANCE12_ALPHA4" },
  109. { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA12, "LUMINANCE12_ALPHA12" },
  110. { GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, "LUMINANCE16_ALPHA16" },
  111. };
  112. #define NUM_RGB_FORMATS (sizeof(rgbFormats) / sizeof(rgbFormats[0]))
  113. struct formatInfo rgbFormats[] =
  114. {
  115. { GL_RGB, GL_RGB, "RGB" },
  116. { GL_RGB, GL_R3_G3_B2, "R3_G3_B2" },
  117. { GL_RGB, GL_RGB4, "RGB4" },
  118. { GL_RGB, GL_RGB5, "RGB5" },
  119. { GL_RGB, GL_RGB8, "RGB8" },
  120. { GL_RGB, GL_RGB10, "RGB10" },
  121. { GL_RGB, GL_RGB12, "RGB12" },
  122. { GL_RGB, GL_RGB16, "RGB16" },
  123. };
  124. #define NUM_RGBA_FORMATS (sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
  125. struct formatInfo rgbaFormats[] =
  126. {
  127. { GL_RGBA, GL_RGBA, "RGBA" },
  128. { GL_RGBA, GL_RGBA2, "RGBA2" },
  129. { GL_RGBA, GL_RGBA4, "RGBA4" },
  130. { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
  131. { GL_RGBA, GL_RGBA8, "RGBA8" },
  132. { GL_RGBA, GL_RGB10_A2, "RGB10_A2" },
  133. { GL_RGBA, GL_RGBA12, "RGBA12" },
  134. { GL_RGBA, GL_RGBA16, "RGBA16" },
  135. };
  136. struct baseFormatInfo {
  137. struct formatInfo *format;
  138. int current, number;
  139. };
  140. #define NUM_BASE_FORMATS (sizeof(baseFormats) / sizeof(baseFormats[0]))
  141. int baseFormat;
  142. struct baseFormatInfo baseFormats[] =
  143. {
  144. { luminanceFormats, 0, NUM_LUMINANCE_FORMATS },
  145. { alphaFormats, 0, NUM_ALPHA_FORMATS },
  146. { intensityFormats, 0, NUM_INTENSITY_FORMATS },
  147. { luminanceAlphaFormats, 0, NUM_LUMINANCE_ALPHA_FORMATS },
  148. { rgbFormats, 0, NUM_RGB_FORMATS },
  149. { rgbaFormats, 0, NUM_RGBA_FORMATS },
  150. };
  151. #define NUM_ENV_COLORS (sizeof(envColors) / sizeof(envColors[0]))
  152. int envColor = 0;
  153. GLfloat envColors[][4] =
  154. {
  155. { 0.0, 0.0, 0.0, 1.0 },
  156. { 1.0, 0.0, 0.0, 1.0 },
  157. { 0.0, 1.0, 0.0, 1.0 },
  158. { 0.0, 0.0, 1.0, 1.0 },
  159. { 1.0, 1.0, 1.0, 1.0 },
  160. };
  161. struct envModeInfo {
  162. GLenum mode;
  163. char *name;
  164. };
  165. /* allow for run-time check for GL_EXT_texture_env_add */
  166. int NUM_ENV_MODES = 5;
  167. struct envModeInfo envModes[] =
  168. {
  169. { GL_REPLACE, "REPLACE" },
  170. { GL_MODULATE, "MODULATE" },
  171. { GL_BLEND, "BLEND" },
  172. { GL_DECAL, "DECAL" },
  173. #if GL_EXT_texture_env_add
  174. { GL_ADD, "ADD" },
  175. #endif
  176. };
  177. static void checkErrors( void )
  178. {
  179. GLenum error;
  180. while ( (error = glGetError()) != GL_NO_ERROR ) {
  181. fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
  182. }
  183. }
  184. static void drawString( const char *string, GLfloat x, GLfloat y,
  185. const GLfloat color[4] )
  186. {
  187. glColor4fv( color );
  188. glRasterPos2f( x, y );
  189. while ( *string ) {
  190. glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
  191. string++;
  192. }
  193. }
  194. static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
  195. const GLfloat color[4],
  196. const GLfloat outline[4] )
  197. {
  198. drawString( string, x - 1, y, outline );
  199. drawString( string, x + 1, y, outline );
  200. drawString( string, x, y - 1, outline );
  201. drawString( string, x, y + 1, outline );
  202. drawString( string, x, y, color );
  203. }
  204. static void begin2D( int width, int height )
  205. {
  206. glMatrixMode( GL_PROJECTION );
  207. glPushMatrix();
  208. glLoadIdentity();
  209. glOrtho( 0, width, 0, height, -1, 1 );
  210. glMatrixMode( GL_MODELVIEW );
  211. glPushMatrix();
  212. glLoadIdentity();
  213. }
  214. static void end2D( void )
  215. {
  216. glMatrixMode( GL_PROJECTION );
  217. glPopMatrix();
  218. glMatrixMode( GL_MODELVIEW );
  219. glPopMatrix();
  220. }
  221. static void initialize( void )
  222. {
  223. glMatrixMode( GL_PROJECTION );
  224. glLoadIdentity();
  225. glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
  226. glMatrixMode(GL_MODELVIEW);
  227. glLoadIdentity();
  228. glShadeModel( GL_FLAT );
  229. }
  230. /* ARGSUSED1 */
  231. static void keyboard( unsigned char c, int x, int y )
  232. {
  233. switch ( c ) {
  234. case 'c':
  235. envColor++;
  236. envColor = envColor % (int) NUM_ENV_COLORS;
  237. break;
  238. case 'g':
  239. drawBackground = !drawBackground;
  240. break;
  241. case 'b':
  242. drawBlended = !drawBlended;
  243. break;
  244. case 's':
  245. drawSmooth = !drawSmooth;
  246. break;
  247. case 't':
  248. drawTextured = !drawTextured;
  249. break;
  250. case 'i':
  251. displayLevelInfo = !displayLevelInfo;
  252. break;
  253. case 27: /* Escape key should force exit. */
  254. glutDestroyWindow(Win);
  255. exit(0);
  256. break;
  257. default:
  258. break;
  259. }
  260. glutPostRedisplay();
  261. }
  262. /* ARGSUSED1 */
  263. static void special( int key, int x, int y )
  264. {
  265. switch ( key ) {
  266. case GLUT_KEY_DOWN:
  267. if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
  268. baseFormat = 0;
  269. }
  270. break;
  271. case GLUT_KEY_UP:
  272. if ( --baseFormat < 0 ) {
  273. baseFormat = NUM_BASE_FORMATS - 1;
  274. }
  275. break;
  276. case GLUT_KEY_LEFT:
  277. --baseFormats[baseFormat].current;
  278. if ( baseFormats[baseFormat].current < 0 ) {
  279. baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
  280. }
  281. break;
  282. case GLUT_KEY_RIGHT:
  283. ++baseFormats[baseFormat].current;
  284. if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
  285. baseFormats[baseFormat].current = 0;
  286. }
  287. break;
  288. default:
  289. break;
  290. }
  291. glutPostRedisplay();
  292. }
  293. static void
  294. reshape( int w, int h )
  295. {
  296. winWidth = w;
  297. winHeight = h;
  298. /* No need to call glViewPort here since "draw" calls it! */
  299. }
  300. static void loadTexture( int width, int height,
  301. const struct formatInfo *format )
  302. {
  303. int luminanceSize = 0;
  304. int alphaSize = 0;
  305. int rgbSize = 0;
  306. GLenum textureFormat;
  307. GLubyte *texImage, *p;
  308. int elementsPerGroup, elementSize, groupSize, rowSize;
  309. int i, j;
  310. switch ( format->baseFormat ) {
  311. case GL_LUMINANCE:
  312. luminanceSize = 1;
  313. textureFormat = GL_LUMINANCE;
  314. break;
  315. case GL_INTENSITY:
  316. luminanceSize = 1;
  317. /* Note: format=GL_INTENSITY for glTexImage is not legal */
  318. textureFormat = GL_LUMINANCE;
  319. break;
  320. case GL_ALPHA:
  321. alphaSize = 1;
  322. textureFormat = GL_ALPHA;
  323. break;
  324. case GL_LUMINANCE_ALPHA:
  325. luminanceSize = 1;
  326. alphaSize = 1;
  327. textureFormat = GL_LUMINANCE_ALPHA;
  328. break;
  329. case GL_RGB:
  330. rgbSize = 3;
  331. textureFormat = GL_RGB;
  332. break;
  333. case GL_RGBA:
  334. rgbSize = 3;
  335. alphaSize = 1;
  336. textureFormat = GL_RGBA;
  337. break;
  338. default:
  339. fprintf(stderr, "bad internal format info\n");
  340. return;
  341. }
  342. elementsPerGroup = luminanceSize + alphaSize + rgbSize;
  343. elementSize = sizeof(GLubyte);
  344. groupSize = elementsPerGroup * elementSize;
  345. rowSize = width * groupSize;
  346. if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
  347. fprintf( stderr, "texture malloc failed\n" );
  348. return;
  349. }
  350. for ( i = 0 ; i < height ; i++ )
  351. {
  352. p = texImage + i * rowSize;
  353. for ( j = 0 ; j < width ; j++ )
  354. {
  355. if ( luminanceSize > 0 )
  356. {
  357. /**
  358. ** +-----+-----+
  359. ** | | |
  360. ** | W | LG |
  361. ** | | |
  362. ** +-----+-----+
  363. ** | | |
  364. ** | DG | B |
  365. ** | | |
  366. ** +-----+-----+
  367. **/
  368. if ( i > height / 2 ) {
  369. if ( j < width / 2 ) {
  370. p[0] = 0xff;
  371. } else {
  372. p[0] = 0xaa;
  373. }
  374. } else {
  375. if ( j < width / 2 ) {
  376. p[0] = 0x55;
  377. } else {
  378. p[0] = 0x00;
  379. }
  380. }
  381. p += elementSize;
  382. }
  383. if ( rgbSize > 0 )
  384. {
  385. /**
  386. ** +-----+-----+
  387. ** | | |
  388. ** | R | G |
  389. ** | | |
  390. ** +-----+-----+
  391. ** | | |
  392. ** | Y | B |
  393. ** | | |
  394. ** +-----+-----+
  395. **/
  396. if ( i > height / 2 ) {
  397. if ( j < width / 2 ) {
  398. p[0] = 0xff;
  399. p[1] = 0x00;
  400. p[2] = 0x00;
  401. } else {
  402. p[0] = 0x00;
  403. p[1] = 0xff;
  404. p[2] = 0x00;
  405. }
  406. } else {
  407. if ( j < width / 2 ) {
  408. p[0] = 0xff;
  409. p[1] = 0xff;
  410. p[2] = 0x00;
  411. } else {
  412. p[0] = 0x00;
  413. p[1] = 0x00;
  414. p[2] = 0xff;
  415. }
  416. }
  417. p += 3 * elementSize;
  418. }
  419. if ( alphaSize > 0 )
  420. {
  421. /**
  422. ** +-----------+
  423. ** | W |
  424. ** | +-----+ |
  425. ** | | | |
  426. ** | | B | |
  427. ** | | | |
  428. ** | +-----+ |
  429. ** | |
  430. ** +-----------+
  431. **/
  432. int i2 = i - height / 2;
  433. int j2 = j - width / 2;
  434. int h8 = height / 8;
  435. int w8 = width / 8;
  436. if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
  437. p[0] = 0x00;
  438. } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
  439. p[0] = 0x55;
  440. } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
  441. p[0] = 0xaa;
  442. } else {
  443. p[0] = 0xff;
  444. }
  445. p += elementSize;
  446. }
  447. }
  448. }
  449. glTexImage2D( GL_TEXTURE_2D, 0,
  450. format->internalFormat, width, height, 0,
  451. textureFormat, GL_UNSIGNED_BYTE, texImage );
  452. free( texImage );
  453. }
  454. static void drawCheck( int w, int h, const GLfloat lightCheck[4],
  455. const GLfloat darkCheck[4] )
  456. {
  457. float dw = 2.0 / w;
  458. float dh = 2.0 / h;
  459. int i, j;
  460. for ( i = 0 ; i < w ; i++ ) {
  461. GLfloat x0 = -1.0 + i * dw;
  462. GLfloat x1 = x0 + dw;
  463. glBegin( GL_QUAD_STRIP );
  464. for ( j = 0 ; j <= h ; j++ ) {
  465. GLfloat y = -1.0 + j * dh;
  466. if ( (i ^ j) & 1 ) {
  467. glColor4fv( lightCheck );
  468. } else {
  469. glColor4fv( darkCheck );
  470. }
  471. glVertex2f( x0, y );
  472. glVertex2f( x1, y );
  473. }
  474. glEnd();
  475. }
  476. }
  477. static const char *lookupFormat( GLint format )
  478. {
  479. switch ( format ) {
  480. case GL_RGBA:
  481. return "GL_RGBA";
  482. case GL_RGB:
  483. return "GL_RGB";
  484. case GL_ALPHA:
  485. return "GL_ALPHA";
  486. case GL_LUMINANCE:
  487. return "GL_LUMINANCE";
  488. case GL_LUMINANCE_ALPHA:
  489. return "GL_LUMINANCE_ALPHA";
  490. case GL_INTENSITY:
  491. return "GL_INTENSITY";
  492. case GL_COLOR_INDEX:
  493. return "GL_COLOR_INDEX";
  494. case GL_BGRA:
  495. return "GL_BGRA";
  496. case GL_BGR:
  497. return "GL_BGR";
  498. default:
  499. return "unknown format";
  500. }
  501. }
  502. static void drawSample( int x, int y, int w, int h,
  503. const struct formatInfo *format,
  504. const struct envModeInfo *envMode )
  505. {
  506. glViewport( x, y, w, h );
  507. glScissor( x, y, w, h );
  508. glClearColor( 0.1, 0.1, 0.1, 1.0 );
  509. glClear( GL_COLOR_BUFFER_BIT );
  510. begin2D( w, h );
  511. drawString( format->name, 10, h - 15, labelInfoColor );
  512. drawString( envMode->name, 10, 5, labelInfoColor );
  513. end2D();
  514. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
  515. glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
  516. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  517. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  518. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  519. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  520. loadTexture( textureWidth, textureHeight, format );
  521. if ( drawBackground ) {
  522. drawCheck( 15, 15, lightCheck, darkCheck );
  523. }
  524. if ( drawBlended ) {
  525. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  526. glEnable( GL_BLEND );
  527. }
  528. if ( drawSmooth ) {
  529. glShadeModel( GL_SMOOTH );
  530. }
  531. else {
  532. glShadeModel( GL_FLAT );
  533. glColor4f(1, 1, 1, 1);
  534. }
  535. if ( drawTextured ) {
  536. glEnable( GL_TEXTURE_2D );
  537. }
  538. /*
  539. * if (drawSmooth) then draw quad which goes from purple at the
  540. * bottom (100% alpha) to green at the top (50% alpha).
  541. */
  542. glBegin( GL_QUADS );
  543. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  544. glTexCoord2f( 0.0, 0.0 );
  545. glVertex2f( -0.8, -0.8 );
  546. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  547. glTexCoord2f( 1.0, 0.0 );
  548. glVertex2f( 0.8, -0.8 );
  549. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
  550. glTexCoord2f( 1.0, 1.0 );
  551. glVertex2f( 0.8, 0.8 );
  552. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
  553. glTexCoord2f( 0.0, 1.0 );
  554. glVertex2f( -0.8, 0.8 );
  555. glEnd();
  556. glDisable( GL_BLEND );
  557. glShadeModel( GL_FLAT );
  558. glDisable( GL_TEXTURE_2D );
  559. if ( envMode->mode == GL_DECAL &&
  560. (format->baseFormat == GL_ALPHA ||
  561. format->baseFormat == GL_LUMINANCE ||
  562. format->baseFormat == GL_LUMINANCE_ALPHA ||
  563. format->baseFormat == GL_INTENSITY)) {
  564. /* undefined format/mode combination */
  565. begin2D( w, h );
  566. drawStringOutline( "UNDEFINED MODE", 15, h / 2,
  567. labelLevelColor0, labelLevelColor1 );
  568. end2D();
  569. }
  570. else if ( displayLevelInfo ) {
  571. GLint width, height, border, format;
  572. GLint redSize, greenSize, blueSize, alphaSize;
  573. GLint luminanceSize, intensitySize;
  574. char buf[255];
  575. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
  576. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
  577. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
  578. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
  579. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
  580. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
  581. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
  582. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
  583. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
  584. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
  585. begin2D( w, h );
  586. sprintf( buf, "dimensions: %d x %d", width, height );
  587. drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
  588. sprintf( buf, "border: %d", border );
  589. drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
  590. sprintf( buf, "internal format:" );
  591. drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
  592. sprintf( buf, " %s", lookupFormat( format ) );
  593. drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
  594. sprintf( buf, "sizes:" );
  595. drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
  596. sprintf( buf, " %d / %d / %d / %d / %d / %d",
  597. redSize, greenSize, blueSize, alphaSize,
  598. luminanceSize, intensitySize );
  599. drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
  600. end2D();
  601. }
  602. }
  603. static void display( void )
  604. {
  605. int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
  606. float xBase = (float) winWidth * 0.01;
  607. float xOffset = (winWidth - xBase) / numX;
  608. float xSize = max( xOffset - xBase, 1 );
  609. float yBase = (float) winHeight * 0.01;
  610. float yOffset = (winHeight - yBase) / numY;
  611. float ySize = max( yOffset - yBase, 1 );
  612. float x, y;
  613. int i, j;
  614. glViewport( 0, 0, winWidth, winHeight );
  615. glDisable( GL_SCISSOR_TEST );
  616. glClearColor( 0.0, 0.0, 0.0, 0.0 );
  617. glClear( GL_COLOR_BUFFER_BIT );
  618. glEnable( GL_SCISSOR_TEST );
  619. x = xBase;
  620. y = (winHeight - 1) - yOffset;
  621. for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
  622. {
  623. struct formatInfo *format;
  624. if ( i == baseFormat ) {
  625. labelInfoColor = labelColor1;
  626. } else {
  627. labelInfoColor = labelColor0;
  628. }
  629. format = &baseFormats[i].format[baseFormats[i].current];
  630. for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
  631. struct envModeInfo *envMode;
  632. envMode = &envModes[j];
  633. drawSample( x, y, xSize, ySize, format, envMode );
  634. x += xOffset;
  635. }
  636. x = xBase;
  637. y -= yOffset;
  638. }
  639. if ( doubleBuffered ) {
  640. glutSwapBuffers();
  641. } else {
  642. glFlush();
  643. }
  644. checkErrors();
  645. }
  646. static void usage( char *name )
  647. {
  648. fprintf( stderr, "usage: %s [ options ]\n", name );
  649. fprintf( stderr, "\n" );
  650. fprintf( stderr, "options:\n" );
  651. fprintf( stderr, " -sb single buffered\n" );
  652. fprintf( stderr, " -db double buffered\n" );
  653. fprintf( stderr, " -info print OpenGL driver info\n" );
  654. }
  655. static void instructions( void )
  656. {
  657. fprintf( stderr, "texenv - texture environment and internal format test\n" );
  658. fprintf( stderr, "\n" );
  659. fprintf( stderr, " [c] - cycle through background colors\n" );
  660. fprintf( stderr, " [g] - toggle background\n" );
  661. fprintf( stderr, " [b] - toggle blend\n" );
  662. fprintf( stderr, " [s] - toggle smooth shading\n" );
  663. fprintf( stderr, " [t] - toggle texturing\n" );
  664. fprintf( stderr, " [i] - toggle information display\n" );
  665. fprintf( stderr, " up/down - select row\n" );
  666. fprintf( stderr, " left/right - change row's internal format\n" );
  667. }
  668. int main( int argc, char *argv[] )
  669. {
  670. GLboolean info = GL_FALSE;
  671. int i;
  672. glutInitWindowSize( winWidth, winHeight );
  673. glutInit( &argc, argv );
  674. for ( i = 1 ; i < argc ; i++ ) {
  675. if ( !strcmp( "-sb", argv[i] ) ) {
  676. doubleBuffered = GL_FALSE;
  677. } else if ( !strcmp( "-db", argv[i] ) ) {
  678. doubleBuffered = GL_TRUE;
  679. } else if ( !strcmp( "-info", argv[i] ) ) {
  680. info = GL_TRUE;
  681. } else {
  682. usage( argv[0] );
  683. exit( 1 );
  684. }
  685. }
  686. if ( doubleBuffered ) {
  687. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  688. } else {
  689. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
  690. }
  691. Win = glutCreateWindow( "Texture Environment Test" );
  692. initialize();
  693. instructions();
  694. if ( info ) {
  695. printf( "\n" );
  696. printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
  697. printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
  698. printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
  699. printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
  700. }
  701. #if GL_EXT_texture_env_add
  702. if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
  703. fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
  704. NUM_ENV_MODES--;
  705. }
  706. #endif
  707. glutDisplayFunc( display );
  708. glutReshapeFunc( reshape );
  709. glutKeyboardFunc( keyboard );
  710. glutSpecialFunc( special );
  711. glutMainLoop();
  712. return 0;
  713. }