LWJGL在调用glDrawElements时崩溃。

huangapple go评论65阅读模式
英文:

LWJGL crashes on glDrawElements

问题

我遇到了这个错误,我不知道可能是什么问题。
所以我正在初始化GLFW,创建OpenGL的功能,加载着色器和VAO到VRAM。

在这一点上,我真的不知道可能是什么。

public static void main(String[] args){
        GLFWErrorCallback.createPrint(System.err).set();
		if(!glfwInit()) {
			assert false : "failed to inialize GLFW";
		}
		
		glfwDefaultWindowHints();
		glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
		glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
		
		long windowID = glfwCreateWindow(800, 600, "title", NULL, NULL);
		if(windowID == NULL) {
			assert false : "Failed to create GLFW-Window";
		}
		
		glfwMakeContextCurrent(windowID);
		glfwSwapInterval(0);
		glfwShowWindow(windowID);
		GL.createCapabilities();
		
		
		Shader shader = new Shader(
				FileReader.readRaw("assets/shaders/basicMesh.vs"),
				FileReader.readRaw("assets/shaders/basicMesh.fs"));
		
		
		int vaoID = glGenVertexArrays();
		glBindVertexArray(vaoID);
		
		int vbo = glGenBuffers();
		glBindBuffer(GL_VERTEX_ARRAY, vbo);
		glBufferData(GL_VERTEX_ARRAY, new float[] {
				0.0f, 0.0f, 0.0f,
				0.0f, 1.0f, 0.0f,
				1.0f, 1.0f, 0.0f,
				1.0f, 0.0f, 0.0f
		}, GL_STATIC_DRAW);
		
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
		
		glBindBuffer(GL_VERTEX_ARRAY, 0);
		
		int ebo = glGenBuffers();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, new int[] {
				0,1,2,
				2,3,0
		}, GL_STATIC_DRAW);
		
		glBindVertexArray(0);
		
		while(!glfwWindowShouldClose(windowID)) {
			
			glfwPollEvents();
			
			glUseProgram(shader.getID());
			
			glBindVertexArray(vaoID);
			// 在这里崩溃!!!
			glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
			
			glBindVertexArray(0);
			
			glUseProgram(0);
			
			glfwSwapBuffers(windowID);
			
		}
}

目前,着色器代码非常简单。为了回答任何问题,这里是代码。

basicMesh.vs

#version 420 core

layout(location=0) in vec3 position;

void main() {
    gl_Position = vec4(position, 1.0);
}

basicMesh.fs

#version 420 core

out vec4 color;

void main(){
    color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}
英文:

I have run into this error and i have no Idea what it could be.
So iam initalizing GLFW, creating the Capability for OpenGL, loading the Shader and the VAO into VRAM.

I really have no Idea what it could be at this point.

public static void main(String[] args){
GLFWErrorCallback.createPrint(System.err).set();
if(!glfwInit()) {
assert false : "failed to inialize GLFW";
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
long windowID = glfwCreateWindow(800, 600, "title", NULL, NULL);
if(windowID == NULL) {
assert false : "Failed to create GLFW-Window";
}
glfwMakeContextCurrent(windowID);
glfwSwapInterval(0);
glfwShowWindow(windowID);
GL.createCapabilities();
Shader shader = new Shader(
FileReader.readRaw("assets/shaders/basicMesh.vs"),
FileReader.readRaw("assets/shaders/basicMesh.fs"));
int vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
int vbo = glGenBuffers();
glBindBuffer(GL_VERTEX_ARRAY, vbo);
glBufferData(GL_VERTEX_ARRAY, new float[] {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f
}, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_VERTEX_ARRAY, 0);
int ebo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, new int[] {
0,1,2,
2,3,0
}, GL_STATIC_DRAW);
glBindVertexArray(0);
while(!glfwWindowShouldClose(windowID)) {
glfwPollEvents();
glUseProgram(shader.getID());
glBindVertexArray(vaoID);
//crahes here!!!
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(windowID);
}
}

Shader code is pretty simple for the Moment. Just to answer any questions here is the Code.

basicMesh.vs

#version 420 core
layout(location=0) in vec3 position;
void main() {
gl_Position = vec4(position, 1.0);
}

basicMesh.fs

#version 420 core
out vec4 color;
void main(){
color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}

答案1

得分: 3

GL_VERTEX_ARRAY 不是有效的缓冲绑定点,例如 glBindBuffer。崩溃的原因是因为您启用了通用顶点属性 0,但没有为其指定任何顶点数据,因为您绑定到了非法的绑定点。glVertexAttribPointer 在寻找绑定到 GL_ARRAY_BUFFER 而不是 GL_VERTEX_ARRAY 的缓冲区。您想要绑定的不是 GL_VERTEX_ARRAY,而是 GL_ARRAY_BUFFER

英文:

GL_VERTEX_ARRAY is not a valid buffer binding point for e.g. glBindBuffer. The reason why it crashes is because you enabled generic vertex attribute 0 without having specified any vertex data for that, since you bind to an illegal binding point. glVertexAttribPointer looks for a buffer bound to GL_ARRAY_BUFFER, not GL_VERTEX_ARRAY.

What you meant to bind to is not GL_VERTEX_ARRAY but GL_ARRAY_BUFFER.

huangapple
  • 本文由 发表于 2020年9月10日 20:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63829797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定