123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include <cmath>
- #include <iostream>
- #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 List<Vector3, 122880> list;
- static float tData[16][16][16];
- 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 < 12; i++) {
- list.add(MarchingCubes::getVector(index, i) + v);
- }
- }
- 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));
- 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[z][x][y] = 0.1f;
- } 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[z][x][y] = (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_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());
- glPointSize(10.0f);
- // vertexBuffer.draw(list.getLength());
- GL::checkAndPrintError("test3");
- vertexBuffer.drawPoints(0);
- GL::checkAndPrintError("HERE");
- 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);
- }
- }
|