summaryrefslogtreecommitdiffstats
path: root/graphics/Display.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/Display.cpp')
-rw-r--r--graphics/Display.cpp267
1 files changed, 193 insertions, 74 deletions
diff --git a/graphics/Display.cpp b/graphics/Display.cpp
index 01dd903..380a8ec 100644
--- a/graphics/Display.cpp
+++ b/graphics/Display.cpp
@@ -1,103 +1,222 @@
-#include <iostream>
#include "Display.hpp"
-#include "Shader.hpp"
-
-Display *Display::instance = nullptr;
-
-Display::Display(int w, int h, std::string title, World *worldPtr) {
- if (instance != nullptr)
- throw 516;
- instance = this;
- world = worldPtr;
- //GLFW
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- window = glfwCreateWindow(w, h, title.c_str(), nullptr, nullptr);
- if (window == nullptr) {
- std::cerr << "Can't create GLFW window" << std::endl;
- glfwTerminate();
- throw 517;
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, &Display::callback_key);
- //GLEW
+#include "AssetManager.hpp"
+
+Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winTitle, World *worldPtr) : world(
+ worldPtr) {
+ sf::ContextSettings contextSetting;
+ contextSetting.majorVersion = 3;
+ contextSetting.minorVersion = 3;
+ contextSetting.attributeFlags = contextSetting.Core;
+ contextSetting.depthBits = 24;
+ window = new sf::Window(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Default, contextSetting);
+ window->setVerticalSyncEnabled(true);
+ window->setMouseCursorVisible(false);
+ sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window);
+
+ //Glew
glewExperimental = GL_TRUE;
- GLenum glewStatus = glewInit();
- if (glewStatus != GLEW_OK) {
- std::cerr << "Can't initialize GLEW: " << glewGetErrorString(glewStatus) << std::endl;
- throw 518;
+ if (glewInit() != GLEW_OK) {
+ std::cout << "Failed to initialize GLEW" << std::endl;
+ throw 3;
}
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
-}
-
-Display::~Display() {
- instance = nullptr;
- glfwTerminate();
+ glViewport(0, 0, width(), height());
+ glEnable(GL_DEPTH_TEST);
}
bool Display::IsClosed() {
- return false;
+ return !window->isOpen();
}
-void Display::SetPlayerPos(float x, float y) {
+void Display::SetPlayerPos(double playerX, double playerY, double playerZ) {
+ const int ChunkDistance = 2;
+ PositionI playerPos((int) playerX, (int) playerZ, (int) playerY);
+ for (auto &it:world->m_sections) {
+ PositionI delta = it.first-playerPos;
+ }
}
void Display::MainLoop() {
- Shader vertexShader("./graphics/simpleVS.vs");
- Shader fragmentShader("./graphics/simpleFS.fs", false);
- ShaderProgram program;
- program.Attach(vertexShader);
- program.Attach(fragmentShader);
- program.Link();
+ Shader shader("./shaders/simple.vs", "./shaders/simple.fs");
+ Texture &texture1 = *(AssetManager::GetAsset("minecraft/textures/blocks/brick").data.texture);
+ Texture &texture2 = *(AssetManager::GetAsset("minecraft/textures/blocks/beacon").data.texture);
GLfloat vertices[] = {
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
+ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
+
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
+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exAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
- glEnableVertexAttribArray(0);
-
- GLuint VAO;
+ glGenBuffers(1, &EBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
- // 2. Копируем наш массив вершин в буфер для OpenGL
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // 3. Устанавливаем указатели на вершинные атрибуты
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
- glEnableVertexAttribArray(0);
- //4. Отвязываем VAO
+ {
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *) (3 * sizeof(GLfloat)));
+ glEnableVertexAttribArray(2);
+ }
glBindVertexArray(0);
- while (!glfwWindowShouldClose(window)) {
- glfwPollEvents();
-
+ shader.Use();
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, texture1.texture);
+ glUniform1i(glGetUniformLocation(shader.Program, "texture1"), 0);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, texture2.texture);
+ glUniform1i(glGetUniformLocation(shader.Program, "texture2"), 1);
+
+ glm::vec3 cubePositions[] = {
+ glm::vec3(0.0f, 0.0f, 0.0f),
+ glm::vec3(2.0f, 5.0f, -15.0f),
+ glm::vec3(-1.5f, -2.2f, -2.5f),
+ glm::vec3(-3.8f, -2.0f, -12.3f),
+ glm::vec3(2.4f, -0.4f, -3.5f),
+ glm::vec3(-1.7f, 3.0f, -7.5f),
+ glm::vec3(1.3f, -2.0f, -2.5f),
+ glm::vec3(1.5f, 2.0f, -2.5f),
+ glm::vec3(1.5f, 0.2f, -1.5f),
+ glm::vec3(-1.3f, 1.0f, -1.5f)
+ };
+ Camera3D camera;
+ bool captureMouse = true;
+
+ bool isRunning = true;
+ while (isRunning) {
+ static sf::Clock clock, clock1;
+ float deltaTime = clock.getElapsedTime().asSeconds();
+ float absTime = clock1.getElapsedTime().asSeconds();
+ clock.restart();
+ sf::Event event;
+ while (window->pollEvent(event)) {
+ switch (event.type) {
+ case sf::Event::Closed:
+ window->close();
+ isRunning = false;
+ break;
+ case sf::Event::Resized:
+ glViewport(0, 0, width(), height());
+ break;
+ case sf::Event::KeyPressed:
+ switch (event.key.code) {
+ case sf::Keyboard::Escape:
+ isRunning = false;
+ break;
+ case sf::Keyboard::T:
+ captureMouse = !captureMouse;
+ window->setMouseCursorVisible(!captureMouse);
+ sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2),
+ *window);
+ break;
+ case sf::Keyboard::R:
+ shader.Reload();
+ break;
+ default:
+ break;
+ }
+ case sf::Event::MouseWheelScrolled:
+ camera.ProcessMouseScroll(event.mouseWheelScroll.delta);
+ break;
+ default:
+ break;
+ }
+ }
+ if (captureMouse) {
+ sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
+ sf::Vector2i center = sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2);
+ sf::Mouse::setPosition(center, *window);
+ int deltaX = (mousePos - center).x, deltaY = (center - mousePos).y;
+ camera.ProcessMouseMovement(deltaX, deltaY);
+ }
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
+ camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
+ camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
+ camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
+ camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime);
+
+
+ //Render code
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ shader.Use();
+
+ GLint modelLoc = glGetUniformLocation(shader.Program, "model");
+ GLint projectionLoc = glGetUniformLocation(shader.Program, "projection");
+ GLint viewLoc = glGetUniformLocation(shader.Program, "view");
+ glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f);
+ glm::mat4 view = camera.GetViewMatrix();
+ glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
+ glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- glUseProgram((GLuint) program);
glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 3);
+ for (GLuint i = 0; i < 10; i++) {
+ glm::mat4 model;
+ model = glm::translate(model, cubePositions[i]);
+
+ GLfloat angle = 20.0f * (i);
+ model = glm::rotate(model, glm::radians(angle * absTime), glm::vec3(1.0f, 0.3f, 0.5f));
+ glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
+
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+ }
glBindVertexArray(0);
- glfwSwapBuffers(window);
+ //End of render code
+
+ window->display();
}
-}
-void Display::callback_key(GLFWwindow *window, int key, int scancode, int action, int mode) {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(instance->window, GL_TRUE);
}