0

I have an OpenGL 4.1 code using VAO and VBOs. Generation of buffer and array objects happens properly, however as soon as I want to draw my vertices, I get an INVALID OPERATION (code 1282) error. One of the possible explanations is that "the shader is not compiled".

Hence my question: Is it a MUST to write & compile my own vertex and fragment shaders, or can I do without ?

As reference, my code:
Preparing:

glGenVertexArrays(1, vaoID); // Create new VAO
glBindVertexArray(vaoID);

glGenBuffers(1, vboID); // Create new VBO
glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind vbo1 as current vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeOfVertices, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, floatsPerPosition, GL_FLOAT, GL_FALSE, 0, 0);

glGenBuffers(1, eboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeOfIndices, indices, GL_STATIC_DRAW);

// reset bindings for VAO, VBO and EBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Rendering:

glBindVertexArray(vaoID); // No Error
glDrawArrays(GL_TRIANGLES, 0, 6); // Generates Error 1282
glBindVertexArray(0);
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • Great, thanks for your answer ! I will have to look somewhere else then. – Laurent Crivello Dec 01 '16 at 13:05
  • 1
    Modern OpenGL is designed to be used with shaders, at the most a vertex and fragment shaders. You don't need a fancy shader, the basic ones that just forward the values to the GPU will work here. The old fixed function pipeline did this automatically, now it is the programmers job to do this. In Vulkan now you need to manage the memory yourself. More performance always comes with more complexity. – rlam12 Dec 01 '16 at 14:43

2 Answers2

4

In a core OpenGL profile, since 3.2, the use of a program object is not optional.

However, the only shader stage that is manditory is the vertex shader. The use of a fragment shader is optional, but of course, it would only be useful for depth or stencil operations (since it would not generate fragment colors).

In a compatibility OpenGL profile, you can render using the fixed-function pipeline, but you cannot use user-defined vertex attributes. You have to use the fixed-function attributes, set up with glVertexPointer, glColorPointer, etc. Note that NVIDIA plays fast-and-loose with the spec in this regard, mapping user-defined attributes to fixed-function ones.

Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
2

According to the OpenGL specification, a shader must always be used when rendering geometry. When you have no shader enabled, the result of the draw call is undefined.

However, from what I can tell this is a bit more murky in practice. At least most NVidia and Intel drivers seem to come with a kind of "default shader" enabled by default, which inhibits the undefined behaviour.

These shaders have pre-specified vertex attribute input indices (for instance, NVidia uses 0 for vertices, 4 for vertex colours, etc), and implement what you could consider a default pipeline.

Please note here that while these default shaders exist, their implementation can vary significantly between vendors, or might not exist at all for specific vendors or driver versions.

So you should therefore always use a shader when drawing geometry in OpenGL 4.

Bartvbl
  • 2,878
  • 3
  • 28
  • 45