Advertisement
SomeGenericUsername

Untitled

Jul 22nd, 2019
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.03 KB | None
  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.0f, 1.0f, 0.0f,
  44.         0.0f, 0.0f, 0.0f,
  45.         1.0f, 0.0f, 0.0f,
  46.         1.0f, 1.0f, 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.     GLint timeLocation = glGetUniformLocation(program, "time");
  104.     GLint projectionLocation = glGetUniformLocation(program, "projection");
  105.     GLint modelLocation = glGetUniformLocation(program, "model");
  106.  
  107.     glUseProgram(program);
  108.     std::vector<float> projection {
  109.         0.001953f,       0.0f,  0.0f, 0.0f,
  110.              0.0f, -0.002604f,  0.0f, 0.0f,
  111.              0.0f,       0.0f, -1.0f, 0.0f,
  112.             -1.0f,       1.0f,  0.0f, 1.0f
  113.     };
  114.     glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projection.data());
  115.  
  116.     std::vector<float> model {
  117.         200.0f,   0.0f, 0.0f, 0.0f,
  118.           0.0f, 200.0f, 0.0f, 0.0f,
  119.           0.0f,   0.0f, 1.0f, 0.0f,
  120.          50.0f,  50.0f, 0.0f, 1.0f
  121.     };
  122.     glUniformMatrix4fv(modelLocation, 1, GL_FALSE, model.data());
  123.     glUseProgram(0);
  124.  
  125.     sf::Clock clock{ };
  126.     sf::Event event{ };
  127.     while (window.isOpen()) {
  128.         while (window.pollEvent(event)) {
  129.             if (event.type == sf::Event::Closed) {
  130.                 window.close();
  131.             }
  132.         }
  133.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  134.  
  135.         glUseProgram(program);
  136.         glBindVertexArray(vao);
  137.         glBindTexture(GL_TEXTURE_2D, texture);
  138.         glDrawElements(GL_TRIANGLES, iboData.size(), GL_UNSIGNED_INT, nullptr);
  139.         glBindTexture(GL_TEXTURE_2D, 0);
  140.         glBindVertexArray(0);
  141.  
  142.         glUniform1f(timeLocation, (float) clock.getElapsedTime().asSeconds());
  143.         glUseProgram(0);
  144.  
  145.         window.display();
  146.     }
  147.  
  148.     glDeleteTextures(1, &texture);
  149.     glDeleteBuffers(3, vbo);
  150.     glDeleteBuffers(1, &ibo);
  151.     glDeleteVertexArrays(1, &vao);
  152.     glDeleteProgram(program);
  153.  
  154.     return 0;
  155. }
  156.  
  157. GLuint createShaderProgram(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
  158.     std::stringstream ss{ };
  159.     std::string vertexShaderSource{ };
  160.     std::string fragmentShaderSource{ };
  161.     std::ifstream file{ vertexShaderPath };
  162.     if (file.is_open()) {
  163.         ss << file.rdbuf();
  164.         vertexShaderSource = ss.str();
  165.         file.close();
  166.     }
  167.  
  168.     ss.str(std::string{ });
  169.     file.open(fragmentShaderPath);
  170.     if (file.is_open()) {
  171.         ss << file.rdbuf();
  172.         fragmentShaderSource = ss.str();
  173.         file.close();
  174.     }
  175.  
  176.     GLuint program = glCreateProgram();
  177.  
  178.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  179.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  180.  
  181.     const char* cVertexSource = vertexShaderSource.c_str();
  182.     glShaderSource(vertexShader, 1, &cVertexSource, nullptr);
  183.     glCompileShader(vertexShader);
  184.  
  185.     GLint logSize = 0;
  186.     std::vector<GLchar> logText{ };
  187.     glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logSize);
  188.     if (logSize > 0) {
  189.         logText.resize(logSize);
  190.         glGetShaderInfoLog(vertexShader, logSize, &logSize, logText.data());
  191.         std::cout << logText.data() << std::endl;
  192.     }
  193.  
  194.     const char* cFragmentSource = fragmentShaderSource.c_str();
  195.     glShaderSource(fragmentShader, 1, &cFragmentSource, nullptr);
  196.     glCompileShader(fragmentShader);
  197.  
  198.     logSize = 0;
  199.     logText.clear();
  200.     glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logSize);
  201.     if (logSize > 0) {
  202.         logText.resize(logSize);
  203.         glGetShaderInfoLog(fragmentShader, logSize, &logSize, &logText[0]);
  204.         std::cout << logText.data() << std::endl;
  205.     }
  206.  
  207.     glAttachShader(program, vertexShader);
  208.     glAttachShader(program, fragmentShader);
  209.     glLinkProgram(program);
  210.  
  211.     logSize = 0;
  212.     logText.clear();
  213.     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
  214.     if (logSize > 0) {
  215.         logText.resize(logSize);
  216.         glGetProgramInfoLog(program, logSize, &logSize, &logText[0]);
  217.         std::cout << logText.data() << std::endl;
  218.     }
  219.  
  220.     glDetachShader(program, vertexShader);
  221.     glDetachShader(program, fragmentShader);
  222.     glDeleteShader(vertexShader);
  223.     glDeleteShader(fragmentShader);
  224.  
  225.     return program;
  226. }
Advertisement
RAW Paste Data Copied
Advertisement