glTranslate/rotate 在我的代码中不起作用
我想绘制一个立方体并翻译它,但模型视图矩阵上的转换似乎不起作用。它们的效果为0。
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
drawCube()
内部,您正在重置模型视图矩阵:我怀疑您想在此处使用
glPushMatrix()
和glPopMatrix()
。附带说明:自 OpenGL 3.1 起,这些操作已被弃用。
Inside
drawCube()
you're resetting the modelview matrix:I suspect you want to use
glPushMatrix()
andglPopMatrix()
here.As a side note: those operations are deprecated since OpenGL 3.1.