Search code examples
javaopengllwjgl

GL ERROR: 1282 - Invalid operation


My code is this:

   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    glEnable(GL_TEXTURE_2D);

    glBegin(GL_TRIANGLES);

    int bunnyDisplayList = glGenLists(1);
    glNewList(bunnyDisplayList, GL_COMPILE);

    List<Model> models = NPC.getModels();

    glBegin(GL_TRIANGLES);

    for (Model m : models) {
        for (Model.Face face : m.getFaces()) {
            Vector3f n1 = addVectors(m.getNormals().get(face.getNormalIndices()[0] - 1), m.getPosition());
            glNormal3f(n1.x, n1.y, n1.z);
            Vector3f v1 = addVectors(m.getVertices().get(face.getVertexIndices()[0] - 1), m.getPosition());
            glVertex3f(v1.x, v1.y, v1.z);
            Vector3f n2 = addVectors(m.getNormals().get(face.getNormalIndices()[1] - 1), m.getPosition());
            glNormal3f(n2.x, n2.y, n2.z);
            Vector3f v2 = addVectors(m.getVertices().get(face.getVertexIndices()[1] - 1), m.getPosition());
            glVertex3f(v2.x, v2.y, v2.z);
            Vector3f n3 = addVectors(m.getNormals().get(face.getNormalIndices()[2] - 1), m.getPosition());
            glNormal3f(n3.x, n3.y, n3.z);
            Vector3f v3 = addVectors(m.getVertices().get(face.getVertexIndices()[2] - 1), m.getPosition());
            glVertex3f(v3.x, v3.y, v3.z);
        }
    }
    glEnd();
    glEndList();

The models get drawn(but in a transparent way), but every time I call this function I get the following error:

GL ERROR: 1282 - Invalid operation

Solution

  • You are calling glBegin(GL_TRIANGLES); twice. Remove the second statement.