opengl中的对象透明度
我创建了一个固定框架,然后绘制了平行六面体。接下来,我创建了另一个框架,它应该是附加到对象的对象框架。在启用深度测试之前,对象框架的轴在原点相交,但平行六面体的颜色似乎发生干扰,一切都变得混乱。例如:物体的正面是红色的,如果我在绘制物体之前不添加深度测试,底部就会变成红色,如下图所示。
当我在绘制对象之前添加深度测试时,颜色是正确的,但对象框架的轴不正确如图所示相交,其中物体的正面为红色。 这是我的主窗口的图片:
这是我的代码:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
glPushMatrix();
glTranslatef(1.1, 2.1, -3.1);
glScalef(0.8,0.8,0.8);
glLineWidth(3.0f);
glBegin(GL_LINES);
//X_axis
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
//Y_axis
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
//Z_axis
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();
// rotation about X axis
glRotatef(x,0.0,0.0,1.0);
// rotation about Y axis
glRotatef(y,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(z,-1.0,0.0,0.0);
//glRotatef(90.0f,0.0f,0.1f,0.0f);
// Draw_Object
glEnable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
// Top (y=1.0)
glColor4f(0.0f, 1.0f, 0.0f,0.5f); // Green
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor4f(1.0f, 0.5f, 0.0f,0.5f); // Orange
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor4f(1.0f, 0.0f,0.0f,0.5f); // Red
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor4f(1.0f, 1.0f, 0.0f,0.5f); // Yellow
glVertex3f(2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor4f(0.0f, 0.0f, 1.0f,0.5f); // Blue
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor4f(1.0f, 0.0f, 1.0f,0.5f); // Purple
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
glEnd();
glFlush();
// Object_frame
glRotated(-90,0.0,1.0,0.0);
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();
I created a fixed frame then I drew the parallelepiped. Next, I created another frame that's supposed to be the object frame which is attached to the object. Before enabling the depth test, the object frame's axes intersect at the origin but the colors of the parallelepiped seems to interfere and everything is messed up. For example: the front face of the object is red, if I don't add the depth test just before drawing the object, the bottom becomes red as shown in the following picture.
When I add the depth test just before drawing the object, the colors are correct but the axes of the object's frame don't intersect as shown in the picture where the front face of the object is red.
Here is a picture of my MainWindow:
Here is my code:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
glPushMatrix();
glTranslatef(1.1, 2.1, -3.1);
glScalef(0.8,0.8,0.8);
glLineWidth(3.0f);
glBegin(GL_LINES);
//X_axis
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
//Y_axis
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
//Z_axis
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();
// rotation about X axis
glRotatef(x,0.0,0.0,1.0);
// rotation about Y axis
glRotatef(y,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(z,-1.0,0.0,0.0);
//glRotatef(90.0f,0.0f,0.1f,0.0f);
// Draw_Object
glEnable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
// Top (y=1.0)
glColor4f(0.0f, 1.0f, 0.0f,0.5f); // Green
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor4f(1.0f, 0.5f, 0.0f,0.5f); // Orange
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor4f(1.0f, 0.0f,0.0f,0.5f); // Red
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor4f(1.0f, 1.0f, 0.0f,0.5f); // Yellow
glVertex3f(2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor4f(0.0f, 0.0f, 1.0f,0.5f); // Blue
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor4f(1.0f, 0.0f, 1.0f,0.5f); // Purple
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
glEnd();
glFlush();
// Object_frame
glRotated(-90,0.0,1.0,0.0);
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论