我会首先发布代码,然后是下面的问题:
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);
没有线程btw正常工作
答案 0 :(得分:0)
OpenGL调用(来自一个GL上下文)只能在一个线程上运行。您的代码通过两个不同的线程在相同的GL上下文中运行OpenGL代码。在默认线程上运行game.initDisplay()
和game.initGL()
,并在您自己的新线程上运行剩余的OpenGL代码。您需要确保所有OpenGL调用都来自一个线程,或者查看上下文共享。我会选择一个单独的线程,直到证明是一个问题。
答案 1 :(得分:0)
该错误基本上意味着对于此版本的OpenGL,不支持该功能。您要么使用旧版本的OpenGL,要么使用不支持它的较新版本。要设置版本,而不是Display.create(),请执行Display.create(new PixelFormat(),new ContextAttributes(3,0)),这将把您的OpenGL版本设置为3.0,这是一个支持glMatrixMode的版本。