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.

skybox.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <GL/glew.h>
  7. #include <GL/glu.h>
  8. #include "readtex.h"
  9. #include "skybox.h"
  10. static int
  11. load(GLenum target, const char *filename,
  12. GLboolean flipTB, GLboolean flipLR)
  13. {
  14. GLint w, h;
  15. GLenum format;
  16. GLubyte *img = LoadRGBImage( filename, &w, &h, &format );
  17. if (!img) {
  18. printf("Error: couldn't load texture image %s\n", filename);
  19. return 0;
  20. }
  21. assert(format == GL_RGB);
  22. printf("Load cube face 0x%x: %s %d x %d\n", target, filename, w, h);
  23. /* <sigh> the way the texture cube mapping works, we have to flip
  24. * images to make things look right.
  25. */
  26. if (flipTB) {
  27. const int stride = 3 * w;
  28. GLubyte temp[3*1024];
  29. int i;
  30. for (i = 0; i < h / 2; i++) {
  31. memcpy(temp, img + i * stride, stride);
  32. memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
  33. memcpy(img + (h - i - 1) * stride, temp, stride);
  34. }
  35. }
  36. if (flipLR) {
  37. const int stride = 3 * w;
  38. GLubyte temp[3];
  39. GLubyte *row;
  40. int i, j;
  41. for (i = 0; i < h; i++) {
  42. row = img + i * stride;
  43. for (j = 0; j < w / 2; j++) {
  44. int k = w - j - 1;
  45. temp[0] = row[j*3+0];
  46. temp[1] = row[j*3+1];
  47. temp[2] = row[j*3+2];
  48. row[j*3+0] = row[k*3+0];
  49. row[j*3+1] = row[k*3+1];
  50. row[j*3+2] = row[k*3+2];
  51. row[k*3+0] = temp[0];
  52. row[k*3+1] = temp[1];
  53. row[k*3+2] = temp[2];
  54. }
  55. }
  56. }
  57. gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
  58. free(img);
  59. return 1;
  60. }
  61. GLuint
  62. LoadSkyBoxCubeTexture(const char *filePosX,
  63. const char *fileNegX,
  64. const char *filePosY,
  65. const char *fileNegY,
  66. const char *filePosZ,
  67. const char *fileNegZ)
  68. {
  69. GLuint tex;
  70. glGenTextures(1, &tex);
  71. glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
  72. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  73. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,
  74. GL_LINEAR_MIPMAP_NEAREST);
  75. if (!load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, filePosX, GL_TRUE, GL_TRUE))
  76. return 0;
  77. if (!load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, fileNegX, GL_TRUE, GL_TRUE))
  78. return 0;
  79. if (!load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, filePosY, 1+GL_FALSE, GL_TRUE))
  80. return 0;
  81. if (!load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, fileNegY, 1+GL_FALSE, GL_TRUE))
  82. return 0;
  83. if (!load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, filePosZ, GL_TRUE, GL_TRUE))
  84. return 0;
  85. if (!load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, fileNegZ, GL_TRUE, GL_TRUE))
  86. return 0;
  87. return tex;
  88. }
  89. #define eps1 0.99
  90. #define br 20.0 /* box radius */
  91. void
  92. DrawSkyBoxCubeTexture(GLuint tex)
  93. {
  94. struct vertex {
  95. float x, y, z, s, t, r;
  96. };
  97. static const struct vertex verts[24] = {
  98. /* +X side */
  99. { br, -br, -br, 1.0, -eps1, -eps1 },
  100. { br, -br, br, 1.0, -eps1, eps1 },
  101. { br, br, br, 1.0, eps1, eps1 },
  102. { br, br, -br, 1.0, eps1, -eps1 },
  103. /* -X side */
  104. { -br, br, -br, -1.0, eps1, -eps1 },
  105. { -br, br, br, -1.0, eps1, eps1 },
  106. { -br, -br, br, -1.0, -eps1, eps1 },
  107. { -br, -br, -br, -1.0, -eps1, -eps1 },
  108. /* +Y side */
  109. { br, br, -br, eps1, 1.0, -eps1 },
  110. { br, br, br, eps1, 1.0, eps1 },
  111. { -br, br, br, -eps1, 1.0, eps1 },
  112. { -br, br, -br, -eps1, 1.0, -eps1 },
  113. /* -Y side */
  114. { -br, -br, -br, -eps1, -1.0, -eps1 },
  115. { -br, -br, br, -eps1, -1.0, eps1 },
  116. { br, -br, br, eps1, -1.0, eps1 },
  117. { br, -br, -br, eps1, -1.0, -eps1 },
  118. /* +Z side */
  119. { br, -br, br, eps1, -eps1, 1.0 },
  120. { -br, -br, br, -eps1, -eps1, 1.0 },
  121. { -br, br, br, -eps1, eps1, 1.0 },
  122. { br, br, br, eps1, eps1, 1.0 },
  123. /* -Z side */
  124. { br, br, -br, eps1, eps1, -1.0 },
  125. { -br, br, -br, -eps1, eps1, -1.0 },
  126. { -br, -br, -br, -eps1, -eps1, -1.0 },
  127. { br, -br, -br, eps1, -eps1, -1.0 },
  128. };
  129. static GLuint vbo = 0;
  130. if (!vbo ) {
  131. glGenBuffersARB(1, &vbo);
  132. glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
  133. glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
  134. GL_STATIC_DRAW_ARB);
  135. }
  136. else {
  137. glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
  138. }
  139. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  140. glVertexPointer(3, GL_FLOAT, sizeof(struct vertex),
  141. (void *) offsetof(struct vertex, x));
  142. glTexCoordPointer(3, GL_FLOAT, sizeof(struct vertex),
  143. (void *) offsetof(struct vertex, s));
  144. glEnableClientState(GL_VERTEX_ARRAY);
  145. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  146. glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
  147. glEnable(GL_TEXTURE_CUBE_MAP);
  148. glDisable(GL_BLEND);
  149. glDrawArrays(GL_QUADS, 0, 24);
  150. glDisable(GL_TEXTURE_CUBE_MAP);
  151. glDisableClientState(GL_VERTEX_ARRAY);
  152. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  153. glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  154. }