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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //shaders
  42. shaders = LoadShaders("res/shaders/voxelvert.vert.glsl", "res/shaders/voxelfrag.frag.glsl");
  43. glUseProgram(shaders);
  44. mCamLastLoadPosition = glm::vec3(0.0f, 64.0f, 0.0f);
  45. camera.setPosition(glm::vec3(0.0f, 64.0f, 0.0f));
  46. camera.setAspectRatio(linkedGame->window.getAspect());
  47. viewMatUni = glGetUniformLocation(shaders, "view");
  48. projMatUni = glGetUniformLocation(shaders, "proj");
  49. modelMatUni = glGetUniformLocation(shaders, "model");
  50. world.modelMatUni = modelMatUni;
  51. mSkyboxTask = new RenderTask(mSkybox, camera, linkedGame->window);
  52. glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
  53. glUniformMatrix4fv(projMatUni, 1, GL_FALSE, glm::value_ptr(linkedGame->window.getProjectionMatrix()));
  54. //nanovg
  55. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  56. nvgCreateFont(vg, "mono", "res/fonts/DejaVuSansMono.ttf");
  57. //sdl mouse settings
  58. SDL_SetRelativeMouseMode(SDL_TRUE);
  59. sensitivity = linkedGame->getConfig()->getValue<float>("controls.mouse.sensitivity", 5.0f);
  60. //get config
  61. Config* conf = linkedGame->getConfig();
  62. //add actions to handler
  63. handler.setAction("Move Forward", conf->getValue<std::string>("controls.bindings.movement.forward", "W" ));
  64. handler.setAction("Move Backward", conf->getValue<std::string>("controls.bindings.movement.backward", "S" ));
  65. handler.setAction("Move Left", conf->getValue<std::string>("controls.bindings.movement.left", "A" ));
  66. handler.setAction("Move Right", conf->getValue<std::string>("controls.bindings.movement.right", "D" ));
  67. handler.setAction("Move Up", conf->getValue<std::string>("controls.bindings.movement.up", "Space" ));
  68. handler.setAction("Move Down", conf->getValue<std::string>("controls.bindings.movement.down", "Left Ctrl"));
  69. handler.setAction("Place Voxel", conf->getValue<std::string>("controls.bindings.action.placevoxel", "Mouse Left"));
  70. handler.setAction("Delete Voxel", conf->getValue<std::string>("controls.bindings.action.delvoxel", "Mouse Right"));
  71. handler.setAction("Select Type 1", conf->getValue<std::string>("controls.bindings.typesel.type1", "1" ));
  72. handler.setAction("Select Type 2", conf->getValue<std::string>("controls.bindings.typesel.type2", "2" ));
  73. //set signals for handler
  74. handler.getEventSignal(SDL_QUIT ).connect<Game , &Game::stop >(linkedGame);
  75. handler.getEventSignal(SDL_MOUSEMOTION).connect<TestScene, &TestScene::look>(this);
  76. world.voxelInfo.setTextureData(1, Face3D::RIGHT, Orientation2D::UP, 0.0f);
  77. world.voxelInfo.setTextureData(1, Face3D::LEFT, Orientation2D::UP, 0.0f);
  78. world.voxelInfo.setTextureData(1, Face3D::TOP, Orientation2D::UP, 0.0f);
  79. world.voxelInfo.setTextureData(1, Face3D::BOTTOM, Orientation2D::UP, 0.0f);
  80. world.voxelInfo.setTextureData(1, Face3D::FRONT, Orientation2D::UP, 0.0f);
  81. world.voxelInfo.setTextureData(1, Face3D::BACK, Orientation2D::UP, 0.0f);
  82. world.voxelInfo.setTextureData(2, Face3D::RIGHT, Orientation2D::UP, 1.0f);
  83. world.voxelInfo.setTextureData(2, Face3D::LEFT, Orientation2D::UP, 1.0f);
  84. world.voxelInfo.setTextureData(2, Face3D::TOP, Orientation2D::UP, 2.0f);
  85. world.voxelInfo.setTextureData(2, Face3D::BOTTOM, Orientation2D::UP, 1.0f);
  86. world.voxelInfo.setTextureData(2, Face3D::FRONT, Orientation2D::UP, 1.0f);
  87. world.voxelInfo.setTextureData(2, Face3D::BACK, Orientation2D::UP, 1.0f);
  88. world.voxelInfo.setAllTextureIndexes(1, 0);
  89. world.voxelInfo.setAllTextureIndexes(2, 1);
  90. world.voxelInfo.setTextureIndex(2, FaceDirection::TOP, 2);
  91. //world.voxelInfo.setTransparent(1, false);
  92. //world.voxelInfo.setTransparent(2, false);
  93. std::cout << std::endl;
  94. world.queueChunkLoadsAroundPoint(glm::vec3(0.0,0.0,0.0), 16);
  95. //world.forceGlobalGeometryUpdate();
  96. voxelType = 1;
  97. }
  98. void TestScene::reInit() {
  99. }
  100. void TestScene::update(const float& dTime) {
  101. this->dTime = dTime;
  102. camera.update(dTime);
  103. handler.update();
  104. world.update();
  105. mFPS = 1.0f/dTime;
  106. if (handler.isActionDown("Move Forward" )) camera.moveRelative(glm::vec3 (0.0f, 0.0f, 1.0f) * dTime * 16.0f);
  107. if (handler.isActionDown("Move Backward")) camera.moveRelative(glm::vec3( 0.0f, 0.0f, -1.0f) * dTime * 16.0f);
  108. if (handler.isActionDown("Move Left" )) camera.moveRelative(glm::vec3(-1.0f, 0.0f, 0.0f) * dTime * 16.0f);
  109. if (handler.isActionDown("Move Right" )) camera.moveRelative(glm::vec3( 1.0f, 0.0f, 0.0f) * dTime * 16.0f);
  110. if (handler.isActionDown("Move Up" )) camera.move(glm::vec3( 0.0f, 1.0f, 0.0f) * dTime * 8.0f);
  111. if (handler.isActionDown("Move Down" )) camera.move(glm::vec3( 0.0f, -1.0f, 0.0f) * dTime * 8.0f);
  112. if (handler.isActionDown("Select Type 1")) voxelType = 1;
  113. if (handler.isActionDown("Select Type 2")) voxelType = 2;
  114. //place voxel testing
  115. if ((handler.isActionDown("Place Voxel") || handler.isActionDown("Delete Voxel")) && !placeVoxel) {
  116. placeVoxel = true;
  117. //perform a raycast
  118. glm::vec3 hitPos;
  119. glm::vec3 hitNormal;
  120. bool success;
  121. world.voxelMath.rayCast(hitPos, hitNormal, success, camera.getPosition(), camera.getAngleVector(), 10);
  122. if (success) {
  123. std::cout << "Hit voxel at: " << hitPos.x << ", " << hitPos.y << ", " << hitPos.z << std::endl;
  124. if (handler.isActionDown("Place Voxel")) {
  125. world.setVoxelType((int)(hitPos.x + hitNormal.x), (int)(hitPos.y + hitNormal.y), (int)(hitPos.z + hitNormal.z), voxelType, true);
  126. } else {
  127. world.setVoxelType((int)(hitPos.x), (int)(hitPos.y), (int)(hitPos.z), 0, true);
  128. }
  129. }
  130. } else if (!(handler.isActionDown("Place Voxel") || handler.isActionDown("Delete Voxel")) && placeVoxel) {
  131. placeVoxel = false;
  132. }
  133. float distance = glm::distance(camera.getPosition(), mCamLastLoadPosition);
  134. if (distance >= 16.0f) {
  135. mCamLastLoadPosition = camera.getPosition();
  136. world.queueChunkLoadsAroundPoint(camera.getPosition(), 16);
  137. }
  138. }
  139. void TestScene::draw() {
  140. mSkyboxTask->draw();
  141. //draw world TODO: move this to rendertasks
  142. gls::setShader(shaders);
  143. glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
  144. world.draw();
  145. glDisable(GL_DEPTH_TEST);
  146. int nvgWidth = 1920;
  147. int nvgHeight = 1080;
  148. nvgBeginFrame(vg, nvgWidth, nvgHeight, 1);
  149. nvgFontSize(vg, 14.0f);
  150. nvgFontFace(vg, "mono");
  151. nvgFillColor(vg, nvgRGBA(0, 0, 0, 190));
  152. std::string fpsString = std::to_string(mFPS);
  153. nvgText(vg, 5, 19, fpsString.data(), NULL);
  154. nvgBeginPath(vg);
  155. nvgRect(vg, ((float)nvgWidth / 2.0f) - 15.0f, ((float)nvgHeight / 2.0f) - 15.0f, 30.0f, 30.0f);
  156. nvgFillColor(vg, nvgRGBA(153, 0, 204, 80));
  157. nvgFill(vg);
  158. nvgEndFrame(vg);
  159. glFrontFace(GL_CW);
  160. glEnable(GL_CULL_FACE);
  161. glEnable(GL_DEPTH_TEST);
  162. }
  163. void TestScene::look() {
  164. int x, y; //mouse change
  165. SDL_GetRelativeMouseState(&x, &y);
  166. camera.rotate((float)x * sensitivity * 0.001f, (float)y * sensitivity * 0.001f);
  167. //camera.rotate((float)x, (float)y);
  168. }
  169. }