OPENGL编程练习

1.绘制一个圆形


#include <windows.h>
#include <GL/glut.h>
#include <bits/stdc++.h>
using namespace std;

GLdouble R=0.5;
const GLdouble PI=acos(-1.0);
//const GLdouble PI=atan(1.0);

void myDisplay(void)
{

     glClear(GL_COLOR_BUFFER_BIT);
     int num=200;
     glBegin(GL_POLYGON);
        for(int i=0;i<num;i++)
        {
            GLdouble rad=(2*PI)*i/num ;
            glVertex2d(R*cos( rad),R*sin(rad) );
        }
     glEnd();
     glFlush();

}

int main(int argc, char *argv[])

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

     glutInitWindowPosition(100, 100);

     glutInitWindowSize(400, 400);

     glutCreateWindow("第一个OpenGL程序");

     glutDisplayFunc(&myDisplay);

     glutMainLoop();

     return 0;

}


2.绘制五角星


1.利用线段绘制 GL_LINES
#include <windows.h>
#include <GL/glut.h>
#include <bits/stdc++.h>
using namespace std;
const GLdouble PI=acos(-1.0);

struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}

}p[5];
void myDisplay(void)
{

    GLdouble R=0.75;

    glClear(GL_COLOR_BUFFER_BIT);

    double rad=PI/2,delta= 72.0/180.0*PI;
     for(int i=0;i<5;i++)
    {
        rad-=delta;
        p[i]=Point(R*cos(rad),R*sin(rad));
    }
    glBegin(GL_LINES);
        for(int i=0;i<5;i++)
        {
            int j= (i+2)%5;
            glVertex2d(p[i].x,p[i].y   );
            glVertex2d(p[j].x,p[j].y   );
        }
    glEnd();




    glFlush();

}

int main(int argc, char *argv[])

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

     glutInitWindowPosition(100, 100);

     glutInitWindowSize(400, 400);

     glutCreateWindow("第一个OpenGL程序");

     glutDisplayFunc(&myDisplay);

     glutMainLoop();

     return 0;

}


2.利用 GL_LINE_LOOP绘制

#include <windows.h>
#include <GL/glut.h>
#include <bits/stdc++.h>
using namespace std;
const GLdouble PI=acos(-1.0);

struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}

}p[5];
void myDisplay(void)
{

    GLdouble R=0.75;

    glClear(GL_COLOR_BUFFER_BIT);

    double rad=PI/2,delta= 72.0/180.0*PI;
     for(int i=0;i<5;i++)
    {
        rad-=delta;
        p[i]=Point(R*cos(rad),R*sin(rad));
    }

    int cur=0;
    glBegin(GL_LINE_LOOP);
        for(int i=0;i<5;i++)
        {
            glVertex2d(p[cur].x,p[cur].y   );
            cur=(cur+2)%5;
        }
    glEnd();

    glFlush();

}

int main(int argc, char *argv[])

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

     glutInitWindowPosition(100, 100);

     glutInitWindowSize(400, 400);

     glutCreateWindow("第一个OpenGL程序");

     glutDisplayFunc(&myDisplay);

     glutMainLoop();

     return 0;

}

glMatrixMode()函数的参数中GL_PROJECTION和GL_MODELVIEW的作用:

原文地址:glMatrixMode()函数的参数中GL_PROJECTION和GL_MODELVIEW的作用     作者:WAN潮羅

这两个都是glMatrixMode()函数的参数,那就先说说glMatrixMode吧~,这个函数其实就是对接下来要做什么进行一下声明,也就是在要做下一步之前告诉计算机我要对“什么”进行操作了,这个“什么”在glMatrixMode的“()”里的选项(参数)有,GL_PROJECTION,GL_MODELVIEW和GL_TEXTURE;

如果参数是GL_PROJECTION,这个是投影的意思,就是要对投影相关进行操作,也就是把物体投影到一个平面上,就像我们照相一样,把3维物体投到2维的平面上。这样,接下来的语句可以是跟透视相关的函数,比如glFrustum()或gluPerspective();
如果参数是GL_MODELVIEW,这个是对模型视景的操作,接下来的语句描绘一个以模型为基础的适应,这样来设置参数,接下来用到的就是像gluLookAt()这样的函数;
若是GL_TEXTURE,就是对纹理相关进行操作;
顺便说下,OpenGL里面的操作,很多是基于对矩阵的操作的,比如位移,旋转,缩放,所以,这里其实说的规范一点就是glMatrixMode是用来指定哪一个矩阵是当前矩阵,而它的参数代表要操作的目标,GL_PROJECTION是对投影矩阵操作,GL_MODELVIEW是对模型视景矩阵操作,GL_TEXTURE是对纹理矩阵进行随后的操作。


设置清除颜色:
glClearColor(0.5, 0.5, 0.5, 1.0);


这句话可以调用上面设置的颜色,对屏幕染上该色:
glClear(GL_COLOR_BUFFER_BIT);

如果没有这句话,屏幕会一片空白,最终什么都没画上去:
 glFlush();

选择画笔的颜色:
glColor3f(0.0, 1.0, 0.0);




3.鼠标指针处画方框


#include <windows.h>
#include <GL/glut.h>
#include <bits/stdc++.h>
using namespace std;

GLint ww=500,wh=500;
GLdouble siz=5;
void init()
{
 /* attributes */
    glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
    glColor3f(1.0, 0.0, 0.0); /* draw in red */
 /* set up viewing */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, ww, 0.0, wh);
    glMatrixMode(GL_MODELVIEW);
}


void reshape(int w, int h)//设置视图方式,现在还不是太懂,目前改变窗口大小,窗口内的内容不会保存
{
    glViewport(0, 0, w, h); // 设置视口为整个窗口
    glMatrixMode(GL_PROJECTION); // 根据视口调整裁剪窗口
    glLoadIdentity();
    if(w<=h) // 宽小于高,裁剪矩形宽度置为4,高度按同样比例放大
    gluOrtho2D(0, ww,0,wh*(GLfloat)h/(GLfloat)w);
    else // 高小于宽,裁剪矩形高度置为4,宽度按同样比例放大
    gluOrtho2D(0,ww*(GLfloat)w/(GLfloat)h,0, wh);
    glMatrixMode(GL_MODELVIEW); /*return to modelview mode*/
    glLoadIdentity();
}



void myDisplay(void)
{

    glClear(GL_COLOR_BUFFER_BIT);


    glFlush();

}


void drawSquare(int x,int y)//绘制一个方框
{
    y=wh-y;//鼠标点击函数传来的y轴是指向屏幕下方的。
    glColor3ub(rand()%256,rand()%256,rand()%256);//unsigned byte 参数为[0,255]的整数,如果
    //是glColor3f 那么参数为[0,1]的浮点数
    glBegin(GL_POLYGON);
        glVertex2d(x-siz,y-siz);
        glVertex2d(x+siz,y-siz);
        glVertex2d(x+siz,y+siz);
        glVertex2d(x-siz,y+siz);
    glEnd();
    glFlush();
}
void mouse2(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) drawSquare(x,y);
    else if(button==GLUT_RIGHT_BUTTON&&state==GLUT_UP)  exit(0);

}
int main(int argc, char *argv[])

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

     glutInitWindowPosition(100, 100);

     glutInitWindowSize(ww,wh);

     glutCreateWindow("mouse");

     init();

     glutDisplayFunc(myDisplay);

    glutMouseFunc(mouse2);

    glutReshapeFunc(reshape);


     glutMainLoop();

     return 0;

}


4.方框随机颜色旋转


#include <windows.h>
#include <GL/glut.h>
#include <bits/stdc++.h>
using namespace std;
const GLdouble PI=acos(-1.0);
const GLdouble R=400;

GLint ww=500,wh=500;
GLdouble delta=0;
void init()
{
    glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
}

void reshape(int w, int h) //设置视图方式
{
	ww = w;
	wh = h;
//	glViewport(0, 0, ww, wh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-ww, ww, -wh, wh);
	glMatrixMode(GL_MODELVIEW);
}

void idle()
{
    delta+=0.1;
    if(delta>PI) delta=0;
    glutPostRedisplay();
}

void myDisplay(void)
{

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3ub(rand()%256,rand()%256,rand()%256);

    glBegin(GL_POLYGON);
        glVertex2d(R*cos(delta),R*sin(delta));
        glVertex2d(R*cos(delta+PI/2),R*sin(delta+PI/2));
        glVertex2d(R*cos(delta+PI),R*sin(delta+PI));
        glVertex2d(R*cos(delta+3*PI/2),R*sin(delta+3*PI/2));
    glEnd();



    glFlush();

}
void mouse(int btn, int state, int x, int y)
{
	if(btn== GLUT_LEFT_BUTTON&& state==GLUT_DOWN)
        glutIdleFunc(idle);
    else if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
        glutIdleFunc(NULL);//必须传一个值进去
    else if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_UP)
         exit(0);

}

int main(int argc, char *argv[])

{

     glutInit(&argc, argv);

     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

     glutInitWindowPosition(100, 100);

     glutInitWindowSize(ww, wh);

     glutCreateWindow("第一个OpenGL程序");

     init();


     glutDisplayFunc(myDisplay);

    glutReshapeFunc(reshape);

//    glutIdleFunc(idle);

   glutMouseFunc(mouse);

     glutMainLoop();

     return 0;

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值