OpenGL---绘制简单图形

参看:http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html

20边形

#include<gl/GLUT.H>  
#include<math.h>

const int n = 20;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{  
    int i;  //定义在最前面
    glClear(GL_COLOR_BUFFER_BIT);  //清除颜色
    glBegin(GL_POLYGON);  //显示多边形模式
    for(i=0; i<n; i++)
        glVertex2f( R*cos(2*Pi/n*i), R*sin(2*Pi/n*i) );
    glEnd();
    glFlush();  //立即执行前面的命令,而不是在缓冲区中等待
}  

int main(int argc, char *argv[])  
{  
    glutInit(&argc,argv);  //对GLUT进行初始化,必须在其它的GLUT使用之前调用一次
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);  //设置显示方式,使用RGB颜色,单缓冲
    glutInitWindowPosition(100, 100);  //设置窗口在屏幕中的位置
    glutInitWindowSize(400, 400);  //设置窗口的大小
    glutCreateWindow("opengl");  //创建窗口,窗口标题为“opengl”
    glutDisplayFunc(&myDisplay);  //设置画图的函数
    glutMainLoop();  //消息循环
    return 0;  
}  

这里写图片描述

sin曲线

#include<gl/GLUT.H>  
#include<math.h>

const GLfloat factor = 0.1f;
void myDisplay(void)
{  
    GLfloat x;  
    glClear(GL_COLOR_BUFFER_BIT);  //清除颜色
    glBegin(GL_LINES);  //每两个顶点为一条直线模式
        glVertex2f(-1.0, 0); //x轴
        glVertex2f(1, 0); 

        glVertex2f(0, -1.0);  //y轴
        glVertex2f(0, 1.0);
    glEnd();

    glBegin(GL_LINE_STRIP);  //所有的点相连模式
        for(x=-1.0f/factor; x<1.0/factor; x+=0.01f)
            glVertex2f(x*factor, sin(x)*factor);
    glEnd();
    glFlush();  //立即执行前面的命令,而不是在缓冲区中等待
}  

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值