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.

quad-tex-2d.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glut.h>
  28. static GLenum Target = GL_TEXTURE_2D;
  29. static GLenum Filter = GL_NEAREST;
  30. GLenum doubleBuffer;
  31. static float Rot = 0;
  32. static int win = 0;
  33. static void Init(void)
  34. {
  35. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  36. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  37. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  38. fflush(stderr);
  39. glClearColor(0.0, 0.0, 1.0, 0.0);
  40. #define SIZE 32
  41. {
  42. GLubyte tex2d[SIZE][SIZE][3];
  43. GLint s, t;
  44. for (s = 0; s < SIZE; s++) {
  45. for (t = 0; t < SIZE; t++) {
  46. #if 0
  47. tex2d[t][s][0] = (s < SIZE/2) ? 0 : 255;
  48. tex2d[t][s][1] = (t < SIZE/2) ? 0 : 255;
  49. tex2d[t][s][2] = 0;
  50. #else
  51. tex2d[t][s][0] = s*255/(SIZE-1);
  52. tex2d[t][s][1] = t*255/(SIZE-1);
  53. tex2d[t][s][2] = 0;
  54. #endif
  55. }
  56. }
  57. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  58. if (Target == GL_TEXTURE_1D)
  59. glTexImage1D(Target, 0, 3, SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, tex2d);
  60. else
  61. glTexImage2D(Target, 0, 3, SIZE, SIZE, 0,
  62. GL_RGB, GL_UNSIGNED_BYTE, tex2d);
  63. glEnable(Target);
  64. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  65. glTexParameterf(Target, GL_TEXTURE_WRAP_R, GL_REPEAT);
  66. glTexParameterf(Target, GL_TEXTURE_MIN_FILTER, Filter);
  67. glTexParameterf(Target, GL_TEXTURE_MAG_FILTER, Filter);
  68. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  69. }
  70. }
  71. static void Reshape(int width, int height)
  72. {
  73. glViewport(0, 0, (GLint)width, (GLint)height);
  74. glMatrixMode(GL_PROJECTION);
  75. glLoadIdentity();
  76. #if 0
  77. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  78. #else
  79. glFrustum(-1, 1, -1, 1, 10, 20);
  80. #endif
  81. glMatrixMode(GL_MODELVIEW);
  82. glLoadIdentity();
  83. glTranslatef(0, 0, -15);
  84. }
  85. static void Key(unsigned char key, int x, int y)
  86. {
  87. switch (key) {
  88. case 'r':
  89. Rot += 10.0;
  90. break;
  91. case 'R':
  92. Rot -= 10.0;
  93. break;
  94. case 27:
  95. glutDestroyWindow(win);
  96. exit(0);
  97. default:
  98. return;
  99. }
  100. glutPostRedisplay();
  101. }
  102. static void Draw(void)
  103. {
  104. glClear(GL_COLOR_BUFFER_BIT);
  105. glPushMatrix();
  106. glRotatef(Rot, 0, 1, 0);
  107. glBegin(GL_QUADS);
  108. glTexCoord2f(1,0);
  109. glVertex3f( 0.9, -0.9, 0.0);
  110. glTexCoord2f(1,1);
  111. glVertex3f( 0.9, 0.9, 0.0);
  112. glTexCoord2f(0,1);
  113. glVertex3f(-0.9, 0.9, 0.0);
  114. glTexCoord2f(0,0);
  115. glVertex3f(-0.9, -0.9, 0.0);
  116. glEnd();
  117. glPopMatrix();
  118. glFlush();
  119. if (doubleBuffer) {
  120. glutSwapBuffers();
  121. }
  122. }
  123. static GLenum Args(int argc, char **argv)
  124. {
  125. GLint i;
  126. doubleBuffer = GL_FALSE;
  127. for (i = 1; i < argc; i++) {
  128. if (strcmp(argv[i], "-sb") == 0) {
  129. doubleBuffer = GL_FALSE;
  130. } else if (strcmp(argv[i], "-db") == 0) {
  131. doubleBuffer = GL_TRUE;
  132. } else {
  133. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  134. return GL_FALSE;
  135. }
  136. }
  137. return GL_TRUE;
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. GLenum type;
  142. glutInit(&argc, argv);
  143. if (Args(argc, argv) == GL_FALSE) {
  144. exit(1);
  145. }
  146. glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
  147. type = GLUT_RGB;
  148. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  149. glutInitDisplayMode(type);
  150. win = glutCreateWindow(*argv);
  151. if (!win) {
  152. exit(1);
  153. }
  154. Init();
  155. glutReshapeFunc(Reshape);
  156. glutKeyboardFunc(Key);
  157. glutDisplayFunc(Draw);
  158. glutMainLoop();
  159. return 0;
  160. }