标题:发个网友做的动画
只看楼主
鸣镝
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-12-9
得分:0 
终于下完了,回去欣赏下。
2010-12-09 16:34
wflCJV
Rank: 1
等 级:新手上路
帖 子:13
专家分:3
注 册:2010-12-9
得分:0 
我编译成功了,自己做了一个抛物线的 , 哎哟。。不错

有迷茫,也会过去。。。
2010-12-09 22:03
御坂美琴
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:魔術の禁書目錄
等 级:小飞侠
威 望:9
帖 子:952
专家分:2929
注 册:2010-8-18
得分:0 
你也做了一个?发上看看看

永远为正义而奋斗,锄强扶弱的Level 5 超能力者
とある魔術の禁書目錄インデックス__御み坂さか美み琴こと
http://bbs.bccn.net/space.php?action=threads&uid=483997
2010-12-09 22:12
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
得分:0 
你也做了一个? 发上来看看

我就是真命天子,顺我者生,逆我者死!
2010-12-09 22:44
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
得分:0 
你也做了一个? 发上来看看

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-12-09 22:46
你们都要疼我哦
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:火星
等 级:贵宾
威 望:49
帖 子:1296
专家分:2746
注 册:2008-7-13
得分:0 
你也做了一个? 发上来看看

小妹,哥哥看你骨骼清奇,绝非凡人,将来必成大业,不如这样,你先把裤裤脱了,待哥哥为你开启灵窍,然后我们一起努力钻研如何
2010-12-09 23:27
mengcong3959
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:41
专家分:113
注 册:2010-12-6
得分:0 
回复 5楼 御坂美琴
我下不了呀,,呵呵,,你能发我邮箱里面吗,,谢谢啦,,邮箱是395951405@
2010-12-10 16:46
御坂美琴
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:魔術の禁書目錄
等 级:小飞侠
威 望:9
帖 子:952
专家分:2929
注 册:2010-8-18
得分:0 
以下是引用mengcong3959在2010-12-10 16:46:10的发言:

我下不了呀,,呵呵,,你能发我邮箱里面吗,,谢谢啦,,邮箱是395951405@
发好了,多附带了一个物理碰撞的演示动画,里面附带了源码说明

永远为正义而奋斗,锄强扶弱的Level 5 超能力者
とある魔術の禁書目錄インデックス__御み坂さか美み琴こと
http://bbs.bccn.net/space.php?action=threads&uid=483997
2010-12-10 17:28
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
得分:0 
我仿了一份他的代码, 效果有差别。/
一直不理解,他的点怎么画成那个样子了,不干净

[ 本帖最后由 BlueGuy 于 2010-12-18 19:52 编辑 ]

我就是真命天子,顺我者生,逆我者死!
2010-12-18 19:47
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <GL/glut.h>

#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 640
#define WINDOW_X 200
#define WINDOW_Y 200 

#define FRAME_PER_SECOND 20
#define KEY_EXIT 27

#define POINT_NUM 200
#define MATRIX_NUM 8

#define bgRand(m) ((float)(rand() % 10000) * m / 10000.0f)

typedef struct bgPoint
{
    float x, y;
    float vx, vy;
    int r;
    int g;
    int b;
}BGPoint;
typedef BGPoint* Point;

typedef struct bgMatrix
{
    BGPoint point[POINT_NUM];
    int timer;
    int targetTime;
    int pauseTime;
}BGMatrix;
typedef BGMatrix* Matrix;

Matrix* matrix;

void handleKeyEvent(unsigned char key, int x, int y);
void handleMouseEvent(int button, int state, int x, int y);
void handleReshape(int width, int height);

void run(int null);
void repaint(void);
void update(void);

void initScene(void);
void doScene(void);
void drawScene(void);
void exitScene(void);

void initMatrix(void);
void doMatrix(void);
void drawMatrix(void);

Matrix creatSubMatrix(int subMatrxId);
void freeSubMatrix(int subMatrxId);

void sleepMilliSeconds(long milliSeconds);

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(WINDOW_X, WINDOW_Y);
    glutCreateWindow("BestMatrix");

    glutKeyboardFunc(handleKeyEvent);
    glutMouseFunc(handleMouseEvent);
    glutReshapeFunc(handleReshape);

    initScene();

    glutDisplayFunc(repaint);
    glutTimerFunc(FRAME_PER_SECOND, run, 0);

    glutMainLoop();

    return 0;  
}

void run(int null)
{  
    clock_t startTime, endTime;

    startTime = clock();

    update();
    repaint();

    endTime = clock();

    if (endTime - startTime < FRAME_PER_SECOND)
    {
        sleepMilliSeconds(FRAME_PER_SECOND - (endTime - startTime));
    }

    glutPostRedisplay();
    glutTimerFunc(FRAME_PER_SECOND, run, 0);       
}

void update(void)
{
    doScene();
}

void repaint(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    drawScene();

    glutSwapBuffers();
}

void initScene(void)
{
    srand(time(0));

    initMatrix();

    glPointSize(3.0);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D(0, (GLdouble)WINDOW_WIDTH, 0, (GLdouble)WINDOW_HEIGHT);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glEnable(GL_COLOR_MATERIAL);

}

void doScene(void)
{
    doMatrix();
}

void drawScene(void)
{
    drawMatrix();
}

void exitScene(void)
{
    exit(0);
}

void initMatrix(void)
{
    int i;

    matrix = malloc(sizeof(Matrix) * MATRIX_NUM);

    for (i = 0; i < MATRIX_NUM; i++)
    {
        matrix[i] = creatSubMatrix(i);
    }
}

void doMatrix()
{
    int i, j;

    for (i = 0; i < MATRIX_NUM; i++)
    {
        if (matrix[i]->timer++ > matrix[i]->targetTime)
        {
            for (j = 0; j < POINT_NUM; j++)
            {
                matrix[i]->point[j].vy += 0.1f;
                matrix[i]->point[j].x += matrix[i]->point[j].vx;
                matrix[i]->point[j].y += matrix[i]->point[j].vy;
            }
         }
       
         if (matrix[i]->timer > matrix[i]->targetTime + matrix[i]->pauseTime)
         {
             freeSubMatrix(i);
             matrix[i] = creatSubMatrix(i);
         }
    }
}

void drawMatrix(void)
{
    int i, j;

    unsigned char r, g, b;

    for (i = 0; i < MATRIX_NUM; i++)
    {
        for (j = 0; j < POINT_NUM; j++)
        {
            r = matrix[i]->point[j].r;
            g = matrix[i]->point[j].g;
            b = matrix[i]->point[j].b;
       
            glColor3ub(r, g, b);
            glBegin(GL_POINTS);
            glVertex2f(matrix[i]->point[j].x, WINDOW_HEIGHT-matrix[i]->point[j].y);
            glEnd();
        }
    }
}

Matrix creatSubMatrix(int subMatrxId)
{
    Matrix matrix;
    float x, y;
    int r, g, b;
    int i;

    matrix = malloc(sizeof(BGMatrix));

    matrix->timer = 0;
    matrix->targetTime = rand()%50;
    matrix->pauseTime = 50;

    x = 20.0f + bgRand(600.0f);
    y = 100.f + bgRand(100.0f);
    r = 50 + rand()%(255-50);
    g = 50 + rand()%(255-50);
    b = 50 + rand()%(255-50);

    for (i = 0; i < POINT_NUM; i++)
    {
        matrix->point[i].x = x;
        matrix->point[i].y = y;
        matrix->point[i].vx = 1.0f - bgRand(2.0f);
        matrix->point[i].vy = 1.0f - bgRand(2.0f);
        matrix->point[i].r = r;
        matrix->point[i].g = g;
        matrix->point[i].b = b;
    }

    return matrix;
}

void freeSubMatrix(int subMatrxId)
{
    free(matrix[subMatrxId]);
}

void handleKeyEvent(unsigned char key, int x, int y)
{
    switch (key)
    {
    case KEY_EXIT:
        exitScene();
        break;
    default:
        break;
    }
}

void handleMouseEvent(int button, int state, int x, int y)
{
    switch (button)
    {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(repaint);
        break;
    case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(NULL);
        break;
    default:
        break;
    }
}

void handleReshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, (GLdouble)width, 0, (GLdouble)height);
}

void sleepMilliSeconds(long milliSeconds)
{
    clock_t start, end;

    start = clock();
    end  = start + milliSeconds;

    while (start < end)
    {
        start = clock();
    }
}

我就是真命天子,顺我者生,逆我者死!
2010-12-18 20:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-326372-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.493201 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved