java – 修复LWJGL 3和Slick-Util之间的不兼容性
发布时间:2020-12-15 02:13:08 所属栏目:Java 来源:网络整理
导读:我想要做的是将纹理映射到LWJGL中的OpenGL模型.我知道LWJGL 3和slick-util有时会一起工作,有时却不能.在我的情况下,它工作了一点,然后尝试访问OpenGL中的一个方法,LWJGL 3不再存在.我的帖子不一定是试图修复这个错误,而是找到绕过它的方法.我已经尝试降级/运
|
我想要做的是将纹理映射到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);
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);
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,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;
}
}
解决方法
你可以在这里下载与lwjgl3兼容的slick util:
*click*
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
