Advertisement
Guest User

Background.cpp

a guest
Jul 2nd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <experimental/filesystem>
  2. namespace fs = std::experimental::filesystem;
  3.  
  4. #include <stb_image.h>
  5. #include <glad.h>
  6.  
  7. #include "Background.hpp"
  8. #include "Shader.hpp"
  9.  
  10. Background::Background() noexcept :
  11.     shader(new Shader("image"))
  12. {
  13.     shader->use();
  14.     shader->pass("opacity", 1.0f);
  15.     shader->disuse();
  16.  
  17.     glGenTextures(1, &texture);
  18.     glBindTexture(GL_TEXTURE_2D, texture);
  19.  
  20.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  21.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  22.  
  23.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  24.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  25.  
  26.     {
  27.         int width, height;
  28.         fs::path const path = fs::current_path() / "gamedata" / "images" / "background.jpg";
  29.         unsigned char *const texture = stbi_load(path.u8string().c_str(), &width, &height, nullptr, STBI_rgb);
  30.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
  31.         stbi_image_free(texture);
  32.     }
  33.  
  34.     glBindTexture(GL_TEXTURE_2D, NULL);
  35.  
  36.     glGenVertexArrays(1, &VAO);
  37.     glBindVertexArray(VAO);
  38.  
  39.     int4u VBO;
  40.     glGenBuffers(1, &VBO);
  41.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  42.  
  43.     {
  44.         float const vertices[30] {
  45.             -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
  46.             -1.0f,  1.0f, 0.0f, 0.0f, 1.0f,
  47.              1.0f,  1.0f, 0.0f, 1.0f, 1.0f,
  48.             -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
  49.              1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
  50.              1.0f,  1.0f, 0.0f, 1.0f, 1.0f
  51.         };
  52.  
  53.         glBufferData(GL_ARRAY_BUFFER, 30 * sizeof(float), vertices, GL_STATIC_DRAW);
  54.     }
  55.  
  56.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
  57.     glEnableVertexAttribArray(0);
  58.  
  59.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast <void*> (3 * sizeof(float)));
  60.     glEnableVertexAttribArray(1);
  61.  
  62.     glBindBuffer(GL_ARRAY_BUFFER, NULL);
  63.     glBindVertexArray(NULL);
  64.    
  65.     glDeleteBuffers(1, &VBO);
  66. }
  67.  
  68. void Background::render() const noexcept {
  69.     shader->use();
  70.  
  71.     glBindVertexArray(VAO);
  72.     glActiveTexture(GL_TEXTURE0);
  73.     glBindTexture(GL_TEXTURE_2D, texture);
  74.     glDrawArrays(GL_TRIANGLES, 0, 6);
  75.     glBindTexture(GL_TEXTURE_2D, NULL);
  76.     glBindVertexArray(NULL);
  77.  
  78.     shader->disuse();
  79. }
  80.  
  81. Background::~Background() {
  82.     glDeleteVertexArrays(1, &VAO);
  83.     glDeleteTextures(1, &texture);
  84.  
  85.     delete shader;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement