gpt4 book ai didi

java - 使用 LWJGL opengl 对球体进行纹理化?

转载 作者:行者123 更新时间:2023-11-30 05:28:52 25 4
gpt4 key购买 nike

我想绘制一个球体并对其进行纹理处理,我用三角形绘制它,当我尝试对其进行纹理处理时,某些三角形未被覆盖

我正在使用此函数来生成坐标

public void createSolidSphere()
{
float R = (float) (1./(float)(rings-1));
float S = (float) (1./(float)(sectors-1));
int r, s;
int texCoordsIndex = -1;
int verticesIndex = -1;
int normalsIndex = -1;
int indicesIndex = -1;
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
float y = (float)Math.sin( -Math.PI/2 + Math.PI * r * R );
float x = (float)Math.cos(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );
float z = (float)Math.sin(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );

texcoords[++texCoordsIndex] = s*S;
texcoords[++texCoordsIndex] = r*R;

vertices[++verticesIndex] = x * radius;
vertices[++verticesIndex] = y * radius;
vertices[++verticesIndex] = z * radius;

normals[++normalsIndex] = x;
normals[++normalsIndex] = y;
normals[++normalsIndex] = z;
}
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {


indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;
}
}

最佳答案

您必须绘制四边形而不是三角形。 2个三角形可以组成一个四边形。
每个四边形由 4 个点组成:

0: r * sectors + (s+1)
1: (r+1) * sectors + (s+1)
2: (r+1) * sectors + s
3: r * sectors + s

这4个点可以排列成2个三角形:

    2           1
+ +-------+
| \ \ |
| \ \ |
| \ \ |
+------+ +
3 0

您必须为每个四边形添加 6 个索引,而不是 3 个:

for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {

// triangle 1
indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;

// triangle 2
indices[++indicesIndex] = r * sectors + (s+1);
indices[++indicesIndex] = (r+1) * sectors + s;
indices[++indicesIndex] = r * sectors + s+;
}

关于java - 使用 LWJGL opengl 对球体进行纹理化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com