OpenGL glfw无法绘制点数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenGL glfw无法绘制点数相关的知识,希望对你有一定的参考价值。
我按照教程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" << std::endl;
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" << std::endl;
glfwTerminate();
return false;
}
glfwMakeContextCurrent(gWindow);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
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<CL_KERNEL_WORK_GROUP_SIZE>(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();
答案
您的代码的一个问题是您告诉GLFW创建OpenGL 3.3核心配置文件。核心配置文件上下文不支持传统API,例如客户端顶点数组(glVertexPointer
,glEnableClientState
等)。假设没有其他问题,你应该能够通过将GLFW_OPENGL_CORE_PROFILE
更改为GLFW_OPENGL_COMPAT_PROFILE
来实现它,或者删除对glfwWindowHint
的所有调用。
以上是关于OpenGL glfw无法绘制点数的主要内容,如果未能解决你的问题,请参考以下文章
OpenGL代码适用于GLFW,但不适用于Qt OpenGL