Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. struct formatInfo {
  69. GLenum baseFormat;
  70. GLenum internalFormat;
  71. char *name;
  72. };
  73. #define NUM_LUMINANCE_FORMATS (sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
  74. struct formatInfo luminanceFormats[] =
  75. {
  76. { GL_LUMINANCE, GL_LUMINANCE, "LUMINANCE" },
  77. { GL_LUMINANCE, GL_LUMINANCE4, "LUMINANCE4" },
  78. { GL_LUMINANCE, GL_LUMINANCE8, "LUMINANCE8" },
  79. { GL_LUMINANCE, GL_LUMINANCE12, "LUMINANCE12" },
  80. { GL_LUMINANCE, GL_LUMINANCE16, "LUMINANCE16" },
  81. };
  82. #define NUM_ALPHA_FORMATS (sizeof(alphaFormats) / sizeof(alphaFormats[0]))
  83. struct formatInfo alphaFormats[] =
  84. {
  85. { GL_ALPHA, GL_ALPHA, "ALPHA" },
  86. { GL_ALPHA, GL_ALPHA4, "ALPHA4" },
  87. { GL_ALPHA, GL_ALPHA8, "ALPHA8" },
  88. { GL_ALPHA, GL_ALPHA12, "ALPHA12" },
  89. { GL_ALPHA, GL_ALPHA16, "ALPHA16" },
  90. };
  91. #define NUM_INTENSITY_FORMATS (sizeof(intensityFormats) / sizeof(intensityFormats[0]))
  92. struct formatInfo intensityFormats[] =
  93. {
  94. { GL_INTENSITY, GL_INTENSITY, "INTENSITY" },
  95. { GL_INTENSITY, GL_INTENSITY4, "INTENSITY4" },
  96. { GL_INTENSITY, GL_INTENSITY8, "INTENSITY8" },
  97. { GL_INTENSITY, GL_INTENSITY12, "INTENSITY12" },
  98. { GL_INTENSITY, GL_INTENSITY16, "INTENSITY16" },
  99. };
  100. #define NUM_LUMINANCE_ALPHA_FORMATS (sizeof(luminanceAlphaFormats) / sizeof(luminanceAlphaFormats[0]))
  101. struct formatInfo luminanceAlphaFormats[] =
  102. {
  103. { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, "LUMINANCE_ALPHA" },
  104. { GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, "LUMINANCE4_ALPHA4" },
  105. { GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, "LUMINANCE6_ALPHA2" },
  106. { GL_LUMINANCE_ALPHA, GL_LUMINANCE8_ALPHA8, "LUMINANCE8_ALPHA8" },
  107. { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA4, "LUMINANCE12_ALPHA4" },
  108. { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA12, "LUMINANCE12_ALPHA12" },
  109. { GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, "LUMINANCE16_ALPHA16" },
  110. };
  111. #define NUM_RGB_FORMATS (sizeof(rgbFormats) / sizeof(rgbFormats[0]))
  112. struct formatInfo rgbFormats[] =
  113. {
  114. { GL_RGB, GL_RGB, "RGB" },
  115. { GL_RGB, GL_R3_G3_B2, "R3_G3_B2" },
  116. { GL_RGB, GL_RGB4, "RGB4" },
  117. { GL_RGB, GL_RGB5, "RGB5" },
  118. { GL_RGB, GL_RGB8, "RGB8" },
  119. { GL_RGB, GL_RGB10, "RGB10" },
  120. { GL_RGB, GL_RGB12, "RGB12" },
  121. { GL_RGB, GL_RGB16, "RGB16" },
  122. };
  123. #define NUM_RGBA_FORMATS (sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
  124. struct formatInfo rgbaFormats[] =
  125. {
  126. { GL_RGBA, GL_RGBA, "RGBA" },
  127. { GL_RGBA, GL_RGBA2, "RGBA2" },
  128. { GL_RGBA, GL_RGBA4, "RGBA4" },
  129. { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
  130. { GL_RGBA, GL_RGBA8, "RGBA8" },
  131. { GL_RGBA, GL_RGB10_A2, "RGB10_A2" },
  132. { GL_RGBA, GL_RGBA12, "RGBA12" },
  133. { GL_RGBA, GL_RGBA16, "RGBA16" },
  134. };
  135. struct baseFormatInfo {
  136. struct formatInfo *format;
  137. int current, number;
  138. };
  139. #define NUM_BASE_FORMATS (sizeof(baseFormats) / sizeof(baseFormats[0]))
  140. int baseFormat;
  141. struct baseFormatInfo baseFormats[] =
  142. {
  143. { luminanceFormats, 0, NUM_LUMINANCE_FORMATS },
  144. { alphaFormats, 0, NUM_ALPHA_FORMATS },
  145. { intensityFormats, 0, NUM_INTENSITY_FORMATS },
  146. { luminanceAlphaFormats, 0, NUM_LUMINANCE_ALPHA_FORMATS },
  147. { rgbFormats, 0, NUM_RGB_FORMATS },
  148. { rgbaFormats, 0, NUM_RGBA_FORMATS },
  149. };
  150. #define NUM_ENV_COLORS (sizeof(envColors) / sizeof(envColors[0]))
  151. int envColor;
  152. GLfloat envColors[][4] =
  153. {
  154. { 0.0, 0.0, 0.0, 1.0 },
  155. { 1.0, 0.0, 0.0, 1.0 },
  156. { 0.0, 1.0, 0.0, 1.0 },
  157. { 0.0, 0.0, 1.0, 1.0 },
  158. { 1.0, 1.0, 1.0, 1.0 },
  159. };
  160. struct envModeInfo {
  161. GLenum mode;
  162. char *name;
  163. };
  164. /* allow for run-time check for GL_EXT_texture_env_add */
  165. int NUM_ENV_MODES = 5;
  166. struct envModeInfo envModes[] =
  167. {
  168. { GL_REPLACE, "REPLACE" },
  169. { GL_MODULATE, "MODULATE" },
  170. { GL_BLEND, "BLEND" },
  171. { GL_DECAL, "DECAL" },
  172. #if GL_EXT_texture_env_add
  173. { GL_ADD, "ADD" },
  174. #endif
  175. };
  176. static void checkErrors( void )
  177. {
  178. GLenum error;
  179. return;
  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 = ++envColor % (int) NUM_ENV_COLORS;
  236. break;
  237. case 'g':
  238. drawBackground = !drawBackground;
  239. break;
  240. case 'b':
  241. drawBlended = !drawBlended;
  242. break;
  243. case 's':
  244. drawSmooth = !drawSmooth;
  245. break;
  246. case 't':
  247. drawTextured = !drawTextured;
  248. break;
  249. case 'i':
  250. displayLevelInfo = !displayLevelInfo;
  251. break;
  252. case 27: /* Escape key should force exit. */
  253. exit(0);
  254. break;
  255. default:
  256. break;
  257. }
  258. glutPostRedisplay();
  259. }
  260. /* ARGSUSED1 */
  261. static void special( int key, int x, int y )
  262. {
  263. switch ( key ) {
  264. case GLUT_KEY_DOWN:
  265. if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
  266. baseFormat = 0;
  267. }
  268. break;
  269. case GLUT_KEY_UP:
  270. if ( --baseFormat < 0 ) {
  271. baseFormat = NUM_BASE_FORMATS - 1;
  272. }
  273. break;
  274. case GLUT_KEY_LEFT:
  275. --baseFormats[baseFormat].current;
  276. if ( baseFormats[baseFormat].current < 0 ) {
  277. baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
  278. }
  279. break;
  280. case GLUT_KEY_RIGHT:
  281. ++baseFormats[baseFormat].current;
  282. if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
  283. baseFormats[baseFormat].current = 0;
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. glutPostRedisplay();
  290. }
  291. static void
  292. reshape( int w, int h )
  293. {
  294. winWidth = w;
  295. winHeight = h;
  296. /* No need to call glViewPort here since "draw" calls it! */
  297. }
  298. static void loadTexture( int width, int height,
  299. const struct formatInfo *format )
  300. {
  301. int luminanceSize = 0;
  302. int alphaSize = 0;
  303. int rgbSize = 0;
  304. GLenum textureFormat;
  305. GLubyte *texImage, *p;
  306. int elementsPerGroup, elementSize, groupSize, rowSize;
  307. int i, j;
  308. switch ( format->baseFormat ) {
  309. case GL_LUMINANCE:
  310. luminanceSize = 1;
  311. textureFormat = GL_LUMINANCE;
  312. break;
  313. case GL_INTENSITY:
  314. luminanceSize = 1;
  315. textureFormat = GL_INTENSITY;
  316. break;
  317. case GL_ALPHA:
  318. alphaSize = 1;
  319. textureFormat = GL_ALPHA;
  320. break;
  321. case GL_LUMINANCE_ALPHA:
  322. luminanceSize = 1;
  323. alphaSize = 1;
  324. textureFormat = GL_LUMINANCE_ALPHA;
  325. break;
  326. case GL_RGB:
  327. rgbSize = 3;
  328. textureFormat = GL_RGB;
  329. break;
  330. case GL_RGBA:
  331. rgbSize = 3;
  332. alphaSize = 1;
  333. textureFormat = GL_RGBA;
  334. break;
  335. default:
  336. fprintf(stderr, "bad internal format info\n");
  337. return;
  338. }
  339. elementsPerGroup = luminanceSize + alphaSize + rgbSize;
  340. elementSize = sizeof(GLubyte);
  341. groupSize = elementsPerGroup * elementSize;
  342. rowSize = width * groupSize;
  343. if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
  344. fprintf( stderr, "texture malloc failed\n" );
  345. return;
  346. }
  347. for ( i = 0 ; i < height ; i++ )
  348. {
  349. p = texImage + i * rowSize;
  350. for ( j = 0 ; j < width ; j++ )
  351. {
  352. if ( luminanceSize > 0 )
  353. {
  354. /**
  355. ** +-----+-----+
  356. ** | | |
  357. ** | W | LG |
  358. ** | | |
  359. ** +-----+-----+
  360. ** | | |
  361. ** | DG | B |
  362. ** | | |
  363. ** +-----+-----+
  364. **/
  365. if ( i > height / 2 ) {
  366. if ( j < width / 2 ) {
  367. p[0] = 0xff;
  368. } else {
  369. p[0] = 0xaa;
  370. }
  371. } else {
  372. if ( j < width / 2 ) {
  373. p[0] = 0x55;
  374. } else {
  375. p[0] = 0x00;
  376. }
  377. }
  378. p += elementSize;
  379. }
  380. if ( rgbSize > 0 )
  381. {
  382. /**
  383. ** +-----+-----+
  384. ** | | |
  385. ** | R | G |
  386. ** | | |
  387. ** +-----+-----+
  388. ** | | |
  389. ** | Y | B |
  390. ** | | |
  391. ** +-----+-----+
  392. **/
  393. if ( i > height / 2 ) {
  394. if ( j < width / 2 ) {
  395. p[0] = 0xff;
  396. p[1] = 0x00;
  397. p[2] = 0x00;
  398. } else {
  399. p[0] = 0x00;
  400. p[1] = 0xff;
  401. p[2] = 0x00;
  402. }
  403. } else {
  404. if ( j < width / 2 ) {
  405. p[0] = 0xff;
  406. p[1] = 0xff;
  407. p[2] = 0x00;
  408. } else {
  409. p[0] = 0x00;
  410. p[1] = 0x00;
  411. p[2] = 0xff;
  412. }
  413. }
  414. p += 3 * elementSize;
  415. }
  416. if ( alphaSize > 0 )
  417. {
  418. /**
  419. ** +-----------+
  420. ** | W |
  421. ** | +-----+ |
  422. ** | | | |
  423. ** | | B | |
  424. ** | | | |
  425. ** | +-----+ |
  426. ** | |
  427. ** +-----------+
  428. **/
  429. int i2 = i - height / 2;
  430. int j2 = j - width / 2;
  431. int h8 = height / 8;
  432. int w8 = width / 8;
  433. if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
  434. p[0] = 0x00;
  435. } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
  436. p[0] = 0x55;
  437. } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
  438. p[0] = 0xaa;
  439. } else {
  440. p[0] = 0xff;
  441. }
  442. p += elementSize;
  443. }
  444. }
  445. }
  446. glTexImage2D( GL_TEXTURE_2D, 0,
  447. format->internalFormat, width, height, 0,
  448. textureFormat, GL_UNSIGNED_BYTE, texImage );
  449. free( texImage );
  450. }
  451. static void drawCheck( int w, int h, const GLfloat lightCheck[4],
  452. const GLfloat darkCheck[4] )
  453. {
  454. float dw = 2.0 / w;
  455. float dh = 2.0 / h;
  456. int i, j;
  457. for ( i = 0 ; i < w ; i++ ) {
  458. GLfloat x0 = -1.0 + i * dw;
  459. GLfloat x1 = x0 + dw;
  460. glBegin( GL_QUAD_STRIP );
  461. for ( j = 0 ; j <= h ; j++ ) {
  462. GLfloat y = -1.0 + j * dh;
  463. if ( (i ^ j) & 1 ) {
  464. glColor4fv( lightCheck );
  465. } else {
  466. glColor4fv( darkCheck );
  467. }
  468. glVertex2f( x0, y );
  469. glVertex2f( x1, y );
  470. }
  471. glEnd();
  472. }
  473. }
  474. static const char *lookupFormat( GLint format )
  475. {
  476. switch ( format ) {
  477. case GL_RGBA:
  478. return "GL_RGBA";
  479. case GL_RGB:
  480. return "GL_RGB";
  481. case GL_ALPHA:
  482. return "GL_ALPHA";
  483. case GL_LUMINANCE:
  484. return "GL_LUMINANCE";
  485. case GL_LUMINANCE_ALPHA:
  486. return "GL_LUMINANCE_ALPHA";
  487. case GL_INTENSITY:
  488. return "GL_INTENSITY";
  489. case GL_COLOR_INDEX:
  490. return "GL_COLOR_INDEX";
  491. case GL_BGRA:
  492. return "GL_BGRA";
  493. case GL_BGR:
  494. return "GL_BGR";
  495. default:
  496. return "unknown format";
  497. }
  498. }
  499. static void drawSample( int x, int y, int w, int h,
  500. const struct formatInfo *format,
  501. const struct envModeInfo *envMode )
  502. {
  503. glViewport( x, y, w, h );
  504. glScissor( x, y, w, h );
  505. glClearColor( 0.1, 0.1, 0.1, 1.0 );
  506. glClear( GL_COLOR_BUFFER_BIT );
  507. begin2D( w, h );
  508. drawString( format->name, 10, h - 15, labelInfoColor );
  509. drawString( envMode->name, 10, 5, labelInfoColor );
  510. end2D();
  511. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
  512. glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
  513. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  514. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  515. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  516. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  517. loadTexture( textureWidth, textureHeight, format );
  518. if ( drawBackground ) {
  519. drawCheck( 15, 15, lightCheck, darkCheck );
  520. }
  521. if ( drawBlended ) {
  522. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  523. glEnable( GL_BLEND );
  524. }
  525. if ( drawSmooth ) {
  526. glShadeModel( GL_SMOOTH );
  527. }
  528. else {
  529. glShadeModel( GL_FLAT );
  530. glColor4f(1, 1, 1, 1);
  531. }
  532. if ( drawTextured ) {
  533. glEnable( GL_TEXTURE_2D );
  534. }
  535. /*
  536. * if (drawSmooth) then draw quad which goes from purple at the
  537. * bottom (100% alpha) to green at the top (50% alpha).
  538. */
  539. glBegin( GL_QUADS );
  540. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  541. glTexCoord2f( 0.0, 0.0 );
  542. glVertex2f( -0.8, -0.8 );
  543. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  544. glTexCoord2f( 1.0, 0.0 );
  545. glVertex2f( 0.8, -0.8 );
  546. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
  547. glTexCoord2f( 1.0, 1.0 );
  548. glVertex2f( 0.8, 0.8 );
  549. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
  550. glTexCoord2f( 0.0, 1.0 );
  551. glVertex2f( -0.8, 0.8 );
  552. glEnd();
  553. glDisable( GL_BLEND );
  554. glShadeModel( GL_FLAT );
  555. glDisable( GL_TEXTURE_2D );
  556. if ( envMode->mode == GL_DECAL &&
  557. (format->baseFormat == GL_ALPHA ||
  558. format->baseFormat == GL_LUMINANCE ||
  559. format->baseFormat == GL_LUMINANCE_ALPHA ||
  560. format->baseFormat == GL_INTENSITY)) {
  561. /* undefined format/mode combination */
  562. begin2D( w, h );
  563. drawStringOutline( "UNDEFINED MODE", 15, h / 2,
  564. labelLevelColor0, labelLevelColor1 );
  565. end2D();
  566. }
  567. else if ( displayLevelInfo ) {
  568. GLint width, height, border, format;
  569. GLint redSize, greenSize, blueSize, alphaSize;
  570. GLint luminanceSize, intensitySize;
  571. char buf[255];
  572. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
  573. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
  574. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
  575. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
  576. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
  577. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
  578. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
  579. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
  580. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
  581. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
  582. begin2D( w, h );
  583. sprintf( buf, "dimensions: %d x %d", width, height );
  584. drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
  585. sprintf( buf, "border: %d", border );
  586. drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
  587. sprintf( buf, "internal format:" );
  588. drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
  589. sprintf( buf, " %s", lookupFormat( format ) );
  590. drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
  591. sprintf( buf, "sizes:" );
  592. drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
  593. sprintf( buf, " %d / %d / %d / %d / %d / %d",
  594. redSize, greenSize, blueSize, alphaSize,
  595. luminanceSize, intensitySize );
  596. drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
  597. end2D();
  598. }
  599. }
  600. static void display( void )
  601. {
  602. int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
  603. float xBase = (float) winWidth * 0.01;
  604. float xOffset = (winWidth - xBase) / numX;
  605. float xSize = max( xOffset - xBase, 1 );
  606. float yBase = (float) winHeight * 0.01;
  607. float yOffset = (winHeight - yBase) / numY;
  608. float ySize = max( yOffset - yBase, 1 );
  609. float x, y;
  610. int i, j;
  611. glViewport( 0, 0, winWidth, winHeight );
  612. glDisable( GL_SCISSOR_TEST );
  613. glClearColor( 0.0, 0.0, 0.0, 0.0 );
  614. glClear( GL_COLOR_BUFFER_BIT );
  615. glEnable( GL_SCISSOR_TEST );
  616. x = xBase;
  617. y = (winHeight - 1) - yOffset;
  618. for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
  619. {
  620. struct formatInfo *format;
  621. if ( i == baseFormat ) {
  622. labelInfoColor = labelColor1;
  623. } else {
  624. labelInfoColor = labelColor0;
  625. }
  626. format = &baseFormats[i].format[baseFormats[i].current];
  627. for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
  628. struct envModeInfo *envMode;
  629. envMode = &envModes[j];
  630. drawSample( x, y, xSize, ySize, format, envMode );
  631. x += xOffset;
  632. }
  633. x = xBase;
  634. y -= yOffset;
  635. }
  636. if ( doubleBuffered ) {
  637. glutSwapBuffers();
  638. } else {
  639. glFlush();
  640. }
  641. checkErrors();
  642. }
  643. static void usage( char *name )
  644. {
  645. fprintf( stderr, "usage: %s [ options ]\n", name );
  646. fprintf( stderr, "\n" );
  647. fprintf( stderr, "options:\n" );
  648. fprintf( stderr, " -sb single buffered\n" );
  649. fprintf( stderr, " -db double buffered\n" );
  650. fprintf( stderr, " -info print OpenGL driver info\n" );
  651. }
  652. static void instructions( void )
  653. {
  654. fprintf( stderr, "texenv - texture environment and internal format test\n" );
  655. fprintf( stderr, "\n" );
  656. fprintf( stderr, " [c] - cycle through background colors\n" );
  657. fprintf( stderr, " [g] - toggle background\n" );
  658. fprintf( stderr, " [b] - toggle blend\n" );
  659. fprintf( stderr, " [s] - toggle smooth shading\n" );
  660. fprintf( stderr, " [t] - toggle texturing\n" );
  661. fprintf( stderr, " [i] - toggle information display\n" );
  662. fprintf( stderr, " up/down - select row\n" );
  663. fprintf( stderr, " left/right - change row's internal format\n" );
  664. }
  665. int main( int argc, char *argv[] )
  666. {
  667. GLboolean info = GL_FALSE;
  668. int i;
  669. glutInit( &argc, argv );
  670. for ( i = 1 ; i < argc ; i++ ) {
  671. if ( !strcmp( "-sb", argv[i] ) ) {
  672. doubleBuffered = GL_FALSE;
  673. } else if ( !strcmp( "-db", argv[i] ) ) {
  674. doubleBuffered = GL_TRUE;
  675. } else if ( !strcmp( "-info", argv[i] ) ) {
  676. info = GL_TRUE;
  677. } else {
  678. usage( argv[0] );
  679. exit( 1 );
  680. }
  681. }
  682. if ( doubleBuffered ) {
  683. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  684. } else {
  685. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
  686. }
  687. glutInitWindowSize( winWidth, winHeight );
  688. glutInitWindowPosition( 0, 0 );
  689. glutCreateWindow( "Texture Environment Test" );
  690. initialize();
  691. instructions();
  692. if ( info ) {
  693. printf( "\n" );
  694. printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
  695. printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
  696. printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
  697. printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
  698. }
  699. #if GL_EXT_texture_env_add
  700. if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
  701. fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
  702. NUM_ENV_MODES--;
  703. }
  704. #endif
  705. glutDisplayFunc( display );
  706. glutReshapeFunc( reshape );
  707. glutKeyboardFunc( keyboard );
  708. glutSpecialFunc( special );
  709. glutMainLoop();
  710. return 0;
  711. }