Thanks to @dchichkov for some of the fixes in the eglRendererPlugin!
Rename tinyRenderer -> eglRenderer in the eglRendererPlugin.
Allow to run the eglRendererPlugin to run on Windows (not in EGL mode but Win32OpenGLWindow mode)
Here is a script I tested on Windows:
~~~~~~~~~~~~
mport pybullet as p
import time
p.connect(p.DIRECT)
plugin = p.loadPlugin("e:/develop/bullet3/bin/pybullet_eglRendererPlugin_vs2010_x64_debug.dll","_eglRendererPlugin")
print("plugin=",plugin)
p.setGravity(0,0,-10)
p.loadURDF("plane.urdf",[0,0,-1])
p.loadURDF("r2d2.urdf")
pixelWidth = 320
pixelHeight = 220
while (1):
p.stepSimulation()
viewMatrix = [1.0, 0.0, -0.0, 0.0, -0.0, 0.1736481785774231, -0.9848078489303589, 0.0, 0.0, 0.9848078489303589, 0.1736481785774231, 0.0, -0.0, -5.960464477539063e-08, -4.0, 1.0]
projectionMatrix = [1.0825318098068237, 0.0, 0.0, 0.0, 0.0, 1.732050895690918, 0.0, 0.0, 0.0, 0.0, -1.0002000331878662, -1.0, 0.0, 0.0, -0.020002000033855438, 0.0]
#img_arr = p.getCameraImage(pixelWidth, pixelHeight, viewMatrix,projectionMatrix, shadow=1,lightDirection=[1,1,1])#,renderer=pybullet.ER_BULLET_HARDWARE_OPENGL)
img_arr = p.getCameraImage(pixelWidth, pixelHeight, shadow=1,lightDirection=[1,1,1])#,renderer=pybullet.ER_BULLET_HARDWARE_OPENGL)
#print("img_arr=",img_arr)
time.sleep(1)
~~~~~~~~~~~~~
This commit is contained in:
@@ -38,9 +38,16 @@ subject to the following restrictions:
|
||||
#include "stb_image/stb_image.h"
|
||||
|
||||
|
||||
#include "../OpenGLWindow/EGLOpenGLWindow.h"
|
||||
#include "../OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "../OpenGLWindow/GLRenderToTexture.h"
|
||||
#ifdef _WIN32
|
||||
#include "OpenGLWindow/Win32OpenGLWindow.h"
|
||||
typedef Win32OpenGLWindow DefaultOpenGLWindow;
|
||||
#else
|
||||
#include "OpenGLWindow/EGLOpenGLWindow.h"
|
||||
typedef EGLOpenGLWindow DefaultOpenGLWindow;
|
||||
#endif
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "OpenGLWindow/GLRenderToTexture.h"
|
||||
|
||||
static void printGLString(const char *name, GLenum s) {
|
||||
const char *v = (const char *) glGetString(s);
|
||||
@@ -62,6 +69,7 @@ struct TinyRendererObjectArray
|
||||
btAlignedObjectArray< TinyRenderObjectData*> m_renderObjects;
|
||||
int m_objectUniqueId;
|
||||
int m_linkIndex;
|
||||
int m_graphicsInstanceId;
|
||||
btTransform m_worldTransform;
|
||||
btVector3 m_localScaling;
|
||||
|
||||
@@ -69,13 +77,14 @@ struct TinyRendererObjectArray
|
||||
{
|
||||
m_worldTransform.setIdentity();
|
||||
m_localScaling.setValue(1,1,1);
|
||||
m_graphicsInstanceId = -1;
|
||||
}
|
||||
};
|
||||
|
||||
#define START_WIDTH 640
|
||||
#define START_HEIGHT 480
|
||||
|
||||
struct TinyRendererVisualShapeConverterInternalData
|
||||
struct EGLRendererVisualShapeConverterInternalData
|
||||
{
|
||||
class CommonWindowInterface* m_window;
|
||||
class GLInstancingRenderer* m_instancingRenderer;
|
||||
@@ -111,7 +120,7 @@ struct TinyRendererVisualShapeConverterInternalData
|
||||
SimpleCamera m_camera;
|
||||
|
||||
|
||||
TinyRendererVisualShapeConverterInternalData()
|
||||
EGLRendererVisualShapeConverterInternalData()
|
||||
:m_upAxis(2),
|
||||
m_swWidth(START_WIDTH),
|
||||
m_swHeight(START_HEIGHT),
|
||||
@@ -137,7 +146,7 @@ struct TinyRendererVisualShapeConverterInternalData
|
||||
|
||||
// OpenGL window
|
||||
bool allowRetina=true;
|
||||
m_window = new EGLOpenGLWindow();
|
||||
m_window = new DefaultOpenGLWindow();
|
||||
m_window->setAllowRetina(allowRetina);
|
||||
b3gWindowConstructionInfo ci;
|
||||
ci.m_title = "Title";
|
||||
@@ -177,9 +186,9 @@ struct TinyRendererVisualShapeConverterInternalData
|
||||
|
||||
|
||||
|
||||
TinyRendererVisualShapeConverter::TinyRendererVisualShapeConverter()
|
||||
EGLRendererVisualShapeConverter::EGLRendererVisualShapeConverter()
|
||||
{
|
||||
m_data = new TinyRendererVisualShapeConverterInternalData();
|
||||
m_data = new EGLRendererVisualShapeConverterInternalData();
|
||||
|
||||
float dist = 1.5;
|
||||
float pitch = -10;
|
||||
@@ -190,53 +199,53 @@ TinyRendererVisualShapeConverter::TinyRendererVisualShapeConverter()
|
||||
|
||||
|
||||
}
|
||||
TinyRendererVisualShapeConverter::~TinyRendererVisualShapeConverter()
|
||||
EGLRendererVisualShapeConverter::~EGLRendererVisualShapeConverter()
|
||||
{
|
||||
resetAll();
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightDirection(float x, float y, float z)
|
||||
void EGLRendererVisualShapeConverter::setLightDirection(float x, float y, float z)
|
||||
{
|
||||
m_data->m_lightDirection.setValue(x, y, z);
|
||||
m_data->m_hasLightDirection = true;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightColor(float x, float y, float z)
|
||||
void EGLRendererVisualShapeConverter::setLightColor(float x, float y, float z)
|
||||
{
|
||||
m_data->m_lightColor.setValue(x, y, z);
|
||||
m_data->m_hasLightColor = true;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightDistance(float dist)
|
||||
void EGLRendererVisualShapeConverter::setLightDistance(float dist)
|
||||
{
|
||||
m_data->m_lightDistance = dist;
|
||||
m_data->m_hasLightDistance = true;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setShadow(bool hasShadow)
|
||||
void EGLRendererVisualShapeConverter::setShadow(bool hasShadow)
|
||||
{
|
||||
m_data->m_hasShadow = hasShadow;
|
||||
}
|
||||
void TinyRendererVisualShapeConverter::setFlags(int flags)
|
||||
void EGLRendererVisualShapeConverter::setFlags(int flags)
|
||||
{
|
||||
m_data->m_flags = flags;
|
||||
}
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightAmbientCoeff(float ambientCoeff)
|
||||
void EGLRendererVisualShapeConverter::setLightAmbientCoeff(float ambientCoeff)
|
||||
{
|
||||
m_data->m_lightAmbientCoeff = ambientCoeff;
|
||||
m_data->m_hasLightAmbientCoeff = true;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightDiffuseCoeff(float diffuseCoeff)
|
||||
void EGLRendererVisualShapeConverter::setLightDiffuseCoeff(float diffuseCoeff)
|
||||
{
|
||||
m_data->m_lightDiffuseCoeff = diffuseCoeff;
|
||||
m_data->m_hasLightDiffuseCoeff = true;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::setLightSpecularCoeff(float specularCoeff)
|
||||
void EGLRendererVisualShapeConverter::setLightSpecularCoeff(float specularCoeff)
|
||||
{
|
||||
m_data->m_lightSpecularCoeff = specularCoeff;
|
||||
m_data->m_hasLightSpecularCoeff = true;
|
||||
@@ -609,7 +618,7 @@ static btVector4 sColors[4] =
|
||||
// If you are getting segfaults in this function it may be ecause you are
|
||||
// compliling the plugin with differently from pybullet, try complining the
|
||||
// plugin with distutils too.
|
||||
void TinyRendererVisualShapeConverter::convertVisualShapes(
|
||||
void EGLRendererVisualShapeConverter::convertVisualShapes(
|
||||
int linkIndex, const char* pathPrefix, const btTransform& localInertiaFrame,
|
||||
const UrdfLink* linkPtr, const UrdfModel* model,
|
||||
int collisionObjectUniqueId, int bodyUniqueId)
|
||||
@@ -739,14 +748,17 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(
|
||||
unsigned char* textureImage1=0;
|
||||
int textureWidth=0;
|
||||
int textureHeight=0;
|
||||
bool isCached = false;
|
||||
bool isCached = false;
|
||||
int textureIndex = -1;
|
||||
|
||||
if (textures.size())
|
||||
{
|
||||
textureImage1 = textures[0].textureData1;
|
||||
textureWidth = textures[0].m_width;
|
||||
textureHeight = textures[0].m_height;
|
||||
isCached = textures[0].m_isCached;
|
||||
}
|
||||
textureIndex = m_data->m_instancingRenderer->registerTexture(textureImage1, textureWidth, textureHeight);
|
||||
}
|
||||
|
||||
{
|
||||
B3_PROFILE("registerMeshShape");
|
||||
@@ -760,10 +772,11 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(
|
||||
B3_PROFILE("m_instancingRenderer register");
|
||||
|
||||
// register mesh to m_instancingRenderer too.
|
||||
int textureIndex = m_data->m_instancingRenderer->registerTexture(textureImage1, textureWidth, textureHeight);
|
||||
int shapeIndex = m_data->m_instancingRenderer->registerShape(&vertices[0].xyzw[0], vertices.size(), &indices[0], indices.size(),textureIndex);
|
||||
|
||||
int shapeIndex = m_data->m_instancingRenderer->registerShape(&vertices[0].xyzw[0], vertices.size(), &indices[0], indices.size(),B3_GL_TRIANGLES, textureIndex);
|
||||
btVector3 scaling(1,1,1);
|
||||
m_data->m_instancingRenderer->registerGraphicsInstance(shapeIndex, &visualShape.m_localVisualFrame[0], &visualShape.m_localVisualFrame[3], &visualShape.m_rgbaColor[0],scaling);
|
||||
visuals->m_graphicsInstanceId = m_data->m_instancingRenderer->registerGraphicsInstance(shapeIndex, &visualShape.m_localVisualFrame[0], &visualShape.m_localVisualFrame[3], &visualShape.m_rgbaColor[0],scaling);
|
||||
|
||||
m_data->m_instancingRenderer->writeTransforms();
|
||||
}
|
||||
}
|
||||
@@ -778,7 +791,7 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(
|
||||
}
|
||||
}
|
||||
|
||||
int TinyRendererVisualShapeConverter::getNumVisualShapes(int bodyUniqueId)
|
||||
int EGLRendererVisualShapeConverter::getNumVisualShapes(int bodyUniqueId)
|
||||
{
|
||||
int start = -1;
|
||||
//find first one, then count how many
|
||||
@@ -810,7 +823,7 @@ int TinyRendererVisualShapeConverter::getNumVisualShapes(int bodyUniqueId)
|
||||
return count;
|
||||
}
|
||||
|
||||
int TinyRendererVisualShapeConverter::getVisualShapesData(int bodyUniqueId, int shapeIndex, struct b3VisualShapeData* shapeData)
|
||||
int EGLRendererVisualShapeConverter::getVisualShapesData(int bodyUniqueId, int shapeIndex, struct b3VisualShapeData* shapeData)
|
||||
{
|
||||
int start = -1;
|
||||
//find first one, then count how many
|
||||
@@ -837,7 +850,7 @@ int TinyRendererVisualShapeConverter::getVisualShapesData(int bodyUniqueId, int
|
||||
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::changeRGBAColor(int bodyUniqueId, int linkIndex, int shapeIndex, const double rgbaColor[4])
|
||||
void EGLRendererVisualShapeConverter::changeRGBAColor(int bodyUniqueId, int linkIndex, int shapeIndex, const double rgbaColor[4])
|
||||
{
|
||||
//int start = -1;
|
||||
for (int i = 0; i < m_data->m_visualShapes.size(); i++)
|
||||
@@ -874,7 +887,7 @@ void TinyRendererVisualShapeConverter::changeRGBAColor(int bodyUniqueId, int lin
|
||||
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::setUpAxis(int axis)
|
||||
void EGLRendererVisualShapeConverter::setUpAxis(int axis)
|
||||
{
|
||||
m_data->m_upAxis = axis;
|
||||
m_data->m_camera.setCameraUpAxis(axis);
|
||||
@@ -882,7 +895,7 @@ void TinyRendererVisualShapeConverter::setUpAxis(int axis)
|
||||
m_data->m_instancingRenderer->updateCamera();
|
||||
|
||||
}
|
||||
void TinyRendererVisualShapeConverter::resetCamera(float camDist, float yaw, float pitch, float camPosX,float camPosY, float camPosZ)
|
||||
void EGLRendererVisualShapeConverter::resetCamera(float camDist, float yaw, float pitch, float camPosX,float camPosY, float camPosZ)
|
||||
{
|
||||
m_data->m_camera.setCameraDistance(camDist);
|
||||
m_data->m_camera.setCameraPitch(pitch);
|
||||
@@ -892,7 +905,7 @@ void TinyRendererVisualShapeConverter::resetCamera(float camDist, float yaw, flo
|
||||
m_data->m_camera.update();
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::clearBuffers(TGAColor& clearColor)
|
||||
void EGLRendererVisualShapeConverter::clearBuffers(TGAColor& clearColor)
|
||||
{
|
||||
float farPlane = m_data->m_camera.getCameraFrustumFar();
|
||||
for(int y=0;y<m_data->m_swHeight;++y)
|
||||
@@ -908,7 +921,7 @@ void TinyRendererVisualShapeConverter::clearBuffers(TGAColor& clearColor)
|
||||
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::render()
|
||||
void EGLRendererVisualShapeConverter::render()
|
||||
{
|
||||
/*
|
||||
ATTRIBUTE_ALIGNED16(float viewMat[16]);
|
||||
@@ -920,13 +933,23 @@ void TinyRendererVisualShapeConverter::render()
|
||||
cout<<viewMat[4*2 + 0]<<" "<<viewMat[4*2+1]<<" "<<viewMat[4*2+2]<<" "<<viewMat[4*2+3] << endl;
|
||||
cout<<viewMat[4*3 + 0]<<" "<<viewMat[4*3+1]<<" "<<viewMat[4*3+2]<<" "<<viewMat[4*3+3] << endl;
|
||||
*/
|
||||
|
||||
B3_PROFILE("m_instancingRenderer render");
|
||||
m_data->m_instancingRenderer->setActiveCamera(&m_data->m_camera);
|
||||
m_data->m_instancingRenderer->updateCamera();
|
||||
m_data->m_instancingRenderer->writeTransforms();
|
||||
if (m_data->m_hasLightDirection)
|
||||
{
|
||||
m_data->m_instancingRenderer->setLightPosition(m_data->m_lightDirection);
|
||||
}
|
||||
m_data->m_instancingRenderer->setActiveCamera(&m_data->m_camera);
|
||||
m_data->m_instancingRenderer->updateCamera(m_data->m_upAxis);
|
||||
|
||||
m_data->m_instancingRenderer->renderScene();
|
||||
m_data->m_window->endRendering();
|
||||
m_data->m_window->startRendering();
|
||||
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::render(const float viewMat[16], const float projMat[16])
|
||||
void EGLRendererVisualShapeConverter::render(const float viewMat[16], const float projMat[16])
|
||||
{
|
||||
// This code is very similar to that of
|
||||
// PhysicsServerCommandProcessor::processRequestCameraImageCommand
|
||||
@@ -934,10 +957,10 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo
|
||||
|
||||
// Tiny allows rendering with viewMat, projMat explicitly, but
|
||||
// GLInstancingRender calls m_activeCamera, so set this.
|
||||
m_data->m_camera.setVRCamera(viewMat,projMat);
|
||||
m_data->m_instancingRenderer->setActiveCamera(&m_data->m_camera);
|
||||
m_data->m_instancingRenderer->updateCamera();
|
||||
m_data->m_instancingRenderer->renderScene();
|
||||
|
||||
m_data->m_camera.setVRCamera(viewMat,projMat);
|
||||
|
||||
render();
|
||||
|
||||
//cout<<viewMat[4*0 + 0]<<" "<<viewMat[4*0+1]<<" "<<viewMat[4*0+2]<<" "<<viewMat[4*0+3] << endl;
|
||||
//cout<<viewMat[4*1 + 0]<<" "<<viewMat[4*1+1]<<" "<<viewMat[4*1+2]<<" "<<viewMat[4*1+3] << endl;
|
||||
@@ -945,14 +968,14 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo
|
||||
//cout<<viewMat[4*3 + 0]<<" "<<viewMat[4*3+1]<<" "<<viewMat[4*3+2]<<" "<<viewMat[4*3+3] << endl;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::getWidthAndHeight(int& width, int& height)
|
||||
void EGLRendererVisualShapeConverter::getWidthAndHeight(int& width, int& height)
|
||||
{
|
||||
width = m_data->m_swWidth;
|
||||
height = m_data->m_swHeight;
|
||||
}
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::setWidthAndHeight(int width, int height)
|
||||
void EGLRendererVisualShapeConverter::setWidthAndHeight(int width, int height)
|
||||
{
|
||||
m_data->m_swWidth = width;
|
||||
m_data->m_swHeight = height;
|
||||
@@ -966,7 +989,7 @@ void TinyRendererVisualShapeConverter::setWidthAndHeight(int width, int height)
|
||||
}
|
||||
|
||||
//copied from OpenGLGuiHelper.cpp
|
||||
void TinyRendererVisualShapeConverter::copyCameraImageDataGL(
|
||||
void EGLRendererVisualShapeConverter::copyCameraImageDataGL(
|
||||
unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
|
||||
float* depthBuffer, int depthBufferSizeInPixels,
|
||||
int* segmentationMaskBuffer, int segmentationMaskSizeInPixels,
|
||||
@@ -1079,7 +1102,7 @@ void TinyRendererVisualShapeConverter::copyCameraImageDataGL(
|
||||
}
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
|
||||
void EGLRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
|
||||
float* depthBuffer, int depthBufferSizeInPixels,
|
||||
int* segmentationMaskBuffer, int segmentationMaskSizeInPixels,
|
||||
int startPixelIndex, int* widthPtr, int* heightPtr, int* numPixelsCopied)
|
||||
@@ -1091,7 +1114,7 @@ void TinyRendererVisualShapeConverter::copyCameraImageData(unsigned char* pixels
|
||||
startPixelIndex, widthPtr, heightPtr, numPixelsCopied);
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::removeVisualShape(int collisionObjectUniqueId)
|
||||
void EGLRendererVisualShapeConverter::removeVisualShape(int collisionObjectUniqueId)
|
||||
{
|
||||
TinyRendererObjectArray** ptrptr = m_data->m_swRenderInstances[collisionObjectUniqueId];
|
||||
if (ptrptr && *ptrptr)
|
||||
@@ -1110,7 +1133,7 @@ void TinyRendererVisualShapeConverter::removeVisualShape(int collisionObjectUniq
|
||||
}
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::resetAll()
|
||||
void EGLRendererVisualShapeConverter::resetAll()
|
||||
{
|
||||
for (int i=0;i<m_data->m_swRenderInstances.size();i++)
|
||||
{
|
||||
@@ -1142,7 +1165,7 @@ void TinyRendererVisualShapeConverter::resetAll()
|
||||
}
|
||||
|
||||
|
||||
void TinyRendererVisualShapeConverter::changeShapeTexture(int objectUniqueId, int jointIndex, int shapeIndex, int textureUniqueId)
|
||||
void EGLRendererVisualShapeConverter::changeShapeTexture(int objectUniqueId, int jointIndex, int shapeIndex, int textureUniqueId)
|
||||
{
|
||||
btAssert(textureUniqueId < m_data->m_textures.size());
|
||||
if (textureUniqueId >= 0 && textureUniqueId < m_data->m_textures.size())
|
||||
@@ -1170,7 +1193,7 @@ void TinyRendererVisualShapeConverter::changeShapeTexture(int objectUniqueId, in
|
||||
}
|
||||
}
|
||||
|
||||
int TinyRendererVisualShapeConverter::registerTexture(unsigned char* texels, int width, int height)
|
||||
int EGLRendererVisualShapeConverter::registerTexture(unsigned char* texels, int width, int height)
|
||||
{
|
||||
MyTexture2 texData;
|
||||
texData.m_width = width;
|
||||
@@ -1181,7 +1204,7 @@ int TinyRendererVisualShapeConverter::registerTexture(unsigned char* texels, int
|
||||
return m_data->m_textures.size()-1;
|
||||
}
|
||||
|
||||
int TinyRendererVisualShapeConverter::loadTextureFile(const char* filename)
|
||||
int EGLRendererVisualShapeConverter::loadTextureFile(const char* filename)
|
||||
{
|
||||
B3_PROFILE("loadTextureFile");
|
||||
int width,height,n;
|
||||
@@ -1194,7 +1217,7 @@ int TinyRendererVisualShapeConverter::loadTextureFile(const char* filename)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void TinyRendererVisualShapeConverter::syncTransform(int collisionObjectUniqueId, const btTransform& worldTransform, const btVector3& localScaling)
|
||||
void EGLRendererVisualShapeConverter::syncTransform(int collisionObjectUniqueId, const btTransform& worldTransform, const btVector3& localScaling)
|
||||
{
|
||||
TinyRendererObjectArray** renderObjPtr = m_data->m_swRenderInstances[collisionObjectUniqueId];
|
||||
if (renderObjPtr)
|
||||
@@ -1202,5 +1225,11 @@ void TinyRendererVisualShapeConverter::syncTransform(int collisionObjectUniqueId
|
||||
TinyRendererObjectArray* renderObj = *renderObjPtr;
|
||||
renderObj->m_worldTransform = worldTransform;
|
||||
renderObj->m_localScaling = localScaling;
|
||||
if (renderObj->m_graphicsInstanceId>=0)
|
||||
{
|
||||
btVector3 pos = worldTransform.getOrigin();
|
||||
btQuaternion orn = worldTransform.getRotation();
|
||||
m_data->m_instancingRenderer->writeSingleInstanceTransformToCPU(pos, orn, renderObj->m_graphicsInstanceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user