GL30 库中不存在 glGenVertexArrays()。 (JAVA 刘文杰)

glGenVertexArrays() does not exist in the GL30 library. (JAVA LWJGL)

我正在尝试按照 this OpenGL 教程来渲染一个简单的三角形。在教程开始时,在“The VAO”部分下,我被告知要编写以下代码:

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

此 C 代码段的 Java 代码是

int VertexArrayID;
glGenVertexArrays(1, VertexArrayID);
glBindVertexArray(VertexArrayID);

the documentation中说明我导入的GL30class包含方法glGenVertexArray()和glBindVertexArray()。然而 IntelliJ 未能将此视为有效方法。

我的导入:

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

我的 GLFW Window 提示:

glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // For macOS
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

我正在使用 Maven 作为我的依赖项,在我的 pom.xml 中,我正在使用 LWJGL customizer.

中的预设“Everything”

请注意,在 C 代码段中,它是指向正在传递给 glGenVertexArrays 的 int 变量的 指针 - 您正在传递应使用生成的 ID 值填充的内存地址.

在 Java 中,您没有相同的基本类型指针概念 - 在您的代码片段中,您传递的是整数 ,而不是指针。 您尝试使用的 API class 没有 glGenVertexArrays(int, int) 方法,因此您的 IDE 正在抱怨。

而是使用 glGenVertexArrays(int[])glGenVertexArrays(IntBuffer),例如:

int[] vertexArrayIDs = new int[1]; // create an array where the generated ids will be stored
glGenVertexArrays(vertexArrayIDs); // no need to say how many IDs we want - it's implicit in the array length
glBindVertexArray(vertexArrayIDs[0]) // access the array to get the generated ID;

你的 IDE 没有“看到” glGenVertexArrays 方法的原因是因为你没有 import static GL30 静态方法,就像你对GL11 class.

为了在您的 class 范围内解析 class GL30 的静态方法,您还需要 import static GL30 与:

import static org.lwjgl.opengl.GL30.*;

此外,从版本 3.2.0 of LWJGL 3 开始,版本 classes,如 GL11、GL12、GL13、GL20 等。继承 来自各自的以前的版本。

因此,当您至少使用 LWJGL 3.2.0 时,您可以删除 import static org.lwjgl.opengl.GL11.*; 行并且 只有 import static org.lwjgl.opengl.GL30.*;.

并且,由于您从 GLFW 请求 OpenGL 3.3 版,并且还使用了 core 配置文件,因此您也可以使用 import static org.lwjgl.opengl.GL33C.*; 来仅拥有 OpenGL 核心功能可见(而不是来自兼容性配置文件的功能 - 您没有请求,因此在运行时将不可用)。这样,您就不会不小心使用 deprecated/compatibility 函数。