-// nbody.cpp : Template program for N-body
-// Copyright UWA, 2012
-
-#define _ANSI_SOURCE
+/**
+ * @file nbody.c
+ * @author Sam Moore (20503628) 2012
+ * @purpose N-Body simulator - Definition of simulation functions; single threaded version
+ */
#include <math.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>
-#define M_PI 3.14159265358979323846264338327950288 /* pi */
-#define WIDTH 800
-#define HEIGHT 800
-#define POINT_SIZE 1
-#define POSITION_X 112
-#define POSITION_Y 20
-#define WORLD_LEFT -10000
-#define WORLD_RIGHT 10000
-#define WORLD_BOTTOM -10000
-#define WORLD_TOP 10000
-#define VIEW_ANGLE 45
-#define RHO 100
-#define WORLD_NEAR 0.1
-#define WORLD_FAR 1000000
-#define BALL_SIZE 0.5
-#define REFRESH_RATE 0.001
-#define LINE_SIZE 1000
-#define G 6.67428E-11
-#define DELTA_T 0.05
-
-#define square(x) ((x)*(x))
-
-typedef struct {
- double mass;
- double X;
- double Y;
- double Z;
- double Vx;
- double Vy;
- double Vz;
- double Fx;
- double Fy;
- double Fz;
- int color;
-} Particle;
-
double previousTime, eyeTheta, eyePhi, eyeRho;
float look[3];
int windowWidth, windowHeight, upY;
int N;
int numberOfProcessors=1;
-/*
- * Initialization of graphics
- */
-void Init(void) {
-
- glClearColor(1.0,1.0,1.0,0.0);
- glColor3f(0.0f, 0.0f, 0.0f);
- glPointSize(POINT_SIZE);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
-
- /*init lighting */
-
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 };
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
-
- glEnable(GL_COLOR_MATERIAL); // Enable color
-
- double displayRatio = 1.0 * WIDTH / HEIGHT;
- windowWidth = WIDTH;
- windowHeight = HEIGHT;
- previousTime = clock();
- eyeTheta = 0;
- eyePhi = 0.5 * M_PI;
- eyeRho = RHO;
- upY = 1;
- look[0] = 0;
- look[1] = 0;
- look[2] = 0;
- gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR);
-}
-
-/*
- * This function redraws the screen after the positions of particles
- * have been updated
- */
-void Display(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
- eyeRho * sin(eyePhi) * cos(eyeTheta),
- look[0], look[1], look[2], 0, upY, 0);
-
- for (int i = 0; i < N; i++) {
- //glClearColor(1.0,1.0,1.0,0.0);
- glColor3f(0.0f, body[i].mass/1e11*100, 0.0f);
- //glColor3f(1.0f, 0.0f, 0.0f);
- glPushMatrix(); // to save the current matrix
- glTranslated(SCALE*body[i].X, SCALE*body[i].Y, SCALE*body[i].Z);
- glutSolidSphere (BALL_SIZE, 10, 10);
- glPopMatrix(); // restore the previous matrix
- }
- glFlush();
-}
/*
* Prints the body on screen