我要做的是将纹理映射到LWJGL中的OpenGL模型。我知道LWJGL 3和slick-util有时会一起工作,有时候也不会。在我的情况下,它工作了一点,然后尝试访问OpenGL中不再存在LWJGL 3的方法。我发布这个并不一定是试图解决这个错误,而是找到一种方法规避它。我已经尝试降级/运行LWJGL 2而不是/用LWJGL 3,这非常糟糕,而且我也尝试了几个不同的PNG解码器以获得我想要的东西,但我不能搞清楚。此外,我已经获得了" LWJGL 3兼容的slick-util"但它没有我需要的任何类/方法。如果有人可以为我修改我的代码或告诉我如何自己修复它,那就太可爱了。
这是错误:
Mon Feb 15 18:38:13 EST 2016 INFO:Use Java PNG Loader = true
Exception in thread "main" java.lang.NoSuchMethodError: org.lwjgl.opengl.GL11.glGetInteger(ILjava/nio/IntBuffer;)V
at org.newdawn.slick.opengl.renderer.ImmediateModeOGLRenderer.glGetInteger(ImmediateModeOGLRenderer.java:194)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:317)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:254)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:200)
at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
at Graphics.Loader.loadTexture(Loader.java:37)
at Graphics.LaunchWindow.loop(LaunchWindow.java:116)
at Graphics.LaunchWindow.run(LaunchWindow.java:43)
at Graphics.LaunchWindow.main(LaunchWindow.java:155)
这是LaunchWindow类:
package Graphics;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.Display;
import Controls.KeyParser;
import Controls.KeyboardInput;
import Controls.MouseInput;
import Controls.MouseParser;
import Graphics.Shaders.StaticShader;
import Controls.Cursor;
import Models.Model1;
import Models.RawModel;
import Models.TexturedModel;
import Textures.ModelTexture;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.ByteBuffer;
@SuppressWarnings("unused")
public class LaunchWindow {
private GLFWErrorCallback errorCallback;
private GLFWKeyCallback keyCallback;
private GLFWMouseButtonCallback mouseButtonCallback;
private GLFWCursorPosCallback cursorPosCallback;
public int width = 1024;
public int height = 600;
public String title = "Duplicity";
public long fullscreen = NULL;
public long window;
private Cursor cursor;
public void run() {
try {
init();
loop();
glfwDestroyWindow(window);
keyCallback.release();
} finally {
glfwTerminate();
errorCallback.release();
}
}
private void init() {
glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
KeyboardInput.initiate();
MouseInput.initiateMouse();
if ( glfwInit() != GLFW_TRUE )
throw new IllegalStateException("Unable to initialize GLFW");
if(fullscreen == NULL){
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
}
window = glfwCreateWindow(width, height, title, fullscreen, NULL);
if (window == NULL)
throw new RuntimeException("Failed to create the GLFW window");
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
keyCallback = GLFWKeyCallback.create(KeyboardInput::glfw_key_callback);
keyCallback.set(window);
mouseButtonCallback = GLFWMouseButtonCallback.create(MouseInput::glfw_mouse_button_callback);
mouseButtonCallback.set(window);
cursorPosCallback = GLFWCursorPosCallback.create(MouseInput::glfw_cursor_pos_callback);
cursorPosCallback.set(window);
cursor = new Cursor(Cursor.Standard.ARROW);
glfwSetCursor(window, cursor.getCursor());
GL.createCapabilities();
}
public void updateInput() {
KeyboardInput.update();
MouseInput.update();
glfwPollEvents();
}
public void loop() {
Loader loader = new Loader();
Render render = new Render();
Model1 model1 = new Model1();
Display display = new Display();
StaticShader shader = new StaticShader();
RawModel model = loader.loadToVAO(model1.Model1Vertices(), model1.Model1Indices());
ModelTexture texture = new ModelTexture(loader.loadTexture("marble"));
TexturedModel texturedModel = new TexturedModel(model, texture);
GL.createCapabilities();
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
if(width != 1024 && height != 600){
System.out.println("resized");
}
while ( glfwWindowShouldClose(window) == GLFW_FALSE ) {
render.prepare();
shader.start();
render.render(texturedModel);
shader.stop();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
try {
new KeyParser().checkKeyState();
new MouseParser().checkMouseState();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
shader.cleanUp();
loader.cleanUp();
}
public void setCursor(Cursor cursor) {
this.cursor = cursor;
glfwSetCursor(window, cursor.getCursor());
}
public Cursor getCursor() {
return cursor;
}
public static void main(String[] args) {
new LaunchWindow().run();
}
}
这里的装载机:
package Graphics;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import Models.RawModel;
public class Loader {
private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();
private List<Integer> textures = new ArrayList<Integer>();
public RawModel loadToVAO(float[] positions, int[] indices){
int vaoID = createVAO();
bindIndicesBuffer(indices);
storeDataInAttributeList(0, positions);
unbindVAO();
return new RawModel(vaoID, indices.length);
}
public int loadTexture(String fileName){
Texture texture = null;
try {
texture = TextureLoader.getTexture("PNG", new FileInputStream("res/" + fileName + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
int textureID = texture.getTextureID();
textures.add(textureID);
return textureID;
}
public void cleanUp(){
for(int vao:vaos){
GL30.glDeleteVertexArrays(vao);
}
for(int vbo:vbos){
GL15.glDeleteBuffers(vbo);
}
for(int texture:textures){
GL11.glDeleteTextures(texture);
}
}
private int createVAO(){
int vaoID = GL30.glGenVertexArrays();
vaos.add(vaoID);
GL30.glBindVertexArray(vaoID);
return vaoID;
}
private void storeDataInAttributeList(int attNumber, float[] data){
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
FloatBuffer buffer = storeDataInFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_DYNAMIC_DRAW); //change to static if static model
GL20.glVertexAttribPointer(attNumber, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbindVAO(){
GL30.glBindVertexArray(0);
}
private void bindIndicesBuffer(int[] indices){
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
IntBuffer buffer = storeDataInIntBuffer(indices);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_DYNAMIC_DRAW); //change to static of static model
}
private IntBuffer storeDataInIntBuffer(int[] data){
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
private FloatBuffer storeDataInFloatBuffer(float[] data){
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
}