main.cpp
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 4k
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_opengl.h"
  8. #include "render_data.h"
  9. #ifdef _ENABLE_DX10_
  10. #include "render_dx10.h"
  11. #endif
  12. GutTimer g_Timer;
  13. void GetUserInput(void)
  14. {
  15. // `读取鼠标`
  16. GutMouseInfo mouse;
  17. GutReadMouse(&mouse);
  18. // `读取键盘`
  19. char keyboard_state[256];
  20. GutReadKeyboard(keyboard_state);
  21. // `获得画完前一个画面到现在所经历的时间`
  22. float time_diff = g_Timer.Stop();
  23. g_Timer.Restart();
  24. float moving_speed = 2.0f * time_diff;
  25. float rotation_speed = 1.0 * time_diff;
  26. // `极坐标系统`
  27. static float theta = -MATH_PI * 0.5f;
  28. static float phi = 0.0f;
  29. // `如果左击, 就旋转镜头.`
  30. if ( mouse.button[0] ) 
  31. {
  32. theta += mouse.x * rotation_speed;
  33. phi -= mouse.y * rotation_speed;
  34. }
  35. float sin_phi, cos_phi;
  36. float sin_theta, cos_theta;
  37. FastMath::SinCos(phi, sin_phi, cos_phi);
  38. FastMath::SinCos(theta, sin_theta, cos_theta);
  39. // `计算镜头的法线`
  40. Vector4 camera_facing;
  41. camera_facing[0] = cos_phi * cos_theta;
  42. camera_facing[1] = sin_phi;
  43. camera_facing[2] = cos_phi * sin_theta;
  44. // `计算镜头正上方的轴向`
  45. Vector4 camera_up;
  46. FastMath::SinCos(phi + MATH_PI*0.5f, sin_phi, cos_phi);
  47. camera_up[0] = cos_phi * cos_theta;
  48. camera_up[1] = sin_phi;
  49. camera_up[2] = cos_phi * sin_theta;
  50. // `获得镜面右方的方向`
  51. Vector4 camera_right = Vector3CrossProduct(camera_facing, camera_up);
  52. // `按下W或方向键向上`
  53. if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] )
  54. {
  55. g_eye += camera_facing * moving_speed;
  56. }
  57. // `按下S或方向键向下`
  58. if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] )
  59. {
  60. g_eye -= camera_facing * moving_speed;
  61. }
  62. // `按下A或方向键向左`
  63. if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] )
  64. {
  65. g_eye -= camera_right * moving_speed;
  66. }
  67. // `按下D或方向键向右`
  68. if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] )
  69. {
  70. g_eye += camera_right * moving_speed;
  71. }
  72. // `计算出镜头对准的点, 生成镜头转换矩阵时会用到.`
  73. g_lookat = g_eye + camera_facing;
  74. // `因为是对2个轴转动, 需要更新镜头朝上的轴.`
  75. g_up = camera_up;
  76. }
  77. void main(int argc, char *argv[])
  78. {
  79. // 默认使用DirectX 9来绘图
  80. char *device = "dx9";
  81. void (*render)(void) = RenderFrameDX9;
  82. bool (*init_resource)(void) = InitResourceDX9;
  83. bool (*release_resource)(void) = ReleaseResourceDX9;
  84. #ifdef _ENABLE_DX10_
  85. printf("Pressn(1) for Direct3D9n(2) for OpenGLn(3) for Direct3D10n");
  86. #else
  87. printf("Pressn(1) for Direct3D9n(2) for OpenGLn");
  88. #endif
  89. int c = getche();
  90. switch(c)
  91. {
  92. default:
  93. case '1':
  94. render = RenderFrameDX9;
  95. init_resource = InitResourceDX9;
  96. release_resource = ReleaseResourceDX9;
  97. break;
  98. case '2':
  99. device = "opengl";
  100. init_resource = InitResourceOpenGL;
  101. release_resource = ReleaseResourceOpenGL;
  102. render = RenderFrameOpenGL;
  103. break;
  104. case '3':
  105. #ifdef _ENABLE_DX10_
  106. device = "dx10";
  107. init_resource = InitResourceDX10;
  108. release_resource = ReleaseResourceDX10;
  109. render = RenderFrameDX10;
  110. #endif
  111. break;
  112. }
  113. // 在(100,100)的位置打开一个大小为(512x512)的窗口
  114. GutCreateWindow(100, 100, 512, 512, device);
  115. // 做OpenGL或DirectX初始化
  116. if ( !GutInitGraphicsDevice(device) )
  117. {
  118. printf("Failed to initialize %s devicen", device);
  119. exit(0);
  120. }
  121. GutInputInit();
  122. g_view_matrix.Identity();
  123. // 载入shader
  124. if ( !init_resource() )
  125. {
  126. release_resource();
  127. printf("Failed to load resourcesn");
  128. exit(0);
  129. }
  130. // 主循环
  131. while( GutProcessMessage() )
  132. {
  133. GetUserInput();
  134. render();
  135. }
  136. // 释放shader
  137. release_resource();
  138. // 关闭OpenGL/DirectX绘图装备
  139. GutReleaseGraphicsDevice();
  140. }