Advertisement
SomeGenericUsername

Untitled

Jul 19th, 2019
278
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.     GLuint vao = 0;
  36.     glGenVertexArrays(1, &vao);
  37.     glBindVertexArray(vao);
  38.  
  39.     GLuint vbo = 0;
  40.     glGenBuffers(1, &vbo);
  41.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  42.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData[0]) * vertexData.size(), vertexData.data(), GL_STATIC_DRAW);
  43.  
  44.     glEnableVertexAttribArray(0);
  45.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  46.  
  47.     glBindVertexArray(0);
  48.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  49.  
  50.     GLuint program = createShaderProgram("vert.glsl", "frag.glsl");
  51.  
  52.     sf::Event event{ };
  53.     while (window.isOpen()) {
  54.         while (window.pollEvent(event)) {
  55.             if (event.type == sf::Event::Closed) {
  56.                 window.close();
  57.             }
  58.         }
  59.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  60.  
  61.         glUseProgram(program);
  62.         glBindVertexArray(vao);
  63.         glDrawArrays(GL_QUADS, 0, 4);
  64.         glBindVertexArray(0);
  65.  
  66.         window.display();
  67.     }
  68.  
  69.     glDeleteBuffers(1, &vbo);
  70.     glDeleteVertexArrays(1, &vao);
  71.     glDeleteProgram(program);
  72.  
  73.     return 0;
  74. }
  75.  
  76. GLuint createShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
  77.     std::stringstream ss{ };
  78.     std::string vertexShaderSource{ };
  79.     std::string fragmentShaderSource{ };
  80.     std::ifstream file{ vertexShaderPath };
  81.     if (file.is_open()) {
  82.         ss << file.rdbuf();
  83.         vertexShaderSource = ss.str();
  84.         file.close();
  85.     }
  86.  
  87.     ss.str(std::string{ });
  88.     file.open(fragmentShaderPath);
  89.     if (file.is_open()) {
  90.         ss << file.rdbuf();
  91.         fragmentShaderSource = ss.str();
  92.         file.close();
  93.     }
  94.  
  95.     GLuint program = glCreateProgram();
  96.  
  97.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  98.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  99.  
  100.     const char* cVertexSource = vertexShaderSource.c_str();
  101.     glShaderSource(vertexShader, 1, &cVertexSource, nullptr);
  102.     glCompileShader(vertexShader);
  103.  
  104.     GLint logSize = 0;
  105.     std::vector<GLchar> logText{ };
  106.     glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logSize);
  107.     if (logSize > 0) {
  108.         logText.resize(logSize);
  109.         glGetShaderInfoLog(vertexShader, logSize, &logSize, &logText[0]);
  110.         std::cout << logText.data() << std::endl;
  111.     }
  112.  
  113.     const char* cFragmentSource = fragmentShaderSource.c_str();
  114.     glShaderSource(fragmentShader, 1, &cFragmentSource, nullptr);
  115.     glCompileShader(fragmentShader);
  116.  
  117.     logSize = 0;
  118.     logText.clear();
  119.     glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logSize);
  120.     if (logSize > 0) {
  121.         logText.resize(logSize);
  122.         glGetShaderInfoLog(fragmentShader, logSize, &logSize, &logText[0]);
  123.         std::cout << logText.data() << std::endl;
  124.     }
  125.  
  126.     glAttachShader(program, vertexShader);
  127.     glAttachShader(program, fragmentShader);
  128.     glLinkProgram(program);
  129.  
  130.     logSize = 0;
  131.     logText.clear();
  132.     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
  133.     if (logSize > 0) {
  134.         logText.resize(logSize);
  135.         glGetProgramInfoLog(program, logSize, &logSize, &logText[0]);
  136.         std::cout << logText.data() << std::endl;
  137.     }
  138.  
  139.     glDetachShader(program, vertexShader);
  140.     glDetachShader(program, fragmentShader);
  141.     glDeleteShader(vertexShader);
  142.     glDeleteShader(fragmentShader);
  143.  
  144.     return program;
  145. }
Advertisement
RAW Paste Data Copied
Advertisement