【问题标题】:glGenVertexArrays() does not exist in the GL30 library. (JAVA LWJGL)GL30 库中不存在 glGenVertexArrays()。 (JAVA LWJGL)
【发布时间】:2021-08-30 08:24:49
【问题描述】:

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

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

我的这个 C sn-p 的 Java 代码是

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

the documentation 中,说明我导入的GL30 类包括方法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 窗口提示:

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”。

【问题讨论】:

  • 就是这样 - 非常感谢。有没有办法将其标记为答案?或者,也许您可​​以将其添加为答案,我可以接受?

标签: java intellij-idea opengl lwjgl glfw


【解决方案1】:

请注意,在 C sn-p 中,它是一个指向正在传递给 glGenVertexArrays 的 int 变量的 指针 - 您正在传递应该使用生成的 ID 值填充的内存地址。 p>

在 Java 中,您没有相同的指向原始类型的指针的概念——在您的 sn-p 中,您传递的是整数 ,而不是指针。 您尝试使用的 API 类没有 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;

【讨论】:

  • 感谢您改进我的代码,但该方法仍然不存在 - glGenVertexArrays “无法解析”。
【解决方案2】:

您的 IDE 没有“看到”glGenVertexArrays 方法的原因是因为您没有 import static GL30 静态方法,就像您对 GL11 类所做的那样。

为了使GL30类的静态方法在你的类范围内可解析,你还需要import staticGL30的方法:

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

此外,从版本 3.2.0 of LWJGL 3 开始,版本化类,如 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 核心功能(而不是来自兼容性配置文件 - 您没有请求,因此在运行时不可用)。这样,您就不会意外使用已弃用/兼容功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多