Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. glGenFramebuffersEXT(1, &fbo);
  2. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  3. glEnable(GL_TEXTURE_2D);
  4. glGenTextures(1, &fbo_texture);
  5. glBindTexture(GL_TEXTURE_2D, fbo_texture);
  6. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  8. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  9. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_texture, 0);
  10. GLenum status;
  11. if ((status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)) != GL_FRAMEBUFFER_COMPLETE_EXT) {
  12. fprintf(stderr, "glCheckFramebufferStatus: error %p", status);
  13. }
  14. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  15.  
  16. #include "main.hpp"
  17.  
  18. GLuint fbo, fbo_texture, rbo_depth;
  19. GLuint vbo_fbo_vertices;
  20. GLuint program_postproc, attribute_v_coord_postproc, uniform_fbo_texture;
  21. GLuint vs, fs;
  22. Shader shader;
  23.  
  24. int main(void)
  25. {
  26. init();
  27.  
  28. glGenFramebuffersEXT(1, &fbo);
  29. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  30. glEnable(GL_TEXTURE_2D);
  31. glGenTextures(1, &fbo_texture);
  32. glBindTexture(GL_TEXTURE_2D, fbo_texture);
  33. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  36. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_texture, 0);
  37.  
  38. GLenum status;
  39. if ((status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)) != GL_FRAMEBUFFER_COMPLETE_EXT) {
  40. fprintf(stderr, "glCheckFramebufferStatus: error %p", status);
  41. }
  42. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  43.  
  44. GLfloat fbo_vertices[] = { -1, -1, 1, -1,-1, 1, 1, 1 };
  45. glGenBuffers(1, &vbo_fbo_vertices);
  46. glBindBuffer(GL_ARRAY_BUFFER, vbo_fbo_vertices);
  47. glBufferData(GL_ARRAY_BUFFER, sizeof(fbo_vertices), fbo_vertices, GL_STATIC_DRAW);
  48. glBindBuffer(GL_ARRAY_BUFFER, 0);
  49.  
  50. shader.load("shaders/post_processing.vert", "shaders/post_processing.frag");
  51.  
  52. attribute_v_coord_postproc = glGetAttribLocation(shader.program(), "v_coord");
  53. if (attribute_v_coord_postproc == -1) {
  54. fprintf(stderr, "Could not bind attribute %sn", "v_coord");
  55. return 0;
  56. }
  57. uniform_fbo_texture = glGetUniformLocation(shader.program(), "fbo_texture");
  58. if (uniform_fbo_texture == -1) {
  59. fprintf(stderr, "Could not bind uniform %sn", "fbo_texture");
  60. return 0;
  61. }
  62.  
  63. while (!glfwWindowShouldClose(m_window))
  64. {
  65. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  66. glClear(GL_COLOR_BUFFER_BIT);
  67. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  68. glClear(GL_COLOR_BUFFER_BIT);
  69. shader.use();
  70. glBindTexture(GL_TEXTURE_2D, fbo_texture);
  71. glUniform1i(uniform_fbo_texture, /*GL_TEXTURE*/0);
  72. glEnableVertexAttribArray(attribute_v_coord_postproc);
  73. glBindBuffer(GL_ARRAY_BUFFER, vbo_fbo_vertices);
  74. glVertexAttribPointer(attribute_v_coord_postproc, 2, GL_FLOAT, GL_FALSE, 0, 0);
  75. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  76. glDisableVertexAttribArray(attribute_v_coord_postproc);
  77. glfwSwapBuffers(m_window);
  78. glfwPollEvents();
  79. }
  80.  
  81. glDeleteRenderbuffersEXT(1, &rbo_depth);
  82. glDeleteTextures(1, &fbo_texture);
  83. glDeleteFramebuffersEXT(1, &fbo);
  84. glDeleteBuffers(1, &vbo_fbo_vertices);
  85. glDeleteProgram(shader.program());
  86. glfwDestroyWindow(m_window);
  87. glfwTerminate();
  88. exit(EXIT_SUCCESS);
  89. }
  90.  
  91. void callbackError(int error, const char* description)
  92. {
  93. fputs(description, stderr);
  94. }
  95.  
  96. void callbackKey(GLFWwindow* window, int key, int scancode, int action, int mods)
  97. {
  98. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  99. glfwSetWindowShouldClose(window, GL_TRUE);
  100. }
  101.  
  102. void callbackFramebufferSize(GLFWwindow* window, int width, int height)
  103. {
  104. m_width = width; m_height = height;
  105. glBindTexture(GL_TEXTURE_2D, fbo_texture);
  106. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  107. glBindTexture(GL_TEXTURE_2D, 0);
  108. }
  109.  
  110. void init()
  111. {
  112. glfwSetErrorCallback(callbackError);
  113. if (!glfwInit()) exit(EXIT_FAILURE);
  114. m_width = 800; m_height = 600;
  115. m_window = glfwCreateWindow(m_width, m_height, "Framebuffer Test", NULL, NULL);
  116. if (!m_window) { glfwTerminate(); exit(EXIT_FAILURE); }
  117. glfwMakeContextCurrent(m_window);
  118. glfwSwapInterval(0);
  119. glfwSetKeyCallback(m_window, callbackKey);
  120. glfwSetFramebufferSizeCallback(m_window, callbackFramebufferSize);
  121. glewExperimental = GL_TRUE;
  122. if (glewInit() != GLEW_OK) std::cout << "GLEW Init Error" << std::endl;
  123. glClearColor(0.2, 0.3, 0.4, 1.0);
  124. glEnable(GL_BLEND);
  125. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  126. }
  127.  
  128. attribute vec2 v_coord;
  129. uniform sampler2D fbo_texture;
  130. varying vec2 f_texcoord;
  131.  
  132. void main(void)
  133. {
  134. gl_Position = vec4(v_coord, 0.0, 1.0);
  135. f_texcoord = (v_coord + 1.0) / 2.0;
  136. }
  137.  
  138. uniform sampler2D fbo_texture;
  139. varying vec2 f_texcoord;
  140.  
  141. void main(void)
  142. {
  143. gl_FragColor = texture2D(fbo_texture, f_texcoord);
  144. if (f_texcoord.x < 0.2)
  145. gl_FragColor *= vec4(1.0, 2.0, 1.0, 1.0);
  146. else if (f_texcoord.x > 0.8)
  147. gl_FragColor *= vec4(1.0, 2.0, 2.0, 1.0);
  148. else gl_FragColor *= vec4(2.0, 1.0, 2.0, 1.0);
  149. }
  150.  
  151. glGenFramebuffers(1, &fbo);
  152. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  153. glEnable(GL_TEXTURE_2D);
  154. glGenTextures(1, &fbo_texture);
  155. glBindTexture(GL_TEXTURE_2D, fbo_texture);
  156. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  157. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  158. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  159. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_texture, 0);
  160.  
  161. glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement