【问题标题】:How to rotate Shape which is aggregation of rotated individual parts?如何旋转形状,它是旋转的各个部分的聚合?
【发布时间】:2015-12-28 20:24:51
【问题描述】:

我的绘图又是小线条或圆圈的集合,我在各个角度旋转(主要是线条)。 现在我想将整个绘图旋转到某个角度。

你可以看到我想要实现的目标。 所以我的问题是如何旋转整个绘图。

@Override
public void display(GLAutoDrawable arg0) {
    final GL2 gl = arg0.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
   
    drwaMan(gl);
    gl.glFlush();
   
}

private void drwaMan(GL2 gl) {
    // this line is not working i was hoping by doing something like this entire shape will rotate
    //gl.glRotatef(15, 0f, 0f, 1.0f);
    
    float radius = 50;
    float cx = 100, cy = 400; // center of circle
    int bodyAngel = 180; // draw line at angel
    float bodyLineLength = 150;
    int lineAngel = 270;

    // calculate first point of line 
    float px = (float) (radius * Math.sin(Math.toRadians(bodyAngel))) + cx;
    float py = (float) (radius * Math.cos(Math.toRadians(bodyAngel))) + cy;
    
    // draw head
    drawCircle(gl,radius,cx,cy);
    // draw line
    drawBoadyLine(gl,px,py,bodyLineLength,lineAngel);
    
    // drawhands 50 distance from starting point
    drawHands(gl,px,py-50,225,315,100);
    
    // lags at end of line
    drawHands(gl,px,py-150,225,315,100);
}

void drawCircle(GL2 gl,float radius,float cx,float cy)
{
   gl.glLoadIdentity();
   gl.glColor3f(1.0f, 1.0f, 1.0f);
   gl.glBegin(GL.GL_LINE_LOOP);
 
   for (int i = 0; i <= 360; i++) {
            float x1, y1;
            x1 = (float) (radius * Math.sin(Math.toRadians(i))) + cx;
            y1 = (float) (radius * Math.cos(Math.toRadians(i))) + cy;
            gl.glVertex2d(x1,y1);
    }
 
   gl.glEnd();
}
private void drawHands(GL2 gl, float x, float y, int a1, int a2,int l) {

    gl.glLoadIdentity();
    gl.glTranslatef(x, y, 0);
    gl.glRotatef(a1, 0, 0, 1);
    
    gl.glBegin(GL.GL_LINE_LOOP);
        gl.glVertex2f(0, 0);
        gl.glVertex2f(l, 0);
    gl.glEnd();
    
    gl.glLoadIdentity();
    gl.glTranslatef(x, y, 0);
    gl.glRotatef(a2, 0, 0, 1);
    gl.glBegin(GL.GL_LINE_LOOP);
        gl.glVertex2f(0, 0);
        gl.glVertex2f(l, 0);
    gl.glEnd();
}

private void drawBoadyLine(GL2 gl, float x, float y, float bodyLineLength, int bodyAngel) {
    gl.glLoadIdentity();
    gl.glTranslatef(x, y, 0);
    gl.glRotatef(bodyAngel, 0f, 0f, 1.0f);
    
    gl.glBegin(GL.GL_LINE_LOOP);
        gl.glVertex2f(0, 0);
        gl.glVertex2f(bodyLineLength, 0);
    gl.glEnd();
    
}

如果你们提供一些解决方案或任何指向相关军事的帮助,通过一些例子解决这个问题,我很感激。

注意:我已经用OpenGL - JOGL绑定写的是代码。

提前谢谢你。

【问题讨论】:

  • 对我来说听起来像是 2D 转换。我发现this page helpful 是一种用于转换的备忘单。
  • @dbrown93 这篇文章只是关于理论知识,没有任何api如何实现的真实例子。
  • 是的,但它只是应用关于一个点的旋转部分。因此,您可以将人的“中心”平移到原点,旋转坐标,然后将人平移回来
  • 你应该做点什么similar

标签: java opengl rotation lwjgl jogl


【解决方案1】:

drawCircle的第一条语句是:

gl.glLoadIdentity();

这会将当前的转换矩阵重置为单位。

这使得gl.glRotatef(15, 0f, 0f, 1.0f); 呼叫毫无用处。

您应该删除所有对glLoadIdentity 的调用,并且在display 函数中的glClear 之后只执行一次。

你还应该看看:

  • glMatrixMode知道女巫矩阵受到影响。
  • glPush/glPop 这允许您进行临时转换(针对对象的一部分)。

【讨论】:

  • 通过删除整个形状变得不连贯并且在随机位置绘制形状
  • 也许可以,但您可以取消注释 glRotate 命令并看到“随机”图像旋转。实际上,没有什么是随机的,您必须进一步考虑您想要转换的位置(如关节)以及您的坐标必须是静态的。掌握基础变化需要时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多