Advertisement
SomeGenericUsername

Untitled

Jul 20th, 2019
223
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/glew.h>
  2. #include <GL/gl.h>
  3. #include <SFML/Graphics.hpp>
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8.  
  9. GLuint createShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath);
  10.  
  11.  
  12. int main() {
  13.     sf::ContextSettings context{ 24, 8, 4, 3, 3 };
  14.  
  15.     sf::Window window{ sf::VideoMode{ 1024, 768 }, "title", sf::Style::Default, context };
  16.     window.setVerticalSyncEnabled(true);
  17.     window.setActive(true);
  18.  
  19.     GLenum err;
  20.  
  21.     glewExperimental = GL_TRUE;
  22.     if ((err = glewInit()) != GLEW_OK) {
  23.         std::cout << glewGetErrorString(err) << std::endl;
  24.     }
  25.  
  26.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  27.  
  28.     sf::Image image;
  29.     image.loadFromFile("my_tex.tga");
  30.     image.flipVertically();
  31.  
  32.     GLuint texture = 0;
  33.     glGenTextures(1, &texture);
  34.     glBindTexture(GL_TEXTURE_2D, texture);
  35.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  36.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  37.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  38.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  39.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
  40.     glBindTexture(GL_TEXTURE_2D, 0);
  41.  
  42.     std::vector<float> vertexData {
  43.         -0.5f,  0.5f, 0.0f,
  44.         -0.5f, -0.5f, 0.0f,
  45.          0.5f, -0.5f, 0.0f,
  46.          0.5f,  0.5f, 0.0f
  47.     };
  48.  
  49.     std::vector<float> colorData {
  50.         1.0f, 0.0f, 0.0f,
  51.         0.0f, 1.0f, 0.0f,
  52.         0.0f, 0.0f, 1.0f,
  53.         1.0f, 1.0f, 0.0f
  54.     };
  55.  
  56.     std::vector<float> texCoords {
  57.         0.0f, 1.0f,
  58.         0.0f, 0.0f,
  59.         1.0f, 0.0f,
  60.         1.0f, 1.0f
  61.     };
  62.  
  63.     std::vector<unsigned int> iboData {
  64.         0, 1, 2,
  65.         0, 2, 3
  66.     };
  67.  
  68.     GLuint vao = 0;
  69.     glGenVertexArrays(1, &vao);
  70.     glBindVertexArray(vao);
  71.  
  72.     GLuint vbo[3];
  73.     glGenBuffers(3, vbo);
  74.     glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  75.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData[0]) * vertexData.size(), vertexData.data(), GL_STATIC_DRAW);
  76.  
  77.     glEnableVertexAttribArray(0);
  78.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  79.  
  80.     GLuint ibo = 0;
  81.     glGenBuffers(1, &ibo);
  82.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  83.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iboData[0]) * iboData.size(), iboData.data(), GL_STATIC_DRAW);;
  84.  
  85.     glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  86.     glBufferData(GL_ARRAY_BUFFER, sizeof(colorData[0]) * colorData.size(), colorData.data(), GL_STATIC_DRAW);
  87.  
  88.     glEnableVertexAttribArray(1);
  89.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  90.  
  91.     glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
  92.     glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords[0]) * texCoords.size(), texCoords.data(), GL_STATIC_DRAW);
  93.  
  94.     glEnableVertexAttribArray(2);
  95.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  96.  
  97.     glBindVertexArray(0);
  98.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  99.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  100.  
  101.     GLuint program = createShaderProgram("vert.glsl", "frag.glsl");
  102.  
  103.     GLuint timePosition = glGetUniformLocation(program, "time");
  104.  
  105.     sf::Clock clock{ };
  106.     sf::Event event{ };
  107.     while (window.isOpen()) {
  108.         while (window.pollEvent(event)) {
  109.             if (event.type == sf::Event::Closed) {
  110.                 window.close();
  111.             }
  112.         }
  113.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.  
  115.         glUseProgram(program);
  116.         glBindVertexArray(vao);
  117.         glBindTexture(GL_TEXTURE_2D, texture);
  118.         glDrawElements(GL_TRIANGLES, iboData.size(), GL_UNSIGNED_INT, nullptr);
  119.         glBindVertexArray(0);
  120.  
  121.         window.display();
  122.         glUniform1f(timePosition, clock.getElapsedTime().asSeconds());
  123.     }
  124.  
  125.     glDeleteTextures(1, &texture);
  126.     glDeleteBuffers(3, vbo);
  127.     glDeleteBuffers(1, &ibo);
  128.     glDeleteVertexArrays(1, &vao);
  129.     glDeleteProgram(program);
  130.  
  131.     return 0;
  132. }
  133.  
  134. GLuint createShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
  135.     std::stringstream ss{ };
  136.     std::string vertexShaderSource{ };
  137.     std::string fragmentShaderSource{ };
  138.     std::ifstream file{ vertexShaderPath };
  139.     if (file.is_open()) {
  140.         ss << file.rdbuf();
  141.         vertexShaderSource = ss.str();
  142.         file.close();
  143.     }
  144.  
  145.     ss.str(std::string{ });
  146.     file.open(fragmentShaderPath);
  147.     if (file.is_open()) {
  148.         ss << file.rdbuf();
  149.         fragmentShaderSource = ss.str();
  150.         file.close();
  151.     }
  152.  
  153.     GLuint program = glCreateProgram();
  154.  
  155.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  156.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  157.  
  158.     const char* cVertexSource = vertexShaderSource.c_str();
  159.     glShaderSource(vertexShader, 1, &cVertexSource, nullptr);
  160.     glCompileShader(vertexShader);
  161.  
  162.     GLint logSize = 0;
  163.     std::vector<GLchar> logText{ };
  164.     glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logSize);
  165.     if (logSize > 0) {
  166.         logText.resize(logSize);
  167.         glGetShaderInfoLog(vertexShader, logSize, &logSize, &logText[0]);
  168.         std::cout << logText.data() << std::endl;
  169.     }
  170.  
  171.     const char* cFragmentSource = fragmentShaderSource.c_str();
  172.     glShaderSource(fragmentShader, 1, &cFragmentSource, nullptr);
  173.     glCompileShader(fragmentShader);
  174.  
  175.     logSize = 0;
  176.     logText.clear();
  177.     glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logSize);
  178.     if (logSize > 0) {
  179.         logText.resize(logSize);
  180.         glGetShaderInfoLog(fragmentShader, logSize, &logSize, &logText[0]);
  181.         std::cout << logText.data() << std::endl;
  182.     }
  183.  
  184.     glAttachShader(program, vertexShader);
  185.     glAttachShader(program, fragmentShader);
  186.     glLinkProgram(program);
  187.  
  188.     logSize = 0;
  189.     logText.clear();
  190.     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
  191.     if (logSize > 0) {
  192.         logText.resize(logSize);
  193.         glGetProgramInfoLog(program, logSize, &logSize, &logText[0]);
  194.         std::cout << logText.data() << std::endl;
  195.     }
  196.  
  197.     glDetachShader(program, vertexShader);
  198.     glDetachShader(program, fragmentShader);
  199.     glDeleteShader(vertexShader);
  200.     glDeleteShader(fragmentShader);
  201.  
  202.     return program;
  203. }
Advertisement
RAW Paste Data Copied
Advertisement