Advertisement
Guest User

Transform feedback broken

a guest
Feb 16th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. glGenTransformFeedbacks(1, &transformFeedbackObject);
  2. glGenVertexArrays(1, &tfVertexArray);
  3. glGenBuffers(1, &tfVertexBuffer);
  4. glGenBuffers(1, &tfVolumeOutputBuffer);
  5. glGenBuffers(1, &tfVoxelBuffer);
  6. glGenBuffers(1, &tfAtomBuffer);
  7.  
  8. glBindVertexArray(tfVertexArray);
  9. glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedbackObject);
  10.  
  11. // Assume we have the volume size at this point
  12. long numVoxels = dimensions[0] * dimensions[1] * dimensions[2] * 3;
  13. GLfloat *voxelData = (GLfloat*)malloc(sizeof(GLfloat) * numVoxels);
  14. GLint *voxelIndexData = (GLint*)malloc(sizeof(GLint) * numVoxels);
  15. int index = 0;
  16.  
  17. NSLog(@"Creating voxel indices");
  18.  
  19. for(int i = 0; i < dimensions[0]; ++i)
  20. {
  21. for(int j = 0; j < dimensions[1]; ++j)
  22. {
  23. for(int k = 0; k < dimensions[2]; ++k)
  24. {
  25. voxelData[index * 3 + 0] = (minDims[0] * (float)i / kVolumeMultiplierF);
  26. voxelData[index * 3 + 1] = (minDims[1] * (float)j / kVolumeMultiplierF);
  27. voxelData[index * 3 + 2] = (minDims[2] * (float)k / kVolumeMultiplierF);
  28.  
  29. voxelIndexData[index * 3 + 0] = i;
  30. voxelIndexData[index * 3 + 1] = j;
  31. voxelIndexData[index * 3 + 2] = k;
  32. ++index;
  33. }
  34. }
  35. }
  36. NSLog(@"Buffering voxel indices");
  37.  
  38. glBindBuffer(GL_ARRAY_BUFFER, tfVertexBuffer);
  39. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVoxels, voxelData, GL_STATIC_DRAW);
  40. glBindBuffer(GL_ARRAY_BUFFER, tfVoxelBuffer);
  41. glBufferData(GL_ARRAY_BUFFER, sizeof(GLint) * numVoxels, voxelIndexData, GL_STATIC_DRAW);
  42.  
  43. tfNumVoxels = (GLuint)(numVoxels / 3);
  44.  
  45. // Now, create a buffer with all the atom info - that is, location and radius
  46. GLfloat* atomData = (GLfloat*)malloc(sizeof(GLfloat) * [crystal.atoms count] * 4);
  47.  
  48. int atomIndex = 0;
  49. CMVectorRec atomOrtho;
  50. NSLog(@"Creating atom info buffer");
  51.  
  52. for(CMAtom* atom in crystal.atoms)
  53. {
  54. atomOrtho = atom.orthoCoordinates;
  55.  
  56. atomData[atomIndex++] = (GLfloat)atomOrtho.x;
  57. atomData[atomIndex++] = (GLfloat)atomOrtho.y;
  58. atomData[atomIndex++] = (GLfloat)atomOrtho.z;
  59. atomData[atomIndex++] = (GLfloat)atom.radius;
  60. }
  61.  
  62. glBindBuffer(GL_ARRAY_BUFFER, tfAtomBuffer);
  63. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * [crystal.atoms count], atomData, GL_STATIC_DRAW);
  64. glGenTextures(1, &tfAtomTexture);
  65. glActiveTexture(GL_TEXTURE8);
  66. glBindTexture(GL_TEXTURE_BUFFER, tfAtomTexture);
  67. glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, tfAtomBuffer);
  68.  
  69. glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tfVolumeOutputBuffer);
  70. glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(GLuint) * dimensions[0] * dimensions[1] * dimensions[2], NULL, GL_DYNAMIC_COPY);
  71.  
  72. NSLog(@"Re-linking");
  73.  
  74. NSArray *varyings = [[NSArray alloc] initWithObjects:@"result", nil];
  75.  
  76. GLint shader = [crystal.graphics compileShaderProgram:@"volumeGenShader" withVaryings:varyings];
  77. char buff[256];
  78. GLsizei length;
  79. GLsizei size;
  80. GLenum type;
  81.  
  82. glGetTransformFeedbackVarying(shader, 0, 256, &length, &size, &type, buff);
  83.  
  84. glUseProgram(shader);
  85. GLint prog;
  86. glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
  87. if(prog != shader)
  88. {
  89. NSLog(@"Not using the shader");
  90. }
  91. GLint location = glGetUniformLocation(shader, "atomTexture");
  92. glUniform1i(location, 8); // tex unit 8
  93.  
  94. location = glGetUniformLocation(shader, "numberOfAtoms");
  95. glUniform1i(location, (GLint)[crystal.atoms count]);
  96.  
  97. location = glGetAttribLocation(shader, "voxelPosition");
  98. if(location > -1)
  99. {
  100. glBindBuffer(GL_ARRAY_BUFFER, tfVertexBuffer);
  101. glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  102. glEnableVertexAttribArray(location);
  103. }
  104.  
  105. location = glGetAttribLocation(shader, "voxelIndex");
  106. if(location > -1)
  107. {
  108. glBindBuffer(GL_ARRAY_BUFFER, tfVoxelBuffer);
  109. glVertexAttribPointer(location, 3, GL_UNSIGNED_INT, GL_FALSE, 0, NULL);
  110. glEnableVertexAttribArray(location);
  111. }
  112.  
  113.  
  114. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfVolumeOutputBuffer);
  115.  
  116. GLsizei total = dimensions[0] * dimensions[1] * dimensions[2];
  117.  
  118. glEnable(GL_RASTERIZER_DISCARD);
  119. glBeginTransformFeedback(GL_POINTS);
  120.  
  121. glValidateProgram(shader);
  122. GLint status;
  123. glGetProgramiv(shader, GL_VALIDATE_STATUS, &status);
  124. if(status != GL_TRUE)
  125. {
  126. NSLog(@"Failed to validate program");
  127. [CMGraphicsEngine printProgramLog:shader];
  128. }
  129.  
  130. NSLog(@"Trying!");
  131. glDrawArrays(GL_POINTS, 0, 1);
  132. glEndTransformFeedback();
  133. glDisable(GL_RASTERIZER_DISCARD);
  134.  
  135. tfResults = (GLint*)malloc(sizeof(GLint) * total);
  136. glBindBuffer(GL_ARRAY_BUFFER, tfVolumeOutputBuffer);
  137. glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLint) * total, tfResults);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement