Skip to content

Instantly share code, notes, and snippets.

@tai2
Created May 12, 2013 13:32
Show Gist options
  • Select an option

  • Save tai2/5563567 to your computer and use it in GitHub Desktop.

Select an option

Save tai2/5563567 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GLUT/glut.h>
#define KEY_ESC 27
#define WINDOW_POS_X 100
#define WINDOW_POS_Y 100
#define DEF_SCREEN_WIDTH 640
#define DEF_SCREEN_HEIGHT 360
#define N_STARS 64
#define Z_MIN 100
#define Z_MAX 1000
#define VZ_MIN 0.5
#define VZ_MAX 5.0
#define SCREEN_X(star, w) (((star).x / (star).z) * 100 + (w) / 2)
#define SCREEN_Y(star, h) (((star).y / (star).z) * 100 + (h) / 2)
struct star {
double x;
double y;
double z;
double vz;
double old_screen_x;
double old_screen_y;
};
static int curr_width = 0;
static int curr_height = 0;
static int fullscreen = 0;
static int motionblur = 0;
static struct star stars[N_STARS];
static void
keyboard(unsigned char c, int x, int y)
{
switch (c) {
case KEY_ESC:
exit(0);
break;
case 'f':
if (fullscreen) {
glutReshapeWindow(DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
glutPositionWindow(WINDOW_POS_X, WINDOW_POS_Y);
fullscreen = 0;
} else {
glutFullScreen();
fullscreen = 1;
}
break;
case 'm':
motionblur = !motionblur;
break;
}
}
void
idle()
{
glutPostRedisplay();
}
static void
reshape(int w, int h)
{
curr_width = w;
curr_height = h;
glViewport(0, 0, curr_width, curr_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, curr_width, 0, curr_height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void
drawline(int x0, int y0, int x1, int y1, float r, float g, float b)
{
glColor3f(r, g, b);
glVertex2i(x0, y0);
glVertex2i(x1, y1);
}
static void
drawpoint(int x, int y, float r, float g, float b)
{
glColor3f(r, g, b);
glVertex2i(x, y);
}
static void
display()
{
int i;
int x, y;
glClear(GL_COLOR_BUFFER_BIT);
if (motionblur) {
glBegin(GL_LINES);
} else {
glBegin(GL_POINTS);
}
for (i = 0; i < N_STARS; i++) {
double screen_x, screen_y;
double dx, dy;
double length;
stars[i].z = stars[i].z - stars[i].vz;
screen_x = SCREEN_X(stars[i], curr_width);
screen_y = SCREEN_Y(stars[i], curr_height);
if (screen_x < 0 || curr_width <= screen_x
|| screen_y < 0 || curr_height <= screen_y
|| stars[i].z < 1) {
stars[i].x = rand() % 1001 - 500;
stars[i].y = rand() % 1001 - 500;
stars[i].z = rand() % (Z_MAX - Z_MIN + 1) + Z_MIN;
stars[i].vz = (VZ_MAX - VZ_MIN) * ((double)rand() / RAND_MAX) + VZ_MIN;
stars[i].old_screen_x = SCREEN_X(stars[i], curr_width);
stars[i].old_screen_y = SCREEN_Y(stars[i], curr_height);
} else {
double v;
if (motionblur) {
dx = screen_x - stars[i].old_screen_x;
dy = screen_y - stars[i].old_screen_y;
length = sqrt(dx * dx + dy * dy);
v = (VZ_MAX * Z_MAX * stars[i].vz / stars[i].z);
if (1 < length) v /= length;
drawline(
stars[i].old_screen_x, stars[i].old_screen_y,
screen_x, screen_y,
v, v, v);
} else {
v = stars[i].vz / VZ_MAX * (1000 / stars[i].z);
drawpoint(
screen_x, screen_y,
v, v, v);
}
stars[i].old_screen_x = screen_x;
stars[i].old_screen_y = screen_y;
}
}
glEnd();
glutSwapBuffers();
}
static void
init() {
int i;
for (i = 0; i < N_STARS; i++) {
stars[i].x = rand() % 1001 - 500;
stars[i].y = rand() % 1001 - 500;
stars[i].z = rand() % (Z_MAX - Z_MIN + 1) + Z_MIN;
stars[i].vz = (VZ_MAX - VZ_MIN) * ((double)rand() / RAND_MAX) + VZ_MIN;
stars[i].old_screen_x = SCREEN_X(stars[i], DEF_SCREEN_WIDTH);
stars[i].old_screen_y = SCREEN_Y(stars[i], DEF_SCREEN_HEIGHT);
}
}
int
main(int argc, char** argv)
{
srand(0);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y);
glutCreateWindow(argv[0]);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glPointSize(1.5);
glLineWidth(1.0);
init();
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment