java - 如何旋转由旋转的各个部分聚合而成的形状?

标签 java opengl rotation lwjgl jogl

我的画又是小线条或圆圈的聚合,我正在以不同的角度旋转(主要是线条)。 现在我想将整个绘图旋转到某个角度。

enter image description here

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

@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 绑定(bind)来编写代码。

提前谢谢您。

最佳答案

drawCircle的第一条语句是:

gl.glLoadIdentity();

这会将当前变换矩阵重置为恒等矩阵。

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

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

您还应该看看:

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

关于java - 如何旋转由旋转的各个部分聚合而成的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34500246/

相关文章:

java - IBM MQ - Java api - PCFMessageAgent - 连接失败

c++ - 通过数组绘制对象

javascript - 在旋转 Js 上更改变量的值

java - 如何识别弹出窗口中的X按钮来关闭它?

java - Android MenuItem showAsAction ="always"有组时被忽略

c++ - 图片显示错误

c++ - OpenGL - 在一个函数中加载多个纹理

javascript - mousemove jquery 上的多重转换

java - 游戏对象旋转形成椭圆形而不是圆形

java - Maven 依赖相当于 MOXy