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

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