在macOS上调用glDrawArrays时得到OpenGL错误GL_INVALID_OPERATION

时间:2016-12-22 02:13:45

标签: macos opengl

我在macOS中遇到了一些关于OGL的问题,如下面的示例,它在我的win系统中运行良好,但在API glPushAttrib glDrawArrays glPopAttrib 并获得 GL_INVALID_OPERATION 。有什么想法吗?

我已经阅读了一些有关该问题的主题,但似乎无法在macOS上运行并与win系统进行比较。 我的mac是OS X El Capitan 10.11.6,带有编译器Xcode版本6.4

//gen a texture
glGenTextures(1, &FilteredTexture);
glBindTexture(GL_TEXTURE_2D, FilteredTexture);
printf("Start glTexImage2D FilteredTexture...");
glTexImage2D(GL_TEXTURE_2D, 0, TexInterFormat, cols, rows, 0, TexFormat, DataType, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  
glBindTexture(GL_TEXTURE_2D, 0);

//gen a Renderbuffer
glGenRenderbuffers(1, &GlRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, GlRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, cols, rows);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

//gen a Framebuffer & attach
glGenFramebuffers(1, &GlFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, GlFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FilteredTexture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, GlRenderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

//gen shaderas program (ignore detail here)
program = loadProgramFromFile("empty.vert", "depth.frag", "quad.geom");

//do the computation
glBindFramebuffer(GL_FRAMEBUFFER, GlFramebuffer);
const static GLuint attachment_bufferss[] = { static_cast<GLuint>(GL_COLOR_ATTACHMENT0) };
glDrawBuffers(1, attachment_bufferss);
glPushAttrib(GL_VIEWPORT_BIT);  //get GL_INVALID_OPERATION here
glViewport(0, 0, cols, rows);
glClearColor(0, 0, 0, 0);      
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(1);
glDrawArrays(GL_POINTS, 0, 1);  //get GL_INVALID_OPERATION here
glDrawBuffers(1, attachment_bufferss);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(0);
glPopAttrib();   //get GL_INVALID_OPERATION here
glFinish();

完整代码可在此处下载:https://1drv.ms/u/s!Au9Donvb28A9hgeor0v94Q5hKc0e

1 个答案:

答案 0 :(得分:0)

发生错误是因为您尝试在Core配置文件中使用已弃用的函数(glPushAttribglPopAttrib)。要解决此问题,请替换

glPushAttrib(GL_VIEWPORT_BIT);

GLint vp [4];
glGetIntegerv (GL_VIEWPORT, vp);

glPopAttrib();

glViewport( vp[0], vp[1], vp[2], vp[3] );

代码首先将当前视口复制到数组中,然后从数组中恢复视口。