Advertisement
uraharadono

OpenGL Video 10

Mar 27th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 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. #define ASSERT(x) if (!(x)) __debugbreak();
  15.  
  16. // ovdje pravim macro koji ce automatski zovnuti clear error funkciju itd. svaki put kad se dogodi error
  17. // znaci u ovom slucaju pravim macro koji se zove "GLCall" koji prima x (x je funkcija)
  18. // poziva se prvo "GLClearError", i onda ide backslash "\" da bi mogao nastaviti pisati ispod macro, jer bi bilo previse sve u liniji napisati.
  19. // zatim ide x (to je nasa funckija koju smo proslijedili
  20. // kad pozivamo funkciju (tj. x) trebamo proslijediti 3 paramentra
  21. // kad uradimo "#x" to znaci da cemo uraditi x.ToString(), tkao cemo dobiti ime funkcije
  22. // VELIKI NOTE: Cherno kaze da on SVAKU openGl funkciju wrapa u ovaj call, tako da bi trebao to zapamtiti da je best practice to (ne radi meni - makar na poslu)
  23. #define GLCall(x) GLClearError();\
  24.     x;\
  25.     ASSERT(GLLogCall(#x, __FILE__, __LINE__))
  26.  
  27. static void GLClearError()
  28. {
  29.     // while (!glGetError()); // Can be done like this as well
  30.     while (!glGetError() != GL_NO_ERROR);
  31. }
  32.  
  33. static bool GLLogCall(const char* function, const char* file, int line)
  34. {
  35.     while (GLenum error = glGetError())
  36.     {
  37.         std::cout << "[OpenGl error] (" << error << "): " << function << " " << file << ":" << line << std::endl;
  38.         return false;
  39.     }
  40.     return true;
  41. }
  42.  
  43. struct ShaderProgramSource
  44. {
  45.     std::string VertexSource;
  46.     std::string FragmentSource;
  47. };
  48.  
  49. static ShaderProgramSource ParseShader(const std::string& filepath)
  50. {
  51.     std::ifstream stream(filepath);
  52.  
  53.     enum class ShaderType
  54.     {
  55.         NONE = -1,
  56.         VERTEX = 0,
  57.         FRAGMENT = 1
  58.     };
  59.  
  60.     std::string line;
  61.     std::stringstream ss[2];
  62.     ShaderType type = ShaderType::NONE;
  63.     while (getline(stream, line))
  64.     {
  65.         if (line.find("#shader") != std::string::npos)
  66.         {
  67.             if (line.find("vertex") != std::string::npos)
  68.             {
  69.                 // set type to vertex
  70.                 type = ShaderType::VERTEX;
  71.             }
  72.             else if (line.find("fragment") != std::string::npos)
  73.             {
  74.                 // set type to fragment
  75.                 type = ShaderType::FRAGMENT;
  76.             }
  77.         }
  78.         else
  79.         {
  80.             ss[(int)type] << line << '\n';
  81.         }
  82.     }
  83.     return {ss[0].str(), ss[1].str()};
  84. }
  85.  
  86. static unsigned int CompileShader(unsigned int type, const std::string& source)
  87. {
  88.     unsigned int id = glCreateShader(type);
  89.     const char* src = source.c_str();
  90.     glShaderSource(id, 1, &src, nullptr);
  91.     glCompileShader(id);
  92.  
  93.     // Error handling
  94.     int result;
  95.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  96.     if (result == GL_FALSE)
  97.     {
  98.         int length;
  99.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  100.  
  101.         // On govori ovdje da hoce na stacku da napravi char array, nece pokazivac koji bi bio na heap-u jer ga mora kasnije pocistiti.
  102.         // Ja se ne slazem sa njim, ali da bi mi bilo lakse pratiti video-e i ja cu napraviti kao i on.
  103.         // char* message = new char[length];
  104.  
  105.         char* message = (char*)alloca(length * sizeof(char));
  106.         glGetShaderInfoLog(id, length, &length, message);
  107.         std::cout << "Failed to compile " <<
  108.             (type == GL_VERTEX_SHADER ? "vertext" : "fragment")
  109.             << " shader." << std::endl;
  110.         std::cout << message << std::endl;
  111.         glDeleteShader(id);
  112.         return 0;
  113.     }
  114.  
  115.     return id;
  116. }
  117.  
  118. static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
  119. {
  120.     unsigned int program = glCreateProgram();
  121.     unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  122.     unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  123.  
  124.     glAttachShader(program, vs);
  125.     glAttachShader(program, fs);
  126.     glLinkProgram(program);
  127.     glValidateProgram(program);
  128.  
  129.     // delete intermediates
  130.     glDeleteShader(vs);
  131.     glDeleteShader(fs);
  132.  
  133.     return program;
  134. }
  135.  
  136.  
  137. int main(void)
  138. {
  139.     GLFWwindow* window;
  140.  
  141.     /* Initialize the library */
  142.     if (!glfwInit())
  143.         return -1;
  144.  
  145.     /* Create a windowed mode window and its OpenGL context */
  146.     window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  147.     if (!window)
  148.     {
  149.         glfwTerminate();
  150.         return -1;
  151.     }
  152.  
  153.     /* Make the window's context current */
  154.     glfwMakeContextCurrent(window);
  155.  
  156.     if (glewInit() != GLEW_OK)
  157.     {
  158.         std::cout << "Error!" << std::endl;
  159.     }
  160.  
  161.     std::cout << glGetString(GL_VERSION) << std::endl;
  162.  
  163.     // Vertex buffers and layouts in open GL
  164.     /*float positions[6] = {
  165.     -0.5f, -0.5f,
  166.     0.0f, 0.5f,
  167.     0.5f, -0.5f
  168.     };*/
  169.     float positions[] = {
  170.         -0.5f, -0.5f, // 0
  171.         0.5f, -0.5f, // 1
  172.         0.5f, 0.5f, // 2
  173.         -0.5f, 0.5f // 3
  174.     };
  175.  
  176.     unsigned int indices[] = {
  177.         0, 1, 2,
  178.         2, 3, 0
  179.     };
  180.  
  181.     // Stari array of vertexes nacin crtanja trougla
  182.     unsigned int buffer;
  183.     glGenBuffers(1, &buffer);
  184.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  185.     // Ovdje je prije bilo za 2gi parametar samo "6 * sizeof(float)", ali to je zato sto je bilo 6 tacaka koje treba nacrtati
  186.     glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), positions, GL_STATIC_DRAW);
  187.     // Vertex attributes and layouts in open GL
  188.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  189.     glEnableVertexAttribArray(0);
  190.  
  191.     // INDEX BUFFER
  192.     unsigned int ibo;
  193.     glGenBuffers(1, &ibo);
  194.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  195.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  196.  
  197.     // Creating shaders
  198.     ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
  199.     unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
  200.     glUseProgram(shader);
  201.  
  202.  
  203.     /* Loop until the user closes the window */
  204.     while (!glfwWindowShouldClose(window))
  205.     {
  206.         /* Render here */
  207.         glClear(GL_COLOR_BUFFER_BIT);
  208.  
  209.         // Legacy (Old) OpenGl triangle drawing
  210.         //glBegin(GL_TRIANGLES);
  211.         //glVertex2f(-0.5f, -0.5f);
  212.         //glVertex2f(0.0f, 0.5f);
  213.         //glVertex2f(0.5f, -0.5f);
  214.         //glEnd();
  215.  
  216.         // Crtanje sa nizovima vertexa
  217.         // Za trougao bio treci parametar "3"
  218.         // glDrawArrays(GL_TRIANGLES, 0, 6);
  219.  
  220.         // Index buffer crtanje
  221.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
  222.  
  223.         // GLCall(glDrawElements(GL_TRIANGLES, 6, GL_INT, nullptr)); // uncomment to test error func
  224.  
  225.         /* Swap front and back buffers */
  226.         glfwSwapBuffers(window);
  227.  
  228.         /* Poll for and process events */
  229.         glfwPollEvents();
  230.     }
  231.  
  232.     // Delete shader in the end
  233.     glDeleteProgram(shader);
  234.  
  235.     glfwTerminate();
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement