第5章代码-三维观察
程序员文章站
2023-12-29 10:06:58
...
5.5 编程实例
5.5.1 二维实例——红蓝三角形
#include <GL/glut.h>
typedef GLfloat point2d[2]; //a point data type
void triangle( point2d a, point2d b, point2d c) //display a triangle
{
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}
void display(void)
{
point2d v[3] = {{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glLoadIdentity();
glViewport(0, 0, 300, 400);
triangle(v[0], v[1], v[2]);
glColor3f(1.0,0.0,0.0);
glViewport(300, 0, 300, 400);
glRotatef(90.0, 0.0, 0.0, 1.0);
triangle(v[0], v[1], v[2]);
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0,1.0);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600, 400);
glutCreateWindow("Triangle");
glutDisplayFunc(display);
init();
glutMainLoop();
}
5.5.2 三维实例——立方体透视投影
#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600; //设置初始化窗口大小
/* 观察坐标系参数设置*/
GLfloat x0 = 0.0, y0= 0.0, z0 =5.0; //设置观察坐标系原点
GLfloat xref = 0.0, yref =0.0, zref = 0.0; //设置观察坐标系参考点(视点)
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0; //设置观察坐标系向上向量(y轴)
/*观察体参数设置 */
//设置裁剪窗口坐标范围
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0; //设置远、近裁剪面深度范围
void init (void)
{
glClearColor(1.0,1.0,1.0,0.0);
/*①观察变换*/
gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz); //指定三维观察参数
/*模型变换*/
glMatrixMode (GL_MODELVIEW);
glScalef (2.0, 2.0, 2.0); //比例放缩变换
glRotatef(45.0, 0.0, 1.0, 1.0); //旋转变换
/*②投影变换*/
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//透视投影,设置透视观察体
glFrustum (xwMin, xwMax, ywMin, ywMax, dnear, dfar);
}
void display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0); //设置前景色为绿色
glutSolidCube (1.0); //绘制单位立方体实体
glColor3f (0.0, 0.0, 0.0); //设置前景色为黑色
glLineWidth (2.0); //设置线宽
glutWireCube (1.0); //绘制单位立方体线框
glFlush ();
}
void reshape (GLint newWidth, GLint newHeight)
{
/*③视口变换 */
glViewport (0, 0, newWidth, newHeight); //定义视口大小
winWidth = newWidth;
winHeight = newHeight;
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("单位立方体的透视投影");
init ();
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
}
推荐阅读