grav

changeset 1:3d541da6e48c tip

lalalala
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 May 2014 18:27:42 +0300
parents 68db0e456733
children
files grav.sln grav.vcxproj grav.vcxproj.filters src/main.cc src/sim.cc src/sim.h
diffstat 6 files changed, 351 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/grav.sln	Fri May 23 18:27:42 2014 +0300
     1.3 @@ -0,0 +1,24 @@
     1.4 +
     1.5 +Microsoft Visual Studio Solution File, Format Version 12.00
     1.6 +# Visual Studio 2013
     1.7 +VisualStudioVersion = 12.0.30501.0
     1.8 +MinimumVisualStudioVersion = 10.0.40219.1
     1.9 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grav", "grav.vcxproj", "{C45C3601-EB97-417C-B3F8-9F02529BE312}"
    1.10 +EndProject
    1.11 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0CE743CE-21C9-408E-AE91-4BA9F74D931D}"
    1.12 +EndProject
    1.13 +Global
    1.14 +	GlobalSection(SolutionConfigurationPlatforms) = preSolution
    1.15 +		Debug|Win32 = Debug|Win32
    1.16 +		Release|Win32 = Release|Win32
    1.17 +	EndGlobalSection
    1.18 +	GlobalSection(ProjectConfigurationPlatforms) = postSolution
    1.19 +		{C45C3601-EB97-417C-B3F8-9F02529BE312}.Debug|Win32.ActiveCfg = Debug|Win32
    1.20 +		{C45C3601-EB97-417C-B3F8-9F02529BE312}.Debug|Win32.Build.0 = Debug|Win32
    1.21 +		{C45C3601-EB97-417C-B3F8-9F02529BE312}.Release|Win32.ActiveCfg = Release|Win32
    1.22 +		{C45C3601-EB97-417C-B3F8-9F02529BE312}.Release|Win32.Build.0 = Release|Win32
    1.23 +	EndGlobalSection
    1.24 +	GlobalSection(SolutionProperties) = preSolution
    1.25 +		HideSolutionNode = FALSE
    1.26 +	EndGlobalSection
    1.27 +EndGlobal
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/grav.vcxproj	Fri May 23 18:27:42 2014 +0300
     2.3 @@ -0,0 +1,90 @@
     2.4 +<?xml version="1.0" encoding="utf-8"?>
     2.5 +<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     2.6 +  <ItemGroup Label="ProjectConfigurations">
     2.7 +    <ProjectConfiguration Include="Debug|Win32">
     2.8 +      <Configuration>Debug</Configuration>
     2.9 +      <Platform>Win32</Platform>
    2.10 +    </ProjectConfiguration>
    2.11 +    <ProjectConfiguration Include="Release|Win32">
    2.12 +      <Configuration>Release</Configuration>
    2.13 +      <Platform>Win32</Platform>
    2.14 +    </ProjectConfiguration>
    2.15 +  </ItemGroup>
    2.16 +  <PropertyGroup Label="Globals">
    2.17 +    <ProjectGuid>{C45C3601-EB97-417C-B3F8-9F02529BE312}</ProjectGuid>
    2.18 +    <Keyword>Win32Proj</Keyword>
    2.19 +    <RootNamespace>grav</RootNamespace>
    2.20 +  </PropertyGroup>
    2.21 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
    2.22 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    2.23 +    <ConfigurationType>Application</ConfigurationType>
    2.24 +    <UseDebugLibraries>true</UseDebugLibraries>
    2.25 +    <PlatformToolset>v120</PlatformToolset>
    2.26 +    <CharacterSet>MultiByte</CharacterSet>
    2.27 +  </PropertyGroup>
    2.28 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    2.29 +    <ConfigurationType>Application</ConfigurationType>
    2.30 +    <UseDebugLibraries>false</UseDebugLibraries>
    2.31 +    <PlatformToolset>v120</PlatformToolset>
    2.32 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    2.33 +    <CharacterSet>MultiByte</CharacterSet>
    2.34 +  </PropertyGroup>
    2.35 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
    2.36 +  <ImportGroup Label="ExtensionSettings">
    2.37 +  </ImportGroup>
    2.38 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    2.39 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    2.40 +  </ImportGroup>
    2.41 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    2.42 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    2.43 +  </ImportGroup>
    2.44 +  <PropertyGroup Label="UserMacros" />
    2.45 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    2.46 +    <LinkIncremental>true</LinkIncremental>
    2.47 +  </PropertyGroup>
    2.48 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    2.49 +    <LinkIncremental>false</LinkIncremental>
    2.50 +  </PropertyGroup>
    2.51 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    2.52 +    <ClCompile>
    2.53 +      <PrecompiledHeader>
    2.54 +      </PrecompiledHeader>
    2.55 +      <WarningLevel>Level3</WarningLevel>
    2.56 +      <Optimization>Disabled</Optimization>
    2.57 +      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    2.58 +    </ClCompile>
    2.59 +    <Link>
    2.60 +      <SubSystem>Console</SubSystem>
    2.61 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    2.62 +      <AdditionalDependencies>glut32.lib;libvmath.lib;%(AdditionalDependencies)</AdditionalDependencies>
    2.63 +    </Link>
    2.64 +  </ItemDefinitionGroup>
    2.65 +  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    2.66 +    <ClCompile>
    2.67 +      <WarningLevel>Level3</WarningLevel>
    2.68 +      <PrecompiledHeader>
    2.69 +      </PrecompiledHeader>
    2.70 +      <Optimization>MaxSpeed</Optimization>
    2.71 +      <FunctionLevelLinking>true</FunctionLevelLinking>
    2.72 +      <IntrinsicFunctions>true</IntrinsicFunctions>
    2.73 +      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    2.74 +    </ClCompile>
    2.75 +    <Link>
    2.76 +      <SubSystem>Console</SubSystem>
    2.77 +      <GenerateDebugInformation>true</GenerateDebugInformation>
    2.78 +      <EnableCOMDATFolding>true</EnableCOMDATFolding>
    2.79 +      <OptimizeReferences>true</OptimizeReferences>
    2.80 +      <AdditionalDependencies>glut32.lib;libvmath.lib;%(AdditionalDependencies)</AdditionalDependencies>
    2.81 +    </Link>
    2.82 +  </ItemDefinitionGroup>
    2.83 +  <ItemGroup>
    2.84 +    <ClCompile Include="src\main.cc" />
    2.85 +    <ClCompile Include="src\sim.cc" />
    2.86 +  </ItemGroup>
    2.87 +  <ItemGroup>
    2.88 +    <ClInclude Include="src\sim.h" />
    2.89 +  </ItemGroup>
    2.90 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    2.91 +  <ImportGroup Label="ExtensionTargets">
    2.92 +  </ImportGroup>
    2.93 +</Project>
    2.94 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/grav.vcxproj.filters	Fri May 23 18:27:42 2014 +0300
     3.3 @@ -0,0 +1,30 @@
     3.4 +<?xml version="1.0" encoding="utf-8"?>
     3.5 +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     3.6 +  <ItemGroup>
     3.7 +    <Filter Include="Source Files">
     3.8 +      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
     3.9 +      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
    3.10 +    </Filter>
    3.11 +    <Filter Include="Header Files">
    3.12 +      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
    3.13 +      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
    3.14 +    </Filter>
    3.15 +    <Filter Include="Resource Files">
    3.16 +      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
    3.17 +      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
    3.18 +    </Filter>
    3.19 +  </ItemGroup>
    3.20 +  <ItemGroup>
    3.21 +    <ClCompile Include="src\main.cc">
    3.22 +      <Filter>Source Files</Filter>
    3.23 +    </ClCompile>
    3.24 +    <ClCompile Include="src\sim.cc">
    3.25 +      <Filter>Source Files</Filter>
    3.26 +    </ClCompile>
    3.27 +  </ItemGroup>
    3.28 +  <ItemGroup>
    3.29 +    <ClInclude Include="src\sim.h">
    3.30 +      <Filter>Header Files</Filter>
    3.31 +    </ClInclude>
    3.32 +  </ItemGroup>
    3.33 +</Project>
    3.34 \ No newline at end of file
     4.1 --- a/src/main.cc	Fri May 23 03:13:47 2014 +0300
     4.2 +++ b/src/main.cc	Fri May 23 18:27:42 2014 +0300
     4.3 @@ -1,5 +1,6 @@
     4.4  #include <stdio.h>
     4.5  #include <stdlib.h>
     4.6 +#include <assert.h>
     4.7  #include <GL/glut.h>
     4.8  #include "sim.h"
     4.9  
    4.10 @@ -10,8 +11,12 @@
    4.11  static void idle();
    4.12  static void reshape(int x, int y);
    4.13  static void keyb(unsigned char key, int x, int y);
    4.14 +static void mouse(int bn, int st, int x, int y);
    4.15 +static void motion(int x, int y);
    4.16 +static void draw_grid(float sz, int nlines, float alpha);
    4.17  
    4.18  static SimWorld sim;
    4.19 +static float cam_theta, cam_phi = 25, cam_dist = 8;
    4.20  
    4.21  int main(int argc, char **argv)
    4.22  {
    4.23 @@ -24,16 +29,177 @@
    4.24  	glutIdleFunc(idle);
    4.25  	glutReshapeFunc(reshape);
    4.26  	glutKeyboardFunc(keyb);
    4.27 +	glutMouseFunc(mouse);
    4.28 +	glutMotionFunc(motion);
    4.29 +
    4.30 +	if(!init()) {
    4.31 +		return 1;
    4.32 +	}
    4.33 +	atexit(cleanup);
    4.34 +
    4.35 +	glutMainLoop();
    4.36 +	return 0;
    4.37  }
    4.38  
    4.39  
    4.40  static bool init()
    4.41  {
    4.42 +	glEnable(GL_DEPTH_TEST);
    4.43 +	glEnable(GL_CULL_FACE);
    4.44 +	glEnable(GL_LIGHTING);
    4.45 +	glEnable(GL_LIGHT0);
    4.46 +
    4.47 +	Particle p;
    4.48 +	p.mass = 10.0;
    4.49 +	sim.add_particle(p);
    4.50 +	return true;
    4.51  }
    4.52  
    4.53 -static void cleanup();
    4.54 -static void update(long tmsec);
    4.55 -static void display();
    4.56 -static void idle();
    4.57 -static void reshape(int x, int y);
    4.58 -static void keyb(unsigned char key, int x, int y);
    4.59 +static void cleanup()
    4.60 +{
    4.61 +}
    4.62 +
    4.63 +static void update(long tmsec)
    4.64 +{
    4.65 +	static long prev_upd;
    4.66 +	long interval = tmsec - prev_upd;
    4.67 +
    4.68 +	if(interval >= 1000 / 60) {
    4.69 +		prev_upd = tmsec;
    4.70 +
    4.71 +		float dt = (float)interval / 1000.0f;
    4.72 +		sim.step(dt);
    4.73 +	}
    4.74 +}
    4.75 +
    4.76 +static void display()
    4.77 +{
    4.78 +	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
    4.79 +	update(msec);
    4.80 +
    4.81 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    4.82 +
    4.83 +	glMatrixMode(GL_MODELVIEW);
    4.84 +	glLoadIdentity();
    4.85 +	glTranslatef(0, 0, -cam_dist);
    4.86 +	glRotatef(cam_phi, 1, 0, 0);
    4.87 +	glRotatef(cam_theta, 0, 1, 0);
    4.88 +
    4.89 +	sim.draw();
    4.90 +
    4.91 +	draw_grid(10, 10, 1);
    4.92 +
    4.93 +	glutSwapBuffers();
    4.94 +	assert(glGetError() == 0);
    4.95 +}
    4.96 +
    4.97 +static void generate()
    4.98 +{
    4.99 +	Particle p;
   4.100 +	p.mass = (float)rand() / (float)RAND_MAX + 0.25;
   4.101 +
   4.102 +	p.pos.x = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
   4.103 +	p.pos.y = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
   4.104 +	p.pos.z = ((float)rand() / (float)RAND_MAX - 0.5) * 10.0;
   4.105 +
   4.106 +	p.vel.x = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
   4.107 +	p.vel.y = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
   4.108 +	p.vel.z = ((float)rand() / (float)RAND_MAX - 0.5) * 1.0;
   4.109 +
   4.110 +	sim.add_particle(p);
   4.111 +}
   4.112 +
   4.113 +static void idle()
   4.114 +{
   4.115 +	glutPostRedisplay();
   4.116 +}
   4.117 +
   4.118 +static void reshape(int x, int y)
   4.119 +{
   4.120 +	glMatrixMode(GL_PROJECTION);
   4.121 +	glLoadIdentity();
   4.122 +	gluPerspective(50.0, (float)x / (float)y, 0.5, 1000.0);
   4.123 +
   4.124 +	glViewport(0, 0, x, y);
   4.125 +}
   4.126 +
   4.127 +static void keyb(unsigned char key, int x, int y)
   4.128 +{
   4.129 +	switch(key) {
   4.130 +	case 27:
   4.131 +		exit(0);
   4.132 +
   4.133 +	case ' ':
   4.134 +		generate();
   4.135 +		break;
   4.136 +	}
   4.137 +}
   4.138 +
   4.139 +static bool bnstate[16];
   4.140 +static int prev_x, prev_y;
   4.141 +
   4.142 +static void mouse(int bn, int st, int x, int y)
   4.143 +{
   4.144 +	prev_x = x;
   4.145 +	prev_y = y;
   4.146 +	bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
   4.147 +}
   4.148 +
   4.149 +static void motion(int x, int y)
   4.150 +{
   4.151 +	int dx = x - prev_x;
   4.152 +	int dy = y - prev_y;
   4.153 +	prev_x = x;
   4.154 +	prev_y = y;
   4.155 +
   4.156 +	if(!dx && !dy) return;
   4.157 +
   4.158 +	if(bnstate[0]) {
   4.159 +		cam_theta += dx * 0.5;
   4.160 +		cam_phi += dy * 0.5;
   4.161 +
   4.162 +		if(cam_phi < -90) cam_phi = -90;
   4.163 +		if(cam_phi > 90) cam_phi = 90;
   4.164 +	}
   4.165 +	if(bnstate[2]) {
   4.166 +		cam_dist += dy * 0.1;
   4.167 +		if(cam_dist < 0.0) cam_dist = 0.0;
   4.168 +	}
   4.169 +}
   4.170 +
   4.171 +static void draw_grid(float sz, int nlines, float alpha)
   4.172 +{
   4.173 +	float hsz = sz / 2.0;
   4.174 +	float offs = sz / (float)nlines;
   4.175 +
   4.176 +	glPushAttrib(GL_ENABLE_BIT);
   4.177 +	glDisable(GL_LIGHTING);
   4.178 +	glDisable(GL_TEXTURE_2D);
   4.179 +
   4.180 +	glLineWidth(2.0);
   4.181 +	glBegin(GL_LINES);
   4.182 +	glColor4f(1, 0, 0, alpha);
   4.183 +	glVertex3f(-hsz, 0, 0);
   4.184 +	glVertex3f(hsz, 0, 0);
   4.185 +	glColor4f(0, 0, 1, alpha);
   4.186 +	glVertex3f(0, 0, -hsz);
   4.187 +	glVertex3f(0, 0, hsz);
   4.188 +	glEnd();
   4.189 +
   4.190 +	glLineWidth(1.0);
   4.191 +	glBegin(GL_LINES);
   4.192 +	glColor4f(0.5, 0.5, 0.5, alpha);
   4.193 +	for(int i=0; i<nlines / 2; i++) {
   4.194 +		float dist = (float)(i + 1) * offs;
   4.195 +		for(int j=0; j<2; j++) {
   4.196 +			float sign = j > 0 ? -1.0 : 1.0;
   4.197 +			glVertex3f(-hsz, 0, dist * sign);
   4.198 +			glVertex3f(hsz, 0, dist * sign);
   4.199 +			glVertex3f(dist * sign, 0, -hsz);
   4.200 +			glVertex3f(dist * sign, 0, hsz);
   4.201 +		}
   4.202 +	}
   4.203 +	glEnd();
   4.204 +
   4.205 +	glPopAttrib();
   4.206 +}
   4.207 \ No newline at end of file
     5.1 --- a/src/sim.cc	Fri May 23 03:13:47 2014 +0300
     5.2 +++ b/src/sim.cc	Fri May 23 18:27:42 2014 +0300
     5.3 @@ -1,4 +1,4 @@
     5.4 -#include <GL/gl.h>
     5.5 +#include <GL/glut.h>
     5.6  #include "sim.h"
     5.7  
     5.8  Particle::Particle()
     5.9 @@ -6,9 +6,9 @@
    5.10  	mass = 1.0;
    5.11  }
    5.12  
    5.13 -void Particle::impulse(const Vector3 &imp)
    5.14 +void Particle::add_force(const Vector3 &force)
    5.15  {
    5.16 -	accel += imp;
    5.17 +	accel += force;
    5.18  }
    5.19  
    5.20  void Particle::step(float dt)
    5.21 @@ -18,6 +18,7 @@
    5.22  	vel += accel * dt;
    5.23  	accel.x = accel.y = accel.z = 0.0;
    5.24  
    5.25 +	prev_pos = pos;
    5.26  	pos = npos;
    5.27  }
    5.28  
    5.29 @@ -40,7 +41,21 @@
    5.30  
    5.31  void SimWorld::step(float dt)
    5.32  {
    5.33 -	std::list<Particle>::iterator it = particles.begin();
    5.34 +	for(size_t i=0; i<particles.size(); i++) {
    5.35 +		for(size_t j=0; j<particles.size(); j++) {
    5.36 +			if(i == j) continue;
    5.37 +
    5.38 +			// calculate amount of force applied by particle[j] to particle[i]
    5.39 +			Vector3 gdir = particles[j].pos - particles[i].pos;
    5.40 +			float len = gdir.length();
    5.41 +			if(len > 0.0) {
    5.42 +				gdir /= len;
    5.43 +				particles[i].add_force(gdir * particles[j].mass / (len * len));
    5.44 +			}
    5.45 +		}
    5.46 +	}
    5.47 +
    5.48 +	std::vector<Particle>::iterator it = particles.begin();
    5.49  	while(it != particles.end()) {
    5.50  		Particle *p = &*it;
    5.51  		p->step(dt);
    5.52 @@ -58,16 +73,22 @@
    5.53  	glBlendFunc(GL_ONE, GL_ONE);
    5.54  	glEnable(GL_BLEND);
    5.55  
    5.56 -	glBegin(GL_POINTS);
    5.57  	glColor3f(0.5, 0.6, 0.7);
    5.58  
    5.59 -	std::list<Particle>::const_iterator it = particles.begin();
    5.60 -	while(it != particles.end()) {
    5.61 -		const Particle *p = &*it++;
    5.62 +	for(size_t i=0; i<particles.size(); i++) {
    5.63 +		const Particle *p = &particles[i];
    5.64 +
    5.65 +		glPushMatrix();
    5.66 +		glTranslatef(p->pos.x, p->pos.y, p->pos.z);
    5.67 +		glutSolidSphere(p->mass * 0.1, 12, 6);
    5.68 +		glPopMatrix();
    5.69 +
    5.70 +		glBegin(GL_LINES);
    5.71 +		glNormal3f(0, 0, 1);
    5.72  		glVertex3f(p->pos.x, p->pos.y, p->pos.z);
    5.73 +		glVertex3f(p->prev_pos.x, p->prev_pos.y, p->prev_pos.z);
    5.74 +		glEnd();
    5.75  	}
    5.76  
    5.77 -	glEnd();
    5.78 -
    5.79  	glDisable(GL_BLEND);
    5.80  }
     6.1 --- a/src/sim.h	Fri May 23 03:13:47 2014 +0300
     6.2 +++ b/src/sim.h	Fri May 23 18:27:42 2014 +0300
     6.3 @@ -1,24 +1,25 @@
     6.4  #ifndef SIM_H_
     6.5  #define SIM_H_
     6.6  
     6.7 -#include <list>
     6.8 +#include <vector>
     6.9  #include <vmath/vmath.h>
    6.10  
    6.11  class Particle {
    6.12  public:
    6.13  	float mass;
    6.14  	Vector3 pos, vel;
    6.15 +	Vector3 prev_pos;
    6.16  	Vector3 accel;
    6.17  
    6.18  	Particle();
    6.19  
    6.20 -	void impulse(const Vector3 &imp);
    6.21 +	void add_force(const Vector3 &force);
    6.22  	void step(float dt);
    6.23  };
    6.24  
    6.25  class SimWorld {
    6.26  private:
    6.27 -	std::list<Particle> particles;
    6.28 +	std::vector<Particle> particles;
    6.29  	float radius_sq;
    6.30  
    6.31  public: