1 /* 2 Copyright (c) 2012 Advanced Micro Devices, Inc. 3 4 This software is provided 'as-is', without any express or implied warranty. 5 In no event will the authors be held liable for any damages arising from the use of this software. 6 Permission is granted to anyone to use this software for any purpose, 7 including commercial applications, and to alter it and redistribute it freely, 8 subject to the following restrictions: 9 10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 12 3. This notice may not be removed or altered from any source distribution. 13 */ 14 //Originally written by Erwin Coumans 15 16 #ifndef GL_INSTANCING_RENDERER_H 17 #define GL_INSTANCING_RENDERER_H 18 19 #include "Bullet3Common/b3AlignedObjectArray.h" 20 #include "../CommonInterfaces/CommonRenderInterface.h" 21 #include "SimpleCamera.h" 22 23 class GLInstancingRenderer : public CommonRenderInterface 24 { 25 b3AlignedObjectArray<struct b3GraphicsInstance*> m_graphicsInstances; 26 27 struct InternalDataRenderer* m_data; 28 29 bool m_textureenabled; 30 bool m_textureinitialized; 31 32 int m_screenWidth; 33 int m_screenHeight; 34 35 int m_upAxis; 36 37 int m_planeReflectionShapeIndex; 38 39 int registerGraphicsInstanceInternal(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling); 40 void rebuildGraphicsInstances(); 41 42 public: 43 GLInstancingRenderer(int m_maxObjectCapacity, int maxShapeCapacityInBytes = 56 * 1024 * 1024); 44 virtual ~GLInstancingRenderer(); 45 46 virtual void init(); 47 48 virtual void renderScene(); 49 virtual void renderSceneInternal(int orgRenderMode = B3_DEFAULT_RENDERMODE); 50 51 void InitShaders(); 52 void CleanupShaders(); 53 virtual void removeAllInstances(); 54 virtual void removeGraphicsInstance(int instanceUid); 55 56 virtual void updateShape(int shapeIndex, const float* vertices, int numVertices); 57 58 ///vertices must be in the format x,y,z, nx,ny,nz, u,v 59 virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType = B3_GL_TRIANGLES, int textureIndex = -1); 60 61 virtual int registerTexture(const unsigned char* texels, int width, int height, bool flipPixelsY = true); 62 virtual void updateTexture(int textureIndex, const unsigned char* texels, bool flipPixelsY = true); 63 virtual void activateTexture(int textureIndex); 64 virtual void replaceTexture(int shapeIndex, int textureId); 65 virtual int getShapeIndexFromInstance(int srcIndex); 66 virtual void removeTexture(int textureIndex); 67 68 ///position x,y,z, quaternion x,y,z,w, color r,g,b,a, scaling x,y,z 69 virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling); 70 virtual int registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling); 71 72 void writeTransforms(); 73 74 virtual bool readSingleInstanceTransformToCPU(float* position, float* orientation, int srcIndex); 75 76 virtual void writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex); writeSingleInstanceTransformToCPU(const double * position,const double * orientation,int srcIndex)77 virtual void writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex) 78 { 79 float pos[4]; 80 float orn[4]; 81 pos[0] = (float)position[0]; 82 pos[1] = (float)position[1]; 83 pos[2] = (float)position[2]; 84 pos[3] = (float)position[3]; 85 orn[0] = (float)orientation[0]; 86 orn[1] = (float)orientation[1]; 87 orn[2] = (float)orientation[2]; 88 orn[3] = (float)orientation[3]; 89 writeSingleInstanceTransformToCPU(pos, orn, srcIndex); 90 } 91 92 virtual void readSingleInstanceTransformFromCPU(int srcIndex, float* position, float* orientation); 93 94 virtual void writeSingleInstanceTransformToGPU(float* position, float* orientation, int srcIndex); 95 96 virtual void writeSingleInstanceColorToCPU(const float* color, int srcIndex); 97 virtual void writeSingleInstanceColorToCPU(const double* color, int srcIndex); 98 virtual void writeSingleInstanceFlagsToCPU(int flags, int srcIndex2); 99 100 virtual void writeSingleInstanceSpecularColorToCPU(const double* specular, int srcIndex2); 101 virtual void writeSingleInstanceSpecularColorToCPU(const float* specular, int srcIndex2); 102 103 virtual void writeSingleInstanceScaleToCPU(const float* scale, int srcIndex); 104 virtual void writeSingleInstanceScaleToCPU(const double* scale, int srcIndex); 105 106 virtual struct GLInstanceRendererInternalData* getInternalData(); 107 108 virtual void drawLine(const float from[4], const float to[4], const float color[4], float lineWidth = 1); 109 virtual void drawLine(const double from[4], const double to[4], const double color[4], double lineWidth = 1); 110 virtual void drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize); 111 virtual void drawPoints(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, float pointDrawSize); 112 virtual void drawPoint(const float* position, const float color[4], float pointSize = 1); 113 virtual void drawPoint(const double* position, const double color[4], double pointDrawSize = 1); 114 virtual void drawTexturedTriangleMesh(float worldPosition[3], float worldOrientation[4], const float* vertices, int numvertices, const unsigned int* indices, int numIndices, float color[4], int textureIndex = -1, int vertexLayout = 0); 115 116 virtual void updateCamera(int upAxis = 1); 117 118 virtual const CommonCameraInterface* getActiveCamera() const; 119 virtual CommonCameraInterface* getActiveCamera(); 120 virtual void setActiveCamera(CommonCameraInterface* cam); 121 122 virtual void setLightPosition(const float lightPos[3]); 123 virtual void setLightPosition(const double lightPos[3]); 124 virtual void setShadowMapResolution(int shadowMapResolution); 125 virtual void setShadowMapIntensity(double shadowMapIntensity); 126 virtual void setShadowMapWorldSize(float worldSize); 127 void setLightSpecularIntensity(const float lightSpecularIntensity[3]); 128 virtual void setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]); 129 virtual void setProjectiveTexture(bool useProjectiveTexture); 130 virtual void setBackgroundColor(const double rgbBackground[3]); 131 virtual void resize(int width, int height); getScreenWidth()132 virtual int getScreenWidth() 133 { 134 return m_screenWidth; 135 } getScreenHeight()136 virtual int getScreenHeight() 137 { 138 return m_screenHeight; 139 } 140 141 virtual int getMaxShapeCapacity() const; 142 143 virtual int getInstanceCapacity() const; 144 145 virtual int getTotalNumInstances() const; 146 147 virtual void enableShadowMap(); 148 149 virtual void setPlaneReflectionShapeIndex(int index); 150 151 virtual void clearZBuffer(); 152 153 virtual void setRenderFrameBuffer(unsigned int renderFrameBuffer); 154 }; 155 156 #endif //GL_INSTANCING_RENDERER_H 157