Advertisement
bekovski

pre uniform

Nov 23rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. #define ASSERT(x) if(!(x)) __debugbreak();
  10.  
  11. #ifdef DEBUG
  12.     #define GLCall(x) GLClearError(); x; ASSERT(GLLogCall(...))
  13. #else
  14.     #define GLCall(x) x
  15. #endif
  16.  
  17. static void GLClearError() {
  18.     while (glGetError() != GL_NO_ERROR);    // clear all the errors
  19. }
  20.  
  21. static void GLCheckError() {
  22.     while (GLenum error = glGetError()) {
  23.         std::cout << "[OpenGL_Error] (" << error << ")" << std::endl;
  24.     }
  25. }
  26.  
  27. // returns true if no error
  28. static bool GLLogCall(const char* function, const char* file, int line) {
  29.     if (GLenum error = glGetError()) {
  30.         std::cout << "[OpenGL_Error] (" << error << ")" << std::endl;
  31.         std::cout << function << " " << file << ":" << line << std::endl;
  32.         return false;
  33.     }
  34.  
  35.     return true;
  36. }
  37.  
  38. struct shaderProgramSource {
  39.     std::string vertexSource;
  40.     std::string fragmentSource;
  41. };
  42.  
  43. static shaderProgramSource parseShader(const std::string& filepath) {
  44.     std::ifstream stream(filepath);
  45.  
  46.     enum class ShaderType {
  47.         NONE = -1,
  48.         VERTEX = 0,
  49.         FRAGMENT = 1
  50.     };
  51.  
  52.     std::string line;
  53.     std::stringstream ss[2];
  54.     ShaderType type = ShaderType::NONE;
  55.     while (std::getline(stream, line)) {
  56.         if (line.find("#shader") != std::string::npos) {
  57.             if (line.find("vertex") != std::string::npos)
  58.                 type = ShaderType::VERTEX;
  59.             else if(line.find("fragment") != std::string::npos)
  60.                 type = ShaderType::FRAGMENT;
  61.         }
  62.         else {
  63.             ss[(int)type] << line << '\n';
  64.         }
  65.     }
  66.     std::cout << "here" << std::endl;
  67.     //return { ss[(int)ShaderType::VERTEX].str(), ss[(int)ShaderType::FRAGMENT].str() };
  68.     return { ss[0].str(), ss[1].str() };
  69. }
  70.  
  71. static unsigned int compileShader(unsigned int type, const std::string& source) {
  72.     unsigned int id = glCreateShader(type);
  73.     const char* src = source.c_str();
  74.     glShaderSource(id, 1, &src, nullptr);
  75.     glCompileShader(id);
  76.  
  77.     // error handling
  78.     int result;
  79.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  80.     if (result == GL_FALSE) {
  81.         int length;
  82.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  83.         char* message = (char*)alloca(length * sizeof(char));
  84.         glGetShaderInfoLog(id, length, &length, message);
  85.         std::cout << "Failed to compile " << ((type == GL_VERTEX_SHADER) ? "vertex" : "fragment") << " shader! " << std::endl;
  86.         std::cout << message << std::endl;
  87.         glDeleteShader(id);
  88.         return 0;
  89.     }
  90.  
  91.     return id;
  92. }
  93.  
  94. static unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader) {
  95.     unsigned int program = glCreateProgram();
  96.     unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
  97.     unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
  98.  
  99.     glAttachShader(program, vs);
  100.     glAttachShader(program, fs);
  101.     glLinkProgram(program);
  102.     glValidateProgram(program);
  103.  
  104.     glDeleteShader(vs);
  105.     glDeleteShader(fs);
  106.  
  107.     return program;
  108. }
  109.  
  110. int main(void)
  111. {
  112.     GLFWwindow* window;
  113.  
  114.     /* Initialize the library */
  115.     if (!glfwInit())
  116.         return -1;
  117.  
  118.     /* Create a windowed mode window and its OpenGL context */
  119.     window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  120.     if (!window)
  121.     {
  122.         glfwTerminate();
  123.         return -1;
  124.     }
  125.  
  126.     /* Make the window's context current */
  127.     glfwMakeContextCurrent(window);
  128.  
  129.     if (glewInit() != GLEW_OK) {
  130.         std::cout << "Error!" << std::endl;
  131.     }
  132.  
  133.     std::cout << glGetString(GL_VERSION) << std::endl;
  134.  
  135.     float positions[12] = {
  136.         -0.5f, -0.5f,   // 0
  137.          0.5f, -0.5f,   // 1
  138.          0.5f,  0.5f,   // 2
  139.         -0.5f,  0.5f,   // 3
  140.     };
  141.  
  142.     unsigned int indices[6] = {
  143.         0, 1, 2,
  144.         2, 3, 0
  145.     };
  146.  
  147.     unsigned int buffer;
  148.     GLCall(glGenBuffers(1, &buffer));
  149.     GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
  150.     GLCall(glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW));
  151.  
  152.     GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
  153.     GLCall(glEnableVertexAttribArray(0));
  154.  
  155.     unsigned int ibo;
  156.     GLCall(glGenBuffers(1, &ibo));
  157.     GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
  158.     GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
  159.  
  160.     shaderProgramSource src = parseShader("Basic.shader");
  161.     std::cout << "VERTEX" << std::endl;
  162.     std::cout << src.vertexSource << std::endl;
  163.     std::cout << "FRAGMENT" << std::endl;
  164.     std::cout << src.fragmentSource << std::endl;
  165.    
  166.     unsigned int shader = createShader(src.vertexSource, src.fragmentSource);
  167.     GLCall(glUseProgram(shader));
  168.  
  169.     int location = GLCall(glGetUniformLocation(shader, "u_Color"));
  170.     ASSERT(location != -1);
  171.     GLCall(glUniform4f(location, 0.2f, 0.3f, 0.8f, 0.7f));
  172.  
  173.     /* Loop until the user closes the window */
  174.     while (!glfwWindowShouldClose(window))
  175.     {
  176.         /* Render here */
  177.         glClear(GL_COLOR_BUFFER_BIT);
  178.  
  179.         //glDrawArrays(GL_TRIANGLES, 0, 6);
  180.         //GLClearError();
  181.         GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
  182.         /*GLCheckError();
  183.         ASSERT(GLLogCall());*/
  184.  
  185.         /* Swap front and back buffers */
  186.         glfwSwapBuffers(window);
  187.  
  188.         /* Poll for and process events */
  189.         glfwPollEvents();
  190.     }
  191.  
  192.     glDeleteProgram(shader);
  193.  
  194.     glfwTerminate();
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement