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.

testscene.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: testscene.cpp
  5. *
  6. * Description:
  7. *
  8. * Version: 1.0
  9. * Created: 03/24/2014 11:06:32 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "testscene.h"
  19. #include "loadShader.h"
  20. #include "game.h"
  21. #include "tileset.h"
  22. #include "world.h"
  23. #include "mathplus.h"
  24. #include "graphics/glstate.h"
  25. #include <GL/glew.h>
  26. #include <GL/gl.h>
  27. #define GLM_FORCE_RADIANS
  28. #include <glm/glm.hpp>
  29. #include <glm/gtc/type_ptr.hpp>
  30. #include "nanovg.h"
  31. #define NANOVG_GL3_IMPLEMENTATION
  32. #include "nanovg_gl.h"
  33. #include <iostream>
  34. namespace vtk {
  35. void TestScene::init() {
  36. Tileset tiles;
  37. tiles.buildTexture();
  38. tiles.updateTextureAt(0, "res/stone.png");
  39. tiles.updateTextureAt(1, "res/dirt.png");
  40. tiles.updateTextureAt(2, "res/grass.png");
  41. tiles.updateTextureAt(3, "res/test.png");
  42. tiles.updateTextureAt(4, "res/test-blue.png");
  43. //shaders
  44. shaders = LoadShaders("res/shaders/voxelvert.vert.glsl", "res/shaders/voxelfrag.frag.glsl");
  45. glUseProgram(shaders);
  46. mCamLastLoadPosition = glm::vec3(0.0f, 64.0f, 0.0f);
  47. camera.setPosition(glm::vec3(0.0f, 64.0f, 0.0f));
  48. camera.setAspectRatio(linkedGame->window.getAspect());
  49. viewMatUni = glGetUniformLocation(shaders, "view");
  50. projMatUni = glGetUniformLocation(shaders, "proj");
  51. modelMatUni = glGetUniformLocation(shaders, "model");
  52. world.modelMatUni = modelMatUni;
  53. mSkyboxTask = new RenderTask(mSkybox, camera, linkedGame->window);
  54. glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
  55. glUniformMatrix4fv(projMatUni, 1, GL_FALSE, glm::value_ptr(camera.getProjectionMatrix()));
  56. //nanovg
  57. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  58. nvgCreateFont(vg, "mono", "res/fonts/DejaVuSansMono.ttf");
  59. //sdl mouse settings
  60. SDL_SetRelativeMouseMode(SDL_TRUE);
  61. sensitivity = linkedGame->getConfig()->getValue<float>("controls.mouse.sensitivity", 5.0f);
  62. //get config
  63. Config* conf = linkedGame->getConfig();
  64. //add actions to handler
  65. handler.setAction("Move Forward", conf->getValue<std::string>("controls.bindings.movement.forward", "W" ));
  66. handler.setAction("Move Backward", conf->getValue<std::string>("controls.bindings.movement.backward", "S" ));
  67. handler.setAction("Move Left", conf->getValue<std::string>("controls.bindings.movement.left", "A" ));
  68. handler.setAction("Move Right", conf->getValue<std::string>("controls.bindings.movement.right", "D" ));
  69. handler.setAction("Move Up", conf->getValue<std::string>("controls.bindings.movement.up", "Space" ));
  70. handler.setAction("Move Down", conf->getValue<std::string>("controls.bindings.movement.down", "Left Ctrl"));
  71. handler.setAction("Place Voxel", conf->getValue<std::string>("controls.bindings.action.placevoxel", "Mouse Left"));
  72. handler.setAction("Delete Voxel", conf->getValue<std::string>("controls.bindings.action.delvoxel", "Mouse Right"));
  73. handler.setAction("Select Type 1", conf->getValue<std::string>("controls.bindings.typesel.type1", "1" ));
  74. handler.setAction("Select Type 2", conf->getValue<std::string>("controls.bindings.typesel.type2", "2" ));
  75. handler.setAction("Select Type 3", conf->getValue<std::string>("controls.bindings.typesel.type3", "3" ));
  76. handler.setAction("Select Type 4", conf->getValue<std::string>("controls.bindings.typesel.type4", "4" ));
  77. handler.setAction("Toggle Noclip", conf->getValue<std::string>("controls.bindings.noclip", "V"));
  78. //set signals for handler
  79. handler.getEventSignal(SDL_QUIT ).connect<Game , &Game::stop >(linkedGame);
  80. handler.getEventSignal(SDL_MOUSEMOTION).connect<TestScene, &TestScene::look>(this);
  81. world.voxelInfo.setAllTextureIndexes(1, 0);
  82. world.voxelInfo.setAllTextureIndexes(2, 1);
  83. world.voxelInfo.setTextureIndex(2, FaceDirection::TOP, 2);
  84. world.voxelInfo.setAllTextureIndexes(3, 3);
  85. world.voxelInfo.setAllTextureIndexes(4, 4);
  86. world.voxelInfo.setTransparent(0, true);
  87. world.voxelInfo.setTransparent(1, false);
  88. world.voxelInfo.setTransparent(2, false);
  89. world.voxelInfo.setTransparent(3, false);
  90. world.voxelInfo.setTransparent(4, false);
  91. world.voxelInfo.setEmission(0, 0);
  92. world.voxelInfo.setEmission(1, 0);
  93. world.voxelInfo.setEmission(2, 0);
  94. world.voxelInfo.setEmission(3, 0xF4B0);
  95. world.voxelInfo.setEmission(4, 0x00F0);
  96. std::cout << std::endl;
  97. world.queueChunkLoadsAroundPoint(glm::vec3(0.0,0.0,0.0), 20);
  98. //world.forceGlobalGeometryUpdate();
  99. voxelType = 1;
  100. mNoclip = true;
  101. mNoclipDebounce = false;
  102. }
  103. void TestScene::reInit() {
  104. }
  105. void TestScene::update(const float& dTime) {
  106. this->dTime = dTime;
  107. camera.update(dTime);
  108. handler.update();
  109. world.update();
  110. mFPS = 1.0f/dTime;
  111. if (handler.isActionDown("Move Forward" )) camera.moveRelative(glm::vec3 (0.0f, 0.0f, 1.0f) * dTime * 16.0f);
  112. if (handler.isActionDown("Move Backward")) camera.moveRelative(glm::vec3( 0.0f, 0.0f, -1.0f) * dTime * 16.0f);
  113. if (handler.isActionDown("Move Left" )) camera.moveRelative(glm::vec3(-1.0f, 0.0f, 0.0f) * dTime * 16.0f);
  114. if (handler.isActionDown("Move Right" )) camera.moveRelative(glm::vec3( 1.0f, 0.0f, 0.0f) * dTime * 16.0f);
  115. if (handler.isActionDown("Move Up" ) && mNoclip) camera.move(glm::vec3( 0.0f, 1.0f, 0.0f) * dTime * 8.0f);
  116. if (handler.isActionDown("Move Down" ) && mNoclip) camera.move(glm::vec3( 0.0f, -1.0f, 0.0f) * dTime * 8.0f);
  117. if (handler.isActionDown("Select Type 1")) voxelType = 1;
  118. if (handler.isActionDown("Select Type 2")) voxelType = 2;
  119. if (handler.isActionDown("Select Type 3")) voxelType = 3;
  120. if (handler.isActionDown("Select Type 4")) voxelType = 4;
  121. if (!mNoclipDebounce && handler.isActionDown("Toggle Noclip")) {
  122. mNoclipDebounce = true;
  123. mNoclip = !mNoclip;
  124. std::cout << mNoclip << "\n" << std::flush;
  125. } else {
  126. //mNoclipDebounce = false;
  127. }
  128. //place voxel testing
  129. if ((handler.isActionDown("Place Voxel") || handler.isActionDown("Delete Voxel")) && !placeVoxel) {
  130. placeVoxel = true;
  131. //perform a raycast
  132. glm::vec3 hitPos;
  133. glm::vec3 hitNormal;
  134. bool success;
  135. world.voxelMath.rayCast(hitPos, hitNormal, success, camera.getPosition(), camera.getAngleVector(), 10);
  136. if (success) {
  137. std::cout << "Hit voxel at: " << hitPos.x << ", " << hitPos.y << ", " << hitPos.z << std::endl;
  138. std::cout << "Height @ " << world.getHeight(glm::ivec2(hitPos.x + hitNormal.x, hitPos.z + hitNormal.z)) << std::endl;
  139. if (handler.isActionDown("Place Voxel")) {
  140. world.placeVoxel(hitPos + hitNormal, voxelType);
  141. //world.setVoxelType((int)(hitPos.x + hitNormal.x), (int)(hitPos.y + hitNormal.y), (int)(hitPos.z + hitNormal.z), voxelType, true);
  142. } else {
  143. world.breakVoxel(hitPos);
  144. //world.setVoxelType((int)(hitPos.x), (int)(hitPos.y), (int)(hitPos.z), 0, true);
  145. }
  146. }
  147. } else if (!(handler.isActionDown("Place Voxel") || handler.isActionDown("Delete Voxel")) && placeVoxel) {
  148. placeVoxel = false;
  149. }
  150. float distance = glm::distance(camera.getPosition(), mCamLastLoadPosition);
  151. if (distance >= 16.0f) {
  152. mCamLastLoadPosition = camera.getPosition();
  153. //world.queueChunkLoadsAroundPoint(camera.getPosition(), 16);
  154. }
  155. //player "physics"
  156. if (!mNoclip) {
  157. mVelocity = glm::vec3(0.0f, mVelocity.y + (-9.8f * dTime), 0.0f);
  158. // check ground collision
  159. glm::ivec3 cameraIPos = camera.getPosition();
  160. cameraIPos.y = cameraIPos.y - 2;
  161. if (world.isVoxelSolid(cameraIPos.x, cameraIPos.y, cameraIPos.z)) {
  162. std::cout << cameraIPos.y << "\n" << std::flush;
  163. auto newCameraPos = camera.getPosition();
  164. newCameraPos.y = (float)cameraIPos.y + 2.8f;
  165. camera.setPosition(newCameraPos);
  166. mVelocity.y = 0.0f;
  167. }
  168. camera.move(mVelocity * dTime);
  169. }
  170. }
  171. void TestScene::draw() {
  172. mSkyboxTask->draw();
  173. //draw world TODO: move this to rendertasks
  174. glUseProgram(shaders);
  175. glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
  176. glUniformMatrix4fv(projMatUni, 1, GL_FALSE, glm::value_ptr(camera.getProjectionMatrix()));
  177. world.draw();
  178. glDisable(GL_DEPTH_TEST);
  179. int nvgWidth = 1920;
  180. int nvgHeight = 1080;
  181. nvgBeginFrame(vg, nvgWidth, nvgHeight, 1);
  182. nvgFontSize(vg, 14.0f);
  183. nvgFontFace(vg, "mono");
  184. nvgFillColor(vg, nvgRGBA(0, 0, 0, 190));
  185. std::string fpsString = std::to_string(mFPS);
  186. nvgText(vg, 5, 19, fpsString.data(), NULL);
  187. nvgBeginPath(vg);
  188. nvgRect(vg, ((float)nvgWidth / 2.0f) - 15.0f, ((float)nvgHeight / 2.0f) - 15.0f, 30.0f, 30.0f);
  189. nvgFillColor(vg, nvgRGBA(153, 0, 204, 80));
  190. nvgFill(vg);
  191. nvgEndFrame(vg);
  192. glFrontFace(GL_CW);
  193. glEnable(GL_CULL_FACE);
  194. glEnable(GL_DEPTH_TEST);
  195. }
  196. void TestScene::look() {
  197. int x, y; //mouse change
  198. SDL_GetRelativeMouseState(&x, &y);
  199. camera.rotate((float)x * sensitivity * 0.001f, (float)y * sensitivity * 0.001f);
  200. //camera.rotate((float)x, (float)y);
  201. }
  202. }