123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- #include <cmath>
- #include <iostream>
- #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<Vector3, 122880> list;
- static float tData[16][16][16];
- static Array<List<Array<Vector3, 3>, 4>, 256> table;
- static List<Vector3, 50> points;
- static int known[15] = {0b00000000, 0b10000000, 0b11000000, 0b10000100,
- 0b01110000, 0b11110000, 0b01111000, 0b10100101,
- 0b10110001, 0b01110001, 0b10000010, 0b11000010,
- 0b01001010, 0b10101010, 0b10110010};
- static Array<List<Array<Vector3, 3>, 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<float>(x), static_cast<float>(y),
- static_cast<float>(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<Vector3, 3> 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<Array<Vector3, 3>, 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<Array<Vector3, 3>, 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<Array<Vector3, 3>, 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<Array<Vector3, 3>, 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);
- }
- }
|