OpenGL从1.0开始--曲线图形

来源:互联网 发布:xp无法添加网络打印机 编辑:程序博客网 时间:2024/05/17 08:56

这一节我们继续来实现一些有趣的高级图形,例如蜗形线limacon、心形线cardioid、三叶草threeLeaf、四叶草fourLeaf和螺旋线spiral等漂亮的曲线图形,如下面这些图形所示。
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

#include <gl/glut.h>#include <stdlib.h>#include <math.h>#include <iostream>using namespace std;const GLdouble twoPi = 6.283185;class screenPt{public:    GLint x, y;};typedef enum{limacon=1,cardioid,threeLeaf,fourLeaf,spiral}curveName;//五种图形的枚举GLsizei winWidth = 600, winHeight = 500;void init(void){    glClearColor(1.0, 1.0, 1.0, 1.0);    glMatrixMode(GL_PROJECTION);    gluOrtho2D(0.0, 200.0, 0.0, 150.0);}void lineSegment(screenPt pt1, screenPt pt2)//线段绘制函数,本节也是用曲线细分的方法绘制图形{    glBegin(GL_LINES);    glVertex2i(pt1.x, pt1.y);    glVertex2i(pt2.x, pt2.y);    glEnd();}void drawCurve(GLint curveNum)//核心绘制函数{    const GLint a = 175, b = 60;//任意的两个曲线参数    GLfloat r, theta, dtheta = 1.0 / float(a);    GLint x0 = 200, y0 = 250;//设置一个任意的屏幕初始坐标    screenPt curvePt[2];    glColor3f(0.0, 0.0, 0.0);    curvePt[0].x = x0;//初始化曲线位置    curvePt[0].y = y0;    switch (curveNum)    {    case  limacon:curvePt[0].x += a + b; break;    case cardioid:curvePt[0].x += a + a; break;    case threeLeaf : curvePt[0].x += a; break;    case fourLeaf:curvePt[0].x += a; break;    case spiral:break;    default:        break;    }    theta = dtheta;    while (theta<twoPi)//曲线细分    {        switch (curveNum)//不同的曲线适用不同的极函数        {        case limacon:            r = a*cos(theta) + b; break;        case cardioid:            r = a*(1 + cos(theta)); break;        case threeLeaf:            r = a*cos(3 * theta); break;        case fourLeaf:            r = a*cos(2 * theta);break;        case  spiral:            r = (a / 4.0)*theta; break;        default:            break;        }        curvePt[1].x = x0 + r*cos(theta);//绘制细分线段的终点        curvePt[1].y = y0 + r*sin(theta);        lineSegment(curvePt[0], curvePt[1]);//绘制该细分线段        curvePt[0].x = curvePt[1].x;//细分线段的起点坐标更新为终点坐标        curvePt[0].y = curvePt[1].y;        theta += dtheta;//曲线往前绘制    }}void displayFcn(void){    GLint curveNum;    glClear(GL_COLOR_BUFFER_BIT);    cout << "\nEnter the integer value corresponding to\n";    cout <<"one of the following curve names.\n";    cout << "\Press any other key to exit.\n";    cout << "\n1-limacon,2-cardioid,3-threeLeaf,4-fourLeaf,5-spiral:";    cin >> curveNum;//选择要绘制的曲线类型    if (curveNum == 1 || curveNum == 2 || curveNum == 3 || curveNum == 4 || curveNum == 5)    {        drawCurve(curveNum);//调用曲线绘制函数    }    else    {        exit(0);    }    glFlush();}void winReshapeFcn(GLint newWidth, GLint newHeight){    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));    glClear(GL_COLOR_BUFFER_BIT);}void main(int argc, char**argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);    glutInitWindowPosition(100, 100);    glutInitWindowSize(winWidth, winHeight);    glutCreateWindow("Draw Curves");    init();    glutDisplayFunc(displayFcn);    glutReshapeFunc(winReshapeFcn);    glutMainLoop();}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微博登录密码忘记了怎么办 助学贷款密码忘了怎么办 助学贷款的密码忘了怎么办 助学贷款登录密码忘了怎么办 抖音换了手机登不了怎么办 微信忘记密码申诉失败怎么办 银行卡短信通知没了怎么办 斗鱼手机号被绑定了怎么办 网易邮箱不记得账号怎么办 qq号被盗申诉不回来怎么办 号被盗了申诉不回来怎么办 富士康离职后工资卡号没填怎么办? 填高考志愿时没填电话号码怎么办 铁路12306密码忘了怎么办 微销通提示网络请求失败怎么办 苹果手机出现不停播报情况怎么办 手做俯卧撑抬不起来怎么办 如果qq号被冻结了怎么办 买的qq号被冻结怎么办 联想平板怎么下载东西怎么办 所有浏览器都安装不了怎么办 忘记微博登录邮箱怎么办 丰巢验证码丢了怎么办 微博昵称忘了怎么办 163邮箱账号忘记了怎么办 苹果手机id邮箱忘记了怎么办 微信号没注销了怎么办 微信昵称换不了怎么办 微博支付密码忘记怎么办 微博支付密码忘记了怎么办 华为手机看不见截屏怎么办 支付宝密码错误被锁定怎么办 实名身份证的微信限额怎么办 工行网银登陆密码忘了怎么办 美亚买的东西坏了怎么办 海带宝转运到香港怎么办 海带宝转运仓库入库错误怎么办 三星s7edge密码忘了怎么办 ie网页被拦截了怎么办 手机打开百度网页弹出广告怎么办 网站策略服务已被禁用怎么办