线程“主” org.lwjgl.opengl.OpenGL中的异常:无效的操作(1282)

时间:2018-11-09 03:07:43

标签: java opengl lwjgl

因此,我在Youtube上遵循了本教程,似乎在某个地方出错了。

完全错误:

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
    at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
    at org.lwjgl.opengl.GL11.glDrawArrays(GL11.java:1205)
    at renderEngine.Renderer.render(Renderer.java:21)
    at engineTester.MainGameLoop.main(MainGameLoop.java:33)

代码:

Renderer.java:

package renderEngine;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import renderEngine.RawModel;

public class Renderer {

    public void prepare() {

        GL11.glClearColor(1, 0, 0, 1);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

    }

    public void render(RawModel model) {

        GL30.glBindVertexArray(model.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL11.glDrawArrays(GL11.GL_TRIANGLE, 0, model.getVertexCount());
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);

    }

}

RawModel.java

package renderEngine;

public class RawModel {

    private int vaoID;
    private int vertexCount;

    public RawModel(int vaoID, int vertexCount) {

        this.vaoID = vaoID;
        this.vertexCount = vertexCount;

    }

    public int getVaoID() {

        return vaoID;

    }

    public int getVertexCount() {

        return vertexCount;

    }

}

MainGameLoop.java:

package engineTester;

import org.lwjgl.opengl.Display;
import renderEngine.DisplayManager;
import renderEngine.Loader;
import renderEngine.RawModel;
import renderEngine.Renderer;

public class MainGameLoop {

    public static void main(String[] args) {

    DisplayManager.createDisplay();

    Loader loader = new Loader();
    Renderer renderer = new Renderer(); 

    float[] vertices = {

            -0.5f, 0.5f, 0f,
            -0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            0.5f, 0.5f, 0f,
            -0.5f, 0.5f, 0f

    };

    RawModel model = loader.loadToVao(vertices);

    while(!Display.isCloseRequested()) {
        renderer.prepare();
        renderer.render(model);
        DisplayManager.updateDisplay();
    }

    loader.cleanUp();

    DisplayManager.closeDisplay();

  }

}

DisplayManager.java:

package renderEngine;

import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

public class DisplayManager {

    private static final int WIDTH = 1280;
    private static final int HEIGHT = 720;
    private static final int FPSCAP = 120;

    public static void createDisplay() {

        ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);

        try {
            Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
            Display.create(new PixelFormat(), attribs);
            Display.setTitle("Game");
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        GL11.glViewport(0, 0, WIDTH, HEIGHT);

      }

    public static void updateDisplay() {

        Display.sync(FPSCAP);
        Display.update();

    }

    public static void closeDisplay() {

        Display.destroy();

    }

}

Loader.java:

package renderEngine;

import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;

public class Loader {

    private List<Integer> vaos = new ArrayList<Integer>();
    private List<Integer> vbos = new ArrayList<Integer>();

    public RawModel loadToVao(float[] positions) {

        int vaoID = createVAO();
        storeDataInAttributeList(0,positions);
        unbindVAO();
        return new RawModel(vaoID,positions.length/3);

    }

    public void cleanUp() {

        for(int vao:vaos) {

            GL30.glDeleteVertexArrays(vao);

        }
        for(int vbo:vbos) {

            GL15.glDeleteBuffers(vbo);

        }

    }

    private int createVAO() {

        int vaoID = GL30.glGenVertexArrays();
        vaos.add(vaoID);
        GL30.glBindVertexArray(vaoID);
        return vaoID;

    }

    private void storeDataInAttributeList(int attributeNumber, float[] data) {

        int vboID = GL15.glGenBuffers();
        vbos.add(vboID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
        FloatBuffer buffer = storeDataInFloatBuffer(data);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    }

    private void unbindVAO() {

        GL30.glBindVertexArray(0);

    }

    private FloatBuffer storeDataInFloatBuffer(float[] data) {

        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;

    }

}

我确实看到它告诉我在第21行的渲染器文件中有问题,并且在执行MainGameLoop第33行时遇到了错误

但是除此之外,我无法弄清楚。

我正在使用Java SDK 8 LWJGL 2.8.9(或类似的东西)

0 个答案:

没有答案