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 7.1KB

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