openGL 蓝天白云

#include "stdafx.h"
#include<GL/glut.h>
#include<math.h>
#include<stdlib.h>
#include <ctime>


const double TWO_PI = 6.2831853;

/*Initial display-window size*/
GLsizei winWidth = 800, winHeight = 600;
GLuint regHex;

class screenPt
{
private:
    GLint x, y;
public:
    /*Default Constructor:initalizes coordinate position to(0,0).*/
    screenPt() { x = y = 0; }
    void setGoords(GLint xGoord, GLint yGoord) { x = xGoord; y = yGoord; }
    GLint getx()const { return x; }
    GLint gety()const { return y; }
};

void drawcloud (GLint x, GLint y, GLint z)
{
    GLdouble theta;
    GLint k;
    glBegin(GL_POLYGON);
    for (k = 0; k < 360; k++)
    {

        theta = TWO_PI*k / 360.0;
        glVertex2f(x * cos(theta) + y,x * sin(theta) + z);

    }
    glEnd();
}//画云二的过程

static void init(void)
{
    screenPt hexVertex, circCtr;
    GLdouble theta;
    GLint k,j;

    srand((int)time(0));
    

    circCtr.setGoords(0, winHeight );

    glClearColor(1.0, 1.0, 1.0, 0.0);
    regHex = glGenLists(1);
    glNewList(regHex, GL_COMPILE);

    /*添加*/
    glShadeModel(GL_SMOOTH);

    
    glBegin(GL_QUADS);
    glColor3f(0.6,0.8,0.9);
    glVertex2f(0.0, 0.0);
    glVertex2f(winWidth, 0.0);
    glColor3f(0.0,0.2,0.8);
    glVertex2f(winWidth, winHeight);
    glColor3f(1.0, 1.0, 1.0);
    glVertex2f(0.0, winHeight);//背景
    
    glEnd();
    
    glEnable(GL_POINT_SMOOTH);
    glPointSize(15.0);
    glBegin(GL_POINTS);
    for (j = 0; j <= 8;j++)
    {
        glColor3f(0.60 + j*0.05, 0.60 + j*0.05, 0.60 + j * 0.05);
        for (k = 0; k < (25-j); k++)
        {
            
            glVertex2f(490 + j * 5 + k * 10, 100 + j * 5 + rand() % 8);            
        }    
    }//cloud1

    glEnd();

    
    /*draw cloud2*/
    glColor3f(0.9, 0.9, 0.9);
    for (GLint i = 3; i <=8; i++)
    {
        drawcloud(100 / i, 400 + i * 15, 500 + rand() % 10);

    }//云的腰
    for (GLint i = 9; i <13; i++)
    {
        drawcloud(500/ (i*i), 480+i*5, 500 + rand() % 5);

    }//云的尾巴
    glColor3f(0.91, 0.91, 0.91);
    drawcloud(30, 400, 500);
    drawcloud(30, 420, 530);//云的头
    
    

    /*draw cloud 3*/
    glColor3f(1.0, 1.0, 1.0);
    drawcloud(30, 300, 400);
    drawcloud(30, 320, 430);
    drawcloud(30, 340, 410);
    
    for (int i = 1; i < 20; i++)
    {
        glColor3f(1.0, 1.0,1.0);
        glPointSize(20.0-i);
        glBegin(GL_POINTS);
        glVertex2f(290-i, 380-i+ rand() % 5);
        glEnd();//云的角

    }
    glColor3f(1.0, 1.0,1.0);
    for (GLint i = 3; i <= 8; i++)
    {
        drawcloud(100 / i, 300 + i * 15, 400 + rand() % 10);

    }//云的腰
    for (int i = 1; i < 20; i++)
    {
        glColor3f(1.0, 1.0, 1.0);
        glPointSize(20.0 - i);
        glBegin(GL_POINTS);
        glVertex2f(430 +i, 410 -rand() % 5);
        glEnd();//云的尾巴2

    }
    /*draw cloud 4*/
        glColor3f(0.9, 0.9, 0.9);
    for (GLint i = 3; i <=8; i++)
    {
        drawcloud(100 / i, 200 + i * 15, 200 + rand() % 10);

    }//云的腰
    for (GLint i = 9; i <13; i++)
    {
        drawcloud(500/ (i*i), 280+i*5, 200 + rand() % 5);

    }//云的尾巴
    glColor3f(0.91, 0.91, 0.91);
    drawcloud(30, 200, 200);
    drawcloud(30, 220, 230);//云的头



    glColor3f(1.0, 1.0, 1.0);//Set fill color for sun white
    glBegin(GL_POLYGON);
    for (k = 0; k < 360; k++)
    {
        theta = TWO_PI*k / 360.0;
        hexVertex.setGoords(circCtr.getx() + 80 * cos(theta),
            circCtr.gety() + 80* sin(theta));
        glVertex2i(hexVertex.getx(), hexVertex.gety());
    }
    glEnd();

    

    glEndList();

}

void regHexagon(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glCallList(regHex);
    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(200, 100);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("blue_sky_and_white_cloud");

    init();
    glutDisplayFunc(regHexagon);
    glutReshapeFunc(winReshapeFcn);
    glutMainLoop();
}

 

 

posted @ 2019-07-30 11:25  光之继承人  阅读(516)  评论(0编辑  收藏  举报