glDrawArrays画半球

网上找了一些画球的代码,看代码很难理解其思路,于是根据参考文献,http://paulbourke.net/miscellaneous/sphere_cylinder/自己画了半个球。比较简单的思路,可以引导一步一步优化。自己的思路是先把球的点画出来,再去画三角形,所以代码中有之前先画了半球的点的思路,最后半球由三角形组成的点如下:

    private int         la         = 90;  //定义经度,即最大时每一圈有多少个点
    private int         lo         = 90;  //定义纬度,纬度每上升1,点数减少la/lo个。比如3,60个,最大的圈60个点,第二个圈40个点,第三个圈20个点。(画球是每圈两个点和上层圈1个点绘制三角形,所以la最好和lo相等,否则画出的半球点不够)这两个值为30的时候就已经比较像半球了

        vertices = new float[(la + la / lo + 2) * lo * 3];

        int points = 0;
        for (int j = 0; j < lo; j++) {
            int pointNum = la - j * (la / lo);
            for (int i = 0; i < pointNum; i++) {
                vertices[3 * points] = (float) (Math.cos(toRadians(360 * i / pointNum)) * Math.cos(toRadians(j * 90 / lo)));
                vertices[3 * points + 1] = (float) (Math.sin(toRadians(360 * i / pointNum)) * Math.cos(toRadians(j * 90 / lo)));
                vertices[3 * points + 2] = (float) Math.sin(toRadians(j * 90 / lo));
                points++;
                if (pointNum == 1) {
                    break;
                }
                vertices[3 * points] = (float) (Math.cos(toRadians(360 * i / (pointNum - 1))) * Math.cos(toRadians((j + 1) * 90 / lo)));
                vertices[3 * points + 1] = (float) (Math.sin(toRadians(360 * i / (pointNum - 1))) * Math.cos(toRadians((j + 1) * 90 / lo)));
                vertices[3 * points + 2] = (float) Math.sin(toRadians((j + 1) * 90 / lo));
                points++;
            }
            vertices[3 * points] = (float) (Math.cos(toRadians(0)) * Math.cos(toRadians(j * 90 / lo)));
            vertices[3 * points + 1] = (float) (Math.sin(toRadians(0)) * Math.cos(toRadians(j * 90 / lo)));
            vertices[3 * points + 2] = (float) Math.sin(toRadians(j * 90 / lo));
            points++;
            vertices[3 * points] = (float) (Math.cos(toRadians(0)) * Math.cos(toRadians((j + 1) * 90 / lo)));
            vertices[3 * points + 1] = (float) (Math.sin(toRadians(0)) * Math.cos(toRadians((j + 1) * 90 / lo)));
            vertices[3 * points + 2] = (float) Math.sin(toRadians((j + 1) * 90 / lo));
            points++;
        }

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, (la + la / lo + 2) * lo);//直接STRIP画半球了,不管一层一层单独画了。以后再优化





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值