hnOsmium0001

broken (SegFault)

Oct 16th, 2019
207
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <glm/glm.hpp>
  4. #include <memory>
  5. #include <vector>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <vector>
  9.  
  10. using namespace std;
  11. using namespace glm;
  12.  
  13. #include "../common/shader.hpp"
  14.  
  15. GLFWwindow *window;
  16.  
  17. class Vertices {
  18. private:
  19.   const uint32_t vertParts;
  20.   const uint32_t primitiveSize;
  21.  
  22.   GLuint vbo;
  23.   vector<GLfloat> verts;
  24.   uint32_t pointer;
  25.  
  26. public:
  27.   Vertices(const uint32_t vertParts, const uint32_t primitiveSize, const uint32_t primitives);
  28.   ~Vertices(void);
  29.  
  30.   void PutBufferData(const GLenum bufferType);
  31.   void SetAttribPointer(const uint16_t attrib, GLboolean normalized);
  32.   void DelAttribPointer(const uint16_t attrib);
  33.   void Bind(void);
  34.   Vertices& operator<<(GLfloat val);
  35. };
  36.  
  37. Vertices::Vertices(const uint32_t vertParts, const uint32_t primitiveSize, const uint32_t primitives)
  38.   : vertParts{vertParts}, primitiveSize{primitiveSize}, verts(primitives * vertParts * primitiveSize) {
  39.   glGenBuffers(1, &vbo);
  40. }
  41. Vertices::~Vertices() {
  42.   glDeleteBuffers(1, &vbo);
  43. }
  44.  
  45. void Vertices::PutBufferData(const GLenum bufferType) {
  46.   glBufferData(GL_ARRAY_BUFFER, verts.size(), verts.data(), bufferType);
  47. }
  48. void Vertices::SetAttribPointer(const uint16_t attrib, GLboolean normalized) {
  49.   glEnableVertexAttribArray(attrib);
  50.   glVertexAttribPointer(attrib, primitiveSize, GL_FLOAT, normalized, sizeof(GLfloat) * vertParts, nullptr);
  51. }
  52. void Vertices::DelAttribPointer(const uint16_t attrib) {
  53.   glDisableVertexAttribArray(attrib);
  54. }
  55. void Vertices::Bind() {
  56.   glBindBuffer(GL_ARRAY_BUFFER, vbo);
  57. }
  58. Vertices& Vertices::operator<<(GLfloat val) {
  59.   verts[pointer] = val;
  60.   ++pointer;
  61.   return *this;
  62. }
  63.  
  64. int main(void) {
  65.   // Initialise GLFW
  66.   if (!glfwInit()) {
  67.     fprintf(stderr, "Failed to initialize GLFW\n");
  68.     getchar();
  69.     return -1;
  70.   }
  71.  
  72.   glfwWindowHint(GLFW_SAMPLES, 4);
  73.   glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  74.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  75.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  76.   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,
  77.                  GL_TRUE); // To make MacOS happy; should not be needed
  78.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  79.  
  80.   // Open a window and create its OpenGL context
  81.   window = glfwCreateWindow(1024, 768, "Playground", nullptr, nullptr);
  82.   if (window == nullptr) {
  83.     fprintf(stderr,
  84.             "Failed to open GLFW window. If you have an Intel GPU, they are "
  85.             "not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
  86.     getchar();
  87.     glfwTerminate();
  88.     return -1;
  89.   }
  90.   glfwMakeContextCurrent(window);
  91.  
  92.   // Initialize GLEW
  93.   // IMPORTANT: if this is not enable this might create SegmentationFault
  94.   glewExperimental = true; // For core profile
  95.   if (glewInit() != GLEW_OK) {
  96.     fprintf(stderr, "Failed to initialize GLEW\n");
  97.     getchar();
  98.     glfwTerminate();
  99.     return -1;
  100.   }
  101.  
  102.   // Ensure we can capture the escape key being pressed below
  103.   glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  104.  
  105.   // GL code starts
  106.  
  107.   // Dark blue background
  108.   glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  109.  
  110.   GLuint vao;
  111.   glGenVertexArrays(1, &vao);
  112.   glBindVertexArray(vao);
  113.  
  114.   Shader shader("shader.vert", "shader.frag");
  115.  
  116.   static const uint32_t triangles = 1;
  117.   Vertices vbo(3, 3, triangles);
  118.   Vertices colors(3, 3, triangles);
  119.  
  120.   vbo.Bind();
  121.   vbo << -0.5f << -0.5f << 0.0f;
  122.   vbo << 0.5f << -0.5f << 0.0f;
  123.   vbo << 0.0f << 0.5f << 0.0f;
  124.   vbo.PutBufferData(GL_STATIC_DRAW);
  125.  
  126.   colors.Bind();
  127.   colors << 1.0f << 0.0f << 0.0f;
  128.   colors << 0.0f << 1.0f << 0.0f;
  129.   colors << 0.0f << 0.0f << 1.0f;
  130.   colors.PutBufferData(GL_STATIC_DRAW);
  131.  
  132.   GLint colorLoc = shader.GetLocation("color");
  133.  
  134.   do {
  135.     glClear(GL_COLOR_BUFFER_BIT);
  136.  
  137.     shader.Use();
  138.     float time = glfwGetTime();
  139.     float color = sin(time) * 0.2f;
  140.     glUniform4f(colorLoc, color, color + 0.5f, color, 1.0f);
  141.  
  142.     vbo.Bind();
  143.     vbo.SetAttribPointer(0, GL_FALSE);
  144.    
  145.     colors.Bind();
  146.     colors.SetAttribPointer(1, GL_FALSE);
  147.  
  148.     glDrawArrays(GL_TRIANGLES, 0, triangles * 3);
  149.  
  150.     vbo.DelAttribPointer(0);
  151.     colors.DelAttribPointer(1);
  152.  
  153.     // Swap buffers
  154.     glfwSwapBuffers(window);
  155.     glfwPollEvents();
  156.  
  157.   } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  158.            glfwWindowShouldClose(window) == 0);
  159.  
  160.   glDeleteVertexArrays(1, &vao);
  161.  
  162.   // GL code ends
  163.  
  164.   // Close OpenGL window and terminate GLFW
  165.   glfwTerminate();
  166.  
  167.   return 0;
  168. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×