繁体   English   中英

旋转点openGL

[英]Rotate points openGL

我试图通过旋转从层中读取图形轮廓来生成图形。 我遵循此处和其他类似问题的步骤,但是问题仍然存在。 当我尝试仅用2点36齿旋转Ply时,如果我将camara放在cilinder的顶部,我会得到这个:

衬层

修改方法rotate后的代码是:

void Figura::rotateY(int ngiros){
//Variables de rotacion.
  //double alfa = 2*M_PI/ngiros;
  int long_perfil = vertices.size();

  vector<_vertex3f> new_vertices;

  cout << long_perfil << " vertices" << endl;

  _vertex3f aux1, aux2;
  for(int i=0; i < ngiros; i++){
     double alfa = (2*M_PI/ngiros)*i;
     for(int j=0;  j < long_perfil; j++){
         aux1 = vertices.at(j);
         aux1._0 = (cos(alfa) * aux1._0) + (sin(alfa) * aux1._2);
         aux1._2 = (cos(alfa) * aux1._2) - (sin(alfa) * aux1._0);

      vertices.push_back(aux1);
    }
  }

  //vertices.clear();
  //vertices = new_vertices;

  //caras
  for(int i=0; i < vertices.size(); i++){
     _vertex3i aux(i, i+1, i+long_perfil);
     _vertex3i aux2(i, i+long_perfil+1, i+1);
     caras.push_back(aux);
     caras.push_back(aux2);
     }
   }
}

我找不到我的错误。 欢迎提供一些帮助。

您似乎不清楚原始曲线所在的坐标系,以及如何对其应用旋转。 使用您当前的代码,您只是将点旋转了可变的数量,但是将它们都保持在同一平面内。 您可以从只看代码的表面就能看出:您永远不会为任何点的y坐标设置值,因此整个结果不是3D形状,而是完全在y = 0平面中。 像煎饼一样扁平...

您需要注意的另一件事是,当您仍然使用旧值时不要修改值:

     aux1._0 = (cos(alfa) * aux1._0) + (sin(alfa) * aux1._2);
     aux1._2 = (cos(alfa) * aux1._2) - (sin(alfa) * aux1._0);

在这里,您要在第一条语句中修改aux1._0的值,而第二条语句实际上仍应使用旧值。

假设您的原始曲线在x / y平面上,并且您想绕y轴旋转。 为了获得良好的3D形状,曲线的所有x坐标都应为正:

     ^ y
     |
     |--
     |  \
     |   \_
     |     |   x
--------------->
     |     |
     |    /
     |   /
     | _/
     |/

画出指向屏幕之外的z轴的图片。

现在,要将此曲线绕y轴旋转给定角度alpha ,我们将y坐标保持不变,并在xz平面内将点(x,0)旋转alpha以获取x和z的新值。 形状的输入点(x,y)的新坐标(x',y',z')为:

x' = x * cos(alpha)
y' = y
z' = x * sin(alpha)

作为代码的修改版本:

for(int i=0; i < ngiros; i++){
    double alfa = (2*M_PI/ngiros)*i;
    for(int j=0;  j < long_perfil; j++){
        aux1 = vertices.at(j);
        aux2._0 = cos(alfa) * aux1._0;
        aux2._1 = aux1._1;
        aux2._2 = sin(alfa) * aux1._0;

        vertices.push_back(aux2);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM