Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <experimental/filesystem>
- namespace fs = std::experimental::filesystem;
- #include <stb_image.h>
- #include <glad.h>
- #include "Background.hpp"
- #include "Shader.hpp"
- Background::Background() noexcept :
- shader(new Shader("image"))
- {
- shader->use();
- shader->pass("opacity", 1.0f);
- shader->disuse();
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- {
- int width, height;
- fs::path const path = fs::current_path() / "gamedata" / "images" / "background.jpg";
- unsigned char *const texture = stbi_load(path.u8string().c_str(), &width, &height, nullptr, STBI_rgb);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
- stbi_image_free(texture);
- }
- glBindTexture(GL_TEXTURE_2D, NULL);
- glGenVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
- int4u VBO;
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- {
- float const vertices[30] {
- -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
- -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 0.0f, 1.0f, 1.0f
- };
- glBufferData(GL_ARRAY_BUFFER, 30 * sizeof(float), vertices, GL_STATIC_DRAW);
- }
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast <void*> (3 * sizeof(float)));
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, NULL);
- glBindVertexArray(NULL);
- glDeleteBuffers(1, &VBO);
- }
- void Background::render() const noexcept {
- shader->use();
- glBindVertexArray(VAO);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glBindTexture(GL_TEXTURE_2D, NULL);
- glBindVertexArray(NULL);
- shader->disuse();
- }
- Background::~Background() {
- glDeleteVertexArrays(1, &VAO);
- glDeleteTextures(1, &texture);
- delete shader;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement