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.0KB

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