【发布时间】:2021-09-06 11:42:44
【问题描述】:
我目前正在尝试使用 LWJGL opengl 库实现一个小地图(基本上只是彼此相邻的彩色像素)。
所以当我尝试使用glBegin(GL_POINTS); 时遇到问题
对于以下命令
glColor3i(stack.stack.get(z).minimapColor.getRed(),stack.stack.get(z).minimapColor.getRed(), stack.stack.get(z).minimapColor.getBlue());
glVertex2i(xStart-Board.map(x-x0), yStart-Board.map(y-y0));
它告诉我我没有上下文或当前上下文中不允许该函数:
FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
at org.lwjgl.opengl.GL11.glBegin(Native Method)
at UI.Minimap.draw(Minimap.java:34)
at newUI.game.Renderer.render(Renderer.java:81)
at newUI.game.Game.render(Game.java:52)
at newUI.engine.GameEngine.gameLoop(GameEngine.java:63)
at newUI.engine.GameEngine.run(GameEngine.java:33)
at newUI.game.MainEcoGameLWJGL2_2021.main(MainEcoGameLWJGL2_2021.java:13)
问题是我显然有一个上下文,我在我的代码中检查了它。我也在绘制其他东西的主线程上。
我尝试移动代码(将其放在我的着色器取消绑定之后或我的着色器绑定之前)
public void render(final Window window, final BoardMode boardMode, final Player player, final Board board) {
clear();
if (window.isResized()) {
glViewport(0, 0, window.getWidth(), window.getHeight());
window.setResized(false);
}
shaderProgram.bind();
shaderProgram.setUniform("texture_sampler", 0);
// Render each gameItem
switch(boardMode) {
case STARTING_MENU:
renderStartingMenu(window);
break;
case GAME:
renderGame(window, player, board);
break;
}
drawFPS(window);
drawMode(window);
shaderProgram.unbind();
}
我只是缺少使用非着色器样式 opengl 的一些基本内容,还是无法将着色器与非着色器 opengl 混合使用?
过去 7 小时我在网上找到的大部分内容大多是基于着色器的代码,或者至少它们将坐标绑定到一个数组中,然后使用该数组进行绘制。
我知道调试我的代码需要更多代码,但我只问一般性问题,“是否可以将非常基本的基于非着色器的 opengl 与基于着色器的代码混合使用?”
PS:这里请求的代码部分调用了 glfwWindowHint() 和 GL.createCapabilities() 和 glfwMakeContextCurrent():
public void init() {
// Setup an error callback. The default implementation
// will print the error message in System.err.
GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW. Most GLFW functions will not work before doing this.
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
// Create the window
windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
if (windowHandle == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
// Setup resize callback
glfwSetFramebufferSizeCallback(windowHandle, (window, width, height) -> {
this.width = width;
this.height = height;
this.setResized(true);
});
glfwSetWindowCloseCallback(windowHandle, (window)->{
saveAndQuit();
});
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
KeyProcessor.generateKeyEvents(window, key, scancode, action, mods);
});
// Get the resolution of the primary monitor
final GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Make the OpenGL context current
glfwMakeContextCurrent(windowHandle);
if (isvSync()) {
// Enable v-sync
glfwSwapInterval(1);
}
// Make the window visible
glfwShowWindow(windowHandle);
GL.createCapabilities();
// Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//glEnable(GL_DEPTH_TEST);
}
【问题讨论】:
-
请显示您创建窗口/上下文的代码,包括任何 glfwWindowHint() 调用以及 GL.createCapabilities() 和 glfwMakeContextCurrent() 调用。
-
@KaiBurjack 我用你指定的调用发布了我的窗口代码的初始化部分。
-
是的,问题是
glBegin(引发了异常)在 OpenGL 核心配置文件中不可用。 -
我也是这么想的,我尝试了其他配置文件,但它们似乎没有太大变化。
-
没有那么多 其他可用的配置文件。从 OpenGL 3.2 开始就有了核心和兼容性。另请注意,并非所有驱动程序供应商都在所有地方实施兼容性配置文件。例如,macOS 没有 3.2+ 兼容性配置文件。只有 3.2 之前(核心和兼容性之间还没有区别)和 3.2+ 核心。所以,@genpfault 的答案是正确的。如果您请求兼容性上下文/配置文件(如果该特定操作系统上的驱动程序供应商支持),它应该可以正常工作。
标签: shader lwjgl opengl-1.x