Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- glGenTransformFeedbacks(1, &transformFeedbackObject);
- glGenVertexArrays(1, &tfVertexArray);
- glGenBuffers(1, &tfVertexBuffer);
- glGenBuffers(1, &tfVolumeOutputBuffer);
- glGenBuffers(1, &tfVoxelBuffer);
- glGenBuffers(1, &tfAtomBuffer);
- glBindVertexArray(tfVertexArray);
- glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedbackObject);
- // Assume we have the volume size at this point
- long numVoxels = dimensions[0] * dimensions[1] * dimensions[2] * 3;
- GLfloat *voxelData = (GLfloat*)malloc(sizeof(GLfloat) * numVoxels);
- GLint *voxelIndexData = (GLint*)malloc(sizeof(GLint) * numVoxels);
- int index = 0;
- NSLog(@"Creating voxel indices");
- for(int i = 0; i < dimensions[0]; ++i)
- {
- for(int j = 0; j < dimensions[1]; ++j)
- {
- for(int k = 0; k < dimensions[2]; ++k)
- {
- voxelData[index * 3 + 0] = (minDims[0] * (float)i / kVolumeMultiplierF);
- voxelData[index * 3 + 1] = (minDims[1] * (float)j / kVolumeMultiplierF);
- voxelData[index * 3 + 2] = (minDims[2] * (float)k / kVolumeMultiplierF);
- voxelIndexData[index * 3 + 0] = i;
- voxelIndexData[index * 3 + 1] = j;
- voxelIndexData[index * 3 + 2] = k;
- ++index;
- }
- }
- }
- NSLog(@"Buffering voxel indices");
- glBindBuffer(GL_ARRAY_BUFFER, tfVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVoxels, voxelData, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, tfVoxelBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLint) * numVoxels, voxelIndexData, GL_STATIC_DRAW);
- tfNumVoxels = (GLuint)(numVoxels / 3);
- // Now, create a buffer with all the atom info - that is, location and radius
- GLfloat* atomData = (GLfloat*)malloc(sizeof(GLfloat) * [crystal.atoms count] * 4);
- int atomIndex = 0;
- CMVectorRec atomOrtho;
- NSLog(@"Creating atom info buffer");
- for(CMAtom* atom in crystal.atoms)
- {
- atomOrtho = atom.orthoCoordinates;
- atomData[atomIndex++] = (GLfloat)atomOrtho.x;
- atomData[atomIndex++] = (GLfloat)atomOrtho.y;
- atomData[atomIndex++] = (GLfloat)atomOrtho.z;
- atomData[atomIndex++] = (GLfloat)atom.radius;
- }
- glBindBuffer(GL_ARRAY_BUFFER, tfAtomBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * [crystal.atoms count], atomData, GL_STATIC_DRAW);
- glGenTextures(1, &tfAtomTexture);
- glActiveTexture(GL_TEXTURE8);
- glBindTexture(GL_TEXTURE_BUFFER, tfAtomTexture);
- glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, tfAtomBuffer);
- glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tfVolumeOutputBuffer);
- glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(GLuint) * dimensions[0] * dimensions[1] * dimensions[2], NULL, GL_DYNAMIC_COPY);
- NSLog(@"Re-linking");
- NSArray *varyings = [[NSArray alloc] initWithObjects:@"result", nil];
- GLint shader = [crystal.graphics compileShaderProgram:@"volumeGenShader" withVaryings:varyings];
- char buff[256];
- GLsizei length;
- GLsizei size;
- GLenum type;
- glGetTransformFeedbackVarying(shader, 0, 256, &length, &size, &type, buff);
- glUseProgram(shader);
- GLint prog;
- glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
- if(prog != shader)
- {
- NSLog(@"Not using the shader");
- }
- GLint location = glGetUniformLocation(shader, "atomTexture");
- glUniform1i(location, 8); // tex unit 8
- location = glGetUniformLocation(shader, "numberOfAtoms");
- glUniform1i(location, (GLint)[crystal.atoms count]);
- location = glGetAttribLocation(shader, "voxelPosition");
- if(location > -1)
- {
- glBindBuffer(GL_ARRAY_BUFFER, tfVertexBuffer);
- glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- glEnableVertexAttribArray(location);
- }
- location = glGetAttribLocation(shader, "voxelIndex");
- if(location > -1)
- {
- glBindBuffer(GL_ARRAY_BUFFER, tfVoxelBuffer);
- glVertexAttribPointer(location, 3, GL_UNSIGNED_INT, GL_FALSE, 0, NULL);
- glEnableVertexAttribArray(location);
- }
- glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfVolumeOutputBuffer);
- GLsizei total = dimensions[0] * dimensions[1] * dimensions[2];
- glEnable(GL_RASTERIZER_DISCARD);
- glBeginTransformFeedback(GL_POINTS);
- glValidateProgram(shader);
- GLint status;
- glGetProgramiv(shader, GL_VALIDATE_STATUS, &status);
- if(status != GL_TRUE)
- {
- NSLog(@"Failed to validate program");
- [CMGraphicsEngine printProgramLog:shader];
- }
- NSLog(@"Trying!");
- glDrawArrays(GL_POINTS, 0, 1);
- glEndTransformFeedback();
- glDisable(GL_RASTERIZER_DISCARD);
- tfResults = (GLint*)malloc(sizeof(GLint) * total);
- glBindBuffer(GL_ARRAY_BUFFER, tfVolumeOutputBuffer);
- glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLint) * total, tfResults);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement