glDrawElements成功,但glDrawElementsInstance失败,GL_INVALID_OPERATION

时间:2018-05-10 10:21:54

标签: c++ opengl

我正在将桌面应用程序的3D图形引擎从兼容性配置文件迁移到核心配置文件。由于需要覆盖很多代码,我在兼容性配置文件中逐个更改渲染器,当所有代码都被转换时,我计划将配置文件切换到核心。

为了做到这一点,我正在编写一个小的渲染测试,以确保我了解如何在兼容性配置文件之上使用OpenGL 4.3的高级功能。

我写了以下初始化代码:

glGenVertexArrays (1, &_vertexArrayObjectID);
glBindVertexArray (_vertexArrayObjectID);

glGenBuffers (1, &_vertexBufferObjectID);
glGenBuffers (1, &_indexBufferObjectID);
glGenBuffers (1, &_instanceBufferID);

glBindBuffer (GL_ARRAY_BUFFER, _vertexBufferObjectID);
glBufferData (GL_ARRAY_BUFFER, sizeof (Verts), Verts, GL_STATIC_DRAW);

glVertexAttribPointer ((GLuint) 0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray (0);

glBindBuffer (GL_ARRAY_BUFFER, _instanceBufferID);
glBufferData (GL_ARRAY_BUFFER, sizeof (Centers), Centers, GL_STATIC_DRAW);

glVertexAttribPointer ((GLuint) 1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray (1);
glVertexAttribDivisor (1, 1);

glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (Faces), Faces, GL_STATIC_DRAW);

// creates the shader objects and program and links it in the current VAO scope
generateShaderProgram (); 

glBindVertexArray (0);

我的渲染功能是:

glBindVertexArray (_vertexArrayObjectID);

// Load the shader into the rendering pipeline 
glUseProgram (_program);

// Render the scene:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawElements (GL_LINE_LOOP, _indexCount, GL_UNSIGNED_INT, 0);

// Cleanup all the things we bound 
glUseProgram (0);
glBindVertexArray (0);

此代码正常运行,没有任何错误。 但是,当我将绘图线(不更改任何其他内容)更改为:

glDrawElementsInstanced (GL_LINE_LOOP, _indexCount, GL_UNSIGNED_INT, 0, 4);

我在上述命令后立即收到GL_INVALID_OPERATION(1282)错误。

我的着色器程序是:

// vertex shader
#version 400                        
in layout(location=0) vec3 Position; 
in layout(location=1) vec3 Center; 
out vec3 theColor;                  
void main () {                      
    vec4 v = vec4(Position, 1.0);   
    vec4 offset = vec4(Center, 1.0);    
    mat4 transform = mat4(vec4(0.1,0.0,0.0,0.0),vec4(0.0,0.1,0.0,0.0),vec4(0.0,0.0,0.1,0.0),offset); "
    gl_Position = transform * v;   
    theColor = Position;            
}

//fragment shader
#version 400                            
in vec3 theColor;                       
out vec4 fragColor;                     
void main () {                          
    fragColor = vec4(theColor, 1.0);    
}

我是否错误地使用了实例渲染?

0 个答案:

没有答案