Advertisement
SomeGenericUsername

Untitled

Jul 20th, 2019
225
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::RenderWindow 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.     std::vector<float> vertexData {
  29.         -0.5f,  0.5f, 0.0f,
  30.         -0.5f, -0.5f, 0.0f,
  31.          0.5f, -0.5f, 0.0f,
  32.          0.5f,  0.5f, 0.0f
  33.     };
  34.  
  35.     std::vector<float> colorData {
  36.         1.0f, 0.0f, 0.0f,
  37.         0.0f, 1.0f, 0.0f,
  38.         0.0f, 0.0f, 1.0f,
  39.         1.0f, 1.0f, 0.0f
  40.     };
  41.  
  42.     std::vector<unsigned int> iboData {
  43.         0, 1, 2,
  44.         0, 2, 3
  45.     };
  46.  
  47.     GLuint vao = 0;
  48.     glGenVertexArrays(1, &vao);
  49.     glBindVertexArray(vao);
  50.  
  51.     GLuint vbo[2];
  52.     glGenBuffers(2, vbo);
  53.     glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  54.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData[0]) * vertexData.size(), vertexData.data(), GL_STATIC_DRAW);
  55.  
  56.     glEnableVertexAttribArray(0);
  57.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  58.  
  59.     GLuint ibo = 0;
  60.     glGenBuffers(1, &ibo);
  61.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  62.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iboData[0]) * iboData.size(), iboData.data(), GL_STATIC_DRAW);;
  63.  
  64.     glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  65.     glBufferData(GL_ARRAY_BUFFER, sizeof(colorData[0]) * colorData.size(), colorData.data(), GL_STATIC_DRAW);
  66.  
  67.     glEnableVertexAttribArray(1);
  68.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  69.  
  70.     glBindVertexArray(0);
  71.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  72.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  73.  
  74.     GLuint program = createShaderProgram("vert.glsl", "frag.glsl");
  75.  
  76.     GLuint timePosition = glGetUniformLocation(program, "time");
  77.  
  78.     sf::Clock clock{ };
  79.     sf::Event event{ };
  80.     while (window.isOpen()) {
  81.         while (window.pollEvent(event)) {
  82.             if (event.type == sf::Event::Closed) {
  83.                 window.close();
  84.             }
  85.         }
  86.         glClear(GL_COLOR_BUFFER_BIT);
  87.  
  88.         glUseProgram(program);
  89.         glBindVertexArray(vao);
  90.         glDrawElements(GL_TRIANGLES, iboData.size(), GL_UNSIGNED_INT, nullptr);
  91.         glBindVertexArray(0);
  92.  
  93.         window.display();
  94.         glUniform1f(timePosition, clock.getElapsedTime().asSeconds());
  95.     }
  96.  
  97.     glDeleteBuffers(2, vbo);
  98.     glDeleteBuffers(1, &ibo);
  99.     glDeleteVertexArrays(1, &vao);
  100.     glDeleteProgram(program);
  101.  
  102.     return 0;
  103. }
  104.  
  105. GLuint createShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
  106.     std::stringstream ss{ };
  107.     std::string vertexShaderSource{ };
  108.     std::string fragmentShaderSource{ };
  109.     std::ifstream file{ vertexShaderPath };
  110.     if (file.is_open()) {
  111.         ss << file.rdbuf();
  112.         vertexShaderSource = ss.str();
  113.         file.close();
  114.     }
  115.  
  116.     ss.str(std::string{ });
  117.     file.open(fragmentShaderPath);
  118.     if (file.is_open()) {
  119.         ss << file.rdbuf();
  120.         fragmentShaderSource = ss.str();
  121.         file.close();
  122.     }
  123.  
  124.     GLuint program = glCreateProgram();
  125.  
  126.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  127.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  128.  
  129.     const char* cVertexSource = vertexShaderSource.c_str();
  130.     glShaderSource(vertexShader, 1, &cVertexSource, nullptr);
  131.     glCompileShader(vertexShader);
  132.  
  133.     GLint logSize = 0;
  134.     std::vector<GLchar> logText{ };
  135.     glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logSize);
  136.     if (logSize > 0) {
  137.         logText.resize(logSize);
  138.         glGetShaderInfoLog(vertexShader, logSize, &logSize, &logText[0]);
  139.         std::cout << logText.data() << std::endl;
  140.     }
  141.  
  142.     const char* cFragmentSource = fragmentShaderSource.c_str();
  143.     glShaderSource(fragmentShader, 1, &cFragmentSource, nullptr);
  144.     glCompileShader(fragmentShader);
  145.  
  146.     logSize = 0;
  147.     logText.clear();
  148.     glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logSize);
  149.     if (logSize > 0) {
  150.         logText.resize(logSize);
  151.         glGetShaderInfoLog(fragmentShader, logSize, &logSize, &logText[0]);
  152.         std::cout << logText.data() << std::endl;
  153.     }
  154.  
  155.     glAttachShader(program, vertexShader);
  156.     glAttachShader(program, fragmentShader);
  157.     glLinkProgram(program);
  158.  
  159.     logSize = 0;
  160.     logText.clear();
  161.     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
  162.     if (logSize > 0) {
  163.         logText.resize(logSize);
  164.         glGetProgramInfoLog(program, logSize, &logSize, &logText[0]);
  165.         std::cout << logText.data() << std::endl;
  166.     }
  167.  
  168.     glDetachShader(program, vertexShader);
  169.     glDetachShader(program, fragmentShader);
  170.     glDeleteShader(vertexShader);
  171.     glDeleteShader(fragmentShader);
  172.  
  173.     return program;
  174. }
Advertisement
RAW Paste Data Copied
Advertisement