如何在 OpenGL 中渲染具有不同颜色边的立方体?
How do you render a cube with different colored sides in OpenGL?
作为练习,我正在尝试渲染一个立方体的某些面有不同的颜色,但问题是当我沿 y 轴旋转立方体时,我仍然可以通过侧面看到不同颜色的一面面对相机。我已经尝试将代码拆分为每一侧单独的 glBegin() 块,并且我已经尝试在 google 上四处寻找答案,但没有成功。根据 glColor3f、"glcolor3 variants specify new red, green, and blue values explicitly and set the current alpha value to 1.0 (full intensity) implicitly." 上的 Microsoft 文档,因此透明度应该不是问题...
Here is the picture representing the problem
下面是立方体的渲染代码:
@Override public void render()
{
glPushMatrix( );
{
glTranslatef( 0, 0, -4 );
glRotatef( x, 0, 1, 0 );
glColor3f( 0f, 1f, 0f );
glBegin( GL_QUADS );
{
glVertex3f( -1, 1, 1 );
glVertex3f( -1, -1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, 1, 1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( 1, -1, -1 );
glVertex3f( 1, 1, -1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, 1, 1 );
glVertex3f( 1, 1, 1 );
glVertex3f( 1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, -1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, -1, -1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, -1, 1 );
glVertex3f( -1, 1, 1 );
glColor3f( 1f, 0f, 0f );
glVertex3f( 1, 1, -1 );
glVertex3f( 1, -1, -1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, 1, 1 );
}
glEnd( );
}
glPopMatrix( );
}
这是我的渲染循环:
protected void render( )
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity( );
for ( IGameObject gameObject : gameObjects )
gameObject.render( );
glfwSwapBuffers( window );
}
您需要enable Z-testing:
glEnable(GL_DEPTH_TEST);
更改对 glClear
的调用以同时清除深度缓冲区:
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
您似乎没有使用深度测试。
OpenGL 按照您告诉它的顺序绘制形状,因此如果您最后绘制红色面,它的片段(即像素)会覆盖之前绘制的绿色面。由于您(大概)想要看到 "in front" 实际 出现在前面 的面孔,您必须告诉 OpenGL 不要绘制 "behind" 东西的片段已经画好了。
将 glClear( GL_COLOR_BUFFER_BIT )
行替换为:
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BIT );
这告诉 OpenGL 每次要绘制一个片段时,它应该将其深度与已经绘制的深度进行比较,如果新片段比旧片段更远,则丢弃它。它还会清除深度缓冲区,以便最初屏幕上的每个像素都 "infinitely" 很远,这样立方体的面都将位于背景的前面。这将防止当有绿色人脸靠近相机时出现红色人脸。
作为练习,我正在尝试渲染一个立方体的某些面有不同的颜色,但问题是当我沿 y 轴旋转立方体时,我仍然可以通过侧面看到不同颜色的一面面对相机。我已经尝试将代码拆分为每一侧单独的 glBegin() 块,并且我已经尝试在 google 上四处寻找答案,但没有成功。根据 glColor3f、"glcolor3 variants specify new red, green, and blue values explicitly and set the current alpha value to 1.0 (full intensity) implicitly." 上的 Microsoft 文档,因此透明度应该不是问题...
Here is the picture representing the problem
下面是立方体的渲染代码:
@Override public void render()
{
glPushMatrix( );
{
glTranslatef( 0, 0, -4 );
glRotatef( x, 0, 1, 0 );
glColor3f( 0f, 1f, 0f );
glBegin( GL_QUADS );
{
glVertex3f( -1, 1, 1 );
glVertex3f( -1, -1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, 1, 1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( 1, -1, -1 );
glVertex3f( 1, 1, -1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, 1, 1 );
glVertex3f( 1, 1, 1 );
glVertex3f( 1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, -1, 1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, -1, -1 );
glVertex3f( -1, 1, -1 );
glVertex3f( -1, -1, -1 );
glVertex3f( -1, -1, 1 );
glVertex3f( -1, 1, 1 );
glColor3f( 1f, 0f, 0f );
glVertex3f( 1, 1, -1 );
glVertex3f( 1, -1, -1 );
glVertex3f( 1, -1, 1 );
glVertex3f( 1, 1, 1 );
}
glEnd( );
}
glPopMatrix( );
}
这是我的渲染循环:
protected void render( )
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity( );
for ( IGameObject gameObject : gameObjects )
gameObject.render( );
glfwSwapBuffers( window );
}
您需要enable Z-testing:
glEnable(GL_DEPTH_TEST);
更改对 glClear
的调用以同时清除深度缓冲区:
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
您似乎没有使用深度测试。
OpenGL 按照您告诉它的顺序绘制形状,因此如果您最后绘制红色面,它的片段(即像素)会覆盖之前绘制的绿色面。由于您(大概)想要看到 "in front" 实际 出现在前面 的面孔,您必须告诉 OpenGL 不要绘制 "behind" 东西的片段已经画好了。
将 glClear( GL_COLOR_BUFFER_BIT )
行替换为:
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BIT );
这告诉 OpenGL 每次要绘制一个片段时,它应该将其深度与已经绘制的深度进行比较,如果新片段比旧片段更远,则丢弃它。它还会清除深度缓冲区,以便最初屏幕上的每个像素都 "infinitely" 很远,这样立方体的面都将位于背景的前面。这将防止当有绿色人脸靠近相机时出现红色人脸。