#include #include #include "Game.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" // static GLuint texture3d; static List list; static float tData[16][16][16]; static Array, 4>, 256> table; static List points; static int known[15] = {0b00000000, 0b10000000, 0b11000000, 0b10000100, 0b01110000, 0b11110000, 0b01111000, 0b10100101, 0b10110001, 0b01110001, 0b10000010, 0b11000010, 0b01001010, 0b10101010, 0b10110010}; static Array, 4>, 15> knownTable; static void addCube(int x, int y, int z) { bool b1 = tData[x][y][z] < 0.5f; bool b2 = tData[x + 1][y][z] < 0.5f; bool b3 = tData[x + 1][y][z + 1] < 0.5f; bool b4 = tData[x][y][z + 1] < 0.5f; bool b5 = tData[x][y + 1][z] < 0.5f; bool b6 = tData[x + 1][y + 1][z] < 0.5f; bool b7 = tData[x + 1][y + 1][z + 1] < 0.5f; bool b8 = tData[x][y + 1][z + 1] < 0.5f; int index = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | (b5 << 3) | (b6 << 2) | (b7 << 1) | b8; Vector3 v(static_cast(x), static_cast(y), static_cast(z)); for(int i = 0; i < table[index].getLength(); i++) { list.add(table[index][i][0] + v); list.add(table[index][i][1] + v); list.add(table[index][i][2] + v); } // if(b1) { // list.add(Vector3(x - 0.1f, y + 0.0f, z + 0.0f)); // list.add(Vector3(x + 0.0f, y - 0.1f, z + 0.0f)); // list.add(Vector3(x + 0.0f, y + 0.0f, z - 0.1f)); //} } static void addKnownTable(int index, const Vector3& a, const Vector3& b, const Vector3& c) { Array array; array[0] = a; array[1] = b; array[2] = c; knownTable[index].add(array); } static int swapBits(int index, int a, int b) { int bitA = (index & (1 << a)) != 0; int bitB = (index & (1 << b)) != 0; index &= ~((1 << a) | (1 << b)); index |= bitA << b; index |= bitB << a; return index; } static int flipYZ(int index) { index = swapBits(index, 0, 1); index = swapBits(index, 3, 2); index = swapBits(index, 4, 5); index = swapBits(index, 7, 6); return index; } static int flipXY(int index) { index = swapBits(index, 0, 3); index = swapBits(index, 1, 2); index = swapBits(index, 4, 7); index = swapBits(index, 5, 6); return index; } static int rotateY(int index) { index = swapBits(index, 0, 1); index = swapBits(index, 0, 3); index = swapBits(index, 3, 2); index = swapBits(index, 4, 5); index = swapBits(index, 4, 7); index = swapBits(index, 7, 6); return index; } static int rotateZ(int index) { index = swapBits(index, 0, 1); index = swapBits(index, 0, 4); index = swapBits(index, 4, 5); index = swapBits(index, 3, 2); index = swapBits(index, 3, 7); index = swapBits(index, 7, 6); return index; } static int rotateX(int index) { index = swapBits(index, 2, 1); index = swapBits(index, 1, 5); index = swapBits(index, 5, 6); index = swapBits(index, 3, 0); index = swapBits(index, 0, 4); index = swapBits(index, 4, 7); return index; } static void flipYZ(List, 4>& perm) { Matrix m; m.translateX(-0.5f); m.scale(Vector3(-1.0f, 1.0f, 1.0f)); m.translateX(0.5f); for(int i = 0; i < perm.getLength(); i++) { perm[i][0] = m * perm[i][0]; perm[i][1] = m * perm[i][1]; perm[i][2] = m * perm[i][2]; } } static void flipXY(List, 4>& perm) { Matrix m; m.translateZ(-0.5f); m.scale(Vector3(1.0f, 1.0f, -1.0f)); m.translateZ(0.5f); for(int i = 0; i < perm.getLength(); i++) { perm[i][0] = m * perm[i][0]; perm[i][1] = m * perm[i][1]; perm[i][2] = m * perm[i][2]; } } static void applyMatrix(List, 4>& perm, Matrix& m) { for(int i = 0; i < perm.getLength(); i++) { perm[i][0] = m * perm[i][0]; perm[i][1] = m * perm[i][1]; perm[i][2] = m * perm[i][2]; } } static bool check(int index) { Matrix m; for(int i = 0; i < 15; i++) { List, 4> perm = knownTable[i]; int normal = index; for(int h = 0; h < 4; h++) { for(int w = 0; w < 4; w++) { for(int k = 0; k < 4; k++) { for(int l = 0; l < 2; l++) { /*if(known[i] == flipXY(flipYZ((normal)))) { flipYZ(perm); flipXY(perm); table[index] = perm; return false; } else*/ if(known[i] == normal) { Matrix m2; m2.translate(Vector3(-0.5f, -0.5f, -0.5f)); m *= m2; m.translate(Vector3(0.5f, 0.5f, 0.5f)); applyMatrix(perm, m); table[index] = perm; return false; } normal = ((~normal) & 0xFF); } normal = rotateY(normal); Matrix m2; m2.rotateY(-90.0f); m *= m2; } normal = rotateZ(normal); Matrix m2; m2.rotateZ(90.0f); m *= m2; } normal = rotateX(normal); Matrix m2; m2.rotateX(90.0f); m *= m2; } } return true; } static void generateTable() { addKnownTable(1, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 0.0f, 0.5f)); addKnownTable(2, Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f)); addKnownTable(2, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(3, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 0.0f, 0.5f)); addKnownTable(3, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(4, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(4, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(4, Vector3(0.0f, 0.5f, 1.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(1.0f, 0.5f, 1.0f)); addKnownTable(5, Vector3(0.0f, 0.5f, 0.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(0.0f, 0.5f, 1.0f)); addKnownTable(5, Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(0.0f, 0.5f, 1.0f)); addKnownTable(6, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(6, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(6, Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.5f, 1.0f)); addKnownTable(6, Vector3(0.5f, 1.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(7, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 0.0f, 0.5f)); addKnownTable(7, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(7, Vector3(0.5f, 1.0f, 1.0f), Vector3(0.0f, 0.5f, 1.0f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(7, Vector3(0.5f, 1.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(8, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 1.0f, 0.5f), Vector3(0.5f, 1.0f, 1.0f)); addKnownTable(8, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.5f, 0.0f, 0.0f), Vector3(0.5f, 1.0f, 1.0f)); addKnownTable(8, Vector3(1.0f, 0.5f, 1.0f), Vector3(0.5f, 0.0f, 0.0f), Vector3(0.5f, 1.0f, 1.0f)); addKnownTable(8, Vector3(1.0f, 0.5f, 1.0f), Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.5f)); addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(9, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(9, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(10, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 0.0f, 0.5f)); addKnownTable(10, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(11, Vector3(0.0f, 0.0f, 0.5f), Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f)); addKnownTable(11, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.0f, 0.5f, 0.0f), Vector3(1.0f, 0.5f, 0.0f)); addKnownTable(11, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(12, Vector3(0.5f, 1.0f, 1.0f), Vector3(1.0f, 0.5f, 1.0f), Vector3(1.0f, 1.0f, 0.5f)); addKnownTable(12, Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.5f, 0.0f), Vector3(1.0f, 0.0f, 0.5f)); addKnownTable(12, Vector3(0.5f, 1.0f, 0.0f), Vector3(0.0f, 0.5f, 0.0f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(13, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 0.5f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(13, Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.5f), Vector3(0.0f, 1.0f, 0.5f)); addKnownTable(13, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.5f, 0.0f, 1.0f), Vector3(0.5f, 1.0f, 1.0f)); addKnownTable(13, Vector3(1.0f, 0.0f, 0.5f), Vector3(0.5f, 1.0f, 1.0f), Vector3(0.5f, 1.0f, 1.0f)); addKnownTable(14, Vector3(0.0f, 0.5f, 0.0f), Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 1.0f)); addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 0.0f, 0.0f), Vector3(0.0f, 0.5f, 1.0f)); addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.5f)); addKnownTable(14, Vector3(1.0f, 1.0f, 0.5f), Vector3(0.5f, 1.0f, 1.0f), Vector3(0.0f, 0.5f, 1.0f)); int counter = 0; for(int i = 0; i < 256; i++) { counter += check(i); } std::cout << "Unsupported: " << counter << "\n"; } 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(); vertexBuffer.setAttributes(Attributes().addFloat(3)); generateTable(); /*Matrix m; m.translate(Vector3(-0.5f, -0.5f, -0.5f)); m.rotateX(-90); m.translate(Vector3(0.5f, 0.5f, 0.5f)); Vector3 v(0.5f, 0.0f, 0.0f); v = m * v; std::cout << v[0] << " " << v[1] << " " << v[2] << "\n"; v = m * v; std::cout << v[0] << " " << v[1] << " " << v[2] << "\n"; v = m * v; std::cout << v[0] << " " << v[1] << " " << v[2] << "\n"; v = m * v; std::cout << v[0] << " " << v[1] << " " << v[2] << "\n";*/ Random r(0); for(int x = 0; x < 16; x++) { for(int y = 0; y < 16; y++) { for(int z = 0; z < 16; z++) { if(x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) { tData[x][y][z] = 0.0f; } else { // tData[x][y][z] = 1.0f; 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[x][y][z] = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f); } } } } for(int x = 0; x < 15; x++) { for(int y = 0; y < 15; y++) { for(int z = 0; z < 15; z++) { addCube(x, y, z); } } } vertexBuffer.setStaticData(sizeof(Vector3) * list.getLength(), list.begin()); /*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_REPEAT); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 1, 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); // m.rotateY(-30.0f); // m.scale(Vector3(0.05f, 0.05f, 1.0f)); shader.setMatrix("view", m.getValues()); vertexBuffer.draw(list.getLength()); } 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); } }