我会尽量保持简短,我有一个类,它通过调用类 Cube 来绘制一个玩家,以制作一个头、一个身体、两条手臂和两条腿,每条腿都在我的构造函数中定义了不同的变量。但是调用 head.r = 1.0f 根本不会改变颜色,是不是因为我的立方体方法推送和弹出矩阵?
Player类的头部
head = new Cube(0.0f, 1.0f, 1.0f, h_x, h_y, h_z, 0.0f, 0.0f, 0.0f, 0.0f, h_length, h_height, h_width);
head.cube(drawable);
head.r = 1.0f;
立方法
public void cube(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glPushMatrix();
gl.glColor3f(r, g, b);
gl.glTranslatef(x, y, z);
gl.glRotatef(angle, pitch, yaw, roll);
gl.glScalef(length, height, width);
gl.glBegin(GL_QUADS);
//front
gl.glVertex3f(c, c, c);
gl.glVertex3f(c, -c, c);
gl.glVertex3f(-c, -c, c);
gl.glVertex3f(-c, c, c);
//back
gl.glVertex3f(c, c, -c);
gl.glVertex3f(c, -c, -c);
gl.glVertex3f(-c, -c, -c);
gl.glVertex3f(-c, c, -c);
//top
gl.glVertex3f(c, c, c);
gl.glVertex3f(c, c, -c);
gl.glVertex3f(-c, c, -c);
gl.glVertex3f(-c, c, c);
//bottom
gl.glVertex3f(c, -c, c);
gl.glVertex3f(c, -c, -c);
gl.glVertex3f(-c, -c, -c);
gl.glVertex3f(-c, -c, c);
//left
gl.glVertex3f(-c, c, c);
gl.glVertex3f(-c, -c, c);
gl.glVertex3f(-c, -c, -c);
gl.glVertex3f(-c, c, -c);
//right
gl.glVertex3f(c, c, c);
gl.glVertex3f(c, -c, c);
gl.glVertex3f(c, -c, -c);
gl.glVertex3f(c, c, -c);
gl.glEnd();
gl.glPopMatrix();
}
PS为避免混淆,类为Cube,方法为cube。
编辑:我尝试注释掉推送和弹出,它把一切都搞砸了,但头部的颜色仍然没有改变。