我需要A
使用 opengl 绘制一个 3d 字母和轴。为了画字母A
,我用 2d画字母A
,z == 50
然后A
用画字母z = 50 - offset
。
这里我的opengl和字母坐标初始化
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("PROJECT");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -100, 100);
glMatrixMode(GL_MODELVIEW);
p1.x = 0; p1.y = 400 ; p1.z = 50;
p2.x = -300; p2.y = -500; p2.z = 50;
p3.x = -200; p3.y = -500; p3.z = 50;
p4.x = -100; p4.y = -200; p4.z = 50;
p5.x = 100; p5.y = -200; p5.z = 50;
p6.x = 200; p6.y = -500; p6.z = 50;
p7.x = 300; p7.y = -500; p7.z = 50;
p8.x = -130; p8.y = -100; p8.z = 50;
p9.x = 130; p9.y = -100; p9.z = 50;
p10.x = 0; p10.y = 100; p10.z = 50;
glutDisplayFunc(display);
glutMainLoop();
在函数显示中,我绘制轴和两个字母A
。
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
axes();
drawLetter(0);
drawLetter(100);
glFlush();
glutSwapBuffers();
}
这里轴的初始值
float angle = 17;
float AXIS_X[3] = { 250, 0, 0 },
AXIS_Y[3] = { 0, 250, 0 },
AXIS_Z[3] = { 0, 0, 150 };
我像这样画轴
void axes() {
glPushMatrix();
glTranslatef(WIDTH/2, HEIGHT/2, 0);
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3fv(AXIS_X);
glEnd();
glBegin(GL_LINES);
glColor3f(0, 0, 1);
glVertex3f(0.0, 0.0, 0.0);
glVertex3fv(AXIS_Y);
glEnd();
glBegin(GL_LINES);
glColor3f(0, 1, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3fv(AXIS_Z);
glEnd();
glPopMatrix();
}
这是这封信的代码
void drawLetter(float offset) {
glPushMatrix();
glTranslatef(WIDTH/2, HEIGHT/2, 0);
glScalef(0.3, 0.3, 0.3);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
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();
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
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();
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
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();
glPopMatrix();
}
默认情况下,它看起来像这样
但是当我在它看起来像这样glRotatef(angle, 0.0, 1.0, 0.0);
之前添加时(简单的绿线是的)axes()
有什么问题?
还有一个问题 - 如何为我的信在不同的平面(xy、yz、xz)上创建正交和透视投影?我需要使用glFrustrum
和glOrtho
?