如何在 Android 上用 OpenGL-es 绘制圆柱体?

发布于 2024-12-03 12:27:52 字数 585 浏览 0 评论 0原文

任何人都可以帮我在 OpenGL-es android 中画一个圆柱体吗?无论我画什么,它看起来都像一个矩形。

我将不胜感激任何提示或链接。

这是我尝试过的代码:

  int VERTICES=180; // more than needed
      float coords[] = new float[VERTICES * 3];
      float theta = 0;

      for (int i = 0; i < VERTICES * 3; i += 3) {
        coords[i + 0] = (float) Math.cos(theta);
        coords[i + 1] = (float) Math.sin(theta);
        coords[i + 2] = 0;
        _vertexBuffer.put(coords[i + 0]);
        _vertexBuffer.put(coords[i + 1]);
        _vertexBuffer.put(coords[i + 2]);
        theta += Math.PI / 90;
      }

Can any one help me to draw a cylinder in OpenGL-es android. Whatever i draw its look like a rectangle.

I would appreciate any tips or link.

Here is the code i've tried:

  int VERTICES=180; // more than needed
      float coords[] = new float[VERTICES * 3];
      float theta = 0;

      for (int i = 0; i < VERTICES * 3; i += 3) {
        coords[i + 0] = (float) Math.cos(theta);
        coords[i + 1] = (float) Math.sin(theta);
        coords[i + 2] = 0;
        _vertexBuffer.put(coords[i + 0]);
        _vertexBuffer.put(coords[i + 1]);
        _vertexBuffer.put(coords[i + 2]);
        theta += Math.PI / 90;
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-12-10 12:27:52

这只会画一个圆。圆柱体更加复杂,因为您需要在平移的 z 平面中定义顶点。并用正确的法线定义它们(要么面向内,就像您在圆柱体内部一样 - 即隧道,或者朝外,就像看着管道),这是更棘手的部分。

我现在正在这样做(这就是我来这里的原因)并绘制了圆柱体,但很确定我的法线不正确,因为我的灯光看起来有点不对劲。当我弄清楚时,我会发布一些代码。

编辑:意识到代码实际上也没有画圆。下面是如何做到这一点(在 2D 中):

R = Radius
NUM_VERTICES = Number of vertices you want to use in circle

delta = (Math.PI / 180) * (360 / NUM_VERTICES);  //get delta in radians between vertex definition

for i = 0 ; i < NUM_VERTICES ; i ++

    x = R * cos(Delta * i)
    y = R * sin(Delta * i))

    vertices[i] = x; vertices[i+1] = y; vertices[i+2] = 0;  
end for

//note 可能需要重新定义原始顶点来完成圆,具体取决于您使用的 GL 绘制类型。如果是这样,只需将 arg 设为 sin / cos 为 0 即可完成循环

最后编辑* :刚刚意识到我通过重新使用一些从三角形代码计算法线来使法线变得过于复杂。相反,我意识到,如果将原点 0,0 视为每个圆形条带的中心,则圆柱体的正常计算是多么简单。法线将 = 缩放到长度 1 的顶点位置。对于面向圆柱体(即隧道)的法线,x,y 值将反转(假设您向下看 -z 轴)。

This will only draw a circle. A cylinder is more complicated as you will need to define vertices in a translated z plane. And define them with correct normals (either facing in as if you were inside the cylinder -ie a tunnel or out as in looking at a pipe) which is the trickier part.

I'm currently doing this now (which is what brought me here) and have the cylinder drawn but pretty sure my normals are incorrect as my lighting looks a bit off. I'll post some code when I figure it out.

Edit : realized the code also doesn't actually draw a circle. Here is how to do that (in 2D) :

R = Radius
NUM_VERTICES = Number of vertices you want to use in circle

delta = (Math.PI / 180) * (360 / NUM_VERTICES);  //get delta in radians between vertex definition

for i = 0 ; i < NUM_VERTICES ; i ++

    x = R * cos(Delta * i)
    y = R * sin(Delta * i))

    vertices[i] = x; vertices[i+1] = y; vertices[i+2] = 0;  
end for

//note may need to redefine the original vertex to complete the circle depending on which GL draw type you are using. If so just take the arg to sin / cos to be 0 to complete the loop

Last Edit* : just realized I was overcomplicating the normals by re-using some calculate normal from triangle code I had. Instead I realized how simple the normal calculation is for a cylinder if you consider the the origin 0,0 to be the center of each circular strip. The normal will be = vertex position scaled to length 1. for normals facing in on a cylinder (ie tunnel) the x,y values would be inverted (this is a assuming you are looking down the -z axis).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文