gpt4 book ai didi

Java lwjgl 使用 VAO 的现代 OpenGL 访问冲突异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:29 25 4
gpt4 key购买 nike

我目前正在学习有关使用 VAO 和 VBOS 进行渲染的 ThinMatrix OpenGL 教程。我几乎完全复制了代码(唯一的区别是我将工厂类设置为静态而不是正常使用)。在我的程序版本和他的程序版本之间,我能看到的唯一技术差异是我使用的是 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

环顾四周,我发现了这个 stackoverflow 帖子:Java OpenGL EXCEPTION_ACCESS_VIOLATION on glDrawArrays only on NVIDIA ,这似乎有同样的问题,并且该问题的 OP 实际上发布了解决方案。

我使用的是 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 函数之前,您必须设置当前上下文。

关于Java lwjgl 使用 VAO 的现代 OpenGL 访问冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com