#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" static GLuint texture3d; static float tData[16][16][16]; Game::Game(Shader& shader, Buttons& buttons, const Size& size) : shader(shader), buttons(buttons), size(size), frustum(60, 0.1f, 1000.0f, size), up(buttons.add(GLFW_KEY_SPACE, "Up")), down(buttons.add(GLFW_KEY_LEFT_SHIFT, "Down")), left(buttons.add(GLFW_KEY_A, "left")), right(buttons.add(GLFW_KEY_D, "right")), front(buttons.add(GLFW_KEY_W, "front")), back(buttons.add(GLFW_KEY_S, "back")) { shader.use(); Random r(0); for(int x = 0; x < 16; x++) { for(int y = 0; y < 16; y++) { for(int z = 0; z < 16; z++) { float sinX = sinf(M_PI * x / 8.0f); float cosY = cosf(M_PI * y / 8.0f); float cosZ = cosf(M_PI * z / 8.0f); tData[z][x][y] = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f); } } } glGenTextures(1, &texture3d); glBindTexture(GL_TEXTURE_3D, texture3d); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 16, 0, GL_RED, GL_FLOAT, tData); glActiveTexture(GL_TEXTURE0); } void Game::render(float lag) { Vector3 interPos = Utils::interpolate(oldPosition, position, lag); shader.setMatrix("proj", frustum.updateProjection().getValues()); Matrix m; m.translate(interPos); m.translateZ(-30.0f); m.translateX(-8.0f); shader.setMatrix("view", m.getValues()); vertexBuffer.drawPoints(0); glDrawArrays(GL_POINTS, 0, 16 * 16 * 16); } void Game::tick() { oldPosition = position; if(up.isDown()) { position -= Vector3(0.0f, 0.5f, 0.0f); } if(down.isDown()) { position += Vector3(0.0f, 0.5f, 0.0f); } if(left.isDown()) { position += Vector3(0.5f, 0.0f, 0.0f); } if(right.isDown()) { position -= Vector3(0.5f, 0.0f, 0.0f); } if(front.isDown()) { position += Vector3(0.0f, 0.0f, 0.5f); } if(back.isDown()) { position -= Vector3(0.0f, 0.0f, 0.5f); } }