Source code of Windows XP (NT5)
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.

320 lines
7.0 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <GL/gl.h>
  7. #include <tk.h>
  8. #include "atlantis.h"
  9. fishRec sharks[NUM_SHARKS];
  10. fishRec momWhale;
  11. fishRec babyWhale;
  12. fishRec dolph;
  13. GLint windW, windH;
  14. GLenum directRender;
  15. GLenum mouseLeftAction = GL_FALSE, mouseRightAction = GL_FALSE,
  16. mouseMiddleAction = GL_FALSE;
  17. char *fileName = 0;
  18. TK_RGBImageRec *image;
  19. void InitFishs(void)
  20. {
  21. int i;
  22. for (i = 0; i < NUM_SHARKS; i++) {
  23. sharks[i].x = 70000.0 + rand() % 6000;
  24. sharks[i].y = rand() % 6000;
  25. sharks[i].z = rand() % 6000;
  26. sharks[i].psi = rand() % 360 - 180.0;
  27. sharks[i].v = 1.0;
  28. }
  29. dolph.x = 30000.0;
  30. dolph.y = 0.0;
  31. dolph.z = 6000.0;
  32. dolph.psi = 90.0;
  33. dolph.theta = 0.0;
  34. dolph.v = 3.0;
  35. momWhale.x = 70000.0;
  36. momWhale.y = 0.0;
  37. momWhale.z = 0.0;
  38. momWhale.psi = 90.0;
  39. momWhale.theta = 0.0;
  40. momWhale.v = 3.0;
  41. babyWhale.x = 60000.0;
  42. babyWhale.y = -2000.0;
  43. babyWhale.z = -2000.0;
  44. babyWhale.psi = 90.0;
  45. babyWhale.theta = 0.0;
  46. babyWhale.v = 3.0;
  47. }
  48. void Init(void)
  49. {
  50. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  51. static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  52. static float position[] = {0.0, 1.0, 0.0, 0.0};
  53. static float mat_shininess[] = {90.0};
  54. static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
  55. static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
  56. static float mat_ambient[] = {0.0, 0.1, 0.2, 1.0};
  57. static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
  58. static float lmodel_localviewer[] = {0.0};
  59. GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
  60. GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
  61. glFrontFace(GL_CW);
  62. glDepthFunc(GL_LEQUAL);
  63. glEnable(GL_DEPTH_TEST);
  64. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  65. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  66. glLightfv(GL_LIGHT0, GL_POSITION, position);
  67. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  68. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  69. glEnable(GL_LIGHTING);
  70. glEnable(GL_LIGHT0);
  71. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  72. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  73. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  74. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  75. if (fileName != 0) {
  76. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  77. image = tkRGBImageLoad(fileName);
  78. glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB,
  79. GL_UNSIGNED_BYTE, (unsigned char *)image->data);
  80. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  81. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  82. glPushMatrix();
  83. glMatrixMode(GL_MODELVIEW);
  84. glLoadIdentity();
  85. map1[0] = 1.0 / 200000.0 * 50.0;
  86. map2[2] = 1.0 / 200000.0 * 50.0;
  87. glTexGenfv(GL_S, GL_EYE_PLANE, map1);
  88. glTexGenfv(GL_T, GL_EYE_PLANE, map2);
  89. glPopMatrix();
  90. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  91. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  92. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  93. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  94. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  95. glEnable(GL_TEXTURE_2D);
  96. glEnable(GL_TEXTURE_GEN_S);
  97. glEnable(GL_TEXTURE_GEN_T);
  98. }
  99. InitFishs();
  100. glClearColor(0.0, 0.5, 0.9, 0.0);
  101. }
  102. void Reshape(int width, int height)
  103. {
  104. windW = (GLint)width;
  105. windH = (GLint)height;
  106. glViewport(0, 0, windW, windH);
  107. glMatrixMode(GL_PROJECTION);
  108. glLoadIdentity();
  109. gluPerspective(400.0, 1.0, 1.0, 2000000.0);
  110. glMatrixMode(GL_MODELVIEW);
  111. }
  112. GLenum Key(int key, GLenum mask)
  113. {
  114. switch (key) {
  115. case TK_ESCAPE:
  116. tkQuit();
  117. default:
  118. return GL_FALSE;
  119. }
  120. return GL_TRUE;
  121. }
  122. void DoMouseLeft(void)
  123. {
  124. float x, y;
  125. int mouseX, mouseY;
  126. tkGetMouseLoc(&mouseX, &mouseY);
  127. x = (float)mouseX - ((float)windW / 2.0);
  128. y = (float)mouseY - ((float)windH / 2.0);
  129. glTranslatef(-x*20.0, y*20.0, 0.0);
  130. }
  131. void DoMouseRight(void)
  132. {
  133. float y;
  134. int mouseX, mouseY;
  135. tkGetMouseLoc(&mouseX, &mouseY);
  136. y = (float)mouseY - ((float)windH / 2.0);
  137. glTranslatef(0.0, 0.0, -y*20.0);
  138. }
  139. void DoMouseMiddle(void)
  140. {
  141. float x, y;
  142. int mouseX, mouseY;
  143. tkGetMouseLoc(&mouseX, &mouseY);
  144. x = (float)mouseX - ((float)windW / 2.0);
  145. y = (float)mouseY - ((float)windH / 2.0);
  146. glRotatef(-x/100.0, 0.0, 1.0, 0.0);
  147. glRotatef(-y/100.0, 1.0, 0.0, 0.0);
  148. }
  149. GLenum MouseDown(int mouseX, int mouseY, GLenum button)
  150. {
  151. float x, y;
  152. if (button & TK_LEFTBUTTON) {
  153. mouseLeftAction = GL_TRUE;
  154. }
  155. if (button & TK_RIGHTBUTTON) {
  156. mouseRightAction = GL_TRUE;
  157. }
  158. if (button & TK_MIDDLEBUTTON) {
  159. mouseMiddleAction = GL_TRUE;
  160. }
  161. return GL_TRUE;
  162. }
  163. GLenum MouseUp(int mouseX, int mouseY, GLenum button)
  164. {
  165. float x, y;
  166. if (button & TK_LEFTBUTTON) {
  167. mouseLeftAction = GL_FALSE;
  168. }
  169. if (button & TK_RIGHTBUTTON) {
  170. mouseRightAction = GL_FALSE;
  171. }
  172. if (button & TK_MIDDLEBUTTON) {
  173. mouseMiddleAction = GL_FALSE;
  174. }
  175. return GL_TRUE;
  176. }
  177. void Animate(void)
  178. {
  179. int i;
  180. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  181. if (mouseLeftAction == GL_TRUE) {
  182. DoMouseLeft();
  183. }
  184. if (mouseRightAction == GL_TRUE) {
  185. DoMouseRight();
  186. }
  187. if (mouseMiddleAction == GL_TRUE) {
  188. DoMouseMiddle();
  189. }
  190. for (i = 0; i < NUM_SHARKS; i++) {
  191. glPushMatrix();
  192. SharkPilot(&sharks[i]);
  193. SharkMiss(i);
  194. FishTransform(&sharks[i]);
  195. DrawShark(&sharks[i]);
  196. glPopMatrix();
  197. }
  198. glPushMatrix();
  199. WhalePilot(&dolph);
  200. dolph.phi++;
  201. FishTransform(&dolph);
  202. DrawDolphin(&dolph);
  203. glPopMatrix();
  204. glPushMatrix();
  205. WhalePilot(&momWhale);
  206. momWhale.phi++;
  207. FishTransform(&momWhale);
  208. DrawWhale(&momWhale);
  209. glPopMatrix();
  210. glPushMatrix();
  211. WhalePilot(&babyWhale);
  212. babyWhale.phi++;
  213. FishTransform(&babyWhale);
  214. glScalef(0.45, 0.45, 0.3);
  215. DrawWhale(&babyWhale);
  216. glPopMatrix();
  217. tkSwapBuffers();
  218. }
  219. GLenum Args(int argc, char **argv)
  220. {
  221. GLint i;
  222. directRender = GL_TRUE;
  223. for (i = 1; i < argc; i++) {
  224. if (strcmp(argv[i], "-dr") == 0) {
  225. directRender = GL_TRUE;
  226. } else if (strcmp(argv[i], "-ir") == 0) {
  227. directRender = GL_FALSE;
  228. } else if (strcmp(argv[i], "-f") == 0) {
  229. if (i+1 >= argc || argv[i+1][0] == '-') {
  230. printf("-f (No file name).\n");
  231. return GL_FALSE;
  232. } else {
  233. fileName = argv[++i];
  234. }
  235. } else {
  236. printf("%s (Bad option).\n", argv[i]);
  237. return GL_FALSE;
  238. }
  239. }
  240. return GL_TRUE;
  241. }
  242. void main(int argc, char **argv)
  243. {
  244. GLenum type;
  245. if (Args(argc, argv) == GL_FALSE) {
  246. tkQuit();
  247. }
  248. windW = 600;
  249. windH = 600;
  250. tkInitPosition(10, 30, windW, windH);
  251. type = TK_RGB | TK_DOUBLE | TK_DEPTH16;
  252. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  253. tkInitDisplayMode(type);
  254. if (tkInitWindow("Atlantis Demo") == GL_FALSE) {
  255. tkQuit();
  256. }
  257. Init();
  258. tkExposeFunc(Reshape);
  259. tkReshapeFunc(Reshape);
  260. tkKeyDownFunc(Key);
  261. tkMouseDownFunc(MouseDown);
  262. tkMouseUpFunc(MouseUp);
  263. tkIdleFunc(Animate);
  264. tkExec();
  265. }