调用glTexImage2D时LWJGL3 EXCEPTION_ACCESS_VIOLATION

时间:2019-05-01 13:47:32

标签: java opengl lwjgl access-violation texture2d

我试图使用lwjgl3加载并显示一个简单的纹理,但是每当我调用glTexImage2D时,我都会得到一个EXCEPTION_ACCESS_VIOLATION。

我举了一个小例子,将加载代码替换为红色的8x8纹理:

Texture.java:

package tabaqui.lwjgl;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.GL11;

public class Texture {

    private int id;
    private int w;
    private int h;

    public Texture(ByteBuffer data, int w, int h) {
        this.w = w;
        this.h = h;
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        id = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, w, h, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
    }

    public int getId() {
        return id;
    }

    public int getW() {
        return w;
    }

    public int getH() {
        return h;
    }
}

Game.java:

package tabaqui;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

import tabaqui.lwjgl.Texture;

public class Game {

    private long id;

    private Texture t;

    public static final String TITLE = "Texture test";
    public static final int W = 640;
    public static final int H = 480;

    public void create() {
        int w = 8;
        int h = 8;
        ByteBuffer bb = ByteBuffer.allocate(w * h * 4);
        for (int y = 0; y < h; y++)
            for (int x = 0; x < w; x++) {
                bb.put((byte)0xff); // R
                bb.put((byte)0x00); // G
                bb.put((byte)0x00); // B
                bb.put((byte)0xff); // A
            }
        bb.flip();
        t = new Texture(bb, w, h);
    }

    public void render() {
        // TODO
    }

    public void dispose() {
        GL11.glDeleteTextures(t.getId());
    }

    public void start() {
        // GLFW init
        GLFWErrorCallback.createPrint(System.err).set();
        if (!GLFW.glfwInit())
            throw new RuntimeException("[GLFW] glfwInit error");
        GLFW.glfwDefaultWindowHints();
        GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
        GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE);
        id = GLFW.glfwCreateWindow(W, H, TITLE, MemoryUtil.NULL, MemoryUtil.NULL);
        if (id == MemoryUtil.NULL)
            throw new RuntimeException("[GLFW] glfwCreateWindow error");
        try (MemoryStack stack = MemoryStack.stackPush()) {
            IntBuffer pw = stack.mallocInt(1);
            IntBuffer ph = stack.mallocInt(1);
            GLFW.glfwGetWindowSize(id, pw, ph);
            GLFWVidMode vm = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
            GLFW.glfwSetWindowPos(id, (vm.width() - pw.get(0)) / 2, (vm.height() - ph.get(0)) / 2);
        }
        GLFW.glfwMakeContextCurrent(id);
        GLFW.glfwSwapInterval(1);
        GLFW.glfwShowWindow(id);
        // OpenGL init
        GL.createCapabilities();
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glViewport(0, 0, W, H);
        GL11.glOrtho(0, W, H, 0, 1, -1);
        GL11.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
        create();
        // Game loop
        while (!GLFW.glfwWindowShouldClose(id)) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            render();
            GLFW.glfwSwapBuffers(id);
            GLFW.glfwPollEvents();
        }
        dispose();
    }

    public static void main(String[] args) {
        new Game().start();
    }
}

每当我运行程序时,我都会收到以下消息:

Java运行时环境检测到致命错误:

EXCEPTION_ACCESS_VIOLATION(0xc0000005)at pc = 0x00000000644c34b9,pid = 27816,tid = 12096

JRE版本:Java(TM)SE运行时环境(11.0.1 + 13)(内部版本11.0.1 + 13-LTS)  Java VM:Java HotSpot(TM)64位服务器VM(11.0.1 + 13-LTS,混合模式,分层,压缩的oops,g1 gc,windows-amd64)  有问题的框架:  C [nvoglv64.DLL + 0xd634b9]

将不写入核心转储。默认情况下,在Windows客户端版本上不启用小型转储

包含更多信息的错误报告文件另存为:  C:\ Users \ Marco \ eclipse-workspace \ GameLw3 \ hs_err_pid27816.log

如果您要提交错误报告,请访问:    http://bugreport.java.com/bugreport/crash.jsp  崩溃发生在使用本机代码的Java虚拟机外部。  请参阅有问题的框架以报告错误。

我想念什么吗?

0 个答案:

没有答案