grav

changeset 0:68db0e456733

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 May 2014 03:13:47 +0300
parents
children 3d541da6e48c
files Makefile src/main.cc src/sim.cc src/sim.h
diffstat 4 files changed, 159 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Fri May 23 03:13:47 2014 +0300
     1.3 @@ -0,0 +1,13 @@
     1.4 +src = $(wildcard src/*.cc)
     1.5 +obj = $(src:.cc=.o)
     1.6 +bin = grav
     1.7 +
     1.8 +CXXFLAGS = -pedantic -Wall -g
     1.9 +LDFLAGS = -lGL -lGLU -lglut -lm -lvmath
    1.10 +
    1.11 +$(bin): $(obj)
    1.12 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    1.13 +
    1.14 +.PHONY: clean
    1.15 +clean:
    1.16 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/main.cc	Fri May 23 03:13:47 2014 +0300
     2.3 @@ -0,0 +1,39 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <GL/glut.h>
     2.7 +#include "sim.h"
     2.8 +
     2.9 +static bool init();
    2.10 +static void cleanup();
    2.11 +static void update(long tmsec);
    2.12 +static void display();
    2.13 +static void idle();
    2.14 +static void reshape(int x, int y);
    2.15 +static void keyb(unsigned char key, int x, int y);
    2.16 +
    2.17 +static SimWorld sim;
    2.18 +
    2.19 +int main(int argc, char **argv)
    2.20 +{
    2.21 +	glutInitWindowSize(1024, 768);
    2.22 +	glutInit(&argc, argv);
    2.23 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    2.24 +	glutCreateWindow("gravity");
    2.25 +
    2.26 +	glutDisplayFunc(display);
    2.27 +	glutIdleFunc(idle);
    2.28 +	glutReshapeFunc(reshape);
    2.29 +	glutKeyboardFunc(keyb);
    2.30 +}
    2.31 +
    2.32 +
    2.33 +static bool init()
    2.34 +{
    2.35 +}
    2.36 +
    2.37 +static void cleanup();
    2.38 +static void update(long tmsec);
    2.39 +static void display();
    2.40 +static void idle();
    2.41 +static void reshape(int x, int y);
    2.42 +static void keyb(unsigned char key, int x, int y);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/sim.cc	Fri May 23 03:13:47 2014 +0300
     3.3 @@ -0,0 +1,73 @@
     3.4 +#include <GL/gl.h>
     3.5 +#include "sim.h"
     3.6 +
     3.7 +Particle::Particle()
     3.8 +{
     3.9 +	mass = 1.0;
    3.10 +}
    3.11 +
    3.12 +void Particle::impulse(const Vector3 &imp)
    3.13 +{
    3.14 +	accel += imp;
    3.15 +}
    3.16 +
    3.17 +void Particle::step(float dt)
    3.18 +{
    3.19 +	Vector3 npos = pos + vel * dt;
    3.20 +
    3.21 +	vel += accel * dt;
    3.22 +	accel.x = accel.y = accel.z = 0.0;
    3.23 +
    3.24 +	pos = npos;
    3.25 +}
    3.26 +
    3.27 +
    3.28 +// ---- SimWorld ----
    3.29 +SimWorld::SimWorld()
    3.30 +{
    3.31 +	set_world_bounds(1000.0f);
    3.32 +}
    3.33 +
    3.34 +void SimWorld::set_world_bounds(float rad)
    3.35 +{
    3.36 +	radius_sq = rad * rad;
    3.37 +}
    3.38 +
    3.39 +void SimWorld::add_particle(const Particle &p)
    3.40 +{
    3.41 +	particles.push_back(p);
    3.42 +}
    3.43 +
    3.44 +void SimWorld::step(float dt)
    3.45 +{
    3.46 +	std::list<Particle>::iterator it = particles.begin();
    3.47 +	while(it != particles.end()) {
    3.48 +		Particle *p = &*it;
    3.49 +		p->step(dt);
    3.50 +
    3.51 +		if(p->pos.length_sq() > radius_sq) {
    3.52 +			it = particles.erase(it);
    3.53 +		} else {
    3.54 +			++it;
    3.55 +		}
    3.56 +	}
    3.57 +}
    3.58 +
    3.59 +void SimWorld::draw() const
    3.60 +{
    3.61 +	glBlendFunc(GL_ONE, GL_ONE);
    3.62 +	glEnable(GL_BLEND);
    3.63 +
    3.64 +	glBegin(GL_POINTS);
    3.65 +	glColor3f(0.5, 0.6, 0.7);
    3.66 +
    3.67 +	std::list<Particle>::const_iterator it = particles.begin();
    3.68 +	while(it != particles.end()) {
    3.69 +		const Particle *p = &*it++;
    3.70 +		glVertex3f(p->pos.x, p->pos.y, p->pos.z);
    3.71 +	}
    3.72 +
    3.73 +	glEnd();
    3.74 +
    3.75 +	glDisable(GL_BLEND);
    3.76 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/sim.h	Fri May 23 03:13:47 2014 +0300
     4.3 @@ -0,0 +1,34 @@
     4.4 +#ifndef SIM_H_
     4.5 +#define SIM_H_
     4.6 +
     4.7 +#include <list>
     4.8 +#include <vmath/vmath.h>
     4.9 +
    4.10 +class Particle {
    4.11 +public:
    4.12 +	float mass;
    4.13 +	Vector3 pos, vel;
    4.14 +	Vector3 accel;
    4.15 +
    4.16 +	Particle();
    4.17 +
    4.18 +	void impulse(const Vector3 &imp);
    4.19 +	void step(float dt);
    4.20 +};
    4.21 +
    4.22 +class SimWorld {
    4.23 +private:
    4.24 +	std::list<Particle> particles;
    4.25 +	float radius_sq;
    4.26 +
    4.27 +public:
    4.28 +	SimWorld();
    4.29 +
    4.30 +	void set_world_bounds(float rad);
    4.31 +
    4.32 +	void add_particle(const Particle &p);
    4.33 +	void step(float dt);
    4.34 +	void draw() const;
    4.35 +};
    4.36 +
    4.37 +#endif	// SIM_H_