导出Eclipse项目会导致错误的纹理和崩溃,

Joe*_*200 14 java eclipse opengl glsl lwjgl

此问题现已修复.我的着色器属性没有正确绑定.

我有一个游戏,从IDE运行时看起来像这样:

在此输入图像描述

但是,当我使用这些设置从Eclipse导出它时,

在此输入图像描述

纹理完全不正确.纹理仍然加载,但它们没有正确地包装到对象上.

在此输入图像描述

代码完全相同,因为我刚刚导出它,而且我目前同时运行游戏的两个窗口(一个在IDE中工作正常,一个从导出的JAR看起来很奇怪).

我还将IDE目录中的所有资源复制到具有外部JAR的文件夹中.IDE目录: 在此输入图像描述

目录我运行外部JAR: 在此输入图像描述

此外,我知道纹理实际上正在加载 - 它们没有正确包装.我知道这是因为:

  • 如果你看飞机,你会发现它仍然有纹理的元素 - 它们只是被拉伸和搞砸了.
  • Skybox也是如此.如果你看一下,零件仍然在那里,但同样,它错误地包裹在OBJ模型周围.
  • 如果我点击Q键以使用DisplayList渲染地形(多次包裹地形纹理),它会显示纹理.无法获得此截图,因为我无法点击Q并截取屏幕截图.

我已经检查了JAR文件,片段和顶点着色器仍在那里.正确的库也似乎在那里(此外,如果不是,游戏甚至不会开始). 在此输入图像描述

更新:当我多次重新启动游戏以检查更多信息时,我注意到,只要显示器显示(因此我可以看到纹理不正确的地形和平面),游戏就会冻结大约90%的时间.没有Stacktrace,只是"这个窗口没有响应,窗口正在关闭它".当我在IDE中运行游戏时,游戏仍能完美运行而不会崩溃.

游戏服务器导出并运行完美.只有客户才是问题.

那么导出可以使游戏与在IDE中运行游戏有什么不同,我该如何解决呢?

更新:所以这是我的纹理加载代码:

loader.loadTexture("PlaneTexture", 1);
//loadTexture() method:
public int loadTexture(String fileName, int mipmap) {
    Texture texture = null;
    try {
        try{
        texture = TextureLoader.getTexture("PNG", new FileInputStream("res/" + fileName + ".png")); //TextureLoader is a Slick-Util class.
        }catch (Exception e){
            e.printStackTrace();
        }
        if (texture == null){
            throw new Exception("Null texture!");
        }
        //texture = TextureLoader.getTexture("GIF", new FileInputStream("res/" + fileName + ".png"));
        if (mipmap > -10){
       GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
       GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, mipmap);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Tried to load texture " + fileName + ".png , didn't work");
        System.exit(1);
        return -1;


    }
    textures.add(texture.getTextureID());
    return texture.getTextureID();
}
Run Code Online (Sandbox Code Playgroud)

我现在有纹理的纹理ID.然后我像这样渲染对象(在这种情况下是平面):

Plane you = Main.TerrainDemo.shipsID.get(Main.TerrainDemo.UID);
    Main.TerrainDemo.shader.start();
    TexturedModel texturedModel = TerrainDemo.shipModel; //The plane model
    RawModel model = texturedModel.getRawModel();
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    GL20.glEnableVertexAttribArray(2);
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TerrainDemo.shipModel.getTexture().getID()); //The ID of the texture.

    glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
Run Code Online (Sandbox Code Playgroud)

而且,虽然我不认为它们很重要,但我的顶点和片段着色器:

//Vertex shader
#version 130

in vec3 position;
in vec2 textureCoords;
in vec3 normal;

out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;

uniform mat4 transformationMatrix;
uniform vec3 lightPosition;

void main(void){
gl_Position = ftransform();
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = (vec3(500, 50000, 500)) - (transformationMatrix * vec4(position, 1.0)).xyz;
}

//Fragment shader
#version 130

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;

out vec4 out_Color;

uniform sampler2D textureSampler;

uniform vec3 lightColour;

void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.2);
brightness = brightness + 0.5;
vec3 diffuse = brightness * vec3(1, 1, 1);

vec4 textureColor = vec4(diffuse, 1.0) * texture(textureSampler, pass_textureCoords);
if(textureColor.a<1){
    discard;
}
out_Color = vec4(textureColor.r, textureColor.g, textureColor.b, 1);

}
Run Code Online (Sandbox Code Playgroud)

同样,我将强调,如果游戏是从IDE运行的,那么所有这一切都能正常运行.只是从外部JAR运行才会出现问题.

我将尝试不同的纹理加载技术和方法(例如将纹理打包到JAR中)并查看是否发生了任何不同的情况.

还有一个更新:所以,我把游戏发送给另一个人(他们也使用Windows 8),游戏完美无缺!没有任何纹理错误!所以现在我不确定问题是我的PC是专门还是别的.

对于那些想要尝试的人,你可以在http://endcraft.net/PlaneGame下载游戏并自己查看(请阅读instructions.txt - 另外,你需要一个解压缩.rar文件的程序).

我会得到尽可能多的人来让游戏一试,看看他们是否有相同的问题,或者纹理是否正确.

当我从IDE运行它时,它完全没问题,但是当我导出到外部jar 时它不起作用,但是当我将它导出到外部jar并将其发送给其他人时它确实有效!

(另一个)更新:我已经将游戏发送给多个人,其中一些人遇到了这个崩溃:

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x1cd6b57f, pid=36828,  tid=35556
#
# JRE version: Java(TM) SE Runtime Environment (8.0_31-b13) (build 1.8.0_31-b13)
# Java VM: Java HotSpot(TM) Client VM (25.31-b07 mixed mode windows-x86 )
# Problematic frame:
# C  [atioglxx.dll+0xc0b57f]
#
Run Code Online (Sandbox Code Playgroud)

在日志文件中,我看到:

Stack: [0x011d0000,0x01220000],  sp=0x0121f1e8,  free space=316k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [atioglxx.dll+0xc0b57f]
C  [atioglxx.dll+0xbe8b95]
C  [atioglxx.dll+0x641852]
C  [lwjgl.dll+0x9a28]
j  org.lwjgl.opengl.GL20.glUniformMatrix4(IZLjava/nio/FloatBuffer;)V+33
j  TMLoading.ShaderProgram.loadMatrix(ILorg/lwjgl/util/vector/Matrix4f;)V+23j
TMLoading.StaticShader.loadTransformationMatrix(Lorg/lwjgl/util/vector/Matrix4f;) V+6
j  Joehot200.Plane.render()V+407
j  Joehot200.TerrainDemo.render()V+4045
j  Joehot200.TerrainDemo.enterGameLoop()V+356
j  Joehot200.TerrainDemo.startGame()V+320
j  StartScreenExperiments.Test2.resartTDemo()V+128
j  StartScreenExperiments.Test2.main([Ljava/lang/String;)V+27
v  ~StubRoutines::call_stub
V  [jvm.dll+0x1473e5]
Run Code Online (Sandbox Code Playgroud)

换句话说,整个问题(纹理和崩溃)开始看起来很像它们与着色器相关(要么向它们解析信息,要么实际着色器代码本身).

我还做了更多的测试,并且纹理工作正常,没有使用DisplayList的着色器.

以下是glUniformMatrix()调用的代码:

//Some unnecessary code has been removed. For example, you do not care about what colour I make the plane depending on what team it is on.    

//Plane class. (Referring to a jet plane which is the main object the player controls)
public void render(){
    twodcoords = TextDemo.getScreenCoords(sx, sy + 30, sz);
    glPushAttrib(GL_ENABLE_BIT);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glPushMatrix();
    glTranslatef(sx, sy, sz);
    GL30.glBindVertexArray(Main.TerrainDemo.vaoID);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
        glRotatef(srot, 0f, 1f, 0f);
        glRotatef(pitch, -1f, 0f, 0f);
    Main.TerrainDemo.shader.start();
    glPushMatrix();
    glDisable(GL_LIGHTING);
    TexturedModel texturedModel = TerrainDemo.shipModel;
    RawModel model = texturedModel.getRawModel();
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    GL20.glEnableVertexAttribArray(2);
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TerrainDemo.shipModel.getTexture().getID());
    org.lwjgl.util.vector.Matrix4f m = Assist.createTransformationMatrix(new Vector3f(sx, sy, sz), new Vector3f(pitch, rot, roll), new Vector3f(5, 5, 5)); //Creates a transformation matrix based on the X, Y, Z, Pitch, yaw, roll, and scale of the plane.
    Main.TerrainDemo.shader.loadTransformationMatrix(m);
    glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);
    GL20.glDisableVertexAttribArray(2);
    GL30.glBindVertexArray(0);
    GL30.glBindVertexArray(0);
    glPopMatrix();
    glPopAttrib();
}
//StaticShader class. This method literally just passes it to the loadMatrix class.    
public void loadTransformationMatrix(Matrix4f matrix){
    super.loadMatrix(location_transformationMatrix, matrix);
}
//ShaderProgram class.
FloatBuffer buf = BufferUtils.createFloatBuffer(4 * 4);
public void loadMatrix(int location, Matrix4f matrix){
    matrix.store(buf);
    buf.flip();
    GL20.glUniformMatrix4(location, false, buf);
}
Run Code Online (Sandbox Code Playgroud)

更新 - 所以,在Bounty还剩一个小时的时候,我想我会添加一些细节:

  • 正如我在上面所说的那样,游戏适用于某些导出时,但不适用于其他人.我注意到游戏在java 7上运行时总是有效,但是我只有我和java 7上的另一个测试人员,这真的不是决定性的.
  • 纹理在DisplayList中正确呈现.纹理正在加载.但是,它们没有正确显示.
  • 即使您不知道问题,尝试游戏(忽略开始屏幕,我需要创建一个新的)并告诉我结果,以及您的操作系统/ Java细节等,将非常感激.
  • 是的,mipmapping是正确的.我知道有人在评论中提到它可能不是,但我已经尝试将它设置得愚蠢得高,而且我确实得到了非常模糊的纹理.
  • 我已经尝试过"将库包装到外部jar中".我很欣赏这个答案的时间,但我在评论中说过我已经尝试过了.
  • 问题可能是片段着色器(正如评论中建议的那样),但我目前不确定如何测试它,也不理解为什么它在IDE内部工作但不在其中.

小智 1

我无法对此进行测试,因此我不确定这是否有帮助,但 OpenGL 的某些实现不会在 VAO 中保存元素缓冲区。因此,尝试在调用 glDrawElements 之前绑定元素缓冲区,例如

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, someBuffer);
glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
Run Code Online (Sandbox Code Playgroud)

如果您不这样做,您可能会使用最后一个绑定缓冲区来进行所有绘制调用。可能不是答案,但测试一下也没什么坏处。我还注意到您使用的是旧版本的 Slick,因为您仍然拥有 slick-util,它不是最新版本的一部分,因此您也可以尝试更新它。