我按照教程OpenCL path tracing tutorial 3: OpenGL viewport, interactive camera and defocus blur。当我尝试将所有过剩的东西切换到GLFW时,他使用GLUT生成一个窗口。但是glfw未能在vbo中得分,我不知道为什么。窗口可以设置为某种颜色但不会出现任何点。
这是我的draw()函数:
glClearColor(0.0f, 0.0f, 0.5f, 1.0f); // blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0f, image_width, 0.0f, image_height);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 16, 0); // size [2, 3, 4], type, stride, pointer
glColorPointer(4, GL_UNSIGNED_BYTE, 16, (GLvoid*) 8); // size [3, 4], type, stride, pointer
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_POINTS, 0, image_width * image_height);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glfwSwapBuffers(gWindow);
用于GLFW渲染循环:
cpu_spheres[6].position.s[1] += 0.01;
queue.enqueueWriteBuffer(cl_spheres, CL_TRUE, 0, num_sphere*sizeof(mySphere), cpu_spheres);
// args order in kernel is diff from tutorial
kernel.setArg(4, cl_spheres);
kernel.setArg(5, myHash(num_frame));
runKernel();
draw(); // cannot draw points
glfwSwapBuffers(gWindow);
这是我的OpenGL初始化代码:
if (!glfwInit()) {
std::cerr <<"GLFW initialization failed" < return false;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
gWindow = glfwCreateWindow(image_width, image_height, APP_TITLE, NULL, NULL);
if (gWindow == NULL) {
std::cerr <<"Failed to create GLFW window" < glfwTerminate();
return false;
}
glfwMakeContextCurrent(gWindow);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr <<"Failed to initialize GLEW" < return false;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0f, image_width, 0.0f, image_height);
runKernel与教程中的完全相同:
std::size_t global_work_size = image_width * image_height;
std::size_t local_work_size = kernel.getWorkGroupInfo(device);
if (global_work_size % local_work_size != 0)
global_work_size = (global_work_size / local_work_size + 1) * local_work_size;
// Make sure OpenGL is done using VBOs
glFinish();
queue.enqueueAcquireGLObjects(&cl_vbos);
queue.finish();
// Launch the kernel
queue.enqueueNDRangeKernel(kernel, cl::NullRange, global_work_size, local_work_size);
queue.finish();
// Release VBOs so OpenGL can use them
queue.enqueueReleaseGLObjects(&cl_vbos);
queue.finish();