Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * GL_ARB_multitexture demo
  3. *
  4. * Command line options:
  5. * -info print GL implementation information
  6. *
  7. *
  8. * Brian Paul November 1998 This program is in the public domain.
  9. * Modified on 12 Feb 2002 for > 2 texture units.
  10. */
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <GL/glew.h>
  16. #include <GL/glut.h>
  17. #include "readtex.h"
  18. #define TEXTURE_1_FILE "../images/girl.rgb"
  19. #define TEXTURE_2_FILE "../images/reflect.rgb"
  20. #define TEX0 1
  21. #define TEX7 8
  22. #define ANIMATE 10
  23. #define QUIT 100
  24. static GLint T0 = 0;
  25. static GLint Frames = 0;
  26. static GLboolean Animate = GL_TRUE;
  27. static GLint NumUnits = 1;
  28. static GLboolean TexEnabled[8];
  29. static GLfloat Drift = 0.0;
  30. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  31. static void Idle( void )
  32. {
  33. if (Animate) {
  34. GLint i;
  35. Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  36. for (i = 0; i < NumUnits; i++) {
  37. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  38. glMatrixMode(GL_TEXTURE);
  39. glLoadIdentity();
  40. if (i == 0) {
  41. glTranslatef(Drift, 0.0, 0.0);
  42. glScalef(2, 2, 1);
  43. }
  44. else if (i == 1) {
  45. glTranslatef(0.0, Drift, 0.0);
  46. }
  47. else {
  48. float tx = 0.5, ty = 0.5;
  49. glTranslatef(tx, ty, 0.0);
  50. glRotatef(180.0 * Drift, 0, 0, 1);
  51. glScalef(1.0/i, 1.0/i, 1.0/i);
  52. glTranslatef(-tx, -ty + i * 0.1, 0.0);
  53. }
  54. }
  55. glMatrixMode(GL_MODELVIEW);
  56. glutPostRedisplay();
  57. }
  58. }
  59. static void DrawObject(void)
  60. {
  61. static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
  62. static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
  63. GLint i, j;
  64. if (!TexEnabled[0] && !TexEnabled[1])
  65. glColor3f(0.1, 0.1, 0.1); /* add onto this */
  66. else
  67. glColor3f(1, 1, 1); /* modulate this */
  68. glBegin(GL_QUADS);
  69. for (j = 0; j < 4; j++ ) {
  70. for (i = 0; i < NumUnits; i++) {
  71. if (TexEnabled[i])
  72. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
  73. tex_coords[j], tex_coords[j+1]);
  74. }
  75. glVertex2f( vtx_coords[j], vtx_coords[j+1] );
  76. }
  77. glEnd();
  78. }
  79. static void Display( void )
  80. {
  81. glClear( GL_COLOR_BUFFER_BIT );
  82. glPushMatrix();
  83. glRotatef(Xrot, 1.0, 0.0, 0.0);
  84. glRotatef(Yrot, 0.0, 1.0, 0.0);
  85. glRotatef(Zrot, 0.0, 0.0, 1.0);
  86. glScalef(5.0, 5.0, 5.0);
  87. DrawObject();
  88. glPopMatrix();
  89. glutSwapBuffers();
  90. Frames++;
  91. {
  92. GLint t = glutGet(GLUT_ELAPSED_TIME);
  93. if (t - T0 >= 5000) {
  94. GLfloat seconds = (t - T0) / 1000.0;
  95. GLfloat fps = Frames / seconds;
  96. printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
  97. fflush(stdout);
  98. T0 = t;
  99. Frames = 0;
  100. }
  101. }
  102. }
  103. static void Reshape( int width, int height )
  104. {
  105. glViewport( 0, 0, width, height );
  106. glMatrixMode( GL_PROJECTION );
  107. glLoadIdentity();
  108. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  109. /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  110. glMatrixMode( GL_MODELVIEW );
  111. glLoadIdentity();
  112. glTranslatef( 0.0, 0.0, -70.0 );
  113. }
  114. static void ToggleUnit(int unit)
  115. {
  116. TexEnabled[unit] = !TexEnabled[unit];
  117. glActiveTextureARB(GL_TEXTURE0_ARB + unit);
  118. if (TexEnabled[unit])
  119. glEnable(GL_TEXTURE_2D);
  120. else
  121. glDisable(GL_TEXTURE_2D);
  122. printf("Enabled: ");
  123. for (unit = 0; unit < NumUnits; unit++)
  124. printf("%d ", (int) TexEnabled[unit]);
  125. printf("\n");
  126. }
  127. static void ModeMenu(int entry)
  128. {
  129. if (entry >= TEX0 && entry <= TEX7) {
  130. /* toggle */
  131. GLint i = entry - TEX0;
  132. ToggleUnit(i);
  133. }
  134. else if (entry==ANIMATE) {
  135. Animate = !Animate;
  136. if (Animate)
  137. glutIdleFunc(Idle);
  138. else
  139. glutIdleFunc(NULL);
  140. }
  141. else if (entry==QUIT) {
  142. exit(0);
  143. }
  144. glutPostRedisplay();
  145. }
  146. static void Key( unsigned char key, int x, int y )
  147. {
  148. (void) x;
  149. (void) y;
  150. switch (key) {
  151. case 'a':
  152. Animate = !Animate;
  153. break;
  154. case '0':
  155. ToggleUnit(0);
  156. break;
  157. case '1':
  158. ToggleUnit(1);
  159. break;
  160. case '2':
  161. ToggleUnit(2);
  162. break;
  163. case '3':
  164. ToggleUnit(3);
  165. break;
  166. case '4':
  167. ToggleUnit(4);
  168. break;
  169. case '5':
  170. ToggleUnit(5);
  171. break;
  172. case '6':
  173. ToggleUnit(6);
  174. break;
  175. case '7':
  176. ToggleUnit(7);
  177. break;
  178. case 27:
  179. exit(0);
  180. break;
  181. }
  182. glutPostRedisplay();
  183. }
  184. static void SpecialKey( int key, int x, int y )
  185. {
  186. float step = 3.0;
  187. (void) x;
  188. (void) y;
  189. switch (key) {
  190. case GLUT_KEY_UP:
  191. Xrot += step;
  192. break;
  193. case GLUT_KEY_DOWN:
  194. Xrot -= step;
  195. break;
  196. case GLUT_KEY_LEFT:
  197. Yrot += step;
  198. break;
  199. case GLUT_KEY_RIGHT:
  200. Yrot -= step;
  201. break;
  202. }
  203. glutPostRedisplay();
  204. }
  205. static void Init( int argc, char *argv[] )
  206. {
  207. GLuint texObj[8];
  208. GLint size, i;
  209. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  210. if (!strstr(exten, "GL_ARB_multitexture")) {
  211. printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
  212. exit(1);
  213. }
  214. glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
  215. printf("%d texture units supported\n", NumUnits);
  216. if (NumUnits > 8)
  217. NumUnits = 8;
  218. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
  219. printf("%d x %d max texture size\n", size, size);
  220. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  221. for (i = 0; i < NumUnits; i++) {
  222. if (i < 2)
  223. TexEnabled[i] = GL_TRUE;
  224. else
  225. TexEnabled[i] = GL_FALSE;
  226. }
  227. /* allocate two texture objects */
  228. glGenTextures(NumUnits, texObj);
  229. /* setup the texture objects */
  230. for (i = 0; i < NumUnits; i++) {
  231. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  232. glBindTexture(GL_TEXTURE_2D, texObj[i]);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  235. if (i == 0) {
  236. if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
  237. printf("Error: couldn't load texture image\n");
  238. exit(1);
  239. }
  240. }
  241. else if (i == 1) {
  242. if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
  243. printf("Error: couldn't load texture image\n");
  244. exit(1);
  245. }
  246. }
  247. else {
  248. /* checker */
  249. GLubyte image[8][8][3];
  250. GLint i, j;
  251. for (i = 0; i < 8; i++) {
  252. for (j = 0; j < 8; j++) {
  253. if ((i + j) & 1) {
  254. image[i][j][0] = 50;
  255. image[i][j][1] = 50;
  256. image[i][j][2] = 50;
  257. }
  258. else {
  259. image[i][j][0] = 25;
  260. image[i][j][1] = 25;
  261. image[i][j][2] = 25;
  262. }
  263. }
  264. }
  265. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
  266. GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
  267. }
  268. /* Bind texObj[i] to ith texture unit */
  269. if (i < 2)
  270. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  271. else
  272. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  273. if (TexEnabled[i])
  274. glEnable(GL_TEXTURE_2D);
  275. }
  276. glShadeModel(GL_FLAT);
  277. glClearColor(0.3, 0.3, 0.4, 1.0);
  278. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  279. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  280. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  281. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  282. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  283. }
  284. }
  285. int main( int argc, char *argv[] )
  286. {
  287. GLint i;
  288. glutInitWindowSize( 300, 300 );
  289. glutInit( &argc, argv );
  290. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  291. glutCreateWindow(argv[0] );
  292. glewInit();
  293. Init( argc, argv );
  294. glutReshapeFunc( Reshape );
  295. glutKeyboardFunc( Key );
  296. glutSpecialFunc( SpecialKey );
  297. glutDisplayFunc( Display );
  298. if (Animate)
  299. glutIdleFunc(Idle);
  300. glutCreateMenu(ModeMenu);
  301. for (i = 0; i < NumUnits; i++) {
  302. char s[100];
  303. sprintf(s, "Toggle Texture %d", i);
  304. glutAddMenuEntry(s, TEX0 + i);
  305. }
  306. glutAddMenuEntry("Toggle Animation", ANIMATE);
  307. glutAddMenuEntry("Quit", QUIT);
  308. glutAttachMenu(GLUT_RIGHT_BUTTON);
  309. glutMainLoop();
  310. return 0;
  311. }