gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:25 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com