home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.parl.clemson.edu
/
2015-02-07.ftp.parl.clemson.edu.tar
/
ftp.parl.clemson.edu
/
pub
/
coven
/
glpv-1.1.tgz
/
glpv-1.1.tar
/
glpv
/
glpv.c
< prev
next >
Wrap
C/C++ Source or Header
|
2003-01-28
|
24KB
|
976 lines
/*
* (C) 2003 Clemson University
*
* See COPYING in top-level directory.
*/
/*
* This simple visualization watches a socket for the locations of particles
* and simply displays them in a 3D world. It allows the user to move around
* the world as well.
*
*/
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <values.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <GL/glut.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "sockio.h"
#define BOOL int
#define FALSE 0
#define TRUE 1
#define FONT_SIZE 12
#define WINDOW_TITLE "glpv: OpenGL Particle Visualization"
int SCWIDTH;
int SCHEIGHT;
typedef struct vector {
double x, y, z;
} vector;
typedef struct body {
int id;
double mass;
vector position;
vector velocity;
vector force;
} body;
typedef struct header {
int numbodies;
int cycle;
float timestep;
} header;
header info_header;
int numbodies = -1;
int iteration = -1;
float timestep = -1.0;
float abtime = 0.0;
/* socket stuff */
struct sockaddr_in mysin, fsin;
body *bodies;
int drawAxes = 1;
int drawLines = 0;
int cubes = 1;
int cur = 0;
int paused = 0;
int drawNumbers = 0;
float cube_size = 0.03;
int def_colors = 0;
struct timeval start_bsps, end_bsps, start_fps, end_fps;
float bsps = 0.0;
float fps = 0.0;
int timing_num_recvd = 0;
int timing_max_recvd = 10;
int fps_num = 0;
int fps_num_max = 10;
int bsps_user_desired = 80;
long usecs_per_bs = (long)((float)1000000/(float)1);
int checkpoint_fd = -1;
int file_input = FALSE;
long elapsed_usecs = 0;
int connected = 0;
int movie_number = 0;
int MAKING_MOVIE = FALSE;
Uint8* keys;
BOOL active=TRUE;
BOOL fullscreen=FALSE;
GLfloat xrot,yrot,zrot, xspeed,yspeed,zspeed, cx,cy,cz=-15;
int key=1;
SDL_Surface *screen;
SDL_Rect dstrect;
SDL_Surface *text;
typedef struct
{
float x, y, z;
} VERTEX;
VERTEX *colors;
void Quit(int retcode);
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0) height=1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLvoid glPrint(const char *s)
{
if(s && strlen(s)) {
while(*s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *s);
s++;
}
}
}
void WritePPM(char *filename, unsigned char *pixels, int xdim, int ydim)
{
FILE *fp;
fp = fopen(filename, "w");
if(fp == NULL) {
fprintf(stderr, "WritePPM() cannot create the PPM file: %s\n",
filename);
return;
}
fprintf(fp, "P6\n%d %d\n255\n", xdim, ydim);
fwrite(pixels, sizeof(int), ydim*xdim, fp);
fflush(fp);
fclose(fp);
}
void drawCube(float x, float y, float z, float th, float cr, float cg, float cb)
{
/* top */
glBegin(GL_QUADS);
glVertex3f(x-th,y-th,z-th);
glVertex3f(x+th,y-th,z-th);
glVertex3f(x+th,y-th,z+th);
glVertex3f(x-th,y-th,z+th);
glVertex3f(x-th,y+th,z-th);
glVertex3f(x+th,y+th,z-th);
glVertex3f(x+th,y+th,z+th);
glVertex3f(x-th,y+th,z+th);
glVertex3f(x+th,y-th,z-th);
glVertex3f(x+th,y+th,z-th);
glVertex3f(x+th,y+th,z+th);
glVertex3f(x+th,y-th,z+th);
glVertex3f(x-th,y-th,z-th);
glVertex3f(x-th,y+th,z-th);
glVertex3f(x-th,y+th,z+th);
glVertex3f(x-th,y-th,z+th);
glVertex3f(x-th,y-th,z-th);
glVertex3f(x+th,y-th,z-th);
glVertex3f(x+th,y+th,z-th);
glVertex3f(x-th,y+th,z-th);
glVertex3f(x-th,y-th,z+th);
glVertex3f(x+th,y-th,z+th);
glVertex3f(x+th,y+th,z+th);
glVertex3f(x-th,y+th,z+th);
glEnd();
}
void Quit(int ret)
{
SDL_Quit();
exit(ret);
}
int InitGL(GLvoid)
{
// Set The Blending Function For Translucency
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
// This Will Clear The Background Color To Black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Enables Clearing Of The Depth Buffer
glClearDepth(1.0);
// The Type Of Depth Test To Do
glDepthFunc(GL_LESS);
// Enables Depth Testing
glEnable(GL_DEPTH_TEST);
// Enables Smooth Color Shading
glShadeModel(GL_SMOOTH);
// Really Nice Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
void CheckSockets(int sock_connected)
{
int retval, i;
fd_set readfds;
struct timeval now;
if(connected) {
FD_ZERO(&readfds);
FD_SET(sock_connected, &readfds);
now.tv_sec = 0;
now.tv_usec = 0;
retval = select(sock_connected+1, &readfds, NULL, NULL, &now);
if(retval > 0) {
if(FD_ISSET(sock_connected, &readfds)) {
if(recv(sock_connected, &info_header, sizeof(header),
MSG_WAITALL)!=sizeof(header)) {
}
else {
if(numbodies == -1) {
numbodies = info_header.numbodies;
printf("RECVD numbodies = %d\n", numbodies);
bodies = (body*)malloc(numbodies*sizeof(body));
colors = (VERTEX*)malloc(sizeof(VERTEX)*numbodies);
for(i=0; i<numbodies; i++)
colors[i].x = colors[i].y = colors[i].z = 0.0;
}
numbodies = info_header.numbodies;
iteration = info_header.cycle;
timestep = info_header.timestep;
abtime = timestep * iteration;
retval = brecv(sock_connected, bodies,
numbodies*sizeof(body));
if(timing_num_recvd == 0) {
gettimeofday(&start_bsps, NULL);
}
timing_num_recvd++;
if(timing_num_recvd == timing_max_recvd) {
int elapsed_secs;
gettimeofday(&end_bsps, NULL);
/* calculate the seconds that have elapsed */
elapsed_secs = end_bsps.tv_sec - start_bsps.tv_sec;
/* if no time has passed, then lets wait twice as
* long and try again */
if(elapsed_secs == 0) timing_max_recvd *= 2;
else {
bsps = ((float)timing_max_recvd)/
((float)elapsed_secs);
if(elapsed_secs > 4) timing_max_recvd /= 2;
timing_num_recvd = 0;
}
}
}
}
}
return;
}
}
void readFileStep(void)
{
int i;
read(checkpoint_fd, &info_header, sizeof(header));
if(numbodies == -1) {
numbodies = info_header.numbodies;
printf("numbodies = %d\n", numbodies);
bodies = (body*)malloc(numbodies*sizeof(body));
colors = (VERTEX*)malloc(sizeof(VERTEX)*numbodies);
for(i=0; i<numbodies; i++)
colors[i].x = colors[i].y = colors[i].z = 0.0;
}
numbodies = info_header.numbodies;
iteration = info_header.cycle;
timestep = info_header.timestep;
abtime = timestep * iteration;
read(checkpoint_fd, bodies, numbodies*sizeof(body));
return;
}
void draw2DText(void)
{
glColor3f(1.0f, 0.0f, 0.0f);
glDisable(GL_DEPTH_TEST);
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
{
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
int numlines = 0;
int xoffset = 5;
int yoffset = 5;
glLoadIdentity();
gluOrtho2D(0, SCWIDTH, 0, SCHEIGHT);
glRasterPos2f(xoffset, yoffset + FONT_SIZE*numlines++);
{
char str[128];
if(file_input == FALSE)
sprintf(str, "%.0f BodysetsPS, %0.2f FPS",
bsps, fps);
else
sprintf(str, "%d BodysetsPS, %0.2f FPS",
bsps_user_desired, fps);
glPrint(str);
}
glRasterPos2f(xoffset, yoffset + FONT_SIZE*numlines++);
if(cubes) glPrint("Draw Mode: CUBES");
else glPrint("Draw Mode: POINTS");
if(numbodies != -1 && iteration != -1 && timestep != -1.0) {
char str[128];
sprintf(str, "Iteration: %d (T = %f, %d bodies, "
"timestep %f)",
iteration, abtime, numbodies, timestep);
glRasterPos2f(xoffset, yoffset + FONT_SIZE*numlines++);
glPrint(str);
}
if(paused) {
glRasterPos2f(xoffset, yoffset + FONT_SIZE*numlines++);
glPrint("[ PAUSED ]");
}
}
glPopMatrix();
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
glMatrixMode(GL_MODELVIEW);
}
int DrawGLScene(int sock_accept, int *sock_connected)
{
int i, j;
fd_set readfds;
struct timeval now;
int retval, fromlen;
float f1, f2, f3;
char str[32];
if(fps_num == 0) gettimeofday(&start_fps, NULL);
fps_num++;
if(file_input == FALSE) {
FD_ZERO(&readfds);
FD_SET(sock_accept, &readfds);
now.tv_sec = 0;
now.tv_usec = 0;
retval = select(sock_accept+1, &readfds, NULL, NULL, &now);
if(retval > 0) {
if(FD_ISSET(sock_accept, &readfds)) {
if((*sock_connected=accept(sock_accept,(struct
sockaddr*)&fsin, &fromlen))!=-1) {
printf("[Connection established]\n");
connected = TRUE;
}
else {
perror("accept");
exit(1);
}
}
}
if(connected && !paused) CheckSockets(*sock_connected);
}
else if(file_input == TRUE) {
if(!paused) {
if(timing_num_recvd == 0) {
gettimeofday(&start_bsps, NULL);
}
gettimeofday(&end_bsps, NULL);
/* calculate the seconds that have elapsed */
elapsed_usecs = 1000000*(end_bsps.tv_sec - start_bsps.tv_sec)
+ (end_bsps.tv_usec - start_bsps.tv_usec);
if(timing_num_recvd < bsps_user_desired) {
if(elapsed_usecs > (timing_num_recvd * usecs_per_bs)) {
readFileStep();
timing_num_recvd++;
}
}
else {
if(elapsed_usecs > 1000000) {
timing_num_recvd = 0;
elapsed_usecs = 0;
}
}
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* if you want to follow a body run this */
/* well - this may work :) */
#if 0
gluLookAt(
bodies_obj.points[1].x + 1,
bodies_obj.points[1].y + 1,
bodies_obj.points[1].z + 1,
bodies_obj.points[1].x + bodies_obj.points[1].y,
bodies_obj.points[1].y + bodies_obj.points[1].y,
bodies_obj.points[1].z + bodies_obj.points[1].z,
0.0f, 1.0f, 0.0f);
TURN OFF XLATE THOUGH
#endif
glTranslatef(cx,cy,cz); // Xlate The The Current Pos To Start Drawing
glRotatef(xrot,1,0,0); // Rotate On The X Axis By xrot
glRotatef(yrot,0,1,0); // Rotate On The Y Axis By yrot
glRotatef(zrot,0,0,1); // Rotate On The Z Axis By zrot
// Increase xrot,yrot & zrot by xspeed, yspeed & zspeed
xrot+=xspeed; yrot+=yspeed; zrot+=zspeed;
if(numbodies > 0) {
if(!cubes) glBegin(GL_POINTS);
for(i=0; i<numbodies; i++) {
if(colors[i].x == 0.0 &&
colors[i].y == 0.0 &&
colors[i].z == 0.0) {
if(bodies[i].mass <= 5.0) {
f1 = 1.0; f2 = 1.0; f3 = 1.0;
}
else if(bodies[i].mass <= 10.0) {
f1 = 1.0; f2 = 0.0; f3 = 0.0;
}
else if(bodies[i].mass <= 50.0) {
f1 = 0.0; f2 = 1.0; f3 = 1.0;
}
else if(bodies[i].mass <= 100.0) {
f1 = 0.0; f2 = 1.0; f3 = 0.0;
}
else {
f1 = 1.0; f2 = 1.0; f3 = 1.0;
}
colors[i].x = f1;
colors[i].y = f2;
colors[i].z = f3;
}
glColor3f(colors[i].x, colors[i].y, colors[i].z);
if(cubes == 1) {
drawCube(bodies[i].position.x, bodies[i].position.y,
bodies[i].position.z, cube_size, colors[i].x,
colors[i].y, colors[i].z);
if(drawNumbers) {
glColor3f(0.0f, 0.0f, 1.0f);
glRasterPos3f(bodies[i].position.x,
bodies[i].position.y, bodies[i].position.z);
sprintf(str,"%3.3f", bodies[i].mass);
glPrint(str);
}
}
else { /* points */
glVertex3f(bodies[i].position.x,
bodies[i].position.y,
bodies[i].position.z);
}
}
glEnd();
if(drawLines == 1) {
glEnable(GL_LINE_SMOOTH);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
for(i=0; i<numbodies; i++) {
glVertex3d(bodies[i].position.x,
bodies[i].position.y,
bodies[i].position.z);
glVertex3d(bodies[(i+1)%numbodies].position.x,
bodies[(i+1)%numbodies].position.y,
bodies[(i+1)%numbodies].position.z);
}
glEnd();
glDisable(GL_LINE_SMOOTH);
}
else if(drawLines == 2) {
glEnable(GL_LINE_SMOOTH);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex3d(bodies[cur].position.x,
bodies[cur].position.y,
bodies[cur].position.z);
glVertex3d(bodies[(cur+1)%numbodies].position.x,
bodies[(cur+1)%numbodies].position.y,
bodies[(cur+1)%numbodies].position.z);
cur = (cur+1)%numbodies;
glEnd();
glDisable(GL_LINE_SMOOTH);
}
else if(drawLines == 3) {
glEnable(GL_LINE_SMOOTH);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
for(i=0; i<numbodies; i++) {
int curelement = i;
float mind = MAXFLOAT;
for(j=0; j<numbodies; j++) {
if(i != j) { /* don't compare w/ myself */
float curd = ((bodies[i].position.x -
bodies[j].position.x)*(bodies[i].position.x -
bodies[j].position.x)) +
((bodies[i].position.y - bodies[j].position.y)*
(bodies[i].position.y - bodies[j].position.y)) +
((bodies[i].position.z - bodies[j].position.z)*
(bodies[i].position.z - bodies[j].position.z));
if(curd < mind) {
mind = curd;
curelement = j;
}
}
}
glVertex3d(bodies[i].position.x,
bodies[i].position.y,
bodies[i].position.z);
glVertex3d(bodies[curelement].position.x,
bodies[curelement].position.y,
bodies[curelement].position.z);
}
glEnd();
glDisable(GL_LINE_SMOOTH);
}
else if(drawLines == 4) {
glEnable(GL_LINE_SMOOTH);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
for(i=0; i<numbodies; i++) {
for(j=0; j<numbodies; j++) {
glVertex3d(bodies[i].position.x,
bodies[i].position.y,
bodies[i].position.z);
glVertex3d(bodies[j].position.x,
bodies[j].position.y,
bodies[j].position.z);
}
}
glEnd();
glDisable(GL_LINE_SMOOTH);
}
// Done Drawing Points
}
if(drawAxes) {
/* SMOOTH lines look good but are hardware intense */
//glEnable(GL_LINE_SMOOTH);
glLineWidth(0.5f);
glColor3f(0.7f, 0.7f, 0.7f);
glEnable(GL_LINE_STIPPLE);
// Set the stippling pattern
glLineStipple(3, 0xAAAA);
//glLineStipple(2, 0x0C0F);
glBegin(GL_LINES);
glVertex3d(-30,0,0);
glVertex3d(30,0,0);
glVertex3d(0,-30,0);
glVertex3d(0,30,0);
glVertex3d(0,0,-30);
glVertex3d(0,0,30);
glEnd();
//glDisable(GL_LINE_SMOOTH);
glDisable(GL_LINE_STIPPLE);
glColor3f(0.0f, 1.0f, 0.0f);
glRasterPos3f(-10.5,0,0);
glPrint("-X");
glRasterPos3f(10.1,0,0);
glPrint("+X");
glRasterPos3f(0,-10.5,0);
glPrint("-Y");
glRasterPos3f(0,10.1,0);
glPrint("+Y");
glRasterPos3f(0,0,-10.5);
glPrint("-Z");
glRasterPos3f(0,0,10.1);
glPrint("+Z");
}
draw2DText();
if(fps_num == fps_num_max) {
int elapsed_secs;
gettimeofday(&end_fps, NULL);
/* calculate the seconds that have elapsed */
elapsed_secs = end_fps.tv_sec - start_fps.tv_sec;
/* if no time has passed, then lets wait twice as long and
* try again */
if(elapsed_secs == 0 && fps_num_max < 100) fps_num_max *= 2;
else {
if(elapsed_secs == 0) elapsed_secs = 1;
fps = ((float)fps_num_max)/((float)elapsed_secs);
if(elapsed_secs > 4) fps_num_max /= 2;
fps_num = 0;
}
}
if(MAKING_MOVIE && ((iteration != movie_number) || paused)) {
unsigned char *pixels, *fn;
pixels = (unsigned char*)malloc(3*SCWIDTH*SCHEIGHT*
sizeof(unsigned char));
fn = (unsigned char*)malloc(80);
glReadBuffer(GL_FRONT);
glReadPixels(0,0,SCWIDTH,SCHEIGHT,GL_RGB,GL_UNSIGNED_BYTE,pixels);
sprintf(fn, "/tmp/sshot-%05d.ppm", movie_number);
WritePPM(fn, pixels, SCWIDTH, SCHEIGHT);
free(pixels);
free(fn);
movie_number++;
}
return TRUE;
}
/* This Code Creates Our OpenGL Window. Parameters Are:
* title - Title To Appear At The Top Of The Window
* width - Width Of The GL Window Or Fullscreen Mode
* height - Height Of The GL Window Or Fullscreen Mode
* bits - Number Of Bits To Use For Color (8/16/24/32)
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed
* Mode (FALSE)
*/
BOOL CreateGLWindow(char* title, int width, int height, int bits,
BOOL fullscreenflag)
{
Uint32 flags;
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
flags = SDL_OPENGL;
if(fullscreenflag) {
flags |= SDL_FULLSCREEN;
}
if((screen = SDL_SetVideoMode(width, height, 0, flags)) == NULL) {
return FALSE;
}
SDL_WM_SetCaption(title, WINDOW_TITLE);
// Set Up Our Perspective GL Screen
ReSizeGLScene(width, height);
// Initialize Our Newly Created GL Window
if(!InitGL()) {
Quit(1); // Reset The Display
return FALSE; // Return FALSE
}
return TRUE; // Success
}
int TOGGLE(int x)
{
if(x == 1) return 0;
else return 1;
}
int main(int argc, char *argv[])
{
BOOL done=FALSE;
int sock_accept = -1, sock_connected;
float zoomz = 0.06;
float fx = 0, fy = 0, fz = 0;
int i;
if(argc != 4) {
fprintf(stderr,"Syntax: %s <screen width> <screen height> "
"<port to bind to OR checkpoint file to read from>\n",
argv[0]);
exit(1);
}
SCWIDTH = atoi(argv[1]);
SCHEIGHT = atoi(argv[2]);
srand((unsigned int)time(NULL));
if(atoi(argv[3]) == 0) {
checkpoint_fd = open(argv[3], O_RDONLY);
if(checkpoint_fd == -1) {
perror("open");
exit(1);
}
file_input = TRUE;
}
else {
sock_accept = new_sock();
printf("Binding to port: %d\n", atoi(argv[3]));
bind_sock(sock_accept, atoi(argv[3]));
if(listen(sock_accept,5)<0) {
perror("listen");
exit(1);
}
}
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't init SDL: %s\n", SDL_GetError());
return 1;
}
// Create Our OpenGL Window
if (!CreateGLWindow(WINDOW_TITLE,SCWIDTH,SCHEIGHT,
16,fullscreen))
{
SDL_Quit();
return 0;
}
printf("COMMANDS:\n");
printf("F1 - toggle full-screen mode\n");
printf("ESC - quit\n");
printf("p - TOGGLE pause\n");
printf("m - TOGGLE movie making (look for screenshots in /tmp)\n");
printf("\n");
printf("3D Commands:\n");
printf("\tPGUP/PGDWN - rotate Z axis\n");
printf("\tKEYPAD LEFT/RIGHT - rotate X axis\n");
printf("\tKEYPAD UP/DOWN - rotate Y axis\n");
printf("\tLEFT SHIFT + any of the above rotations - put spin on "
"that axis\n");
printf("\tw/s - zoom in / out\n");
printf("\ta/d - move view field left / right\n");
printf("\tq/z - move view field up / down\n");
printf("\tk - return to original view\n");
printf("\tx - stop all axial spins\n");
printf("\n");
printf("Additional Commands:\n");
printf("\tj - TOGGLE drawing body numbers\n");
printf("\tc - switch base color drawing\n");
printf("\tv - TOGGLE drawing axes\n");
printf("\tb - TOGGLE drawing points or cubes\n");
printf("\t1 - turn off crazy line drawing\n");
printf("\t2 - draw lines between subsequent bodies\n");
printf("\t3 - draw lines between bodies, one line per iteration\n");
printf("\t4 - draw lines between closest neighbors\n");
printf("\t5 - draw lines between all bodies\n");
printf("\t-/= - modify cube size\n");
if(file_input == TRUE) {
printf("[/] - modify number of datasets read from file per "
"second\n");
}
/* initial view of the world */
xrot = 13.0;
yrot = 28.5;
zrot = 0.0;
cz = -37.619991;
while(!done)
{
SDL_Event event;
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_QUIT:
done=TRUE;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_b:
cubes = cubes ? 0 : 1;
break;
case SDLK_v:
drawAxes = drawAxes ? 0 : 1;
break;
case SDLK_m:
MAKING_MOVIE = MAKING_MOVIE ? 0 : 1;
if(MAKING_MOVIE)
printf("Now making movie, look for "
"screens in /tmp\n");
else
printf("Movie making turned off.\n");
break;
case SDLK_c:
def_colors = (def_colors + 1) % 10;
switch(def_colors) {
case 0:
fx = 0.0; fy = 1.0; fz = 0.0;
break;
case 1:
fx = 0.0; fy = 0.0; fz = 1.0;
break;
case 2:
fx = 1.0; fy = 0.0; fz = 0.0;
break;
case 3:
fx = 1.0; fy = 0.0; fz = 1.0;
break;
case 4:
fx = 0.0; fy = 1.0; fz = 1.0;
break;
case 5:
fx = 1.0; fy = 0.5; fz = 0.3;
break;
case 6:
fx = 0.3; fy = 1.0; fz = 0.5;
break;
case 7:
fx = 0.5; fy = 0.3; fz = 1.0;
break;
case 8:
fx = 1.0; fy = 1.0; fz = 1.0;
break;
case 9:
fx = 1.0; fy = 1.0; fz = 0.0;
break;
}
for(i=0; i<numbodies; i++) {
colors[i].x = fx;
colors[i].y = fy;
colors[i].z = fz;
}
break;
case SDLK_p:
paused = paused ? 0 : 1;
break;
case SDLK_1:
drawLines = 0;
break;
case SDLK_2:
drawLines = 1;
break;
case SDLK_3:
drawLines = 2;
break;
case SDLK_4:
drawLines = 3;
break;
case SDLK_5:
drawLines = 4;
break;
case SDLK_j:
drawNumbers = drawNumbers ? 0 : 1;
break;
case SDLK_k:
xrot = 13.0;
yrot = 28.5;
zrot = 0.0;
cz = -37.619991;
break;
case SDLK_LEFTBRACKET:
if(file_input == TRUE) {
bsps_user_desired--;
if(bsps_user_desired < 1)
bsps_user_desired = 1;
usecs_per_bs = (long)((float)1000000/
(float)bsps_user_desired);
}
break;
case SDLK_RIGHTBRACKET:
if(file_input == TRUE) {
bsps_user_desired++;
usecs_per_bs = (long)((float)1000000/
(float)bsps_user_desired);
}
break;
default: break;
}
default: break;
}
}
keys = SDL_GetKeyState(NULL);
// Draw The Scene. Watch For ESC Key And Quit Messages
// From DrawGLScene()
if ((active && !DrawGLScene(sock_accept, &sock_connected)) ||
keys[SDLK_ESCAPE])
{
done=TRUE;
}
else
{
SDL_GL_SwapBuffers(); // Swap Buffers (Double Buffering)
}
if(keys[SDLK_PAGEUP]) {
if(keys[SDLK_LSHIFT]) zspeed += 0.01f;
else zrot += 0.5f;
}
if(keys[SDLK_PAGEDOWN]) {
if(keys[SDLK_LSHIFT]) zspeed -= 0.01f;
else zrot -= 0.5f;
}
if(keys[SDLK_DOWN]) {
if(keys[SDLK_LSHIFT]) xspeed += 0.01f;
else xrot += 0.5f;
}
if(keys[SDLK_UP]) {
if(keys[SDLK_LSHIFT]) xspeed -= 0.01f;
else xrot -= 0.5f;
}
if(keys[SDLK_RIGHT]) {
if(keys[SDLK_LSHIFT]) yspeed += 0.01f;
else yrot += 0.5f;
}
if(keys[SDLK_LEFT]) {
if(keys[SDLK_LSHIFT]) yspeed -= 0.01f;
else yrot -= 0.5f;
}
/* stop the spinning */
if(keys[SDLK_x]) xspeed = yspeed = zspeed = 0.0f;
if(keys[SDLK_EQUALS]) cube_size += 0.0005;
if(keys[SDLK_MINUS]) cube_size -= 0.0005;
// Move Object Away From Viewer
if (keys[SDLK_s]) cz -= zoomz;
// Move Object Towards Viewer
if (keys[SDLK_w]) cz+=zoomz;
// Move Object Up
if (keys[SDLK_z]) cy+=0.01f;
// Move Object Down
if (keys[SDLK_q]) cy-=0.01f;
// Move Object Right
if (keys[SDLK_a]) cx+=0.01f;
// Move Object Left
if (keys[SDLK_d]) cx-=0.01f;
if (keys[SDLK_F1]) {
fullscreen=!fullscreen; // Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if(!CreateGLWindow(WINDOW_TITLE,SCWIDTH,
SCHEIGHT,16,fullscreen))
{
done=TRUE; // Quit If Window Was Not Created
}
}
}
// Shutdown
close(sock_connected);
close(sock_accept);
Quit(0); // Kill The Window
SDL_Quit();
return 0;
}