什么可能导致网格生成时发生旋转?LWGL

huangapple go评论76阅读模式
英文:

What could be causing meshes to generate rotated? LWGL

问题

我正在从高度图中加载块值以创建无限地形,但在渲染每个网格块时遇到问题。当我加载一个单独的500x500单位网格时,它是一个平滑的网格。但当我加载一个5x5的100x100个网格块时,它变成了混乱的一团。

我让这两种不同类型的网格块保存了每个网格块的高度图像,并且它们都提供了一个平滑的高度图,具有渐变的值变化。

...但当我渲染它们时,看到的是这样的情况:
500x500 网格块
5x5 100x100 网格块

正如你所看到的,5x5的网格块完全不对齐。前三个块0:0, 0:1 和 1:0 看起来是正确的,但其他的都不同。它们的变换矩阵中的所有旋转都是vec3(0,0,0),但它们生成成这样。以下是用于渲染它们的代码:

	public void render() {
		shader.start();
		shader.loadViewMatrix(Maths.createViewMatrix(engine.getPlayer()));
		shader.loadLight(engine.getManagers().getLightManager().getLights().get(0));

		for(Chunk chunk : engine.getManagers().getTerrainManager().getLoadedChunks().values()) {
			RawModel model = chunk.getModel();

			bindChunk(chunk);
			loadShaderUniforms(chunk);
			GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

			unbindChunk();
		}
		shader.stop();
	}

	private void bindChunk(Chunk chunk) {
		RawModel rawModel = chunk.getModel();
		GL30.glBindVertexArray(rawModel.getVaoID());
		GL20.glEnableVertexAttribArray(0); // position
		GL20.glEnableVertexAttribArray(1); // textureCoordinates
		GL20.glEnableVertexAttribArray(2); // normal

		ModelTexture texture = chunk.getTexture();
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
	}

	private void unbindChunk() {
		GL20.glDisableVertexAttribArray(0);
		GL20.glDisableVertexAttribArray(1);
		GL20.glDisableVertexAttribArray(2);
	}

	private void loadShaderUniforms(Chunk chunk) {
		Matrix4f transformation = Maths.createTransformationMatrix(chunk.getLocation().getPosition(),
				chunk.getLocation().getRotation(), 1, true);
		System.out.println(chunk.getLocation().getRotation());
		shader.loadTransformationMatrix(transformation);
		shader.setShowHeightMap(chunk.shouldShowHeightMap());
	}

我不熟悉LWJGL如何使用VAO,所以这可能是我遗忘的一个简单问题。如果能提供帮助,将不胜感激,因为我已经花了好几天的时间,仅将问题缩小到渲染问题而不是Perlin噪声/网格生成问题。

英文:

I am loading chunk values from a height map to create infinite terrain, but I'm having issues with the rendering of each mesh chunk. When I load a single 500x500 unit mesh, it is a smooth mesh. When I load a 5x5 of 100x100 meshes, it is a jumbled up mess.

I made the two different types of meshes save an image of the height of each mesh, and they both give a smooth height map with gradual value changes.

...But when I render them, this is what I see:
500x500 mesh
5x5 100x100 mesh

As you can see, the 5x5 mesh isn't at all aligned. The first three chunks 0:0, 0:1, and 1:0 seem to look correct, but the others are all different. All rotations in their transformation matrix is vec3(0,0,0), but they generate like this. Here is the code used to render them:

	public void render() {
shader.start();
shader.loadViewMatrix(Maths.createViewMatrix(engine.getPlayer()));
shader.loadLight(engine.getManagers().getLightManager().getLights().get(0));
for(Chunk chunk : engine.getManagers().getTerrainManager().getLoadedChunks().values()) {
RawModel model = chunk.getModel();
bindChunk(chunk);
loadShaderUniforms(chunk);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
unbindChunk();
}
shader.stop();
}
private void bindChunk(Chunk chunk) {
RawModel rawModel = chunk.getModel();
GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0); // position
GL20.glEnableVertexAttribArray(1); // textureCoordinates
GL20.glEnableVertexAttribArray(2); // normal
ModelTexture texture = chunk.getTexture();
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
}
private void unbindChunk() {
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
}
private void loadShaderUniforms(Chunk chunk) {
Matrix4f transformation = Maths.createTransformationMatrix(chunk.getLocation().getPosition(),
chunk.getLocation().getRotation(), 1, true);
System.out.println(chunk.getLocation().getRotation());
shader.loadTransformationMatrix(transformation);
shader.setShowHeightMap(chunk.shouldShowHeightMap());
}

I'm not familiar with the details of how LWJGL uses VAOs, so it might be a simple issue I'm forgetting. Any help would be appreciated as I've spent several days just narrowing down the issue to a rendering problem instead of a problem with the perlin noise/mesh generation.

答案1

得分: 0

我发现了问题!我创建网格的方式存在缺陷,导致三角形呈现翻转状态。我通过关闭剔除并观察两侧以及移动三个顶点坐标来发现了这个问题。修复了顶点的朝向(例如,将它从顺时针改为逆时针,然后反转X和Z坐标)后,问题得以解决。

英文:

I discovered the issue! There was a flaw in how I created the mesh which caused it to render the triangles flipped over. I discovered this by turning off culling and observing it from both sides and moving the three vertex coordinates around. Fixing the vertex face (e.g. swapping it from a clockwise to counter-clockwise and then reversing the X and Z coordinates) fixed it.

huangapple
  • 本文由 发表于 2020年8月7日 05:01:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63291678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定