我试图在OpenGL中绘制一个旋转的方块,但是方形没有深度,它只是看起来平坦,我期待一些边缘是对角的...我错过了一些明显的东西吗?
作为一方,z轴不是我想要的方式,我得知z轴正在通过屏幕,但我想看到它......
(摘录1)
- (void)drawRect:(NSRect)rect
{
glEnable(GL_DEPTH_TEST);
float _zoomFactor = 10.0;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glClearColor(1.0,1.0,1.0,1.0);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
float fieldOfView = 75.0;
float imageAspectRatio = rect.size.width / rect.size.height;
float nearClipPlane = 10.0;
float farClipPlane = 300.0;
float scale = tan(radians(fieldOfView * 0.5)) * nearClipPlane;
float r = imageAspectRatio * scale, l = -r;
float t = scale, b = -t;
glFrustum(l*_zoomFactor, r*_zoomFactor, b*_zoomFactor, t*_zoomFactor, nearClipPlane, farClipPlane);
glLoadIdentity();
//-----------------------------
//Draw 3-axis
glPushMatrix();
glLineWidth(2.5);
//x
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
//y
glColor3f( 0.0f, 1.0f, 0.0f );
glBegin(GL_LINES);
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
//z
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
glPopMatrix();
//-----------------------------
//draw a square
glMatrixMode( GL_MODELVIEW ); // Select the modelview matrix
glLoadIdentity();
glPushMatrix();
glRotatef( 45.0, 0.0f, 1.0f, 0.0f );
glColor3f( 0.5f, 0.5f, 1.0f ); // Set color to blue just once
glBegin( GL_QUADS ); // Draw a quad
glVertex3f( 0.0f, 0.5f, 0.0f ); // Top left
glVertex3f( 0.5f, 0.5f, 0.0f ); // Top right
glVertex3f( 0.5f, 0.0f, 0.0f ); // Bottom right
glVertex3f( 0.0f, 0.0f, 0.0f ); // Bottom left
glEnd(); // Quad is complete
glPopMatrix();
glFlush();
}
编辑1:
玩更多,我得到了我期待的一半,但其中一方是直的:
从此代码(代码段2):
- (void)drawRect:(NSRect)rect
{
glEnable(GL_DEPTH_TEST);
float _zoomFactor = 1.0;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glClearColor(1.0,1.0,1.0,1.0);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
float fieldOfView = 45.0;
float imageAspectRatio = rect.size.width / rect.size.height;
float nearClipPlane = 0.1;
float farClipPlane = 300.0;
float scale = tan(radians(fieldOfView * 0.5)) * nearClipPlane;
float r = imageAspectRatio * scale, l = -r;
float t = scale, b = -t;
glFrustum(l*_zoomFactor, r*_zoomFactor, b*_zoomFactor, t*_zoomFactor, nearClipPlane, farClipPlane);
//-----------------------------
glMatrixMode( GL_MODELVIEW );
glTranslated(0,0,-2.0);
//Draw 3-axis
glPushMatrix();
glLineWidth(2.5);
//x
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
//y
glColor3f( 0.0f, 1.0f, 0.0f );
glBegin(GL_LINES);
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
//z
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
glPopMatrix();
//-----------------------------
glPushMatrix();
glRotatef( 45.0, 1.0f, 0.0f, 0.0f );
glColor3f( 0.5f, 0.5f, 1.0f ); // Set color to blue just once
glBegin( GL_QUADS ); // Draw a quad
glVertex3f( 0.0f, 0.5f, 0.0f ); // Top left
glVertex3f( 0.5f, 0.5f, 0.0f ); // Top right
glVertex3f( 0.5f, 0.0f, 0.0f ); // Bottom right
glVertex3f( 0.0f, 0.0f, 0.0f ); // Bottom left
glEnd(); // Quad is complete
glPopMatrix();
glFlush();
}