#include #include #include "Game.h" #include "MarchingCubes.h" #include "gaming-core/utils/Array.h" #include "gaming-core/utils/List.h" #include "gaming-core/utils/Random.h" #include "gaming-core/utils/Utils.h" #include "gaming-core/wrapper/GL.h" Game::Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer, Buttons& buttons, const Size& size) : shader(shader), noiceShader(noiceShader), noiceBuffer(buffer), buttons(buttons), size(size), frustum(60, 0.1f, 1000.0f, size), up(GLFW_KEY_SPACE, "Up"), down(GLFW_KEY_LEFT_SHIFT, "Down"), left(GLFW_KEY_A, "left"), right(GLFW_KEY_D, "right"), front(GLFW_KEY_W, "front"), back(GLFW_KEY_S, "back"), toggle(GLFW_KEY_T, "toggle"), oldHeight(0.0f), height(0.0f) { buttons.add(up); buttons.add(down); buttons.add(left); buttons.add(right); buttons.add(front); buttons.add(back); buttons.add(toggle); rectangleBuffer.setAttributes(Attributes().addFloat(2)); float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0}, {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}}; rectangleBuffer.setStaticData(sizeof(recData), recData); noiceBuffer.bindTextureTo(0); Random r; const int textureSize = 64; float tData[textureSize][textureSize][textureSize][3]; for(int x = 0; x < textureSize; x++) { for(int y = 0; y < textureSize; y++) { for(int z = 0; z < textureSize; z++) { tData[x][y][z][0] = r.nextFloat(); } } } for(int k = 1; k < 3; k++) { int factor = (k * 2 + 1) * (k * 2 + 1) * (k * 2 + 1); for(int x = 0; x < textureSize; x++) { for(int y = 0; y < textureSize; y++) { for(int z = 0; z < textureSize; z++) { float sum = 0.0f; for(int mx = -k; mx <= k; mx++) { for(int my = -k; my <= k; my++) { for(int mz = -k; mz <= k; mz++) { int rx = std::abs(x + mx); int ry = std::abs(y + my); int rz = std::abs(z + mz); rx = rx >= 64 ? 127 - rx : rx; ry = ry >= 64 ? 127 - ry : ry; rz = rz >= 64 ? 127 - rz : rz; sum += tData[rx][ry][rz][0]; } } } tData[x][y][z][0] = sum / factor; } } } } for(int x = 0; x < textureSize; x++) { for(int y = 0; y < textureSize; y++) { for(int z = 0; z < textureSize; z++) { float v = tData[x][y][z][0]; float vvv = v * v * v; v = 6.0f * vvv * v * v - 15.0f * vvv * v + 10.0f * vvv; tData[x][y][z][0] = v; tData[x][y][z][1] = v; tData[x][y][z][2] = v; } } } texture.setData(textureSize, textureSize, textureSize, tData); } void Game::render(float lag) { GL::setViewport(64, 64); noiceShader.use(); noiceBuffer.bindAndClear(); float step = 1.0f / 31.5f; noiceShader.setFloat("height", oldHeight * step); for(int i = 0; i < 64; i++) { noiceShader.setFloat("layer", i * step - 1.0f); noiceBuffer.bindLayer(i); rectangleBuffer.draw(6); } GL::setViewport(size.width, size.height); shader.use(); GL::bindMainFramebuffer(); GL::clear(); shader.setMatrix("proj", frustum.updateProjection().getValues()); Matrix m; m.translate(Utils::interpolate(oldPosition, position, lag)); m.translateZ(-32.0f); m.translateY(-32.0f + (oldHeight - height) * lag); m.translateX(-32.0f); shader.setMatrix("view", m.getValues()); shader.setFloat("height", oldHeight * step * 0.5f); if(toggle.isDown()) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } noiceBuffer.bindTextureTo(0); texture.bindTo(1); emptyBuffer.drawPoints(64 * 64 * 64); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } void Game::tick() { oldHeight = height; oldPosition = position; if(up.isDown()) { height += 1.0f; } if(down.isDown()) { height -= 1.0f; } const float speed = 2.5f; if(left.isDown()) { position += Vector3(speed, 0.0f, 0.0f); } if(right.isDown()) { position -= Vector3(speed, 0.0f, 0.0f); } if(front.isDown()) { position += Vector3(0.0f, 0.0f, speed); } if(back.isDown()) { position -= Vector3(0.0f, 0.0f, speed); } } bool Game::isRunning() const { return true; }