又写了一个 demoscene (64K动画)

软件和网站开发以及相关技术探讨
回复
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

又写了一个 demoscene (64K动画)

#1

帖子 cjxgm » 2011-03-06 13:44

这也是个超 simple 的作品,名曰:Waves
先上图(What a mess!):
#define WAVE_SIZE 10
#define WAVE_SIZE 10
snap1.png (3.64 KiB) 查看 1975 次
#define WAVE_SIZE 100
#define WAVE_SIZE 100
恩,首先,我包装了一下glut (demolino.h)
[c]#ifndef __DEMOLINO_H__
#define __DEMOLINO_H__

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

// Type
#define float GLfloat

// Uilities
#define CLEAR glClear(GL_COLOR_BUFFER_BIT)
#define RGB(R, G, B) (float)(R)/255.0f, (float)(G)/255.0f, (float)(B)/255.0f
#define RGBA(R, G, B, A) RGB(R, G, B), (float)(A)/255.0f
#define SET_COLOR_RGB(R, G, B) glColor3f(RGB(R, G, B))
#define SET_COLOR_RGBA(R, G, B, A) glColor4f(RGBA(R, G, B, A))
#define PUSH_MAT glPushMatrix()
#define POP_MAT glPopMatrix()
#define SWAP glutSwapBuffers()

// Custom info
#ifndef DEMO_TITLE
#define DEMO_TITLE "Demolino"
#endif

#ifndef DEMO_WIN_SIZE
#define DEMO_WIN_SIZE 320,240
#endif

#ifndef DEMO_FPS
#define DEMO_FPS 30
#endif

#ifndef DEMO_CLEAR_COLOR
#define DEMO_CLEAR_COLOR RGBA(0, 0, 0, 0)
#endif

void timer(int value);
void render(void);
void init(void);

// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
timer(value);

glutPostRedisplay();
glutTimerFunc(1000/DEMO_FPS, TimerFunction, 1);
}

void SpecialKeys(int key, int x, int y)
{
/*
if(key == GLUT_KEY_UP) xRot-= 5.0f;
if(key == GLUT_KEY_DOWN) xRot += 5.0f;
if(key == GLUT_KEY_LEFT) yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT) yRot += 5.0f;
if(key > 356.0f) xRot = 0.0f;
if(key < -1.0f) xRot = 355.0f;
if(key > 356.0f) yRot = 0.0f;
if(key < -1.0f) yRot = 355.0f;

// Refresh the Window
glutPostRedisplay();
*/
}

void ChangeSize(int w, int h)
{
float nRange = 100.0f;

// Prevent a divide by zero
if(h == 0) h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(DEMO_WIN_SIZE);
glutCreateWindow(DEMO_TITLE);
glutReshapeFunc(ChangeSize);
glutDisplayFunc(render);
glutSpecialFunc(SpecialKeys);
glutTimerFunc(1000/DEMO_FPS, TimerFunction, 1);
glClearColor(DEMO_CLEAR_COLOR);

init();

glutMainLoop();

return 0;
}

#endif[/c]
主程序(demo.c):
[c]#define DEMO_TITLE "LanDeXithO 002 - Waves"
#define DEMO_WIN_SIZE 480, 480
#include "demolino.h"
#include <math.h>
#define PI 3.1415927

#define WAVE_SIZE 10
struct wave
{
int type; // 0: Sine; 1: Cosine;
float angle;
float angle_inc;
float x, y;
float x_inc;
float y_scale;
int r, g, b;
} waves[WAVE_SIZE];

void circle(float x, float y, float r)
{
glBegin(GL_POLYGON);
float a;
for (a=0; a<2*PI; a+=0.1)
glVertex2f(x+cos(a)*r, y+sin(a)*r);
glEnd();
}

void draw_wave_path(int i)
{
glBegin(GL_LINE_STRIP);
float a, x;
for (a=0, x=-100; x<=100;
a+=waves.angle_inc, x+=waves.x_inc){
float y_offset = 0;
switch (waves.type){
case 0: // Sine
y_offset = sin(a);
break;
case 1: // Cosine
y_offset = cos(a);
break;
}
glVertex2f(x, waves.y + y_offset*waves.y_scale);
}
glEnd();
}

void draw(void)
{
int i;
for (i=0; i<WAVE_SIZE; i++){
float y_offset = 0;
switch (waves.type){
case 0: // Sine
y_offset = sin(waves.angle);
break;
case 1: // Cosine
y_offset = cos(waves.angle);
break;
}
SET_COLOR_RGBA(waves.r, waves.g, waves[i].b, 128);
draw_wave_path(i);
circle(waves[i].x, waves[i].y + y_offset*waves[i].y_scale, 2);
}
}

void render(void)
{
CLEAR;
PUSH_MAT;
draw();
POP_MAT;
SWAP;
}

void init_wave(int i)
{
waves[i].type = rand()%2;
waves[i].angle = 0;
waves[i].angle_inc = (float)(rand()%10)*PI/180;
waves[i].x = -100;
waves[i].y = (rand()%200)-100;
waves[i].x_inc = (rand()%6) + 1;
waves[i].y_scale = (rand()%100)/10.0 + 1;
waves[i].r = (rand()%192) + 64;
waves[i].g = (rand()%192) + 64;
waves[i].b = (rand()%192) + 64;
}

void update_waves(void)
{
int i;
for (i=0; i<WAVE_SIZE; i++){
waves[i].angle += waves[i].angle_inc;
waves[i].x+=waves[i].x_inc;
if (waves[i].x>100) init_wave(i);
}
}

void timer(int value)
{
update_waves();
}

void init(void)
{
int i;
for (i=0; i<WAVE_SIZE; i++){
init_wave(i);
}
}[/c]
恩恩,编译运行:
[bash]$ gcc -o demo demo.c -lglut # 你需要装好libglut先
$ ./demo[/bash]
恩不错,才15KB
啥?想要现成的?好吧,下不为例(-_-||)
demo.tar.gz
(6.8 KiB) 已下载 107 次
Happy programming!
上次由 cjxgm 在 2011-03-06 13:51,总共编辑 1 次。
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
lubcat
帖子: 2061
注册时间: 2010-09-27 12:59

Re: 又写了一个 demoscene (64K动画)

#2

帖子 lubcat » 2011-03-06 13:49

看到了宇宙的起源..外行支持
生活里,有很多转瞬即逝,像在车站的告别,刚刚还相互拥抱,转眼已各自天涯。很多时候,你不懂,我也不懂,就这样,说着说着就变了,听着听着就倦了,看着看着就厌了,跟着跟着就慢了,走着走着就散了,爱着爱着就淡了,想着想着就算了。
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: 又写了一个 demoscene (64K动画)

#3

帖子 月下叹逍遥 » 2011-03-06 13:55

想起了弦理论。 :em02
浮生七十今三十,从此凄惶未可知
回复