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.

star.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <math.h>
  28. #include <GL/glut.h>
  29. #ifndef PI
  30. #define PI 3.141592657
  31. #endif
  32. enum {
  33. NORMAL = 0,
  34. WEIRD = 1
  35. };
  36. enum {
  37. STREAK = 0,
  38. CIRCLE = 1
  39. };
  40. #define MAXSTARS 400
  41. #define MAXPOS 10000
  42. #define MAXWARP 10
  43. #define MAXANGLES 6000
  44. typedef struct _starRec {
  45. GLint type;
  46. float x[2], y[2], z[2];
  47. float offsetX, offsetY, offsetR, rotation;
  48. } starRec;
  49. GLenum doubleBuffer;
  50. GLint windW, windH;
  51. GLenum flag = NORMAL;
  52. GLint starCount = MAXSTARS / 2;
  53. float speed = 1.0;
  54. GLint nitro = 0;
  55. starRec stars[MAXSTARS];
  56. float sinTable[MAXANGLES];
  57. float Sin(float angle)
  58. {
  59. return (sinTable[(GLint)angle]);
  60. }
  61. float Cos(float angle)
  62. {
  63. return (sinTable[((GLint)angle+(MAXANGLES/4))%MAXANGLES]);
  64. }
  65. void NewStar(GLint n, GLint d)
  66. {
  67. if (rand()%4 == 0) {
  68. stars[n].type = CIRCLE;
  69. } else {
  70. stars[n].type = STREAK;
  71. }
  72. stars[n].x[0] = (float)(rand() % MAXPOS - MAXPOS / 2);
  73. stars[n].y[0] = (float)(rand() % MAXPOS - MAXPOS / 2);
  74. stars[n].z[0] = (float)(rand() % MAXPOS + d);
  75. if (rand()%4 == 0 && flag == WEIRD) {
  76. stars[n].offsetX = (float)(rand() % 100 - 100 / 2);
  77. stars[n].offsetY = (float)(rand() % 100 - 100 / 2);
  78. stars[n].offsetR = (float)(rand() % 25 - 25 / 2);
  79. } else {
  80. stars[n].offsetX = 0.0;
  81. stars[n].offsetY = 0.0;
  82. stars[n].offsetR = 0.0;
  83. }
  84. }
  85. void RotatePoint(float *x, float *y, float rotation)
  86. {
  87. float tmpX, tmpY;
  88. tmpX = *x * Cos(rotation) - *y * Sin(rotation);
  89. tmpY = *y * Cos(rotation) + *x * Sin(rotation);
  90. *x = tmpX;
  91. *y = tmpY;
  92. }
  93. void MoveStars(void)
  94. {
  95. float offset;
  96. GLint n;
  97. offset = speed * 60.0;
  98. for (n = 0; n < starCount; n++) {
  99. stars[n].x[1] = stars[n].x[0];
  100. stars[n].y[1] = stars[n].y[0];
  101. stars[n].z[1] = stars[n].z[0];
  102. stars[n].x[0] += stars[n].offsetX;
  103. stars[n].y[0] += stars[n].offsetY;
  104. stars[n].z[0] -= offset;
  105. stars[n].rotation += stars[n].offsetR;
  106. if (stars[n].rotation > MAXANGLES) {
  107. stars[n].rotation = 0.0;
  108. }
  109. else if (stars[n].rotation < 0.0) {
  110. stars[n].rotation += 360.0;
  111. }
  112. }
  113. }
  114. GLenum StarPoint(GLint n)
  115. {
  116. float x0, y0, x1, y1, width;
  117. GLint i;
  118. x0 = stars[n].x[0] * windW / stars[n].z[0];
  119. y0 = stars[n].y[0] * windH / stars[n].z[0];
  120. RotatePoint(&x0, &y0, stars[n].rotation);
  121. x0 += windW / 2.0;
  122. y0 += windH / 2.0;
  123. if (x0 >= 0.0 && x0 < windW && y0 >= 0.0 && y0 < windH) {
  124. if (stars[n].type == STREAK) {
  125. x1 = stars[n].x[1] * windW / stars[n].z[1];
  126. y1 = stars[n].y[1] * windH / stars[n].z[1];
  127. RotatePoint(&x1, &y1, stars[n].rotation);
  128. x1 += windW / 2.0;
  129. y1 += windH / 2.0;
  130. glLineWidth(MAXPOS/100.0/stars[n].z[0]+1.0);
  131. glColor3f(1.0, (MAXWARP-speed)/MAXWARP, (MAXWARP-speed)/MAXWARP);
  132. if (fabs(x0-x1) < 1.0 && fabs(y0-y1) < 1.0) {
  133. glBegin(GL_POINTS);
  134. glVertex2f(x0, y0);
  135. glEnd();
  136. } else {
  137. glBegin(GL_LINES);
  138. glVertex2f(x0, y0);
  139. glVertex2f(x1, y1);
  140. glEnd();
  141. }
  142. } else {
  143. width = MAXPOS / 10.0 / stars[n].z[0] + 1.0;
  144. glColor3f(1.0, 0.0, 0.0);
  145. glBegin(GL_POLYGON);
  146. for (i = 0; i < 8; i++) {
  147. float x = x0 + width * Cos((float)i*MAXANGLES/8.0);
  148. float y = y0 + width * Sin((float)i*MAXANGLES/8.0);
  149. glVertex2f(x, y);
  150. };
  151. glEnd();
  152. }
  153. return GL_TRUE;
  154. } else {
  155. return GL_FALSE;
  156. }
  157. }
  158. void ShowStars(void)
  159. {
  160. GLint n;
  161. glClear(GL_COLOR_BUFFER_BIT);
  162. for (n = 0; n < starCount; n++) {
  163. if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 && speed < MAXWARP)) {
  164. if (StarPoint(n) == GL_FALSE) {
  165. NewStar(n, MAXPOS);
  166. }
  167. } else {
  168. NewStar(n, MAXPOS);
  169. }
  170. }
  171. }
  172. static void Init(void)
  173. {
  174. float angle;
  175. GLint n;
  176. srand((unsigned int) glutGet(GLUT_ELAPSED_TIME) );
  177. for (n = 0; n < MAXSTARS; n++) {
  178. NewStar(n, 100);
  179. }
  180. angle = 0.0;
  181. for (n = 0; n < MAXANGLES ; n++) {
  182. sinTable[n] = sin(angle);
  183. angle += PI / (MAXANGLES / 2.0);
  184. }
  185. glClearColor(0.0, 0.0, 0.0, 0.0);
  186. glDisable(GL_DITHER);
  187. }
  188. void Reshape(int width, int height)
  189. {
  190. windW = (GLint)width;
  191. windH = (GLint)height;
  192. glViewport(0, 0, windW, windH);
  193. glMatrixMode(GL_PROJECTION);
  194. glLoadIdentity();
  195. gluOrtho2D(-0.5, windW+0.5, -0.5, windH+0.5);
  196. glMatrixMode(GL_MODELVIEW);
  197. }
  198. static void Key(unsigned char key, int x, int y)
  199. {
  200. switch (key) {
  201. case 27:
  202. exit(1);
  203. case 32:
  204. flag = (flag == NORMAL) ? WEIRD : NORMAL;
  205. break;
  206. case 't':
  207. nitro = 1;
  208. break;
  209. default:
  210. return;
  211. }
  212. }
  213. void Draw(void)
  214. {
  215. MoveStars();
  216. ShowStars();
  217. if (nitro > 0) {
  218. speed = (float)(nitro / 10) + 1.0;
  219. if (speed > MAXWARP) {
  220. speed = MAXWARP;
  221. }
  222. if (++nitro > MAXWARP*10) {
  223. nitro = -nitro;
  224. }
  225. } else if (nitro < 0) {
  226. nitro++;
  227. speed = (float)(-nitro / 10) + 1.0;
  228. if (speed > MAXWARP) {
  229. speed = MAXWARP;
  230. }
  231. }
  232. glFlush();
  233. if (doubleBuffer) {
  234. glutSwapBuffers();
  235. }
  236. }
  237. static GLenum Args(int argc, char **argv)
  238. {
  239. GLint i;
  240. doubleBuffer = GL_TRUE;
  241. for (i = 1; i < argc; i++) {
  242. if (strcmp(argv[i], "-sb") == 0) {
  243. doubleBuffer = GL_FALSE;
  244. } else if (strcmp(argv[i], "-db") == 0) {
  245. doubleBuffer = GL_TRUE;
  246. }
  247. }
  248. return GL_TRUE;
  249. }
  250. void GLUTCALLBACK glut_post_redisplay_p(void)
  251. {
  252. glutPostRedisplay();
  253. }
  254. int main(int argc, char **argv)
  255. {
  256. GLenum type;
  257. glutInit(&argc, argv);
  258. if (Args(argc, argv) == GL_FALSE) {
  259. exit(1);
  260. }
  261. windW = 300;
  262. windH = 300;
  263. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  264. type = GLUT_RGB;
  265. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  266. glutInitDisplayMode(type);
  267. if (glutCreateWindow("Stars") == GL_FALSE) {
  268. exit(1);
  269. }
  270. Init();
  271. glutReshapeFunc(Reshape);
  272. glutKeyboardFunc(Key);
  273. glutDisplayFunc(Draw);
  274. glutIdleFunc(glut_post_redisplay_p);
  275. glutMainLoop();
  276. return 0;
  277. }