2
0

mainDemo.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <glad/glad.h>
  4. #include <GLFW/glfw3.h>
  5. #include "../headerOnly/gl3d.h"
  6. #include <chrono>
  7. #include <random>
  8. int w = 840;
  9. int h = 640;
  10. #define USE_GPU_ENGINE 1
  11. #define DEBUG_OUTPUT 0
  12. #undef min
  13. #undef max
  14. #pragma region gpu
  15. extern "C"
  16. {
  17. __declspec(dllexport) unsigned long NvOptimusEnablement = USE_GPU_ENGINE;
  18. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = USE_GPU_ENGINE;
  19. }
  20. #pragma endregion
  21. std::vector<gl3d::Entity> balls;
  22. int main()
  23. {
  24. int a = 0;
  25. #pragma region init
  26. if (!glfwInit())
  27. {
  28. std::cout << "err initializing glfw";
  29. }
  30. glfwWindowHint(GLFW_SAMPLES, 1);
  31. #if DEBUG_OUTPUT
  32. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
  33. #endif
  34. //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  35. //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  36. //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  37. gl3d::Renderer3D renderer;
  38. GLFWwindow* wind = glfwCreateWindow(w, h, "geam", nullptr, nullptr);
  39. glfwMakeContextCurrent(wind);
  40. glfwSwapInterval(0);
  41. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  42. {
  43. std::cout << "err initializing glad";
  44. }
  45. #pragma region enable debug output
  46. #if DEBUG_OUTPUT
  47. glEnable(GL_DEBUG_OUTPUT);
  48. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  49. glDebugMessageCallback(gl3d::glDebugOutput, &renderer);
  50. glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
  51. #endif
  52. #pragma endregion
  53. renderer.init(w, h, RESOURCES_PATH "BRDFintegrationMap.png");
  54. const char* names[6] =
  55. {RESOURCES_PATH "skyBoxes/ocean/right.jpg",
  56. RESOURCES_PATH "skyBoxes/ocean/left.jpg",
  57. RESOURCES_PATH "skyBoxes/ocean/top.jpg",
  58. RESOURCES_PATH "skyBoxes/ocean/bottom.jpg",
  59. RESOURCES_PATH "skyBoxes/ocean/front.jpg",
  60. RESOURCES_PATH "skyBoxes/ocean/back.jpg" };
  61. renderer.skyBox = renderer.loadSkyBox(names);
  62. //renderer.skyBox = renderer.atmosfericScattering({0,1,0}, {0.2,0.2,0.5}, {0.6,0.2,0.1}, 10);
  63. //renderer.skyBox = renderer.loadHDRSkyBox(RESOURCES_PATH "skyBoxes/Newport_Loft_Ref.hdr", 0);
  64. //renderer.skyBox.color = {0.2,0.3,0.8};
  65. //auto rockMaterialModel = renderer.loadMaterial(RESOURCES_PATH "rock/rock.mtl", 0);
  66. auto model = renderer.loadModel(RESOURCES_PATH "obj/sphere.obj", gl3d::TextureLoadQuality::maxQuality, 1.f);
  67. for (int x = -10; x < 10; x++)
  68. for (int y = -10; y < 10; y++)
  69. for (int z = 0; z < 20; z++)
  70. {
  71. gl3d::Transform transform{};
  72. transform.position = glm::vec3(x,y,z)*2.2f;
  73. auto entity = renderer.createEntity(model, transform, false);
  74. balls.push_back(entity);
  75. }
  76. std::cout << "Balls cound: " << balls.size() << "\n";
  77. //transform.rotation.x = glm::radians(90.f);
  78. //renderer.setEntityMeshMaterial(entity, 0, steveMaterial[0]);
  79. //renderer.setEntityMeshMaterial(entity, 0, rockMaterialModel[0]);
  80. #pragma region deltaTime
  81. int fpsCount = 0;
  82. float timeFpsCount = 0;
  83. int timeBeg = clock();
  84. #pragma endregion
  85. while (!glfwWindowShouldClose(wind))
  86. {
  87. #pragma region window metrics
  88. glfwGetWindowSize(wind, &w, &h);
  89. w = std::max(w, 1);
  90. h = std::max(h, 1);
  91. #pragma endregion
  92. #pragma region deltatime
  93. int timeEnd = clock();
  94. float deltaTime = (timeEnd - timeBeg) / 1000.f;
  95. timeBeg = clock();
  96. timeFpsCount += deltaTime;
  97. fpsCount++;
  98. if (timeFpsCount > 1)
  99. {
  100. timeFpsCount -= 1;
  101. std::string name = std::to_string(fpsCount);
  102. name = "fps: " + name;
  103. glfwSetWindowTitle(wind, name.c_str());
  104. fpsCount = 0;
  105. }
  106. #pragma endregion
  107. #pragma region camera
  108. int a = 0;
  109. //change position
  110. if (1)
  111. {
  112. for (int i = 0; i < balls.size(); i++)
  113. {
  114. auto t = renderer.getEntityTransform(balls[i]);
  115. renderer.setEntityTransform(balls[i], t);
  116. }
  117. }
  118. //change material
  119. if (1 && !balls.empty())
  120. {
  121. static int counter = 0;
  122. //static float timer;
  123. //timer += deltaTime;
  124. //if (timer >= 1)
  125. for(int i=0; i<4; i++)
  126. {
  127. //timer -= 1;
  128. //auto m = renderer.getEntityMeshMaterialValues(balls[counter], 0);
  129. auto m = gl3d::MaterialValues{};
  130. auto getRandomFloat = []()
  131. {
  132. return (rand() % 1000) / 1000.f;
  133. //std::uniform_real_distribution<float> dist(0, 1.f);
  134. //std::random_device d;
  135. //return dist(d);
  136. };
  137. m.metallic = getRandomFloat();
  138. m.roughness = getRandomFloat();
  139. m.kd.r = getRandomFloat();
  140. m.kd.g = getRandomFloat();
  141. m.kd.b = getRandomFloat();
  142. if (getRandomFloat() > 0.80f)
  143. {
  144. m.emmisive = getRandomFloat();
  145. }
  146. else
  147. {
  148. m.emmisive = 0;
  149. }
  150. renderer.setEntityMeshMaterialValues(balls[counter], 0, m);
  151. counter++;
  152. if (counter >= balls.size()) { counter = 0; }
  153. }
  154. }
  155. //rotate camera
  156. if (0)
  157. {
  158. static float rotation = 0;
  159. rotation += 3.141592 * deltaTime * 0.7;
  160. if (rotation >= 3.141592 * 2) { rotation -= 3.141592 * 2; }
  161. glm::vec3 cameraPos(0, 0, 5);
  162. cameraPos = glm::rotate(rotation, glm::vec3{0,1,0}) * glm::vec4(cameraPos,1);
  163. //std::cout << glm::length(cameraPos) << "\n";
  164. renderer.camera.position = cameraPos;
  165. renderer.camera.viewDirection = -glm::normalize(cameraPos);
  166. }
  167. float speed = 40;
  168. glm::vec3 dir = {};
  169. if (GetAsyncKeyState('W'))
  170. {
  171. dir.z -= speed * deltaTime;
  172. }
  173. if (GetAsyncKeyState('S'))
  174. {
  175. dir.z += speed * deltaTime;
  176. }
  177. if (GetAsyncKeyState('A'))
  178. {
  179. dir.x -= speed * deltaTime;
  180. }
  181. if (GetAsyncKeyState('D'))
  182. {
  183. dir.x += speed * deltaTime;
  184. }
  185. if (GetAsyncKeyState('Q'))
  186. {
  187. dir.y -= speed * deltaTime;
  188. }
  189. if (GetAsyncKeyState('E'))
  190. {
  191. dir.y += speed * deltaTime;
  192. }
  193. renderer.camera.moveFPS(dir);
  194. {
  195. static glm::dvec2 lastMousePos = {};
  196. if (glfwGetMouseButton(wind, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
  197. {
  198. glm::dvec2 currentMousePos = {};
  199. glfwGetCursorPos(wind, &currentMousePos.x, &currentMousePos.y);
  200. float speed = 0.7f;
  201. glm::vec2 delta = lastMousePos - currentMousePos;
  202. delta *= speed * deltaTime;
  203. renderer.camera.rotateCamera(delta);
  204. lastMousePos = currentMousePos;
  205. }
  206. else
  207. {
  208. glfwGetCursorPos(wind, &lastMousePos.x, &lastMousePos.y);
  209. }
  210. }
  211. renderer.camera.aspectRatio = (float)w / h;
  212. #pragma endregion
  213. //transform = renderer.getEntityTransform(entity);
  214. //transform.position.x = std::sin(clock() / 1000.f);
  215. //transform.position.z = -1.f;
  216. //renderer.setEntityTransform(entity, transform);
  217. #pragma region render and events
  218. renderer.render(deltaTime);
  219. glfwSwapBuffers(wind);
  220. glfwPollEvents();
  221. glViewport(0, 0, w, h);
  222. renderer.updateWindowMetrics(w, h);
  223. #pragma endregion
  224. }
  225. return 0;
  226. }