此问题现已修复。我的着色器属性没有正确绑定(bind)。
我有一个游戏,从 IDE 运行时看起来像这样:
但是,当我使用这些设置从 Eclipse 导出它时,
纹理完全不正确。纹理仍然加载,但它们没有正确包裹到对象上。
代码完全相同,因为我只是导出了它,而且我目前正在同时运行游戏的两个窗口(一个在 IDE 中运行良好,一个在导出的 JAR 中看起来很奇怪)。
我还将所有资源从 IDE 目录复制到带有外部 JAR 的文件夹中。 IDE目录:
我从以下位置运行外部 JAR 的目录:
另外,我知道纹理实际上正在加载 - 它们没有正确包装。我知道这是因为:
我检查了 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();
}
我现在有了纹理的纹理 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);
而且,虽然我认为它们不重要,但我的顶点和片段着色器:
//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);
}
再次强调,如果游戏从 IDE 运行,所有这些都可以完美运行。只有当它从外部 JAR 运行时才会出现问题。
我将尝试不同的纹理加载技术和方法(例如,将纹理打包到 JAR 中)并查看是否会发生任何不同的情况。
又一个更新 :
因此,我将游戏发送给了另一个人(他们也使用 Windows 8),并且游戏运行良好!没有任何纹理错误!所以现在我不确定问题是出在我的电脑上还是其他什么地方。
想要尝试的玩家可以到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]
#
在日志文件中,我看到:
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]
换句话说,整个问题(纹理和崩溃)开始看起来很像是与着色器有关(向它们解析信息,或实际的着色器代码本身)。
我还做了更多的测试,并且纹理在没有使用 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);
}
更新 - 所以,在赏金赛还剩一小时时,我想我会添加更多细节:
最佳答案
我无法对此进行测试,因此我不确定这是否会有所帮助,但是 OpenGL 的某些实现不会在 VAO 中保存元素缓冲区。所以尝试在调用 glDrawElements 之前绑定(bind)元素缓冲区,例如
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, someBuffer);
glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
如果您不这样做,您可能正在使用最后一个绑定(bind)缓冲区进行所有绘制调用。可能不是答案,但测试它不会有什么坏处。我还注意到您使用的是旧版本的 Slick,因为您仍然拥有 slick-util,它不是最新版本的一部分,因此您也可以尝试更新它。
关于java - 导出 Eclipse 项目会导致不正确的纹理和崩溃,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222716/