# HG changeset patch # User John Tsiombikas # Date 1400858862 -10800 # Node ID 3d541da6e48cdc72354bcff63abf8c5b2421d167 # Parent 68db0e456733ef8514bb73a0d73f0fb9a42bed1e lalalala diff -r 68db0e456733 -r 3d541da6e48c grav.sln --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/grav.sln Fri May 23 18:27:42 2014 +0300 @@ -0,0 +1,24 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30501.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grav", "grav.vcxproj", "{C45C3601-EB97-417C-B3F8-9F02529BE312}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0CE743CE-21C9-408E-AE91-4BA9F74D931D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C45C3601-EB97-417C-B3F8-9F02529BE312}.Debug|Win32.ActiveCfg = Debug|Win32 + {C45C3601-EB97-417C-B3F8-9F02529BE312}.Debug|Win32.Build.0 = Debug|Win32 + {C45C3601-EB97-417C-B3F8-9F02529BE312}.Release|Win32.ActiveCfg = Release|Win32 + {C45C3601-EB97-417C-B3F8-9F02529BE312}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff -r 68db0e456733 -r 3d541da6e48c grav.vcxproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/grav.vcxproj Fri May 23 18:27:42 2014 +0300 @@ -0,0 +1,90 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C45C3601-EB97-417C-B3F8-9F02529BE312} + Win32Proj + grav + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + glut32.lib;libvmath.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + + Console + true + true + true + glut32.lib;libvmath.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file diff -r 68db0e456733 -r 3d541da6e48c grav.vcxproj.filters --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/grav.vcxproj.filters Fri May 23 18:27:42 2014 +0300 @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff -r 68db0e456733 -r 3d541da6e48c src/main.cc --- a/src/main.cc Fri May 23 03:13:47 2014 +0300 +++ b/src/main.cc Fri May 23 18:27:42 2014 +0300 @@ -1,5 +1,6 @@ #include #include +#include #include #include "sim.h" @@ -10,8 +11,12 @@ static void idle(); static void reshape(int x, int y); static void keyb(unsigned char key, int x, int y); +static void mouse(int bn, int st, int x, int y); +static void motion(int x, int y); +static void draw_grid(float sz, int nlines, float alpha); static SimWorld sim; +static float cam_theta, cam_phi = 25, cam_dist = 8; int main(int argc, char **argv) { @@ -24,16 +29,177 @@ glutIdleFunc(idle); glutReshapeFunc(reshape); glutKeyboardFunc(keyb); + glutMouseFunc(mouse); + glutMotionFunc(motion); + + if(!init()) { + return 1; + } + atexit(cleanup); + + glutMainLoop(); + return 0; } static bool init() { + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + Particle p; + p.mass = 10.0; + sim.add_particle(p); + return true; } -static void cleanup(); -static void update(long tmsec); -static void display(); -static void idle(); -static void reshape(int x, int y); -static void keyb(unsigned char key, int x, int y); +static void cleanup() +{ +} + +static void update(long tmsec) +{ + static long prev_upd; + long interval = tmsec - prev_upd; + + if(interval >= 1000 / 60) { + prev_upd = tmsec; + + float dt = (float)interval / 1000.0f; + sim.step(dt); + } +} + +static void display() +{ + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); + update(msec); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, 0, -cam_dist); + glRotatef(cam_phi, 1, 0, 0); + glRotatef(cam_theta, 0, 1, 0); + + sim.draw(); + + draw_grid(10, 10, 1); + + glutSwapBuffers(); + assert(glGetError() == 0); +} + +static void generate() +{ + Particle p; + p.mass = (float)rand() / (float)RAND_MAX + 0.25; + + p.pos.x = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0; + p.pos.y = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0; + p.pos.z = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0; + + p.vel.x = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0; + p.vel.y = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0; + p.vel.z = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0; + + sim.add_particle(p); +} + +static void idle() +{ + glutPostRedisplay(); +} + +static void reshape(int x, int y) +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0); + + glViewport(0, 0, x, y); +} + +static void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + + case ' ': + generate(); + break; + } +} + +static bool bnstate[16]; +static int prev_x, prev_y; + +static void mouse(int bn, int st, int x, int y) +{ + prev_x = x; + prev_y = y; + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; +} + +static void motion(int x, int y) +{ + int dx = x - prev_x; + int dy = y - prev_y; + prev_x = x; + prev_y = y; + + if(!dx && !dy) return; + + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; + + if(cam_phi < -90) cam_phi = -90; + if(cam_phi > 90) cam_phi = 90; + } + if(bnstate[2]) { + cam_dist += dy * 0.1; + if(cam_dist < 0.0) cam_dist = 0.0; + } +} + +static void draw_grid(float sz, int nlines, float alpha) +{ + float hsz = sz / 2.0; + float offs = sz / (float)nlines; + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + + glLineWidth(2.0); + glBegin(GL_LINES); + glColor4f(1, 0, 0, alpha); + glVertex3f(-hsz, 0, 0); + glVertex3f(hsz, 0, 0); + glColor4f(0, 0, 1, alpha); + glVertex3f(0, 0, -hsz); + glVertex3f(0, 0, hsz); + glEnd(); + + glLineWidth(1.0); + glBegin(GL_LINES); + glColor4f(0.5, 0.5, 0.5, alpha); + for(int i=0; i 0 ? -1.0 : 1.0; + glVertex3f(-hsz, 0, dist * sign); + glVertex3f(hsz, 0, dist * sign); + glVertex3f(dist * sign, 0, -hsz); + glVertex3f(dist * sign, 0, hsz); + } + } + glEnd(); + + glPopAttrib(); +} \ No newline at end of file diff -r 68db0e456733 -r 3d541da6e48c src/sim.cc --- a/src/sim.cc Fri May 23 03:13:47 2014 +0300 +++ b/src/sim.cc Fri May 23 18:27:42 2014 +0300 @@ -1,4 +1,4 @@ -#include +#include #include "sim.h" Particle::Particle() @@ -6,9 +6,9 @@ mass = 1.0; } -void Particle::impulse(const Vector3 &imp) +void Particle::add_force(const Vector3 &force) { - accel += imp; + accel += force; } void Particle::step(float dt) @@ -18,6 +18,7 @@ vel += accel * dt; accel.x = accel.y = accel.z = 0.0; + prev_pos = pos; pos = npos; } @@ -40,7 +41,21 @@ void SimWorld::step(float dt) { - std::list::iterator it = particles.begin(); + for(size_t i=0; i 0.0) { + gdir /= len; + particles[i].add_force(gdir * particles[j].mass / (len * len)); + } + } + } + + std::vector::iterator it = particles.begin(); while(it != particles.end()) { Particle *p = &*it; p->step(dt); @@ -58,16 +73,22 @@ glBlendFunc(GL_ONE, GL_ONE); glEnable(GL_BLEND); - glBegin(GL_POINTS); glColor3f(0.5, 0.6, 0.7); - std::list::const_iterator it = particles.begin(); - while(it != particles.end()) { - const Particle *p = &*it++; + for(size_t i=0; ipos.x, p->pos.y, p->pos.z); + glutSolidSphere(p->mass * 0.1, 12, 6); + glPopMatrix(); + + glBegin(GL_LINES); + glNormal3f(0, 0, 1); glVertex3f(p->pos.x, p->pos.y, p->pos.z); + glVertex3f(p->prev_pos.x, p->prev_pos.y, p->prev_pos.z); + glEnd(); } - glEnd(); - glDisable(GL_BLEND); } diff -r 68db0e456733 -r 3d541da6e48c src/sim.h --- a/src/sim.h Fri May 23 03:13:47 2014 +0300 +++ b/src/sim.h Fri May 23 18:27:42 2014 +0300 @@ -1,24 +1,25 @@ #ifndef SIM_H_ #define SIM_H_ -#include +#include #include class Particle { public: float mass; Vector3 pos, vel; + Vector3 prev_pos; Vector3 accel; Particle(); - void impulse(const Vector3 &imp); + void add_force(const Vector3 &force); void step(float dt); }; class SimWorld { private: - std::list particles; + std::vector particles; float radius_sq; public: