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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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_FALSE;
  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, 4, "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( char *string, GLfloat x, GLfloat y, GLfloat color[4] )
  185. {
  186. glColor4fv( color );
  187. glRasterPos2f( x, y );
  188. while ( *string ) {
  189. glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
  190. string++;
  191. }
  192. }
  193. static void drawStringOutline( char *string, GLfloat x, GLfloat y,
  194. GLfloat color[4], GLfloat outline[4] )
  195. {
  196. drawString( string, x - 1, y, outline );
  197. drawString( string, x + 1, y, outline );
  198. drawString( string, x, y - 1, outline );
  199. drawString( string, x, y + 1, outline );
  200. drawString( string, x, y, color );
  201. }
  202. static void begin2D( int width, int height )
  203. {
  204. glMatrixMode( GL_PROJECTION );
  205. glPushMatrix();
  206. glLoadIdentity();
  207. glOrtho( 0, width, 0, height, -1, 1 );
  208. glMatrixMode( GL_MODELVIEW );
  209. glPushMatrix();
  210. glLoadIdentity();
  211. }
  212. static void end2D( void )
  213. {
  214. glMatrixMode( GL_PROJECTION );
  215. glPopMatrix();
  216. glMatrixMode( GL_MODELVIEW );
  217. glPopMatrix();
  218. }
  219. static void initialize( void )
  220. {
  221. glMatrixMode( GL_PROJECTION );
  222. glLoadIdentity();
  223. glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
  224. glMatrixMode(GL_MODELVIEW);
  225. glLoadIdentity();
  226. glShadeModel( GL_FLAT );
  227. }
  228. /* ARGSUSED1 */
  229. static void keyboard( unsigned char c, int x, int y )
  230. {
  231. switch ( c ) {
  232. case 'c':
  233. envColor = ++envColor % (int) NUM_ENV_COLORS;
  234. break;
  235. case 'g':
  236. drawBackground = !drawBackground;
  237. break;
  238. case 'b':
  239. drawBlended = !drawBlended;
  240. break;
  241. case 's':
  242. drawSmooth = !drawSmooth;
  243. break;
  244. case 't':
  245. drawTextured = !drawTextured;
  246. break;
  247. case 'i':
  248. displayLevelInfo = !displayLevelInfo;
  249. break;
  250. case 27: /* Escape key should force exit. */
  251. exit(0);
  252. break;
  253. default:
  254. break;
  255. }
  256. glutPostRedisplay();
  257. }
  258. /* ARGSUSED1 */
  259. static void special( int key, int x, int y )
  260. {
  261. switch ( key ) {
  262. case GLUT_KEY_DOWN:
  263. if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
  264. baseFormat = 0;
  265. }
  266. break;
  267. case GLUT_KEY_UP:
  268. if ( --baseFormat < 0 ) {
  269. baseFormat = NUM_BASE_FORMATS - 1;
  270. }
  271. break;
  272. case GLUT_KEY_LEFT:
  273. --baseFormats[baseFormat].current;
  274. if ( baseFormats[baseFormat].current < 0 ) {
  275. baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
  276. }
  277. break;
  278. case GLUT_KEY_RIGHT:
  279. ++baseFormats[baseFormat].current;
  280. if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
  281. baseFormats[baseFormat].current = 0;
  282. }
  283. break;
  284. default:
  285. break;
  286. }
  287. glutPostRedisplay();
  288. }
  289. static void
  290. reshape( int w, int h )
  291. {
  292. winWidth = w;
  293. winHeight = h;
  294. /* No need to call glViewPort here since "draw" calls it! */
  295. }
  296. static void loadTexture( int width, int height, struct formatInfo *format )
  297. {
  298. int luminanceSize = 0;
  299. int alphaSize = 0;
  300. int rgbSize = 0;
  301. GLenum textureFormat;
  302. GLubyte *texImage, *p;
  303. int elementsPerGroup, elementSize, groupSize, rowSize;
  304. int i, j;
  305. switch ( format->baseFormat ) {
  306. case GL_LUMINANCE:
  307. luminanceSize = 1;
  308. textureFormat = GL_LUMINANCE;
  309. break;
  310. case GL_INTENSITY:
  311. luminanceSize = 1;
  312. textureFormat = GL_INTENSITY;
  313. break;
  314. case GL_ALPHA:
  315. alphaSize = 1;
  316. textureFormat = GL_ALPHA;
  317. break;
  318. case GL_LUMINANCE_ALPHA:
  319. luminanceSize = 1;
  320. alphaSize = 1;
  321. textureFormat = GL_LUMINANCE_ALPHA;
  322. break;
  323. case GL_RGB:
  324. rgbSize = 3;
  325. textureFormat = GL_RGB;
  326. break;
  327. case GL_RGBA:
  328. rgbSize = 3;
  329. alphaSize = 1;
  330. textureFormat = GL_RGBA;
  331. break;
  332. default:
  333. fprintf(stderr, "bad internal format info\n");
  334. return;
  335. }
  336. elementsPerGroup = luminanceSize + alphaSize + rgbSize;
  337. elementSize = sizeof(GLubyte);
  338. groupSize = elementsPerGroup * elementSize;
  339. rowSize = width * groupSize;
  340. if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
  341. fprintf( stderr, "texture malloc failed\n" );
  342. return;
  343. }
  344. for ( i = 0 ; i < height ; i++ )
  345. {
  346. p = texImage + i * rowSize;
  347. for ( j = 0 ; j < width ; j++ )
  348. {
  349. if ( luminanceSize > 0 )
  350. {
  351. /**
  352. ** +-----+-----+
  353. ** | | |
  354. ** | W | LG |
  355. ** | | |
  356. ** +-----+-----+
  357. ** | | |
  358. ** | DG | B |
  359. ** | | |
  360. ** +-----+-----+
  361. **/
  362. if ( i > height / 2 ) {
  363. if ( j < width / 2 ) {
  364. p[0] = 0xff;
  365. } else {
  366. p[0] = 0xaa;
  367. }
  368. } else {
  369. if ( j < width / 2 ) {
  370. p[0] = 0x55;
  371. } else {
  372. p[0] = 0x00;
  373. }
  374. }
  375. p += elementSize;
  376. }
  377. if ( rgbSize > 0 )
  378. {
  379. /**
  380. ** +-----+-----+
  381. ** | | |
  382. ** | R | G |
  383. ** | | |
  384. ** +-----+-----+
  385. ** | | |
  386. ** | Y | B |
  387. ** | | |
  388. ** +-----+-----+
  389. **/
  390. if ( i > height / 2 ) {
  391. if ( j < width / 2 ) {
  392. p[0] = 0xff;
  393. p[1] = 0x00;
  394. p[2] = 0x00;
  395. } else {
  396. p[0] = 0x00;
  397. p[1] = 0xff;
  398. p[2] = 0x00;
  399. }
  400. } else {
  401. if ( j < width / 2 ) {
  402. p[0] = 0xff;
  403. p[1] = 0xff;
  404. p[2] = 0x00;
  405. } else {
  406. p[0] = 0x00;
  407. p[1] = 0x00;
  408. p[2] = 0xff;
  409. }
  410. }
  411. p += 3 * elementSize;
  412. }
  413. if ( alphaSize > 0 )
  414. {
  415. /**
  416. ** +-----------+
  417. ** | W |
  418. ** | +-----+ |
  419. ** | | | |
  420. ** | | B | |
  421. ** | | | |
  422. ** | +-----+ |
  423. ** | |
  424. ** +-----------+
  425. **/
  426. int i2 = i - height / 2;
  427. int j2 = j - width / 2;
  428. int h8 = height / 8;
  429. int w8 = width / 8;
  430. if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
  431. p[0] = 0x00;
  432. } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
  433. p[0] = 0x55;
  434. } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
  435. p[0] = 0xaa;
  436. } else {
  437. p[0] = 0xff;
  438. }
  439. p += elementSize;
  440. }
  441. }
  442. }
  443. glTexImage2D( GL_TEXTURE_2D, 0,
  444. format->internalFormat, width, height, 0,
  445. textureFormat, GL_UNSIGNED_BYTE, texImage );
  446. free( texImage );
  447. }
  448. static void drawCheck( int w, int h, GLfloat lightCheck[4], GLfloat darkCheck[4] )
  449. {
  450. float dw = 2.0 / w;
  451. float dh = 2.0 / h;
  452. int i, j;
  453. for ( i = 0 ; i < w ; i++ ) {
  454. GLfloat x0 = -1.0 + i * dw;
  455. GLfloat x1 = x0 + dw;
  456. glBegin( GL_QUAD_STRIP );
  457. for ( j = 0 ; j <= h ; j++ ) {
  458. GLfloat y = -1.0 + j * dh;
  459. if ( (i ^ j) & 1 ) {
  460. glColor4fv( lightCheck );
  461. } else {
  462. glColor4fv( darkCheck );
  463. }
  464. glVertex2f( x0, y );
  465. glVertex2f( x1, y );
  466. }
  467. glEnd();
  468. }
  469. }
  470. static void drawSample( int x, int y, int w, int h,
  471. struct formatInfo *format, struct envModeInfo *envMode )
  472. {
  473. glViewport( x, y, w, h );
  474. glScissor( x, y, w, h );
  475. glClearColor( 0.1, 0.1, 0.1, 1.0 );
  476. glClear( GL_COLOR_BUFFER_BIT );
  477. begin2D( w, h );
  478. drawString( format->name, 10, h - 15, labelInfoColor );
  479. drawString( envMode->name, 10, 5, labelInfoColor );
  480. end2D();
  481. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
  482. glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
  483. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  484. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  485. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  486. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  487. loadTexture( textureWidth, textureHeight, format );
  488. if ( drawBackground ) {
  489. drawCheck( 15, 15, lightCheck, darkCheck );
  490. }
  491. if ( drawBlended ) {
  492. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  493. glEnable( GL_BLEND );
  494. }
  495. if ( drawSmooth ) {
  496. glShadeModel( GL_SMOOTH );
  497. }
  498. else {
  499. glShadeModel( GL_FLAT );
  500. glColor4f(1, 1, 1, 1);
  501. }
  502. if ( drawTextured ) {
  503. glEnable( GL_TEXTURE_2D );
  504. }
  505. glBegin( GL_QUADS );
  506. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  507. glTexCoord2f( 0.0, 0.0 );
  508. glVertex2f( -0.8, -0.8 );
  509. if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
  510. glTexCoord2f( 1.0, 0.0 );
  511. glVertex2f( 0.8, -0.8 );
  512. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 1.0 );
  513. glTexCoord2f( 1.0, 1.0 );
  514. glVertex2f( 0.8, 0.8 );
  515. if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 1.0 );
  516. glTexCoord2f( 0.0, 1.0 );
  517. glVertex2f( -0.8, 0.8 );
  518. glEnd();
  519. glDisable( GL_BLEND );
  520. glShadeModel( GL_FLAT );
  521. glDisable( GL_TEXTURE_2D );
  522. if ( envMode->mode == GL_DECAL &&
  523. (format->baseFormat == GL_ALPHA ||
  524. format->baseFormat == GL_LUMINANCE ||
  525. format->baseFormat == GL_LUMINANCE_ALPHA ||
  526. format->baseFormat == GL_INTENSITY)) {
  527. /* undefined format/mode combination */
  528. begin2D( w, h );
  529. drawStringOutline( "UNDEFINED MODE", 15, h / 2,
  530. labelLevelColor0, labelLevelColor1 );
  531. end2D();
  532. }
  533. else if ( displayLevelInfo ) {
  534. GLint width, height, border, components;
  535. GLint redSize, greenSize, blueSize, alphaSize;
  536. GLint luminanceSize, intensitySize;
  537. char buf[255];
  538. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
  539. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
  540. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
  541. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &components );
  542. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
  543. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
  544. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
  545. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
  546. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
  547. glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
  548. begin2D( w, h );
  549. sprintf( buf, "dimensions: %d x %d", width, height );
  550. drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
  551. sprintf( buf, "border: %d", border );
  552. drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
  553. sprintf( buf, "components: 0x%04X", components );
  554. drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
  555. sprintf( buf, "sizes:" );
  556. drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
  557. sprintf( buf, " %d / %d / %d / %d / %d / %d",
  558. redSize, greenSize, blueSize, alphaSize,
  559. luminanceSize, intensitySize );
  560. drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
  561. end2D();
  562. }
  563. }
  564. static void display( void )
  565. {
  566. int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
  567. float xBase = (float) winWidth * 0.01;
  568. float xOffset = (winWidth - xBase) / numX;
  569. float xSize = max( xOffset - xBase, 1 );
  570. float yBase = (float) winHeight * 0.01;
  571. float yOffset = (winHeight - yBase) / numY;
  572. float ySize = max( yOffset - yBase, 1 );
  573. float x, y;
  574. int i, j;
  575. glViewport( 0, 0, winWidth, winHeight );
  576. glDisable( GL_SCISSOR_TEST );
  577. glClearColor( 0.0, 0.0, 0.0, 0.0 );
  578. glClear( GL_COLOR_BUFFER_BIT );
  579. glEnable( GL_SCISSOR_TEST );
  580. x = xBase;
  581. y = (winHeight - 1) - yOffset;
  582. for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
  583. {
  584. struct formatInfo *format;
  585. if ( i == baseFormat ) {
  586. labelInfoColor = labelColor1;
  587. } else {
  588. labelInfoColor = labelColor0;
  589. }
  590. format = &baseFormats[i].format[baseFormats[i].current];
  591. for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
  592. struct envModeInfo *envMode;
  593. envMode = &envModes[j];
  594. drawSample( x, y, xSize, ySize, format, envMode );
  595. x += xOffset;
  596. }
  597. x = xBase;
  598. y -= yOffset;
  599. }
  600. if ( doubleBuffered ) {
  601. glutSwapBuffers();
  602. } else {
  603. glFlush();
  604. }
  605. checkErrors();
  606. }
  607. static void usage( char *name )
  608. {
  609. fprintf( stderr, "usage: %s [ options ]\n", name );
  610. fprintf( stderr, "\n" );
  611. fprintf( stderr, "options:\n" );
  612. fprintf( stderr, " -sb single buffered\n" );
  613. fprintf( stderr, " -db double buffered\n" );
  614. fprintf( stderr, " -info print OpenGL driver info\n" );
  615. }
  616. static void instructions( void )
  617. {
  618. fprintf( stderr, "texenv - texture environment and internal format test\n" );
  619. fprintf( stderr, "\n" );
  620. fprintf( stderr, " [c] - cycle through background colors\n" );
  621. fprintf( stderr, " [g] - toggle background\n" );
  622. fprintf( stderr, " [b] - toggle blend\n" );
  623. fprintf( stderr, " [s] - toggle smooth shading\n" );
  624. fprintf( stderr, " [t] - toggle texturing\n" );
  625. fprintf( stderr, " [i] - toggle information display\n" );
  626. }
  627. int main( int argc, char *argv[] )
  628. {
  629. GLboolean info = GL_FALSE;
  630. int i;
  631. glutInit( &argc, argv );
  632. for ( i = 1 ; i < argc ; i++ ) {
  633. if ( !strcmp( "-sb", argv[i] ) ) {
  634. doubleBuffered = GL_FALSE;
  635. } else if ( !strcmp( "-db", argv[i] ) ) {
  636. doubleBuffered = GL_TRUE;
  637. } else if ( !strcmp( "-info", argv[i] ) ) {
  638. info = GL_TRUE;
  639. } else {
  640. usage( argv[0] );
  641. exit( 1 );
  642. }
  643. }
  644. if ( doubleBuffered ) {
  645. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  646. } else {
  647. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
  648. }
  649. glutInitWindowSize( winWidth, winHeight );
  650. glutCreateWindow( "Texture Environment Test" );
  651. initialize();
  652. instructions();
  653. if ( info ) {
  654. printf( "\n" );
  655. printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
  656. printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
  657. printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
  658. printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
  659. }
  660. #if GL_EXT_texture_env_add
  661. if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
  662. fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
  663. NUM_ENV_MODES--;
  664. }
  665. #endif
  666. glutDisplayFunc( display );
  667. glutReshapeFunc( reshape );
  668. glutKeyboardFunc( keyboard );
  669. glutSpecialFunc( special );
  670. glutMainLoop();
  671. return 0;
  672. }