码迷,mamicode.com
首页 > 其他好文 > 详细

STAR

时间:2016-05-06 19:03:54      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

#include <windows.h>
#include <gl/gl.h>
#include <cmath>
#include <cstdlib>

const long double time_unit=0.0001;
const GLdouble PI= 3.1415926536;
bool Clear = true;
long double detail=1.0;

struct mess_point {
    long double m,x,y,vx,vy;
    void move() {
        x+=vx*time_unit;
        y+=vy*time_unit;
    }
};

typedef struct mess_point MP;

void attract(MP &a,MP &b) {
    long double z=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    z*=sqrt(z);
    z=1/z;
    z*=time_unit;
    a.vx+=b.m*(b.x-a.x)*z;
    b.vx+=a.m*(a.x-b.x)*z;
    a.vy+=b.m*(b.y-a.y)*z;
    b.vy+=a.m*(a.y-b.y)*z;
}

struct universe {
    MP p,q,s;
    void process(double time) {
        int t=floor(time/time_unit+0.5)+1;
        while(--t) {
            attract(p,q);
            attract(p,s);
            attract(q,s);
            p.move();
            q.move();
            s.move();
        }
    }
    void input() {
        p.m= ((long double) (rand()&0xfff))/0x1000;
        p.x=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        p.y=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        p.vx=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        p.vy=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        
        q.m= ((long double) (rand()&0xfff))/0x1000;
        q.x=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        q.y=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        q.vx=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        q.vy=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        
        
        s.m= ((long double) (rand()&0xfff))/0x1000;
        s.x=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        s.y=((long double) (rand()&0x1fff)-0x1000)/0x2000;
        s.vx=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        s.vy=((long double) (rand()&0x1fff)-0x1000)/0x1000;
        
        long double summ=p.m+q.m+s.m,ss=(p.x*p.m+q.x*q.m+s.x*s.m)/summ;
        p.x-=ss;q.x-=ss;s.x-=ss;
        ss=(p.y*p.m+q.y*q.m+s.y*s.m)/summ;
        p.y-=ss;q.x-=ss;s.y-=ss;
        ss=(p.vx*p.m+q.vx*q.m+s.vx*s.m)/summ;
        p.vx-=ss;q.vx-=ss;s.vx-=ss;
        ss=(p.vy*p.m+q.vy*q.m+s.vy*s.m)/summ;
        p.vy-=ss;q.vx-=ss;s.vy-=ss;
    }
    
    void output_gl(HDC &hDC) {
        int count;
        double x=p.x*detail,y=p.y*detail,m=0.01*sqrt(p.m);
        if(Clear){
            glClearColor (0.0, 0.0, 0.0, 0.0);
            glClear (GL_COLOR_BUFFER_BIT);
        }
        glPushMatrix ();
        glColor3d (1.0, 0.0, 0.0);
        
        glBegin(GL_TRIANGLE_FAN);
        glVertex2d(x, y);
        
        for(count=0; count<=8; ++count)
            glVertex2d(x+m*cos(PI/4*count), y+m*sin(PI/4*count));
        glEnd();
        
        x=q.x*detail;
        y=q.y*detail;
        m=0.01*sqrt(q.m);
        glColor3d (0.0, 1.0, 0.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex2d(x, y);
        for(count=0; count<=8; ++count)
            glVertex2d(x+m*cos(PI/4*count), y+m*sin(PI/4*count));
        glEnd();
        
        x=s.x*detail;
        y=s.y*detail;
        m=0.01*sqrt(s.m);
        glColor3d (0.0, 0.0, 1.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex2d(x, y);
        for(count=0; count<=8; ++count)
            glVertex2d(x+m*cos(PI/4*count), y+m*sin(PI/4*count));
        glEnd();
        
        glPopMatrix ();
        SwapBuffers (hDC);
    }
};
/**************************
 * Includes
 *
 **************************/

#include "star3.hpp"
/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

universe MyUniv;
long double process_time=0.005;
/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow) {
    srand(GetTickCount());
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "Star";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
               "Star", "Star",
               WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
               0, 0, 764, 764,
               NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);
    MyUniv.input();
    /* program main loop */
    while (!bQuit) {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT) {
                bQuit = TRUE;
            } else {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        } else {
            /* OpenGL animation code goes here */
            if(process_time>0) {
                MyUniv.process(process_time);
                MyUniv.output_gl(hDC);
            }
            Sleep (3);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam) {

    switch (message) {
        case WM_CREATE:
            return 0;
        case WM_CLOSE:
            PostQuitMessage (0);
            return 0;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
            switch (wParam) {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                    return 0;
                case  :
                    MyUniv.input();
                    process_time = 0.0001;
                    return 0;
                case VK_UP:
                    process_time += 0.001;
                    return 0;
                case VK_DOWN:
                    if((process_time -= 0.001)<0.0) process_time=0.0;
                    return 0;
                case Z:
                    process_time += 0.0001;
                    return 0;
                case X:
                    if((process_time -= 0.0001)<0.0) process_time=0.0;
                    return 0;
                case C:
                    process_time= 0.0;
                    return 0;
                case V:
                    Clear = !Clear;
                    return 0;
                case A:
                    detail *= 2;
                    return 0;
                case S:
                    detail /= 2;
                    return 0;
            }
            return 0;

        default:
            return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) {
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 8;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) {
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

 

STAR

标签:

原文地址:http://www.cnblogs.com/TheName/p/5466445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!