我目前正在尝试使用 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);
}