Advertisement
uraharadono

OpenGL Video 9

Mar 27th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. /*
  2.  * OPEN GL DOCUMENTATION SITE: http://docs.gl/
  3.  * PUNO PUNO PUNO DOBAR SAJT ZA OPENGL TUTORIALE: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ OVO MORAS PROVJERAVATI STALNO.
  4.  */
  5.  
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <sstream>
  13.  
  14. struct ShaderProgramSource
  15. {
  16.     std::string VertexSource;
  17.     std::string FragmentSource;
  18. };
  19.  
  20. static ShaderProgramSource ParseShader(const std::string& filepath)
  21. {
  22.     std::ifstream stream(filepath);
  23.  
  24.     enum class ShaderType
  25.     {
  26.         NONE = -1,
  27.         VERTEX = 0,
  28.         FRAGMENT = 1
  29.     };
  30.  
  31.     std::string line;
  32.     std::stringstream ss[2];
  33.     ShaderType type = ShaderType::NONE;
  34.     while (getline(stream, line))
  35.     {
  36.         if (line.find("#shader") != std::string::npos)
  37.         {
  38.             if (line.find("vertex") != std::string::npos)
  39.             {
  40.                 // set type to vertex
  41.                 type = ShaderType::VERTEX;
  42.             }
  43.             else if (line.find("fragment") != std::string::npos)
  44.             {
  45.                 // set type to fragment
  46.                 type = ShaderType::FRAGMENT;
  47.             }
  48.         }
  49.         else
  50.         {
  51.             ss[(int)type] << line << '\n';
  52.         }
  53.     }
  54.     return { ss[0].str(), ss[1].str() };
  55. }
  56.  
  57. static unsigned int CompileShader(unsigned int type, const std::string& source)
  58. {
  59.     unsigned int id = glCreateShader(type);
  60.     const char* src = source.c_str();
  61.     glShaderSource(id, 1, &src, nullptr);
  62.     glCompileShader(id);
  63.  
  64.     // Error handling
  65.     int result;
  66.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  67.     if (result == GL_FALSE)
  68.     {
  69.         int length;
  70.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  71.  
  72.         // On govori ovdje da hoce na stacku da napravi char array, nece pokazivac koji bi bio na heap-u jer ga mora kasnije pocistiti.
  73.         // Ja se ne slazem sa njim, ali da bi mi bilo lakse pratiti video-e i ja cu napraviti kao i on.
  74.         // char* message = new char[length];
  75.  
  76.         char* message = (char*)alloca(length * sizeof(char));
  77.         glGetShaderInfoLog(id, length, &length, message);
  78.         std::cout << "Failed to compile " <<
  79.             (type == GL_VERTEX_SHADER ? "vertext" : "fragment")
  80.             << " shader." << std::endl;
  81.         std::cout << message << std::endl;
  82.         glDeleteShader(id);
  83.         return 0;
  84.     }
  85.  
  86.     return id;
  87. }
  88.  
  89. static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
  90. {
  91.     unsigned int program = glCreateProgram();
  92.     unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  93.     unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  94.  
  95.     glAttachShader(program, vs);
  96.     glAttachShader(program, fs);
  97.     glLinkProgram(program);
  98.     glValidateProgram(program);
  99.  
  100.     // delete intermediates
  101.     glDeleteShader(vs);
  102.     glDeleteShader(fs);
  103.  
  104.     return program;
  105. }
  106.  
  107.  
  108. int main(void)
  109. {
  110.     GLFWwindow* window;
  111.  
  112.     /* Initialize the library */
  113.     if (!glfwInit())
  114.         return -1;
  115.  
  116.     /* Create a windowed mode window and its OpenGL context */
  117.     window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  118.     if (!window)
  119.     {
  120.         glfwTerminate();
  121.         return -1;
  122.     }
  123.  
  124.     /* Make the window's context current */
  125.     glfwMakeContextCurrent(window);
  126.  
  127.     if (glewInit() != GLEW_OK)
  128.     {
  129.         std::cout << "Error!" << std::endl;
  130.     }
  131.  
  132.     std::cout << glGetString(GL_VERSION) << std::endl;
  133.  
  134.     // Vertex buffers and layouts in open GL
  135.     /*float positions[6] = {
  136.         -0.5f, -0.5f,
  137.         0.0f, 0.5f,
  138.         0.5f, -0.5f
  139.     };*/
  140.     float positions[] = {
  141.         -0.5f, -0.5f,   // 0
  142.         0.5f, -0.5f,    // 1
  143.         0.5f, 0.5f,     // 2
  144.         -0.5f, 0.5f     // 3
  145.     };
  146.  
  147.     unsigned int indices[] = {
  148.         0, 1, 2,
  149.         2, 3, 0
  150.     };
  151.  
  152.  
  153.     // Stari array of vertexes nacin crtanja trougla
  154.     unsigned int buffer;
  155.     glGenBuffers(1, &buffer);
  156.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  157.     // Ovdje je prije bilo za 2gi parametar samo "6 * sizeof(float)", ali to je zato sto je bilo 6 tacaka koje treba nacrtati
  158.     glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), positions, GL_STATIC_DRAW);
  159.     // Vertex attributes and layouts in open GL
  160.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  161.     glEnableVertexAttribArray(0);
  162.  
  163.     // INDEX BUFFER
  164.     unsigned int ibo;
  165.     glGenBuffers(1, &ibo);
  166.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  167.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  168.  
  169.     // Creating shaders
  170.     ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
  171.     unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
  172.     glUseProgram(shader);
  173.  
  174.  
  175.     /* Loop until the user closes the window */
  176.     while (!glfwWindowShouldClose(window))
  177.     {
  178.         /* Render here */
  179.         glClear(GL_COLOR_BUFFER_BIT);
  180.  
  181.         // Legacy (Old) OpenGl triangle drawing
  182.         //glBegin(GL_TRIANGLES);
  183.         //glVertex2f(-0.5f, -0.5f);
  184.         //glVertex2f(0.0f, 0.5f);
  185.         //glVertex2f(0.5f, -0.5f);
  186.         //glEnd();
  187.  
  188.         // Crtanje sa nizovima vertexa
  189.         // Za trougao bio treci parametar "3"
  190.         // glDrawArrays(GL_TRIANGLES, 0, 6);
  191.  
  192.         // Index buffer crtanje
  193.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
  194.  
  195.  
  196.         /* Swap front and back buffers */
  197.         glfwSwapBuffers(window);
  198.  
  199.         /* Poll for and process events */
  200.         glfwPollEvents();
  201.     }
  202.  
  203.     // Delete shader in the end
  204.     glDeleteProgram(shader);
  205.  
  206.     glfwTerminate();
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement