Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <array>
  4. #include <string>
  5. #include <fstream>
  6. #include <sstream>
  7. #include "Log/Log.h"
  8.  
  9. #define ASSERT(x) if (!(x)) __debugbreak();
  10. #define GLCall(x) GLClearError();\
  11.     x;\
  12.     ASSERT(GLLogCall(#x, __FILE__, __LINE__))  
  13.  
  14. static void GLClearError()
  15. {
  16.     while (glGetError() != GL_NO_ERROR);
  17. }
  18.  
  19. static bool GLLogCall(const char* function, const char* file, int line)
  20. {
  21.     while (GLenum error = glGetError())
  22.     {
  23.         GUNN_CORE_ERROR("OpenGL Error: '{0}'\n{1}\nFILE: {2}\nLINE: {3} ", error, function, file, line);
  24.         return false;
  25.     }
  26.  
  27.     return true;
  28. }
  29.  
  30. struct ShaderProgramSource
  31. {
  32.     std::string VertexSource;
  33.     std::string FragmentSource;
  34. };
  35.  
  36. static ShaderProgramSource ParseShader(const std::string& filePath)
  37. {
  38.     std::ifstream stream(filePath);
  39.  
  40.     enum class ShaderType
  41.     {
  42.         NONE = -1,
  43.         VERTEX = 0,
  44.         FRAGMENT = 1
  45.     };
  46.  
  47.     std::string line;
  48.     std::array<std::stringstream, 2> ss;
  49.     ShaderType type = ShaderType::NONE;
  50.     while (getline(stream, line))
  51.     {
  52.         if (line.find("#shader") != std::string::npos)
  53.         {
  54.             if (line.find("vertex") != std::string::npos)
  55.             {
  56.                 type = ShaderType::VERTEX;
  57.             }
  58.             else if (line.find("fragment") != std::string::npos)
  59.             {
  60.                 type = ShaderType::FRAGMENT;
  61.             }
  62.         }
  63.         else
  64.         {
  65.             ss[(int)type] << line << "\n";
  66.         }
  67.     }
  68.  
  69.     return { ss[0].str(), ss[1].str() };
  70. }
  71.  
  72. static unsigned int CompileShader(unsigned int type, const std::string& source)
  73. {
  74.     GLCall(unsigned int id = glCreateShader(type));
  75.     const char* src = source.c_str();
  76.     GLCall(glShaderSource(id, 1, &src, nullptr));
  77.     GLCall(glCompileShader(id));
  78.  
  79.     int result;
  80.     GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
  81.     if (result == GL_FALSE)
  82.     {
  83.         int length;
  84.         GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
  85.         char* message = (char*)alloca(length * sizeof(char));
  86.         GLCall(glGetShaderInfoLog(id, length, &length, message));
  87.  
  88.         GUNN_CORE_ERROR("Failed to compile {0} shader", (type == GL_VERTEX_SHADER ? "vertex" : "fragment"));
  89.         GUNN_CORE_ERROR("{0}", message);
  90.  
  91.         GLCall(glDeleteShader(id));
  92.         return 0;
  93.     }
  94.  
  95.     return id;
  96. }
  97.  
  98. static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
  99. {
  100.     GLCall(unsigned int program = glCreateProgram());
  101.     GLCall(unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader));
  102.     GLCall(unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader));
  103.  
  104.     GLCall(glAttachShader(program, vs));
  105.     GLCall(glAttachShader(program, fs));
  106.     GLCall(glLinkProgram(program));
  107.     GLCall(glDetachShader(program, vs));
  108.     GLCall(glDetachShader(program, fs));
  109.     GLCall(glValidateProgram(program));
  110.  
  111.     GLCall(glDeleteShader(vs));
  112.     GLCall(glDeleteShader(fs));
  113.  
  114.     return program;
  115. }
  116.  
  117. int main()
  118. {
  119.     GLFWwindow* window = nullptr;
  120.  
  121.     Gunn::Log::Init();
  122.  
  123.     /* Initialize the library */
  124.     if (!glfwInit())
  125.     {
  126.         GUNN_CORE_FATAL("ERROR STARTING GLFW!");
  127.  
  128.         glfwDestroyWindow(window);
  129.         window = nullptr;
  130.         glfwTerminate();
  131.  
  132.         return EXIT_FAILURE;
  133.     }
  134.  
  135.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  136.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  137.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  138.  
  139.     /* Create a windowed mode window and its OpenGL context */
  140.     window = glfwCreateWindow(1280, 720, "OpenGL", NULL, NULL);
  141.     if (window == nullptr)
  142.     {
  143.         GUNN_CORE_FATAL("ERROR CREATING GAME WINDOW!");
  144.  
  145.         glfwDestroyWindow(window);
  146.         window = nullptr;
  147.         glfwTerminate();
  148.  
  149.         return EXIT_FAILURE;
  150.     }
  151.  
  152.     /* Make the window's context current */
  153.     glfwMakeContextCurrent(window);
  154.     glfwSwapInterval(1);
  155.  
  156.     if (glewInit() != GLEW_OK)
  157.     {
  158.         GUNN_CORE_FATAL("ERROR STARTING GLEW!");
  159.  
  160.         glfwDestroyWindow(window);
  161.         window = nullptr;
  162.         glfwTerminate();
  163.  
  164.         return EXIT_FAILURE;
  165.     }
  166.  
  167.     GUNN_CORE_INFO("OpenGL Version {0}", glGetString(GL_VERSION));
  168.  
  169.     ////////////////////////////////////////////////////////////////
  170.  
  171.     std::array<GLfloat, 8> positions =
  172.     {
  173.         -0.5f, -0.5f,
  174.          0.5f, -0.5f,
  175.          0.5f,  0.5f,
  176.         -0.5f,  0.5f
  177.     };
  178.  
  179.     std::array<unsigned int, 6> indices =
  180.     {
  181.         0, 1, 2,
  182.         2, 3, 0
  183.     };
  184.  
  185.     unsigned int vao;
  186.     GLCall(glGenVertexArrays(1, &vao));
  187.     GLCall(glBindVertexArray(vao));
  188.  
  189.     unsigned int buffer;
  190.     GLCall(glGenBuffers(1, &buffer));
  191.     GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
  192.     GLCall(glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), positions.data(), GL_STATIC_DRAW));
  193.  
  194.     GLCall(glEnableVertexAttribArray(0));
  195.     GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
  196.  
  197.     unsigned int ibo;
  198.     GLCall(glGenBuffers(1, &ibo));
  199.     GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
  200.     GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW));
  201.  
  202.     ShaderProgramSource source = ParseShader("res/Shaders/Basic.shader");
  203.    
  204.     unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
  205.     GLCall(glUseProgram(shader));
  206.  
  207.     GLCall(int location = glGetUniformLocation(shader, "u_Color"));
  208.     ASSERT(location != -1);
  209.     GLCall(glUniform4f(location, 1.0f, 0.0f, 0.0f, 1.0f));
  210.  
  211.     GLCall(glBindVertexArray(0));
  212.     GLCall(glUseProgram(0));
  213.     GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
  214.     GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
  215.  
  216.     GLfloat r = 0.0f;
  217.     GLfloat increment = 0.05f;
  218.     /* Loop until the user closes the window */
  219.     while (!glfwWindowShouldClose(window))
  220.     {
  221.         /* Render here */
  222.         GLCall(glClear(GL_COLOR_BUFFER_BIT));
  223.  
  224.         GLCall(glUseProgram(shader));
  225.         GLCall(glUniform4f(location, r, 3.0f, 4.0f, 1.0f));
  226.  
  227.         GLCall(glBindVertexArray(vao));
  228.         GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
  229.  
  230.         /* Draw stuff here*/
  231.         GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
  232.  
  233.         if (r > 1.0f)
  234.         {
  235.             increment = -0.05f;
  236.         }
  237.         else if (r < 0.0f)
  238.         {
  239.             increment = 0.05f;
  240.         }
  241.  
  242.         r += increment;
  243.  
  244.         /* Swap front and back buffers */
  245.         glfwSwapBuffers(window);
  246.  
  247.         /* Poll for and process events */
  248.         glfwPollEvents();
  249.     }
  250.  
  251.     GLCall(glDeleteProgram(shader));
  252.  
  253.     glfwDestroyWindow(window);
  254.     glfwTerminate();
  255.     return EXIT_SUCCESS;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement