Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. #version 150
  2.  
  3. in vec2 Tex_coord;
  4.  
  5. out vec4 outColor;
  6.  
  7. uniform sampler2D tex;
  8.  
  9. void main() {
  10. outColor = texture(tex, Tex_coord);
  11. }
  12.  
  13. #version 150
  14.  
  15. in vec2 position;
  16. in vec2 tex_coord;
  17.  
  18. out vec2 Tex_coord;
  19.  
  20. void main() {
  21. gl_Position = vec4(position, 0.0, 1.0);
  22. Tex_coord = tex_coord;
  23. }
  24.  
  25. #include "Draw_Buffer.h"
  26. #include <memory>
  27. #include <algorithm>
  28. #include <stdexcept>
  29. #include <vector>
  30. #include <sstream>
  31. #include <fstream>
  32. #include <string>
  33.  
  34. // Util function to compile a shader from source
  35. void compile_shader(GLuint &shader, const std::string &src) {
  36. std::ifstream is(src);
  37. std::string code;
  38.  
  39. std::string temp_str;
  40. while (std::getline(is, temp_str)) {
  41. code += temp_str + 'n';
  42. }
  43.  
  44. const char *c_code = code.c_str();
  45. glShaderSource(shader, 1, &c_code, NULL);
  46. glCompileShader(shader);
  47. }
  48.  
  49. Draw_Buffer::Draw_Buffer(Window<int> *win, const std::string &vertex_shader_src, const std::string &frag_shader_src) :
  50. window(win), buffer(std::vector<RGB>(window->size())), pos_iter(buffer.begin()) {
  51. // Initialise GLFW
  52. if (!glfwInit()) {
  53. throw std::runtime_error("error: GLFW unable to initialise");
  54. }
  55.  
  56. // Set up the window
  57. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  58. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  59. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  60. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  61.  
  62. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  63.  
  64. screen.reset(glfwCreateWindow(win->width(), win->height(), "Mandelbrot Fractal", nullptr, nullptr));
  65.  
  66. make_current();
  67.  
  68. // Initialise glew
  69. glewExperimental = GL_TRUE;
  70. GLenum glewinit = glewInit();
  71.  
  72. if (glewinit != GLEW_OK) {
  73. std::ostringstream ss;
  74. ss << "error: Glew unable to initialise" << glewinit;
  75. throw std::runtime_error(ss.str());
  76. }
  77.  
  78. // Clear
  79. glClearColor(0, 0, 0, 0);
  80. glClear(GL_COLOR_BUFFER_BIT);
  81.  
  82. // Generate shaders
  83. GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  84. GLuint frag_shader = glCreateShader(GL_VERTEX_SHADER);
  85.  
  86. GLint compile_status;
  87. compile_shader(vertex_shader, vertex_shader_src);
  88. glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compile_status);
  89. if (compile_status != GL_TRUE) {
  90. char buffer[512];
  91. glGetShaderInfoLog(vertex_shader, 512, NULL, buffer);
  92. throw std::runtime_error(buffer);
  93. }
  94.  
  95. compile_shader(frag_shader, frag_shader_src);
  96. glGetShaderiv(frag_shader, GL_COMPILE_STATUS, &compile_status);
  97. if (compile_status != GL_TRUE) {
  98. char buffer[512];
  99. glGetShaderInfoLog(frag_shader, 512, NULL, buffer);
  100. throw std::runtime_error(buffer);
  101. }
  102.  
  103. // Put shaders into shader program
  104. shader_prog = glCreateProgram();
  105. glAttachShader(shader_prog, vertex_shader);
  106. glAttachShader(shader_prog, frag_shader);
  107. glBindFragDataLocation(shader_prog, 0, "outColor");
  108. glLinkProgram(shader_prog);
  109. glUseProgram(shader_prog);
  110.  
  111. // Set shader attributes
  112. GLint pos_attrib = glGetAttribLocation(shader_prog, "position");
  113. glVertexAttribPointer(pos_attrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
  114. glEnableVertexAttribArray(pos_attrib);
  115.  
  116. GLint tex_coord_attrib = glGetAttribLocation(shader_prog, "tex_coord");
  117. glEnableVertexAttribArray(tex_coord_attrib);
  118. glVertexAttribPointer(tex_coord_attrib, 2, GL_FLOAT, GL_FALSE,
  119. 4 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
  120.  
  121. // Generate texture
  122. glGenTextures(1, &mandelbrot_tex);
  123. }
  124.  
  125. Draw_Buffer::~Draw_Buffer() {
  126.  
  127.  
  128. // Terminate GLFW
  129. glfwTerminate();
  130. }
  131.  
  132. void Draw_Buffer::flush() {
  133.  
  134. // Render pixels to image
  135. glBindTexture(GL_TEXTURE_2D, mandelbrot_tex);
  136. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window->width(), window->height(), 0, GL_RGB, GL_FLOAT, &buffer[0]);
  137.  
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  139. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  141. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  142.  
  143. // Draw a rectangle
  144. const static GLfloat vertices[] = {
  145. // Position Tex-coords
  146. -1.0f, 1.0f, 0.0f, 0.0f, // Top-left
  147. 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
  148. 1.0f, -1.0f, 1.0f, 1.0f, // Bottom-right
  149. -1.0f, -1.0f, 0.0f, 1.0f // Bottom-left
  150. };
  151.  
  152. GLuint vbo;
  153. glGenBuffers(1, &vbo);
  154. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  155. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
  156.  
  157. const static GLuint elements[] = {
  158. 0, 1, 2,
  159. 2, 3, 0
  160. };
  161.  
  162. GLuint ebo;
  163. glGenBuffers(1, &ebo);
  164. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  165. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STREAM_DRAW);
  166.  
  167. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  168.  
  169. glfwSwapBuffers(screen.get());
  170. }
  171.  
  172. void Draw_Buffer::keep_window_open() {
  173. while(!glfwWindowShouldClose(screen.get())) {
  174. glfwPollEvents();
  175. }
  176. }
  177.  
  178. Draw_Buffer &operator<<(Draw_Buffer &db, const RGB &pixel) {
  179. if (db.pos_iter != db.buffer.end()) {
  180. *(db.pos_iter) = pixel;
  181. ++db.pos_iter;
  182. return db;
  183. }
  184. else {
  185. throw std::runtime_error("error: Cannot append past end of buffer : Remember to flush the buffer");
  186. return db;
  187. }
  188. }
  189.  
  190. #include "Window.h"
  191. #include "Cleanup.h"
  192.  
  193. struct RGB {
  194. float r;
  195. float g;
  196. float b;
  197. };
  198.  
  199. class Draw_Buffer {
  200. friend Draw_Buffer &operator<<(Draw_Buffer &db, const RGB &pixel);
  201. std::unique_ptr<Window<int>> window;
  202.  
  203. std::unique_ptr<GLFWwindow, Cleaner> screen;
  204.  
  205. std::vector<RGB> buffer;
  206.  
  207. // Tracks position of appending in buffer
  208. std::vector<RGB>::iterator pos_iter;
  209.  
  210. // Texture where pixels are written to
  211. GLuint mandelbrot_tex;
  212.  
  213. // GLSL Shader program
  214. GLuint shader_prog;
  215. public:
  216. Draw_Buffer(Window<int> *, const std::string &, const std::string &);
  217. ~Draw_Buffer();
  218.  
  219. void make_current() {
  220. glfwMakeContextCurrent(screen.get());
  221. }
  222.  
  223. void flush();
  224.  
  225. void keep_window_open();
  226. };
  227.  
  228. Draw_Buffer &operator<<(Draw_Buffer &db, const RGB &pixel);
  229.  
  230.  
  231. #endif //MANDELBROT_FRACTAL_DRAWER_DRAW_BUFFER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement