Java lwjgl 使用 VAO 的现代 OpenGL 访问冲突异常
Java lwjgl Modern OpenGL Access Violation Exception using VAOs
我目前正在学习有关使用 VAO 和 VBOS 进行渲染的 ThinMatrix OpenGL 教程。我几乎完全复制了代码(唯一的区别是我将工厂 class 设置为静态而不是正常使用)。我和他的程序版本之间唯一的技术差异是我使用的是 lwjgl 3 而不是 lwjgl 2。
这是我的源代码:
/*************
MAIN
*************/
import Render.ModelLoader;
import Render.Render;
import Render.RawModel;
import org.lwjgl.Version;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Main {
private static long window;
private static ModelLoader modelLoader;
private static Render renderer;
private static final int WIDTH = 1280;
private static final int HEIGHT = 720;
public static void main(String[] args) {
float[] vertices = {
//Bottom left triangle
-.5f,.5f,0f,
-.5f,-.5f,0f,
.5f,-.5f,0f,
//Top right triangle
.5f,-.5f,0f,
.5f,.5f,0f,
-.5f,.5f,0f
};
RawModel model = modelLoader.loadToVAO(vertices);
initApp();
//Main Loop
while (!glfwWindowShouldClose(window)){
glfwPollEvents();
renderer.prepare();
renderer.render(model);
glfwSwapBuffers(window);
}
cleanUp();
}
public static void initApp(){
System.out.println("LWJGL " + Version.getVersion());
//Set the error callback routine to use System.err
GLFWErrorCallback.createPrint(System.err).set();
//Init GLFW
if (!glfwInit()){
throw new IllegalStateException("Could not initialise GLFW.");
}
//Create the window
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE,GLFW_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, "My Display", NULL, NULL);
if (window == NULL){
throw new IllegalStateException("Could not create window");
}
//Center the window
GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidMode.width()-WIDTH)/2, (vidMode.height()-HEIGHT)/2);
//Link window's context to current thread
glfwMakeContextCurrent(window);
//VSync
glfwSwapInterval(1);
glfwShowWindow(window);
GL.createCapabilities();
System.out.println("OpenGL " + glGetString(GL_VERSION));
}
public static void cleanUp(){
modelLoader.cleanUp();
Callbacks.glfwFreeCallbacks(window); //Release callbacks
glfwDestroyWindow(window); //Destroy the window
glfwTerminate(); //Terminate GLFW
glfwSetErrorCallback(null).set();
}
}
/*************
RAWMODEL
*************/
package Render;
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;
}
}
/*************
MODELLOADER
*************/
package Render;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class ModelLoader {
private static List<Integer> vaos = new ArrayList<>();
private static List<Integer> vbos = new ArrayList<>();
public static RawModel loadToVAO(float[] positions){
int vaoID = createVAO();
vaos.add(vaoID);
storeDataInAttribList(0,positions);
unbindVAO();
return new RawModel(vaoID, positions.length/3);
}
private static int createVAO(){
int vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
return vaoID;
}
private static void storeDataInAttribList(int attribNum, float[] data){
int vboID = glGenBuffers();
vbos.add(vboID);
glBindBuffer(GL_ARRAY_BUFFER,vboID);
FloatBuffer buffer = storeDataInFB(data);
glBufferData(GL_ARRAY_BUFFER,buffer,GL_STATIC_DRAW);
glVertexAttribPointer(attribNum,3,GL_FLOAT,false,0,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
private static FloatBuffer storeDataInFB(float[] data){
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
private static void unbindVAO(){
glBindVertexArray(0);
}
public static void cleanUp(){
for (int vao:vaos) {
glDeleteVertexArrays(vao);
}
for (int vbo:vbos) {
glDeleteBuffers(vbo);
}
}
}
/*************
RENDER
*************/
package Render;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class Render {
public static void prepare(){
glClearColor(0.0f,0.4f,0.6f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
public static void render(RawModel model){
glEnableClientState(GL_VERTEX_ARRAY);
glBindVertexArray(model.getVaoID());
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0,model.getVertexCount());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
我遇到的问题是,每当我尝试 运行 项目时,它就会崩溃并显示以下错误消息:
Connected to the target VM, address: '127.0.0.1:49390', transport: 'socket'
A fatal error has been detected by the Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffffa09862d, pid=12556, tid=0x00000000000020ac
JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode windows-amd64 compressed oops)
Problematic frame:
C [lwjgl_opengl.dll+0x862d]
Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
An error report file with more information is saved as:
C:\Users\TokenGuard\IdeaProjects\ModernOpenGLTutorial\hs_err_pid12556.log
If you would like to submit a bug report, please visit:
http://bugreport.java.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.
Disconnected from the target VM, address: '127.0.0.1:49390', transport: 'socket'
Process finished with exit code 1
环顾四周,我发现了这个 Whosebug post: ,它似乎有同样的问题,而且该问题的 OP 实际上 post 提供了解决方案。
我使用的是 AMD 显卡,但我仍然尝试查看它是否解决了我的问题,但是它什么也没做,因为它仍然出现相同的错误消息。
在其中放置一些断点 我发现问题出在 createVAO()
方法上,更具体地说,由于某种原因,对 glGenVertexArrays()
的调用失败了。我已经尝试明确告诉 glfw 使用 OpenGL 3.0 但它仍然没有帮助。
在这一点上我完全没有想法。
关于我应该做什么的任何指导?
问题是这样的:
RawModel model = modelLoader.loadToVAO(vertices);
initApp();
你需要翻转它:
initApp();
RawModel model = modelLoader.loadToVAO(vertices)
问题在于,当您调用 modelLoader.loadToVAO(vertices)
时,它会调用 glGenVertexArrays()
(如您所见)。然而,此时您还没有设置当前上下文。您在 initApp()
中使用 glfwMakeContextCurrent()
.
执行的操作
在调用任何 OpenGL 函数之前必须设置当前上下文。
我目前正在学习有关使用 VAO 和 VBOS 进行渲染的 ThinMatrix OpenGL 教程。我几乎完全复制了代码(唯一的区别是我将工厂 class 设置为静态而不是正常使用)。我和他的程序版本之间唯一的技术差异是我使用的是 lwjgl 3 而不是 lwjgl 2。
这是我的源代码:
/*************
MAIN
*************/
import Render.ModelLoader;
import Render.Render;
import Render.RawModel;
import org.lwjgl.Version;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Main {
private static long window;
private static ModelLoader modelLoader;
private static Render renderer;
private static final int WIDTH = 1280;
private static final int HEIGHT = 720;
public static void main(String[] args) {
float[] vertices = {
//Bottom left triangle
-.5f,.5f,0f,
-.5f,-.5f,0f,
.5f,-.5f,0f,
//Top right triangle
.5f,-.5f,0f,
.5f,.5f,0f,
-.5f,.5f,0f
};
RawModel model = modelLoader.loadToVAO(vertices);
initApp();
//Main Loop
while (!glfwWindowShouldClose(window)){
glfwPollEvents();
renderer.prepare();
renderer.render(model);
glfwSwapBuffers(window);
}
cleanUp();
}
public static void initApp(){
System.out.println("LWJGL " + Version.getVersion());
//Set the error callback routine to use System.err
GLFWErrorCallback.createPrint(System.err).set();
//Init GLFW
if (!glfwInit()){
throw new IllegalStateException("Could not initialise GLFW.");
}
//Create the window
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE,GLFW_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, "My Display", NULL, NULL);
if (window == NULL){
throw new IllegalStateException("Could not create window");
}
//Center the window
GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidMode.width()-WIDTH)/2, (vidMode.height()-HEIGHT)/2);
//Link window's context to current thread
glfwMakeContextCurrent(window);
//VSync
glfwSwapInterval(1);
glfwShowWindow(window);
GL.createCapabilities();
System.out.println("OpenGL " + glGetString(GL_VERSION));
}
public static void cleanUp(){
modelLoader.cleanUp();
Callbacks.glfwFreeCallbacks(window); //Release callbacks
glfwDestroyWindow(window); //Destroy the window
glfwTerminate(); //Terminate GLFW
glfwSetErrorCallback(null).set();
}
}
/*************
RAWMODEL
*************/
package Render;
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;
}
}
/*************
MODELLOADER
*************/
package Render;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class ModelLoader {
private static List<Integer> vaos = new ArrayList<>();
private static List<Integer> vbos = new ArrayList<>();
public static RawModel loadToVAO(float[] positions){
int vaoID = createVAO();
vaos.add(vaoID);
storeDataInAttribList(0,positions);
unbindVAO();
return new RawModel(vaoID, positions.length/3);
}
private static int createVAO(){
int vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
return vaoID;
}
private static void storeDataInAttribList(int attribNum, float[] data){
int vboID = glGenBuffers();
vbos.add(vboID);
glBindBuffer(GL_ARRAY_BUFFER,vboID);
FloatBuffer buffer = storeDataInFB(data);
glBufferData(GL_ARRAY_BUFFER,buffer,GL_STATIC_DRAW);
glVertexAttribPointer(attribNum,3,GL_FLOAT,false,0,0);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
private static FloatBuffer storeDataInFB(float[] data){
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
private static void unbindVAO(){
glBindVertexArray(0);
}
public static void cleanUp(){
for (int vao:vaos) {
glDeleteVertexArrays(vao);
}
for (int vbo:vbos) {
glDeleteBuffers(vbo);
}
}
}
/*************
RENDER
*************/
package Render;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class Render {
public static void prepare(){
glClearColor(0.0f,0.4f,0.6f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
public static void render(RawModel model){
glEnableClientState(GL_VERTEX_ARRAY);
glBindVertexArray(model.getVaoID());
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0,model.getVertexCount());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
我遇到的问题是,每当我尝试 运行 项目时,它就会崩溃并显示以下错误消息:
Connected to the target VM, address: '127.0.0.1:49390', transport: 'socket'
A fatal error has been detected by the Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffffa09862d, pid=12556, tid=0x00000000000020ac
JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode windows-amd64 compressed oops)
Problematic frame:
C [lwjgl_opengl.dll+0x862d]
Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
An error report file with more information is saved as:
C:\Users\TokenGuard\IdeaProjects\ModernOpenGLTutorial\hs_err_pid12556.log
If you would like to submit a bug report, please visit:
http://bugreport.java.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.
Disconnected from the target VM, address: '127.0.0.1:49390', transport: 'socket'
Process finished with exit code 1
环顾四周,我发现了这个 Whosebug post:
我使用的是 AMD 显卡,但我仍然尝试查看它是否解决了我的问题,但是它什么也没做,因为它仍然出现相同的错误消息。
在其中放置一些断点 我发现问题出在 createVAO()
方法上,更具体地说,由于某种原因,对 glGenVertexArrays()
的调用失败了。我已经尝试明确告诉 glfw 使用 OpenGL 3.0 但它仍然没有帮助。
在这一点上我完全没有想法。 关于我应该做什么的任何指导?
问题是这样的:
RawModel model = modelLoader.loadToVAO(vertices);
initApp();
你需要翻转它:
initApp();
RawModel model = modelLoader.loadToVAO(vertices)
问题在于,当您调用 modelLoader.loadToVAO(vertices)
时,它会调用 glGenVertexArrays()
(如您所见)。然而,此时您还没有设置当前上下文。您在 initApp()
中使用 glfwMakeContextCurrent()
.
在调用任何 OpenGL 函数之前必须设置当前上下文。