Advertisement
dark-s0ul

Untitled

Dec 14th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #define GLEW_STATIC
  4. #include <GL/glew.h>
  5. #include <GLFW/glfw3.h>
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. #include <string.h>
  10. #include <complex>
  11. #include <fstream>
  12. #include <pthread.h>
  13. #include <unistd.h>
  14.  
  15. const char *vertex = R"(
  16. #version 330 core
  17. layout (location = 0) in vec3 position;
  18. layout (location = 1) in vec3 color;
  19. layout (location = 2) in vec2 texCoord;
  20.  
  21. out vec3 ourColor;
  22. out vec2 TexCoord;
  23.  
  24. void main() {
  25. gl_Position = vec4(position, 1.0f);
  26. ourColor = color;
  27. TexCoord = texCoord;
  28. }
  29. )";
  30.  
  31. const char *fragment = R"(
  32. #version 330 core
  33.  
  34. uniform vec2 screenSize;
  35. uniform sampler2D image;
  36.  
  37. in vec3 ourColor;
  38. in vec2 TexCoord;
  39.  
  40. out vec4 color;
  41.  
  42. uniform sampler2D ourTexture;
  43.  
  44. void main() {
  45. color = texture(ourTexture, TexCoord);
  46. }
  47. )";
  48.  
  49. GLuint compileShader(GLenum shaderType, const char* source) {
  50. GLuint shader = glCreateShader(shaderType);
  51. if (shader) {
  52. glShaderSource(shader, 1, &source, NULL);
  53. glCompileShader(shader);
  54. GLint status = 0;
  55. glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  56. if (!status) {
  57. GLint log_len;
  58. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
  59. if (log_len > 0) {
  60. vector<char> message(log_len + 1);
  61. glGetShaderInfoLog(shader, log_len, NULL, &message[0]);
  62. printf("%s\n", &message[0]);
  63. }
  64. glDeleteShader(shader);
  65. shader = 0;
  66. }
  67. }
  68. return shader;
  69. }
  70.  
  71. GLuint createShader(const char* vertexSource, const char* fragmentSource) {
  72. GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSource);
  73. if (!vertexShader) return 0;
  74.  
  75. GLuint pixelShader = compileShader(GL_FRAGMENT_SHADER, fragmentSource);
  76. if (!pixelShader) return 0;
  77.  
  78. GLuint program = glCreateProgram();
  79. if (program) {
  80. glAttachShader(program, vertexShader);
  81. glAttachShader(program, pixelShader);
  82. glLinkProgram(program);
  83. GLint status = GL_FALSE;
  84. glGetProgramiv(program, GL_LINK_STATUS, &status);
  85. if (status != GL_TRUE) {
  86. GLint log_len;
  87. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
  88. if (log_len > 0) {
  89. vector<char> message(log_len + 1);
  90. glGetProgramInfoLog(program, log_len, NULL, &message[0]);
  91. printf("%s\n", &message[0]);
  92. }
  93. glDeleteProgram(program);
  94. program = 0;
  95. }
  96. }
  97. return program;
  98. }
  99.  
  100. struct Color {
  101. unsigned char r, g, b;
  102. };
  103.  
  104. struct TextureData {
  105. GLuint bufferId;
  106. int width, height;
  107. Color *pixels;
  108.  
  109. TextureData(int width, int height) : width(width), height(height) {
  110. pixels = new Color[width * height];
  111. glGenTextures(1, &bufferId);
  112. glBindTexture(GL_TEXTURE_2D, bufferId);
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  117. glBindTexture(GL_TEXTURE_2D, 0);
  118.  
  119. apply();
  120. }
  121.  
  122. void apply() {
  123. //glTexSubImage2D(GL_TEXTURE_2D, 0 ,0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)screenData);
  124. glBindTexture(GL_TEXTURE_2D, bufferId);
  125. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  126. glBindTexture(GL_TEXTURE_2D, 0);
  127. }
  128.  
  129. ~TextureData() {
  130. delete[] pixels;
  131. }
  132.  
  133. Color getPixel(int x, int y) {
  134. return pixels[y * width + x];
  135. }
  136.  
  137. void setPixel(int x, int y, Color color) {
  138. pixels[y * width + x] = color;
  139. }
  140. };
  141.  
  142.  
  143. enum { pallete_size = 64 };
  144. unsigned char pallete[pallete_size][3];
  145.  
  146. bool update = false;
  147. bool running = true;
  148.  
  149. int width, height;
  150. int t = 0;
  151.  
  152. float aspect, scale, offsetX, offsetY;
  153.  
  154. void update_pixels(TextureData *texture) {
  155. for (int y = 0; y < height; ++y) {
  156. for (int x = 0; x < width; ++x) {
  157. complex<double> c(x * scale - offsetX, y * scale - offsetY);
  158. complex<double> z(0.0, 0.0);
  159.  
  160. int n = 0;
  161. while (n < (t % pallete_size)) {
  162. z = z * z + c;
  163. if (abs(z) > 2.0) break;
  164. ++n;
  165. }
  166. unsigned char r = pallete[n][0];
  167. unsigned char g = pallete[n][1];
  168. unsigned char b = pallete[n][2];
  169.  
  170. texture->setPixel(x, y, Color{r, g, b});
  171. }
  172. }
  173. t++;
  174. }
  175.  
  176. void *update_texture(void *ptr) {
  177. TextureData *texture = (TextureData *)ptr;
  178.  
  179. while (running) {
  180. if (update) continue;
  181. update_pixels(texture);
  182. update = true;
  183. //cout << "update" << endl;
  184. }
  185. }
  186.  
  187. int main() {
  188. glfwInit();
  189. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  190. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  191. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  192. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  193.  
  194. GLFWwindow* window = glfwCreateWindow(800, 600, "Computer graphics", nullptr, nullptr);
  195. if (window == nullptr) {
  196. cout << "Failed to create GLFW window" << endl;
  197. glfwTerminate();
  198. return 0;
  199. }
  200. glfwMakeContextCurrent(window);
  201.  
  202. glewExperimental = GL_TRUE;
  203. if (glewInit() != GLEW_OK) {
  204. cout << "Failed to initialize GLEW" << endl;
  205. return 0;
  206. }
  207.  
  208. glfwGetFramebufferSize(window, &width, &height);
  209. glViewport(0, 0, width, height);
  210.  
  211. auto shader = createShader(vertex, fragment);
  212.  
  213. aspect = (float) width / (float) height;
  214.  
  215. scale = 4.0 / (float) height;
  216. offsetX = aspect * 2.0;
  217. offsetY = 2.0;
  218.  
  219. GLfloat vertices[] = {
  220. // Positions // Colors // Texture Coords
  221. 1, 1, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
  222. 1, -1, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
  223. -1, -1, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
  224. -1, 1, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
  225. };
  226. GLuint indices[] = { // Note that we start from 0!
  227. 0, 1, 3, // First Triangle
  228. 1, 2, 3 // Second Triangle
  229. };
  230. GLuint VBO, VAO, EBO;
  231. glGenVertexArrays(1, &VAO);
  232. glGenBuffers(1, &VBO);
  233. glGenBuffers(1, &EBO);
  234.  
  235. glBindVertexArray(VAO);
  236.  
  237. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  238. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  239.  
  240. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  241. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  242.  
  243. // Position attribute
  244. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
  245. glEnableVertexAttribArray(0);
  246. // Color attribute
  247. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  248. glEnableVertexAttribArray(1);
  249. // TexCoord attribute
  250. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
  251. glEnableVertexAttribArray(2);
  252. glBindVertexArray(0); // Unbind VAO
  253.  
  254.  
  255. TextureData texture(width, height);
  256. glUseProgram(shader);
  257. glUniform2f(glGetUniformLocation(shader, "screenSize"), width, height);
  258.  
  259. for (int i = 0; i < pallete_size; i++) {
  260. pallete[i][0] = i < (2 * pallete_size / 3) ? (i * 255 * 3 / (2 * pallete_size)) : 255;
  261. pallete[i][1] = i < pallete_size / 3 ? 0 : (i - pallete_size / 3) * 255 * 3 / (2 * pallete_size);
  262. pallete[i][2] = 0;
  263. }
  264.  
  265. t = 0;
  266.  
  267. bool use_multithread = false;
  268.  
  269. pthread_t thread;
  270. if (use_multithread) {
  271. update = false;
  272. running = true;
  273. pthread_create(&thread, NULL, update_texture, &texture);
  274. }
  275.  
  276. while(!glfwWindowShouldClose(window)) {
  277. glfwPollEvents();
  278.  
  279. glClearColor(0, 0, 1, 1);
  280. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  281.  
  282. //update_texture(&texture);
  283.  
  284. if (use_multithread) {
  285. if (update == true) {
  286. texture.apply();
  287. update = false;
  288. //cout << "end update" << endl;
  289. }
  290. } else {
  291. update_pixels(&texture);
  292. texture.apply();
  293. }
  294.  
  295. glActiveTexture(GL_TEXTURE0);
  296. glBindTexture(GL_TEXTURE_2D, texture.bufferId);
  297. glUniform1i(glGetUniformLocation(shader, "ourTexture"), 0);
  298.  
  299. // Draw container
  300. glBindVertexArray(VAO);
  301. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  302. glBindVertexArray(0);
  303.  
  304. glfwSwapBuffers(window);
  305. }
  306. if (use_multithread) {
  307. running = false;
  308. pthread_join(thread, NULL);
  309. }
  310. glUseProgram(0);
  311. glfwTerminate();
  312. return 0;
  313. //pthread_join(thread, NULL);
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement