glTranslate/rotate not working in my code
I want to draw a cube and translate it, but the transformations on the Model View matrix don't seem to work. They have 0 effect.
gl.glClearColor(0, 0, 0, 0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(gl.GL_DEPTH_BUFFER_BIT);
gl.glFrustum(-10, 10, -10, 10, 0.01, 20);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslated(0.8, 0, 0); //no effect
gl.glRotated(30, 1, 0, 0); //no effect
drawCube(gl);
public void drawCube(GL gl) {
gl.glMatrixMode(GL.GL_MODELVIEW);
//spate
gl.glLoadIdentity();
drawSquare(gl);
//fata
gl.glLoadIdentity();
gl.glTranslated(0, 0, WIDTH);
drawSquare(gl);
//st
gl.glLoadIdentity();
gl.glRotated(-90, 0, 1, 0);
drawSquare(gl);
//dr
gl.glLoadIdentity();
gl.glRotated(-90, 0, 1, 0);
gl.glTranslated(0, 0, -WIDTH);
drawSquare(gl);
//sus
gl.glLoadIdentity();
gl.glRotated(90开发者_运维技巧, 1, 0, 0);
drawSquare(gl);
//jos
gl.glLoadIdentity();
gl.glRotated(90, 1, 0, 0);
gl.glTranslated(0, 0, -WIDTH);
drawSquare(gl);
}
public void drawSquare(GL gl) {
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2d(0, 0);
gl.glVertex3d(0, 0,0);
gl.glTexCoord2d(0, 1);
gl.glVertex3d(0, WIDTH,0);
gl.glTexCoord2d(1, 1);
gl.glVertex3d(WIDTH, WIDTH,0);
gl.glTexCoord2d(1, 0);
gl.glVertex3d(WIDTH, 0,0);
gl.glEnd();
}
Inside drawCube()
you're resetting the modelview matrix:
gl.glMatrixMode(GL.GL_MODELVIEW);
//spate
gl.glLoadIdentity();
I suspect you want to use glPushMatrix()
and glPopMatrix()
here.
As a side note: those operations are deprecated since OpenGL 3.1.
精彩评论