gpt4 book ai didi

java - GL11.glMatrixMode() 的 LWJGL 错误 "function is not supported"

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:40 24 4
gpt4 key购买 nike

我将先发布代码,然后发布以下问题:

import java.util.Properties;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

import ref.Reference;
import ref.Settings;

public class Main implements Runnable {

private static int width;
private static int height;

private long lastframe;
private int fps;
private long lastfps;

private float rotation = 0;
private float x = 400;
private float y = 300;

private boolean running = false;
private Thread thread;
private static Properties settings = new Properties();

public static void main(String[] args) {
Main game = new Main();
Settings.load();
settings = Settings.getSettings();
game.initDisplay();
game.initGL();
game.start();
}

public void run() {
System.out.println("Start");
getDelta();
lastfps = getTime();
while (!Display.isCloseRequested()) {
if (running) {
int delta = getDelta();
tick(delta);
Display.sync(60);
}
render();
updateFPS();
Display.update();
}
if (Display.isCloseRequested()) {
stop();
cleanup();
}
}

private void updateFPS() {
if (getTime() - lastfps > 1000) {
System.out.println("FPS: " + fps);
fps = 0;
lastfps += 1000;
}
fps++;
}

private void tick(int delta) {
System.out.println("Tick");
rotation += 0.15f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta;
if (x < 0) x = 0;
if (x > 800) x = 800;
if (y < 0) y = 0;
if (y > 600) y = 600;
}

private void render() {
System.out.println("Render");
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3f(0.5f, 0.5f, 1.0f);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(rotation, 0f, 0f, 0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x - 50, y - 50);
GL11.glVertex2f(x + 50, y - 50);
GL11.glVertex2f(x + 50, y + 50);
GL11.glVertex2f(x - 50, y + 50);
GL11.glEnd();
GL11.glPopMatrix();
}

private void cleanup() {
Mouse.destroy();
Keyboard.destroy();
Display.destroy();
}

public void initGL() {
System.out.println("initGL");
GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

public void initDisplay() {
System.out.println("initDisplay");

try {
width = Integer.parseInt(settings.getProperty("width"));
height = Integer.parseInt(settings.getProperty("height"));
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(Reference.WINDOWTITLE);
Display.create();
Keyboard.create();
Mouse.create();
} catch (Exception e) {

}
}

public void start() {
if (running) return;
running = true;
thread = new Thread(this, "Display");
thread.start();
}

public void stop() {
if (!running) return;
running = false;
try {
thread.join();
} catch (Exception e) {

}
}

private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public int getDelta() {
long time = getTime();
int delta = (int) (time - lastframe);
lastframe = time;
return delta;
}

}

现在启动时出现错误:

initDisplay
initGL
Exception in thread "main" java.lang.IllegalStateException: Function is not supported
at org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)
at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:2075)
at Main.initGL(Main.java:127)
at Main.main(Main.java:41)

这基本上是来自 LWJGL Wiki 的代码,我只是复制它以查看线程是否工作并遇到了这个。这对我来说是新事物,所以任何人都可以帮助并告诉我什么是不正确的吗?因为我不明白。

第 127 行是 initGL() 的第一行(我为此删除了空行,因此它不再是第 127 行,但这是发生错误的行):

GL11.glMatrixMode(GL11.GL_PROJECTION);

没有线程也能正常工作

最佳答案

OpenGL 调用(来自一个 GL 上下文)只能在一个线程上运行。您的代码在同一 GL 上下文中通过两个不同的线程运行 OpenGL 代码。在默认线程上运行 game.initDisplay()game.initGL(),并在您自己的新线程上运行剩余的 OpenGL 代码。您要么需要确保所有 OpenGL 调用都是从一个线程进行,要么研究上下文共享。我会选择单线程,直到这被证明是一个问题。

关于java - GL11.glMatrixMode() 的 LWJGL 错误 "function is not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com