Advertisement
Frumkin

Untitled

Feb 12th, 2022
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include "Process.hpp"
  2.  
  3. unsigned int CompileShader(const unsigned int type, const std::string source) {
  4.     unsigned int id = glCreateShader(type);
  5.     const char* src = source.c_str();
  6.     glShaderSource(id, 1, &src, nullptr);
  7.     glCompileShader(id);
  8.  
  9.     int result;
  10.     glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  11.     if (!result) {
  12.         int length;
  13.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
  14.         char* message = (char*)alloca(length * sizeof(char));
  15.         glGetShaderInfoLog(id, length, &length, message);
  16.         std::cerr << "Failed to compile shader\n" << message << std::endl;
  17.         glDeleteShader(id);
  18.         return 0;
  19.     }
  20.  
  21.     // TODO: Error handling
  22.  
  23.     return id;
  24. }
  25.  
  26. int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
  27.     unsigned int program = glCreateProgram();
  28.     unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  29.     unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  30.  
  31.     glAttachShader(program, vs);
  32.     glAttachShader(program, fs);
  33.    
  34.     glLinkProgram(program);
  35.     glValidateProgram(program);
  36.  
  37.     glDeleteShader(vs);
  38.     glDeleteShader(fs);
  39.  
  40.     return program;
  41. }
  42.  
  43. void Process::m_update(const float deltaTime) {
  44.     // handle input
  45.     // poll events
  46. }
  47.  
  48. int Process::Start(const unsigned int updatesPerSecond, int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share, const char* iniFileName) {
  49.     m_millisecondsPerUpdate = 1000.0 / updatesPerSecond;
  50.     m_deltaTime = 1.0f / updatesPerSecond;
  51.  
  52.     if (!glfwInit()) return 1;
  53.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  54.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  55.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  56.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  57.     m_window = glfwCreateWindow(width, height, title, monitor, share);
  58.     if (!m_window) {
  59.         glfwTerminate();
  60.         return 1;
  61.     }
  62.  
  63.     glfwMakeContextCurrent(m_window);
  64.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
  65.         std::cerr << "Failed to initialize GLAD" << std::endl;
  66.         return 1;
  67.     }
  68.  
  69.     glViewport(0, 0, width, height);
  70.  
  71.     float positions[] = {
  72.         -0.5f, -0.5f,
  73.          0.5f, -0.5f,
  74.          0.5f,  0.5f,
  75.         -0.5f,  0.5f,
  76.     };
  77.  
  78.     unsigned int indices[] = {
  79.         0, 1, 2,
  80.         2, 3, 0
  81.     };
  82.  
  83.     unsigned int buffer;
  84.     glGenBuffers(1, &buffer);
  85.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  86.     glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), positions, GL_STATIC_DRAW);
  87.  
  88.     glEnableVertexAttribArray(0);
  89.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  90.  
  91.     unsigned int ibo;
  92.     glGenBuffers(1, &ibo);
  93.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  94.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  95.  
  96.     std::ifstream stream;
  97.     stream.open("./res/shaders/vertex.glsl");
  98.     std::string vertexShader = std::string((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
  99.     stream.close();
  100.  
  101.     stream.open("./res/shaders/fragment.glsl");
  102.     std::string fragmentShader = std::string((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
  103.     stream.close();
  104.  
  105.     unsigned int shader = CreateShader(vertexShader, fragmentShader);
  106.     glUseProgram(shader);
  107.    
  108.    
  109.    
  110.    
  111.    
  112.  
  113.     while (!glfwWindowShouldClose(m_window)) {
  114.         glClear(GL_COLOR_BUFFER_BIT);
  115.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
  116.  
  117.         glfwSwapBuffers(m_window);
  118.         glfwPollEvents();
  119.     }
  120.  
  121.     glDeleteProgram(shader);
  122.  
  123.     glfwTerminate();
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement