From 01932c9e79cf3848efc564bd51ef9612f92c4faa Mon Sep 17 00:00:00 2001 From: wolfgangp Date: Wed, 16 Mar 2016 10:42:14 +0100 Subject: [PATCH] Squashed commit of the following: (closes #85) commit cff663ad0e23ed73057201fc0c71f12de82be201 Author: wolfgangp Date: Thu Mar 10 10:48:39 2016 +0100 Make get_normal_world_on_b return a vector. commit 6fe5f5a24d615dc05b532c07486e23123e4cc9f2 Author: wolfgangp Date: Thu Mar 3 00:30:01 2016 +0100 Remove comments. commit f94f0ebd1acde2c3c32ffdaadd24deb4d16d1a84 Author: wolfgangp Date: Wed Mar 2 23:53:05 2016 +0100 Revert header author change. commit d50c41b830034e2dc015298cd4596b6292d582ac Author: wolfgangp Date: Wed Mar 2 23:38:45 2016 +0100 Fix indentation and spaces. commit 792eab5fe1ca77564a36d7ab43b8996e03432dbd Author: wolfgangp Date: Wed Mar 2 20:11:43 2016 +0100 Revert changed date. commit 99f78d4a90d43cf3cc5e5168c319020b307cdbd0 Author: wolfgangp Date: Wed Mar 2 19:01:39 2016 +0100 Fix bulletHeightfieldShape to support non-square images. Add alternative constructor taking a Texture input, intended for use with ShaderTerrainMesh. --- panda/src/bullet/bulletHeightfieldShape.cxx | 48 +++++++++++++++++++-- panda/src/bullet/bulletHeightfieldShape.h | 3 ++ panda/src/bullet/bulletManifoldPoint.cxx | 2 +- panda/src/bullet/bulletManifoldPoint.h | 2 +- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/panda/src/bullet/bulletHeightfieldShape.cxx b/panda/src/bullet/bulletHeightfieldShape.cxx index 8952a1d1bc..f6843d199b 100644 --- a/panda/src/bullet/bulletHeightfieldShape.cxx +++ b/panda/src/bullet/bulletHeightfieldShape.cxx @@ -16,7 +16,9 @@ TypeHandle BulletHeightfieldShape::_type_handle; /** - * + * @brief Creates a collision shape suited for terrains from a rectangular image. + * @details Stores the image's brightness values in a vector Bullet can use, + * while rotating it 90 degrees to the right. */ BulletHeightfieldShape:: BulletHeightfieldShape(const PNMImage &image, PN_stdfloat max_height, BulletUpAxis up) { @@ -28,8 +30,10 @@ BulletHeightfieldShape(const PNMImage &image, PN_stdfloat max_height, BulletUpAx for (int row=0; row < _num_rows; row++) { for (int column=0; column < _num_cols; column++) { - _data[_num_cols * row + column] = - max_height * image.get_bright(column, _num_cols - row - 1); + // Transpose + _data[_num_rows * column + row] = + // Flip y + max_height * image.get_bright(row, _num_cols - column - 1); } } @@ -59,3 +63,41 @@ set_use_diamond_subdivision(bool flag) { return _shape->setUseDiamondSubdivision(flag); } + +/** + * @brief Creates a collision shape suited for terrains from a rectangular texture. + * @details Alternative constructor intended for use with ShaderTerrainMesh. This will + * do bilinear sampling at the corners of all texels. Also works with textures + * that are non-power-of-two and/or rectangular. + */ +BulletHeightfieldShape:: +BulletHeightfieldShape(Texture *tex, PN_stdfloat max_height, BulletUpAxis up) { + + _num_rows = tex->get_x_size() + 1; + _num_cols = tex->get_y_size() + 1; + _data = new float[_num_rows * _num_cols]; + + PN_stdfloat step_x = 1.0 / (PN_stdfloat)tex->get_x_size(); + PN_stdfloat step_y = 1.0 / (PN_stdfloat)tex->get_y_size(); + + PT(TexturePeeker) peeker = tex->peek(); + LColor sample; + + for (int row=0; row < _num_rows; row++) { + for (int column=0; column < _num_cols; column++) { + if (!peeker->lookup_bilinear(sample, row * step_x, column * step_y)) { + bullet_cat.error() << "Could not sample texture." << endl; + } + // Transpose + _data[_num_rows * column + row] = max_height * sample.get_x(); + } + } + + _shape = new btHeightfieldTerrainShape(_num_rows, + _num_cols, + _data, + max_height, + up, + true, false); + _shape->setUserPointer(this); +} \ No newline at end of file diff --git a/panda/src/bullet/bulletHeightfieldShape.h b/panda/src/bullet/bulletHeightfieldShape.h index 495b6752f9..c8bf6f7730 100644 --- a/panda/src/bullet/bulletHeightfieldShape.h +++ b/panda/src/bullet/bulletHeightfieldShape.h @@ -21,6 +21,8 @@ #include "bulletShape.h" #include "pnmImage.h" +#include "texture.h" +#include "texturePeeker.h" /** * @@ -29,6 +31,7 @@ class EXPCL_PANDABULLET BulletHeightfieldShape : public BulletShape { PUBLISHED: BulletHeightfieldShape(const PNMImage &image, PN_stdfloat max_height, BulletUpAxis up=Z_up); + BulletHeightfieldShape(Texture *tex, PN_stdfloat max_height, BulletUpAxis up=Z_up); INLINE BulletHeightfieldShape(const BulletHeightfieldShape ©); INLINE void operator = (const BulletHeightfieldShape ©); INLINE ~BulletHeightfieldShape(); diff --git a/panda/src/bullet/bulletManifoldPoint.cxx b/panda/src/bullet/bulletManifoldPoint.cxx index 05ec4e8b87..319050f29c 100644 --- a/panda/src/bullet/bulletManifoldPoint.cxx +++ b/panda/src/bullet/bulletManifoldPoint.cxx @@ -92,7 +92,7 @@ get_position_world_on_b() const { LPoint3 BulletManifoldPoint:: get_normal_world_on_b() const { - return btVector3_to_LPoint3(_pt.m_normalWorldOnB); + return btVector3_to_LVector3(_pt.m_normalWorldOnB); } /** diff --git a/panda/src/bullet/bulletManifoldPoint.h b/panda/src/bullet/bulletManifoldPoint.h index 0488272885..4184ed9d97 100644 --- a/panda/src/bullet/bulletManifoldPoint.h +++ b/panda/src/bullet/bulletManifoldPoint.h @@ -34,7 +34,7 @@ PUBLISHED: PN_stdfloat get_applied_impulse() const; LPoint3 get_position_world_on_a() const; LPoint3 get_position_world_on_b() const; - LPoint3 get_normal_world_on_b() const; + LVector3 get_normal_world_on_b() const; LPoint3 get_local_point_a() const; LPoint3 get_local_point_b() const;