Skip to content

Commit ae30246

Browse files
committed
glfw shader texture
1 parent 6750447 commit ae30246

29 files changed

+957
-176
lines changed

bullet/common.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <btBulletDynamicsCommon.h>
88

99
#define COMMON_PRINTF_FLOAT "%.3e"
10-
#define COMMON_PRINTF_HEADER "step body x y z vx vy vz"
10+
#define COMMON_PRINTF_HEADER "step body x y z vx vy vz rx ry rz angle rvx rvy rvz"
11+
12+
constexpr double PI2 = 2 * acos(-1.0);
1113

1214
void commonPrintBodyState(const btRigidBody *body, int step, int bodyIndex) {
1315
btTransform trans;
@@ -16,15 +18,26 @@ void commonPrintBodyState(const btRigidBody *body, int step, int bodyIndex) {
1618
} else {
1719
trans = body->getWorldTransform();
1820
}
19-
btVector3 position = trans.getOrigin();
20-
btVector3 velocity = body->getLinearVelocity();
21+
auto position = trans.getOrigin();
22+
auto velocity = body->getLinearVelocity();
23+
24+
auto rotationQuat = body->getCenterOfMassTransform().getRotation();
25+
auto rotationAxis = rotationQuat.getAxis();
26+
auto rotationAngle = rotationQuat.getAngle() / PI2;
27+
28+
auto angularVelocity = body->getAngularVelocity();
2129
std::printf(
2230
"%d %d "
2331
COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " "
24-
COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT,
32+
COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " "
33+
COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " "
34+
COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " " COMMON_PRINTF_FLOAT " "
35+
,
2536
step, bodyIndex,
2637
position.getX(), position.getY(), position.getZ(),
27-
velocity.getX(), velocity.getY(), velocity.getZ()
38+
velocity.getX(), velocity.getY(), velocity.getZ(),
39+
rotationAxis.getX(), rotationAxis.getY(), rotationAxis.getZ(), rotationAngle,
40+
angularVelocity.getX(), angularVelocity.getY(), angularVelocity.getZ()
2841
);
2942
}
3043

bullet/getting-started.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@
44

55
does the following:
66

7-
- compile the C++ files
8-
- run them, and save their output to `.dat` files. That is the integrated solution to the physical problem.
9-
- plot the `.dat` files
7+
- compile the C++ files
8+
9+
- run them, and save their output to `.dat` files. That is the integrated solution to the physical problem.
10+
11+
- plot the `.dat` files into `.png` files.
1012

1113
Each line of the `.dat` files has the format:
1214

13-
<tick number> <point id> <x> <y> <z> <speed-x> <speed-y> <speed-z> [<events>]
15+
<tick number> <point id> <x> <y> <z> <speed-x> <speed-y> <speed-z> \
16+
<rot-x> <rot-y> <rot-z> <rot-angle> <rot-speed-x> <rot-speed-y> <rot-speed-z> [<events>]
17+
18+
Note that for the rotation, only 3 parameters are required, but we use 4 as that is easier to view:
19+
20+
- one vector to give the direction on 3 parameters.
21+
22+
This could be done with 2 angles because this vector can be chosen normal, and thus looses a degree of freedom.
23+
24+
- one angle to turn around the vector with right hand rule
25+
26+
The color convention of the graph lines is:
27+
28+
- `r`: `x`, mnemonic: first of RGB.
29+
30+
- `g`: `y`
31+
32+
- `b`: `z`
33+
34+
- `y`: rotation angle. No mnemonic, color chosen randomly.
1435

15-
It is a bit hard to see the beauty of physics engines without a complicated backend.
36+
We always divide the rotation angle by 2 pi, so that is restricted to `[-1,1]`.

bullet/gravity_collision.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ Derived from gravity.cpp
2323

2424
#include "common.hpp"
2525

26-
constexpr float gravity = -10.0f;
26+
constexpr float gravity = 0.0f;
2727
// How inclined the ground plane is towards +x.
2828
constexpr float groundXNormal = 0.0f;
2929
constexpr float initialX = 0.0f;
3030
constexpr float initialY = 10.0f;
3131
constexpr float initialZ = 0.0f;
32+
constexpr float initialLinearVelocityX = 0.0f;
33+
constexpr float initialLinearVelocityY = -10.0f;
34+
constexpr float initialLinearVelocityZ = 0.0f;
3235
constexpr float timeStep = 1.0f / 60.0f;
3336
// TODO some combinations of coefficients smaller than 1.0
3437
// make the ball go up higher / not lose height. Why?
35-
constexpr float groundRestitution = 0.9f;
36-
constexpr float objectRestitution = 0.9f;
38+
constexpr float groundRestitution = 1.0f;
39+
constexpr float objectRestitution = 1.0f;
3740
constexpr int nSteps = 500;
3841

3942
std::map<const btCollisionObject*,std::vector<btManifoldPoint*>> objectsCollisions;
@@ -73,15 +76,15 @@ int main() {
7376
groundTransform.setIdentity();
7477
groundTransform.setOrigin(btVector3(0, 0, 0));
7578
btCollisionShape* groundShape;
76-
#if 1
77-
// x / z plane at y = -1.
79+
#if 0
80+
// x / z plane at y = -1 (not 0 to compensate the radius of the falling object).
7881
groundShape = new btStaticPlaneShape(btVector3(groundXNormal, 1, 0), -1);
7982
#else
80-
// A cube of width 10 at y = -6.
83+
// A cube of width 10 at y = -6 (upper surface at -1).
8184
// Does not fall because we won't call:
8285
// colShape->calculateLocalInertia
8386
// TODO: remove this from this example into a collision shape example.
84-
groundTransform.setOrigin(btVector3(0, -6, 0));
87+
groundTransform.setOrigin(btVector3(-5, -6, 0));
8588
groundShape = new btBoxShape(
8689
btVector3(btScalar(5.0), btScalar(5.0), btScalar(5.0)));
8790

@@ -97,7 +100,7 @@ int main() {
97100
// Object.
98101
{
99102
btCollisionShape *colShape;
100-
#if 1
103+
#if 0
101104
colShape = new btSphereShape(btScalar(1.0));
102105
#else
103106
// Because of numerical instabilities, the cube bumps all over,
@@ -116,6 +119,7 @@ int main() {
116119
btRigidBody *body = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(
117120
mass, myMotionState, colShape, localInertia));
118121
body->setRestitution(objectRestitution);
122+
body->setLinearVelocity(btVector3(initialLinearVelocityX, initialLinearVelocityY, initialLinearVelocityZ));
119123
dynamicsWorld->addRigidBody(body);
120124
}
121125

@@ -145,7 +149,7 @@ int main() {
145149
}
146150
}
147151
}
148-
puts("");
152+
std::printf("\n");
149153
}
150154
}
151155

bullet/plot

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ import numpy
1818

1919
out_ext = 'png'
2020
time_col = 0
21+
2122
position_cols = [2, 3, 4]
2223
velocity_cols = [5, 6, 7]
24+
rotation_cols = [8, 9, 10, 11]
25+
angular_velocity_cols = [12, 13, 14]
26+
nplots_per_body = 4
2327

2428
def plot_cols(cols, title, nplots, key, plot_index):
2529
for c in cols:
@@ -40,17 +44,25 @@ else:
4044
data_name = 'stdin'
4145
data_file = sys.stdin
4246

43-
a = numpy.loadtxt(data_file, skiprows=1, usecols=range(8))
44-
matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['r', 'g', 'b'])
47+
a = numpy.loadtxt(
48+
data_file,
49+
# TODO: convert first two columns to int here.
50+
# dtype=([('a','i4')] * 2 + [('a','f4')] * 6),
51+
skiprows=1,
52+
usecols=range(15)
53+
)
54+
matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['r', 'g', 'b', 'y'])
4555
body_col = 1
4656
keys = list(set(a[:, body_col]))
47-
nplots = 2 * len(keys)
57+
nplots = nplots_per_body * len(keys)
4858
fig, axs = plt.subplots()
4959
for key in keys:
5060
key = int(key)
5161
v = a[a[:, body_col] == key, :]
52-
plot_cols(position_cols, 'pos', nplots, key, 2 * key )
53-
plot_cols(velocity_cols, 'vel', nplots, key, 2 * key + 1)
62+
plot_cols(position_cols, 'pos', nplots, key, nplots_per_body * key )
63+
plot_cols(velocity_cols, 'vel', nplots, key, nplots_per_body * key + 1)
64+
plot_cols(rotation_cols, 'rot', nplots, key, nplots_per_body * key + 2)
65+
plot_cols(angular_velocity_cols, 'ang_vel', nplots, key, nplots_per_body * key + 3)
5466
# TODO custom per figure plots.
5567
# try:
5668
# custom_plotter = imp.load_source(name, name + '.py')

bullet/two_objects.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include <cstdio>
2+
#include <cstdlib>
3+
#include <map>
4+
#include <type_traits>
5+
#include <vector>
6+
7+
#include <btBulletDynamicsCommon.h>
8+
9+
#include "common.hpp"
10+
11+
constexpr float gravity = 0.0f;
12+
// How inclined the ground plane is towards +x.
13+
constexpr float groundXNormal = 0.0f;
14+
constexpr float initialX = 0.0f;
15+
constexpr float initialY = 5.0f;
16+
constexpr float initialZ = 10.0f;
17+
constexpr float initialLinearVelocityX = 1.0f;
18+
constexpr float initialLinearVelocityY = 2.0f;
19+
constexpr float initialLinearVelocityZ = 30.0f;
20+
constexpr float initialAngularVelocityX = 1.0f;
21+
constexpr float initialAngularVelocityY = 2.0f;
22+
constexpr float initialAngularVelocityZ = 3.0f;
23+
constexpr float timeStep = 1.0f / 60.0f;
24+
// TODO some combinations of coefficients smaller than 1.0
25+
// make the ball go up higher / not lose height. Why?
26+
constexpr float groundRestitution = 0.9f;
27+
constexpr float objectRestitution = 0.9f;
28+
constexpr int nSteps = 50;
29+
30+
std::map<const btCollisionObject*,std::vector<btManifoldPoint*>> objectsCollisions;
31+
void myTickCallback(btDynamicsWorld *dynamicsWorld, btScalar timeStep) {
32+
objectsCollisions.clear();
33+
int numManifolds = dynamicsWorld->getDispatcher()->getNumManifolds();
34+
for (int i = 0; i < numManifolds; i++) {
35+
btPersistentManifold *contactManifold = dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
36+
auto *objA = contactManifold->getBody0();
37+
auto *objB = contactManifold->getBody1();
38+
auto& collisionsA = objectsCollisions[objA];
39+
auto& collisionsB = objectsCollisions[objB];
40+
int numContacts = contactManifold->getNumContacts();
41+
for (int j = 0; j < numContacts; j++) {
42+
btManifoldPoint& pt = contactManifold->getContactPoint(j);
43+
collisionsA.push_back(&pt);
44+
collisionsB.push_back(&pt);
45+
}
46+
}
47+
}
48+
49+
int main() {
50+
btDefaultCollisionConfiguration *collisionConfiguration
51+
= new btDefaultCollisionConfiguration();
52+
btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration);
53+
btBroadphaseInterface *overlappingPairCache = new btDbvtBroadphase();
54+
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
55+
btDiscreteDynamicsWorld *dynamicsWorld = new btDiscreteDynamicsWorld(
56+
dispatcher, overlappingPairCache, solver, collisionConfiguration);
57+
dynamicsWorld->setGravity(btVector3(0, gravity, 0));
58+
dynamicsWorld->setInternalTickCallback(myTickCallback);
59+
btAlignedObjectArray<btCollisionShape*> collisionShapes;
60+
61+
// Object0
62+
{
63+
btCollisionShape *colShape;
64+
#if 0
65+
colShape = new btSphereShape(btScalar(1.0));
66+
#else
67+
// Because of numerical instabilities, the cube bumps all over,
68+
// moving on the x and z directions as well as y.
69+
colShape = new btBoxShape(
70+
btVector3(btScalar(1.0), btScalar(1.0), btScalar(1.0)));
71+
#endif
72+
collisionShapes.push_back(colShape);
73+
btTransform startTransform;
74+
startTransform.setIdentity();
75+
startTransform.setOrigin(btVector3(initialX, initialY, initialZ));
76+
btVector3 localInertia(0, 0, 0);
77+
btScalar mass(1.0f);
78+
colShape->calculateLocalInertia(mass, localInertia);
79+
btDefaultMotionState *myMotionState = new btDefaultMotionState(startTransform);
80+
btRigidBody *body = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(
81+
mass, myMotionState, colShape, localInertia));
82+
body->setRestitution(objectRestitution);
83+
body->setLinearVelocity(btVector3(initialLinearVelocityX, initialLinearVelocityY, initialLinearVelocityZ));
84+
body->setAngularVelocity(btVector3(initialAngularVelocityX, initialAngularVelocityY, initialAngularVelocityZ));
85+
dynamicsWorld->addRigidBody(body);
86+
}
87+
88+
// Object1
89+
{
90+
btCollisionShape *colShape;
91+
#if 0
92+
colShape = new btSphereShape(btScalar(1.0));
93+
#else
94+
// Because of numerical instabilities, the cube bumps all over,
95+
// moving on the x and z directions as well as y.
96+
colShape = new btBoxShape(
97+
btVector3(btScalar(1.0), btScalar(1.0), btScalar(1.0)));
98+
#endif
99+
collisionShapes.push_back(colShape);
100+
btTransform startTransform;
101+
startTransform.setIdentity();
102+
startTransform.setOrigin(btVector3(initialX, initialY, initialZ));
103+
btVector3 localInertia(0, 0, 0);
104+
btScalar mass(1.0f);
105+
colShape->calculateLocalInertia(mass, localInertia);
106+
btDefaultMotionState *myMotionState = new btDefaultMotionState(startTransform);
107+
btRigidBody *body = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(
108+
mass, myMotionState, colShape, localInertia));
109+
body->setRestitution(objectRestitution);
110+
body->setLinearVelocity(btVector3(initialLinearVelocityX, initialLinearVelocityY, initialLinearVelocityZ));
111+
body->setAngularVelocity(btVector3(initialAngularVelocityX, initialAngularVelocityY, initialAngularVelocityZ));
112+
dynamicsWorld->addRigidBody(body);
113+
}
114+
115+
// Main loop.
116+
std::printf(COMMON_PRINTF_HEADER "\n");
117+
for (std::remove_const<decltype(nSteps)>::type step = 0; step < nSteps; ++step) {
118+
dynamicsWorld->stepSimulation(timeStep);
119+
auto nCollisionObjects = dynamicsWorld->getNumCollisionObjects();
120+
for (decltype(nCollisionObjects) objectIndex = 0; objectIndex < nCollisionObjects; ++objectIndex) {
121+
btRigidBody *body = btRigidBody::upcast(dynamicsWorld->getCollisionObjectArray()[objectIndex]);
122+
commonPrintBodyState(body, step, objectIndex);
123+
}
124+
std::printf("\n");
125+
}
126+
127+
// Cleanup.
128+
for (int i = dynamicsWorld->getNumCollisionObjects() - 1; i >= 0; --i) {
129+
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
130+
btRigidBody* body = btRigidBody::upcast(obj);
131+
if (body && body->getMotionState()) {
132+
delete body->getMotionState();
133+
}
134+
dynamicsWorld->removeCollisionObject(obj);
135+
delete obj;
136+
}
137+
for (int i = 0; i < collisionShapes.size(); ++i) {
138+
delete collisionShapes[i];
139+
}
140+
delete dynamicsWorld;
141+
delete solver;
142+
delete overlappingPairCache;
143+
delete dispatcher;
144+
delete collisionConfiguration;
145+
collisionShapes.clear();
146+
}

bullet/which_collision.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ int main() {
107107
} else {
108108
std::printf("1");
109109
}
110-
puts("");
110+
std::printf("\n");
111111
}
112112
}
113113

opengl/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@
3535
1. [Offscreen](offscreen.c)
3636
1. GLUT
3737
1. [glutBitmapCharacter](bitmap_character.c)
38-
1. [Animation](animation.c)
38+
1. [Triangle rotate](triangle_rotate.c)
3939
1. [GLFW](glfw.md)
4040
1. [hello world](glfw_hello_world.c)
41-
1. [animation](glfw_animation.c)
41+
1. [triangle rotate](glfw_triangle_rotate.c)
4242
1. Retained mode + shaders
43-
1. [triangle](glfw_triangle.c)
43+
1. [triangle no VBO](glfw_triangle_no_vbo.c)
44+
1. [triangle VAO](glfw_triangle_vao.c)
4445
1. [color](glfw_color.c)
4546
1. [triangles](glfw_triangles.c)
47+
1. [triangles IBO](glfw_triangles.c)
4648
1. [transform](glfw_transform.c)
49+
1. [gl_FragCoord](glfw_gl_frag_coord.c)
4750
1. Naughty
4851
1. [Infinite loop shader](glfw_infinite_loop_shader.c.off)
4952
1. [Memory overflow shader](glfw_memory_overflow_shader.c.off)

opengl/bibliography.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Tutorials with runnable version tracked code:
3232
- <https://github.com/capnramses/antons_opengl_tutorials_book> GLFW, retained, C.
3333
- <https://github.com/Overv/Open.GL/tree/master/content/code> <https://open.gl/> GLFW, retained
3434
- <https://github.com/tomdalling/opengl-series> <http://www.tomdalling.com/blog/category/modern-opengl/> GLFW, retained
35+
- <https://gitlab.com/wikibooks-opengl/modern-tutorials>
3536

3637
Samples without tutorial:
3738

@@ -60,6 +61,7 @@ Java:
6061
Demos:
6162

6263
- <https://github.com/prideout/recipes>
64+
- <https://www.youtube.com/watch?v=H-3yPXXZUC8>
6365

6466
Unevaluated:
6567

0 commit comments

Comments
 (0)