我有一个使用openGL绘制的3d字母A
。
我一步一步地绘制它 - 绘制2d字母A,绘制带有z
偏移的2d字母A,绘制边,绘制底部矩形等。
结果看起来像这样
问题是我可以看到字母右侧的颜色 - >左侧的多边形看起来像透明 当我切换边的初始化顺序(左 - >右)时,它看起来像这样
如果我不需要旋转字母,那不是问题。在旋转期间,右侧是透明的(等等)。 我应该为多边形使用什么模式?
我画这样的字母
void drawLetter(float offset, float *color) {
Point p2_next = p2;
p2_next.z -= offset;
Point p7_next = p7;
p7_next.z -= offset;
Point p1_next = p1;
p1_next.z -= offset;
Point main_polygon[] = { p2_next, p7_next, p1_next };
Point norm = normal(main_polygon);
glNormal3f(norm.x, norm.y, norm.z);
//p1, p2, p3, p10
glBegin(GL_POLYGON);
glColor3fv(color);
glVertex3f(p1.x, p1.y, p1.z - offset);
glVertex3f(p2.x, p2.y, p2.z - offset);
glVertex3f(p3.x, p3.y, p3.z - offset);
glVertex3f(p10.x, p10.y, p10.z - offset);
glEnd();
//p1, p10, p6, p7
glBegin(GL_POLYGON);
glColor3fv(color);
glVertex3f(p1.x, p1.y, p1.z - offset);
glVertex3f(p10.x, p10.y, p10.z - offset);
glVertex3f(p6.x, p6.y, p6.z - offset);
glVertex3f(p7.x, p7.y, p7.z - offset);
glEnd();
//p8, p4, p5, p9
glBegin(GL_POLYGON);
glColor3fv(color);
glVertex3f(p8.x, p8.y, p8.z - offset);
glVertex3f(p4.x, p4.y, p4.z - offset);
glVertex3f(p5.x, p5.y, p5.z - offset);
glVertex3f(p9.x, p9.y, p9.z - offset);
glEnd();
}
(还有侧面,底部矩形等功能).z
例如drawSides
void drawSides(float offset, Point pa, Point pb, float *color) {
Point p1_next = pa;
Point p2_next = pa;
p2_next.z -= offset;
Point p3_next = pb;
Point main_polygon[] = { p1_next, p2_next, p3_next };
Point norm = normal(main_polygon);
glNormal3f(norm.x, norm.y, norm.z);
//p1, p7
glBegin(GL_POLYGON);
glColor3fv(color);
glVertex3f(pa.x, pa.y, pa.z);
glVertex3f(pa.x, pa.y, pa.z - offset);
glVertex3f(pb.x, pb.y, pb.z - offset);
glVertex3f(pb.x, pb.y, pb.z);
glEnd();
}
这是我的绘制功能
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
axes();
if (angle > 180) angle = 0;
if (angle < 0) angle = 360;
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, LIGHT);
glRotatef(angle, 0.0, 1.0, 0.0);
drawLetter(offset, color_letter);
drawRibs(offset);
drawBottoms(offset, p2, p3, bottom);
drawRibsExt(offset, p2, p3);
drawBottoms(offset, p3, p4, side_left);
drawRibsExt(offset, p3, p4);
drawBottoms(offset, p4, p5, side_right);
drawRibsExt(offset, p4, p5);
drawBottoms(offset, p5, p6, side_left);
drawRibsExt(offset, p5, p6);
drawBottoms(offset, p6, p7, bottom);
drawRibsExt(offset, p6, p7);
drawBottoms(offset, p8, p9, side_left);
drawRibsExt(offset, p8, p9);
drawBottoms(offset, p9, p10, side_left);
drawRibsExt(offset, p9, p10);
drawBottoms(offset, p10, p8, side_left);
drawRibsExt(offset, p10, p8);
drawSides(offset, p2, p1, side_left); //switch -> right is transparent
drawSides(offset, p7, p1, side_right); //switch -> left is transparent
drawLetter(0, color_letter);
drawRibs(0);
glPopMatrix();
glutSwapBuffers();
glFlush();
}