Advertisement
Guest User

Untitled

a guest
May 20th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #ifndef GLUT2_IMAGE_H
  2. #define GLUT2_IMAGE_H
  3.  
  4. #include <GL/glew.h>
  5. #include <GL/gl.h>
  6. #include <helper.h>
  7.  
  8.  
  9. GLuint glob_program;
  10. GLuint glob_texture;
  11. bool once = false;
  12.  
  13. void draw() {
  14.     if (once) {
  15.         return;
  16.     }
  17.     once = true;
  18.     gl(glBindImageTexture(0, glob_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY,
  19.         GL_RGBA16UI));
  20.  
  21.     std::cout << "glob_program is progam "
  22.         << (glIsProgram(glob_program) == GL_TRUE)
  23.         << std::endl;
  24.     GLint uniformTexPosition = gl(glGetUniformLocation(glob_program, "u_image"));
  25.     std::cout << "u_image position " << uniformTexPosition << std::endl;
  26.     gl(glUseProgram(glob_program));
  27.     gl(glUniform1i(uniformTexPosition, 0));
  28.     gl(glDispatchCompute(256, 256, 1));
  29. }
  30.  
  31. void initTextures() {
  32.  
  33.     GLushort texturePtr[256 * 256 * 4];
  34.  
  35.     gl(glGenTextures(1, &glob_texture));
  36.     gl(glBindTexture(GL_TEXTURE_2D, glob_texture));
  37.     gl(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16UI, 256, 256));
  38. }
  39.  
  40. void initShader() {
  41.     const GLchar *compShaderT =
  42.         "#version 440 core\n"
  43.         "layout (location = 1, rgba16ui) uniform uimage2D u_image;\n"
  44.         "uniform atomic_uint u_counter;\n"
  45.         "void main() {\n"
  46.             "uint value = atomicCounterIncrement(u_counter);\n"
  47.             "ivec2 size = imageSize(u_image);\n"
  48.             "if (gl_GlobalInvocationID.x < size.x && gl_GlobalInvocationID.y < size.y)"
  49.             "imageStore(u_image, ivec2(gl_GlobalInvocationID.xy), "
  50.                 "uvec4(vec3(value), 0xffff));"
  51.         "}\n";
  52.  
  53.     GLuint vertexShader = gl(glCreateShader(GL_COMPUTE_SHADER));
  54.     gl(glShaderSource(vertexShader, 1, &compShaderT, nullptr));
  55.     gl(glCompileShader(vertexShader));
  56.  
  57.     glShaderLog(vertexShader);
  58.     glob_program = gl(glCreateProgram());
  59.     gl(glAttachShader(glob_program, vertexShader));
  60.     gl(glLinkProgram(glob_program));
  61.  
  62.     gl(glDeleteShader(vertexShader));
  63.  
  64.     glProgramLog(glob_program);
  65. }
  66.  
  67. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement