123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #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/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(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")),
- toggle(buttons.add(GLFW_KEY_T, "toggle")), oldHeight(0.0f), height(0.0f) {
- rectangleBuffer.setAttributes(Attributes().addFloat(2));
- float data[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(data), data);
- noiceBuffer.bindTextureTo(0);
- }
- void Game::render(float lag) {
- GL::setViewport(64, 64);
- noiceShader.use();
- noiceBuffer.bindAndClear();
- float interHeight = Utils::interpolate(oldHeight, height, lag);
- noiceShader.setFloat("height", interHeight * 0.005f);
- for(int i = 0; i < 64; i++) {
- noiceShader.setFloat("layer", i * (1.0f / 63.0f));
- noiceBuffer.bindLayer(i);
- rectangleBuffer.draw(6);
- }
- GL::setViewport(size.width, size.height);
- shader.use();
- GL::bindMainFramebuffer();
- GL::clearFramebuffer();
- Vector3 interPos = Utils::interpolate(oldPosition, position, lag);
- shader.setMatrix("proj", frustum.updateProjection().getValues());
- Matrix m;
- m.translate(interPos);
- m.translateZ(-32.0f);
- m.translateY(-32.0f);
- m.translateX(-32.0f);
- shader.setMatrix("view", m.getValues());
- if(toggle.isDown()) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }
- noiceBuffer.bindTextureTo(0);
- emptyBuffer.drawPoints(0);
- glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- void Game::tick() {
- oldHeight = height;
- oldPosition = position;
- const float speed = 2.5f;
- if(up.isDown()) {
- height += speed;
- }
- if(down.isDown()) {
- height -= speed;
- }
- 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);
- }
- }
|