main.cpp
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 3k
Category:

OpenGL program

Development Platform:

Visual C++

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include "Gut.h"
  4. #include "GutInput.h"
  5. #include "GutTimer.h"
  6. #include "render_dx9.h"
  7. #include "render_dx10.h"
  8. #include "render_opengl.h"
  9. #include "render_data.h"
  10. GutTimer g_Timer;
  11. float g_fFrame_Time = 0.0f;
  12. void GetUserInput(void)
  13. {
  14. // 读取鼠标
  15. GutMouseInfo mouse;
  16. GutReadMouse(&mouse);
  17. // 获得画完前一个画面到现在所经历的时间
  18. g_fFrame_Time = g_Timer.Stop();
  19. g_Timer.Restart();
  20. float moving_speed = 2.0f * g_fFrame_Time;
  21. float rotation_speed = 1.0 * g_fFrame_Time;
  22. // 如果左击,就旋转镜头
  23. if ( mouse.button[0] ) 
  24. {
  25. g_fRotate_Y += mouse.x * rotation_speed;
  26. g_fRotate_X += mouse.y * rotation_speed;
  27. }
  28. }
  29. void frame_move(void)
  30. {
  31. g_simulation_days += g_fFrame_Time * simulation_speed;
  32. // 把太阳放在世界坐标系原点
  33. g_sun_matrix.Identity();
  34. // 算出地球的位置
  35. g_earth_matrix = g_sun_matrix; // 把地球放到太阳的坐标系上
  36. g_earth_matrix.RotateY( 2.0f * PI * g_simulation_days / days_a_year); 
  37. g_earth_matrix.TranslateX( earth_to_sun_distance );
  38. // 算出月球的位置
  39. g_moon_matrix = g_earth_matrix; // 把月球放到地球的坐标系上
  40. g_moon_matrix.RotateY( 2.0f * PI * g_simulation_days / days_a_month );
  41. g_moon_matrix.TranslateX( moon_to_earth_distance );
  42. }
  43. void main(void)
  44. {
  45. // 默认使用DirectX 9来绘图
  46. char *device = "dx9";
  47. void (*render)(void) = RenderFrameDX9;
  48. bool (*init_resource)(void) = InitResourceDX9;
  49. bool (*release_resource)(void) = ReleaseResourceDX9;
  50. void (*resize_func)(int width, int height) = ResizeWindowDX9;
  51. #ifdef _ENABLE_DX10_
  52. printf("Pressn(1) for Direct3D9n(2) for OpenGLn(3) for Direct3D10n");
  53. #else
  54. printf("Pressn(1) for Direct3D9n(2) for OpenGLn");
  55. #endif
  56. int c = getche();
  57. switch(c)
  58. {
  59. default:
  60. case '1':
  61. render = RenderFrameDX9;
  62. init_resource = InitResourceDX9;
  63. release_resource = ReleaseResourceDX9;
  64. resize_func = ResizeWindowDX9;
  65. break;
  66. case '2':
  67. device = "opengl";
  68. init_resource = InitResourceOpenGL;
  69. release_resource = ReleaseResourceOpenGL;
  70. render = RenderFrameOpenGL;
  71. resize_func = ResizeWindowOpenGL;
  72. break;
  73. case '3':
  74. device = "dx10";
  75. init_resource = InitResourceDX10;
  76. release_resource = ReleaseResourceDX10;
  77. render = RenderFrameDX10;
  78. resize_func = ResizeWindowDX10;
  79. break;
  80. }
  81. GutResizeFunc( resize_func );
  82. // 在(100,100)的位置打开一个大小为(512x512)的窗口
  83. GutCreateWindow(100, 100, 512, 512, device);
  84. // 做OpenGL或DirectX初始化
  85. if ( !GutInitGraphicsDevice(device) )
  86. {
  87. printf("Failed to initialize %s devicen", device);
  88. exit(0);
  89. }
  90. g_view_matrix.Identity();
  91. GutInputInit();
  92. // Sun
  93. float yellow[]={1.0f, 1.0f, 0.0f, 1.0f};
  94. CreateSphere(2.0f, &g_pSunVertices, &g_pSphereIndices, yellow);
  95. // Earch
  96. float blue[]={0.0f, 0.0f, 1.0f, 1.0f};
  97. CreateSphere(1.0f, &g_pEarthVertices, NULL, blue);
  98. // Moon
  99. float white[]={1.0f, 1.0f, 1.0f, 1.0f};
  100. CreateSphere(0.2f, &g_pMoonVertices, NULL);
  101. // 载入shader
  102. if ( !init_resource() )
  103. {
  104. release_resource();
  105. printf("Failed to load resourcesn");
  106. exit(0);
  107. }
  108. // 主循环
  109. while( GutProcessMessage() )
  110. {
  111. GetUserInput();
  112. frame_move();
  113. render();
  114. }
  115. // 释放shader
  116. release_resource();
  117. // 关闭OpenGL/DirectX绘图设备
  118. GutReleaseGraphicsDevice();
  119. }