【问题标题】:texturing a sphere using LWJGL opengl?使用 LWJGL opengl 对球体进行纹理处理?
【发布时间】:2019-09-17 18:44:09
【问题描述】:

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

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

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;
        }
    }

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    您必须绘制四边形而不是三角形。一个四边形可以由 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+;
    }
    

    【讨论】:

    • 我试过了,但是没有用,当我运行的时候;窗口自动关闭并且非常快
    • @Mohamad_Hn,你在调试器中试过了吗?如果程序立即关闭,则可能是未捕获的异常。
    • @Mohamad_Hn 索引数组 (indices) 是否足够大?你必须加倍大小!
    • 当我编辑代码并添加新部分时,线程 "main" java.lang.ArrayIndexOutOfBoundsException: 40000 at Project.Sphere.createSolidSphere(Sphere.java:117) 在 Project 中出现异常异常.Sphere.(Sphere.java:61) 在 engineTester.Main.main(Main.java:134) @Romen
    • @Mohamad_Hn 请阅读上面的评论。如果您有两倍的索引,则缓冲区大小需要加倍。索引缓冲区(命名为indices)设计为每个四边形保存 3 个索引,但现在每个四边形必须保存 6 个索引。
    猜你喜欢
    • 2017-10-29
    • 2011-09-08
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多