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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. }
  110. }
  111. GLenum StarPoint(GLint n)
  112. {
  113. float x0, y0, x1, y1, width;
  114. GLint i;
  115. x0 = stars[n].x[0] * windW / stars[n].z[0];
  116. y0 = stars[n].y[0] * windH / stars[n].z[0];
  117. RotatePoint(&x0, &y0, stars[n].rotation);
  118. x0 += windW / 2.0;
  119. y0 += windH / 2.0;
  120. if (x0 >= 0.0 && x0 < windW && y0 >= 0.0 && y0 < windH) {
  121. if (stars[n].type == STREAK) {
  122. x1 = stars[n].x[1] * windW / stars[n].z[1];
  123. y1 = stars[n].y[1] * windH / stars[n].z[1];
  124. RotatePoint(&x1, &y1, stars[n].rotation);
  125. x1 += windW / 2.0;
  126. y1 += windH / 2.0;
  127. glLineWidth(MAXPOS/100.0/stars[n].z[0]+1.0);
  128. glColor3f(1.0, (MAXWARP-speed)/MAXWARP, (MAXWARP-speed)/MAXWARP);
  129. if (fabs(x0-x1) < 1.0 && fabs(y0-y1) < 1.0) {
  130. glBegin(GL_POINTS);
  131. glVertex2f(x0, y0);
  132. glEnd();
  133. } else {
  134. glBegin(GL_LINES);
  135. glVertex2f(x0, y0);
  136. glVertex2f(x1, y1);
  137. glEnd();
  138. }
  139. } else {
  140. width = MAXPOS / 10.0 / stars[n].z[0] + 1.0;
  141. glColor3f(1.0, 0.0, 0.0);
  142. glBegin(GL_POLYGON);
  143. for (i = 0; i < 8; i++) {
  144. float x = x0 + width * Cos((float)i*MAXANGLES/8.0);
  145. float y = y0 + width * Sin((float)i*MAXANGLES/8.0);
  146. glVertex2f(x, y);
  147. };
  148. glEnd();
  149. }
  150. return GL_TRUE;
  151. } else {
  152. return GL_FALSE;
  153. }
  154. }
  155. void ShowStars(void)
  156. {
  157. GLint n;
  158. glClear(GL_COLOR_BUFFER_BIT);
  159. for (n = 0; n < starCount; n++) {
  160. if (stars[n].z[0] > speed || (stars[n].z[0] > 0.0 && speed < MAXWARP)) {
  161. if (StarPoint(n) == GL_FALSE) {
  162. NewStar(n, MAXPOS);
  163. }
  164. } else {
  165. NewStar(n, MAXPOS);
  166. }
  167. }
  168. }
  169. static void Init(void)
  170. {
  171. float angle;
  172. GLint n;
  173. srand((unsigned int) glutGet(GLUT_ELAPSED_TIME) );
  174. for (n = 0; n < MAXSTARS; n++) {
  175. NewStar(n, 100);
  176. }
  177. angle = 0.0;
  178. for (n = 0; n < MAXANGLES ; n++) {
  179. sinTable[n] = sin(angle);
  180. angle += PI / (MAXANGLES / 2.0);
  181. }
  182. glClearColor(0.0, 0.0, 0.0, 0.0);
  183. glDisable(GL_DITHER);
  184. }
  185. void Reshape(int width, int height)
  186. {
  187. windW = (GLint)width;
  188. windH = (GLint)height;
  189. glViewport(0, 0, windW, windH);
  190. glMatrixMode(GL_PROJECTION);
  191. glLoadIdentity();
  192. gluOrtho2D(-0.5, windW+0.5, -0.5, windH+0.5);
  193. glMatrixMode(GL_MODELVIEW);
  194. }
  195. static void Key(unsigned char key, int x, int y)
  196. {
  197. switch (key) {
  198. case 27:
  199. exit(1);
  200. case 32:
  201. flag = (flag == NORMAL) ? WEIRD : NORMAL;
  202. break;
  203. case 't':
  204. nitro = 1;
  205. break;
  206. default:
  207. return;
  208. }
  209. }
  210. void Draw(void)
  211. {
  212. MoveStars();
  213. ShowStars();
  214. if (nitro > 0) {
  215. speed = (float)(nitro / 10) + 1.0;
  216. if (speed > MAXWARP) {
  217. speed = MAXWARP;
  218. }
  219. if (++nitro > MAXWARP*10) {
  220. nitro = -nitro;
  221. }
  222. } else if (nitro < 0) {
  223. nitro++;
  224. speed = (float)(-nitro / 10) + 1.0;
  225. if (speed > MAXWARP) {
  226. speed = MAXWARP;
  227. }
  228. }
  229. glFlush();
  230. if (doubleBuffer) {
  231. glutSwapBuffers();
  232. }
  233. }
  234. static GLenum Args(int argc, char **argv)
  235. {
  236. GLint i;
  237. doubleBuffer = GL_TRUE;
  238. for (i = 1; i < argc; i++) {
  239. if (strcmp(argv[i], "-sb") == 0) {
  240. doubleBuffer = GL_FALSE;
  241. } else if (strcmp(argv[i], "-db") == 0) {
  242. doubleBuffer = GL_TRUE;
  243. }
  244. }
  245. return GL_TRUE;
  246. }
  247. void GLUTCALLBACK glut_post_redisplay_p(void)
  248. {
  249. glutPostRedisplay();
  250. }
  251. int main(int argc, char **argv)
  252. {
  253. GLenum type;
  254. glutInit(&argc, argv);
  255. if (Args(argc, argv) == GL_FALSE) {
  256. exit(1);
  257. }
  258. windW = 300;
  259. windH = 300;
  260. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  261. type = GLUT_RGB;
  262. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  263. glutInitDisplayMode(type);
  264. if (glutCreateWindow("Stars") == GL_FALSE) {
  265. exit(1);
  266. }
  267. Init();
  268. glutReshapeFunc(Reshape);
  269. glutKeyboardFunc(Key);
  270. glutDisplayFunc(Draw);
  271. glutIdleFunc(glut_post_redisplay_p);
  272. glutMainLoop();
  273. return 0;
  274. }