Advertisement
czaffik

sdl_opengl_simple

Jan 14th, 2018
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #include <gl/glew.h>
  3. #include <gl/gl.h>
  4. #include <SDL2/SDL.h>
  5. #include <SDL2/SDL_image.h>
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <fstream>
  12.  
  13. std::string loadShaderFile(std::string fileName)
  14. {
  15.     std::fstream file;
  16.     file.open(fileName.c_str(), std::ios::in);
  17.  
  18.     std::stringstream buffer;
  19.     buffer << file.rdbuf();
  20.     std::string data(buffer.str());
  21.  
  22.     return data;
  23. }
  24.  
  25. int main(int argc, char*args[])
  26. {
  27. // zmienne:
  28.     SDL_Window* window = nullptr;
  29.     SDL_GLContext glContext;
  30.     SDL_Event event;
  31.     GLuint vao, vbo, ebo;
  32.     GLuint texture;
  33.     GLuint shaderID;
  34.  
  35. // tworzenie okna, inicjalizacja SDL:
  36.     if (SDL_Init( SDL_INIT_EVERYTHING ) < 0) return -1;
  37.  
  38.     if (!SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ))
  39.         std::cout << "Engine::init: Filtrowanie liniowe nie zostalo wlaczone\n";
  40.  
  41.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  42.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
  43.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  44.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  45.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  46.     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  47.     SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
  48.  
  49.     window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600,
  50.                               SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
  51.     if (window == nullptr) return -1;
  52.  
  53.     glContext = SDL_GL_CreateContext(window);
  54.  
  55.     int imgFlags = IMG_INIT_PNG;
  56.     if (!(IMG_Init(imgFlags) & imgFlags)) return -1;
  57.  
  58. // ustawienia opengl:
  59.     glEnable(GL_DEPTH_TEST);
  60.     //glEnable(GL_FRAMEBUFFER_SRGB);
  61.     glEnable(GL_CULL_FACE);
  62.     glCullFace(GL_FRONT);
  63.     glFrontFace(GL_CW);
  64.  
  65.     glewExperimental = GL_TRUE;
  66.     glewInit();
  67.  
  68. // wczytywanie shadera:
  69.     shaderID = glCreateProgram();
  70.  
  71.     GLuint s = glCreateShader(GL_VERTEX_SHADER);
  72.     const GLchar *source = loadShaderFile("frag.glsl").c_str();
  73.     glShaderSource(s, 1, &source, nullptr);
  74.     glCompileShader(s);
  75.     glAttachShader(shaderID, s);
  76.  
  77.     s = glCreateShader(GL_FRAGMENT_SHADER);
  78.     source = loadShaderFile("vert.glsl").c_str();
  79.     glShaderSource(s, 1, &source, nullptr);
  80.     glCompileShader(s);
  81.     glAttachShader(shaderID, s);
  82.  
  83.     GLint vShaderCompiled = GL_TRUE;
  84.     glGetShaderiv(s, GL_COMPILE_STATUS, &vShaderCompiled);
  85.  
  86.     if (vShaderCompiled != GL_TRUE)
  87.     {
  88.         GLint length, len;
  89.         glGetShaderiv(s, GL_INFO_LOG_LENGTH, &length);
  90.         GLchar *info = new GLchar[length];
  91.         glGetShaderInfoLog(s, length, &len, info);
  92.  
  93.         std::stringstream err;
  94.         err << "\nShaderProgram::createShader: Nie mozna skompilowac shadera: " << ". \nBlad: " << info;
  95.         std::cout << err.str() << std::endl;
  96.     }
  97.  
  98.     glLinkProgram(shaderID);
  99.  
  100.     GLint programSuccess = GL_TRUE;
  101.     glGetProgramiv(shaderID, GL_LINK_STATUS, &programSuccess);
  102.     if(programSuccess != GL_TRUE)
  103.     {
  104.         std::string error;
  105.         switch (glGetError())
  106.         {
  107.             case 1280:
  108.                 error = "GL_INVALID_ENUM";
  109.                 break;
  110.             case 1281:
  111.                 error = "GL_INVALID_VALUE";
  112.                 break;
  113.             case 1282:
  114.                 error = "GL_INVALID_OPERATION";
  115.                 break;
  116.             case 1283:
  117.                 error = "GL_STACK_OVERFLOW";
  118.                 break;
  119.             case 1284:
  120.                 error = "GL_STACK_UNDERFLOW";
  121.                 break;
  122.             case 1285:
  123.                 error = "GL_OUT_OF_MEMORY";
  124.                 break;
  125.             default:
  126.                 error = "UNKNOWN";
  127.         }
  128.  
  129.         std::stringstream err;
  130.         err << "ShaderProgram::ShaderProgram: Nie mozna utworzyc programu shadera: " << "\nblad: " << error;
  131.         std::cout << err.str() << std::endl;
  132.     }
  133.  
  134. // ustawienie zmiennych shadera:
  135.     glBindFragDataLocation(shaderID, 0, "outColor");
  136.  
  137.     glm::mat4 model;
  138.     model = glm::translate(model, glm::vec3(0.0f, 0.0f, -3.0f));
  139.     model = glm::rotate(model, glm::radians(55.0f), glm::vec3(1.0, 0.0, 0.0));
  140.  
  141.     glm::mat4 projection;
  142.     projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 1000.0f);
  143.  
  144.     glUseProgram(shaderID);
  145.     glUniform1i(glGetUniformLocation(shaderID, "u_textureSampler"), 0);
  146.     glUniformMatrix4fv(glGetUniformLocation(shaderID, "u_model"), 1, GL_FALSE, glm::value_ptr(model));
  147.     glUniformMatrix4fv(glGetUniformLocation(shaderID, "u_projection"), 1, GL_FALSE, glm::value_ptr(projection));
  148.  
  149. // tworzenie obiektów do rysowania:
  150.     glGenVertexArrays(1, &vao);
  151.     glGenBuffers(1, &vbo);
  152.     glGenBuffers(1, &ebo);
  153.     glGenTextures(1, &texture);
  154.  
  155.     GLfloat vertices[] =
  156.     {
  157.         -1.0f,  1.0f, -0.5f,    0.0f, 1.0f,
  158.          1.0f,  1.0f, -0.5f,    1.0f, 1.0f,
  159.          1.0f, -1.0f, -0.5f,    1.0f, 0.0f,
  160.         -1.0f, -1.0f, -0.5f,    0.0f, 0.0f
  161.     };
  162.     GLuint identices[] =
  163.     {
  164.         1, 3, 2,
  165.         1, 0, 3
  166.     };
  167.  
  168.     glBindVertexArray(vao);
  169.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  170.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  171.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  172.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(identices), identices, GL_STATIC_DRAW);
  173.  
  174.     glEnableVertexAttribArray(0);
  175.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
  176.     glEnableVertexAttribArray(1);
  177.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
  178.  
  179.     SDL_Surface *img;
  180.     glBindTexture(GL_TEXTURE_2D, texture);
  181.     img = IMG_Load("texture.png");
  182.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
  183.     SDL_FreeSurface(img);
  184.     glGenerateMipmap(GL_TEXTURE_2D);
  185.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  186.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  187.  
  188. // glowna petla:
  189.     while (true)
  190.     {
  191.         while (SDL_PollEvent(&event))
  192.         {
  193.             if (event.type == SDL_QUIT)
  194.             {
  195.                 SDL_Quit();
  196.                 exit(0);
  197.             }
  198.         }
  199.  
  200.         glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  201.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  202.  
  203.         glBindTexture(GL_TEXTURE_2D, texture);
  204.         //glBindVertexArray(vao);
  205.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  206.  
  207.         SDL_GL_SwapWindow(window);
  208.     }
  209.  
  210. // koniec:
  211.     glDeleteBuffers(1, &ebo);
  212.     glDeleteBuffers(1, &vbo);
  213.     glDeleteVertexArrays(1, &vao);
  214.     glDeleteTextures(1, &texture);
  215.  
  216.     SDL_GL_DeleteContext(glContext);
  217.     SDL_DestroyWindow(window);
  218.     IMG_Quit();
  219.     SDL_Quit();
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement