diff --git a/Demos3/GpuDemos/GpuDemo.h b/Demos3/GpuDemos/GpuDemo.h index 81e74e20f..4e73ddbab 100644 --- a/Demos3/GpuDemos/GpuDemo.h +++ b/Demos3/GpuDemos/GpuDemo.h @@ -41,9 +41,9 @@ public: :useOpenCL(true), preferredOpenCLPlatformIndex(-1), preferredOpenCLDeviceIndex(-1), - arraySizeX(2), - arraySizeY(2), - arraySizeZ(2), + arraySizeX(30), + arraySizeY(30), + arraySizeZ(30), m_useConcaveMesh(false), gapX(14.3), gapY(14.0), diff --git a/Demos3/GpuDemos/main_opengl3core.cpp b/Demos3/GpuDemos/main_opengl3core.cpp index 958226d3f..b5b9e2260 100644 --- a/Demos3/GpuDemos/main_opengl3core.cpp +++ b/Demos3/GpuDemos/main_opengl3core.cpp @@ -76,7 +76,7 @@ GpuDemo::CreateFunc* allDemos[]= // GpuConvexScene::MyCreateFunc, //ConcaveSphereScene::MyCreateFunc, - GpuRaytraceScene::MyCreateFunc, + //GpuRaytraceScene::MyCreateFunc, GpuBoxPlaneScene::MyCreateFunc, GpuConvexPlaneScene::MyCreateFunc, @@ -700,6 +700,8 @@ int main(int argc, char* argv[]) b3ProfileManager::Increment_Frame_Counter(); // render.reshape(g_OpenGLWidth,g_OpenGLHeight); + ci.m_instancingRenderer->resize(g_OpenGLWidth,g_OpenGLHeight); + prim.setScreenSize(g_OpenGLWidth,g_OpenGLHeight); window->startRendering(); diff --git a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp index 0dce134cd..0c859fc2d 100644 --- a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp +++ b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp @@ -31,14 +31,12 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci) m_data->m_rigidBodyPipeline->writeAllInstancesToGpu(); - - float camPos[4]={0,0,0,0};//ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0}; + float camPos[4]={ci.arraySizeX,ci.arraySizeY/2,ci.arraySizeZ,0}; //float camPos[4]={1,12.5,1.5,0}; m_instancingRenderer->setCameraTargetPosition(camPos); - m_instancingRenderer->setCameraDistance(30); - m_instancingRenderer->setCameraYaw(0); - m_instancingRenderer->setCameraPitch(0); + m_instancingRenderer->setCameraDistance(100); + m_instancingRenderer->updateCamera(); @@ -196,8 +194,8 @@ GpuRaytraceScene::GpuRaytraceScene() m_raytraceData = new GpuRaytraceInternalData; m_raytraceData->m_texId = new GLuint; - m_raytraceData->textureWidth = 1024; - m_raytraceData->textureHeight = 768; + m_raytraceData->textureWidth = 256;//1024; + m_raytraceData->textureHeight = 256; //create new texture glGenTextures(1, m_raytraceData->m_texId); @@ -260,8 +258,82 @@ bool sphere_intersect(const b3Vector3& spherePos, b3Scalar radius, const b3Vect return false; } +int GpuRaytraceScene::createDynamicsObjects(const ConstructionInfo& ci) +{ + float radius=1.f; + int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); + int shapeId = registerGraphicsSphereShape(ci,radius,false); + + int group=1; + int mask=1; + int index=0; + + { + b3Vector4 colors[4] = + { + b3Vector4(1,0,0,1), + b3Vector4(0,1,0,1), + b3Vector4(0,1,1,1), + b3Vector4(1,1,0,1), + }; + + int curColor = 0; + float scaling[4] = {1,1,1,1}; + int prevBody = -1; + int insta = 0; + + + //int colIndex = m_data->m_np->registerSphereShape(1); + for (int i=0;i<1;i++) + //for (int i=0;iregisterGraphicsInstance(shapeId,position,orn,color,scaling); + int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false); + + + if (prevBody>=0) + { + //b3Point2PointConstraint* p2p = new b3Point2PointConstraint(pid,prevBody,b3Vector3(0,-1.1,0),b3Vector3(0,1.1,0)); +// m_data->m_rigidBodyPipeline->addConstraint(p2p);//,false); + } + prevBody = pid; + + index++; + } + } + } + } + return index; + +} + + + void GpuRaytraceScene::renderScene() { + + GpuBoxPlaneScene::renderScene(); + B3_PROFILE("raytrace"); //raytrace into the texels m_instancingRenderer->updateCamera(); @@ -270,17 +342,21 @@ void GpuRaytraceScene::renderScene() float top = 1.f; float bottom = -1.f; float nearPlane = 1.f; + float farPlane = 1000.f; float tanFov = (top-bottom)*0.5f / nearPlane; + float screenWidth = m_instancingRenderer->getScreenWidth(); + float screenHeight = m_instancingRenderer->getScreenHeight(); - float fov = 2.0 * atanf (tanFov); + float fov = 2. * atanf (tanFov); + float aspect = screenWidth / screenHeight; b3Vector3 rayFrom, camTarget; m_instancingRenderer->getCameraPosition(rayFrom); m_instancingRenderer->getCameraTargetPosition(camTarget); b3Vector3 rayForward = camTarget-rayFrom; rayForward.normalize(); - float farPlane = 500.f; + rayForward*= farPlane; b3Vector3 rightOffset; @@ -293,14 +369,21 @@ void GpuRaytraceScene::renderScene() float tanfov = tanf(0.5f*fov); - hor *= 2.f * farPlane * tanfov; + hor *= aspect*2.f * farPlane * tanfov; vertical *= 2.f * farPlane * tanfov; b3Vector3 rayToCenter = rayFrom + rayForward; + float texWidth = m_raytraceData->textureWidth; + float texHeight = m_raytraceData->textureHeight; + + float widthFactor = (screenWidth/texWidth); + float heightFactor = (screenHeight/texHeight); + //should be screenwidth/height - b3Vector3 dHor = hor * 1.f/float(m_raytraceData->textureWidth); - b3Vector3 dVert = vertical * 1.f/float(m_raytraceData->textureHeight); + + b3Vector3 dHor = hor * 1./float(screenWidth); + b3Vector3 dVert = vertical * 1./float(screenHeight); b3Transform rayFromTrans; rayFromTrans.setIdentity(); @@ -321,8 +404,8 @@ void GpuRaytraceScene::renderScene() { b3Vector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; - rayTo += x * dHor; - rayTo -= y * dVert; + rayTo += x * dHor*widthFactor; + rayTo -= y * dVert*heightFactor; //if there is a hit, color the pixels int numBodies = m_data->m_rigidBodyPipeline->getNumBodies(); @@ -371,8 +454,9 @@ void GpuRaytraceScene::renderScene() err = glGetError(); assert(err==GL_NO_ERROR); b3Assert(m_primRenderer); - float color[4] = {1,1,1,1}; - float rect[4] = {0,0,m_raytraceData->textureWidth,m_raytraceData->textureHeight}; + float color[4] = {1,1,1,0.2}; + //float rect[4] = {0,0,m_raytraceData->textureWidth,m_raytraceData->textureHeight}; + float rect[4] = {0,0,m_instancingRenderer->getScreenWidth(),m_instancingRenderer->getScreenHeight()}; float u[2] = {0,1}; float v[2] = {0,1}; int useRGBA = 1; diff --git a/Demos3/GpuDemos/rigidbody/GpuConvexScene.h b/Demos3/GpuDemos/rigidbody/GpuConvexScene.h index 3bf1239c0..fe362c7c1 100644 --- a/Demos3/GpuDemos/rigidbody/GpuConvexScene.h +++ b/Demos3/GpuDemos/rigidbody/GpuConvexScene.h @@ -97,6 +97,8 @@ public: return demo; } + virtual int createDynamicsObjects(const ConstructionInfo& ci); + void renderScene(); }; diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.cpp b/btgui/OpenGLWindow/GLInstancingRenderer.cpp index 03bfdeecf..d3c073795 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.cpp +++ b/btgui/OpenGLWindow/GLInstancingRenderer.cpp @@ -59,8 +59,6 @@ struct b3GraphicsInstance bool m_ortho = false; -int m_glutScreenWidth = 1024; -int m_glutScreenHeight = 768; static GLfloat projectionMatrix[16]; static GLfloat modelviewMatrix[16]; @@ -1016,8 +1014,8 @@ void b3CreateLookAt(const b3Vector3& eye, const b3Vector3& center,const b3Vec void GLInstancingRenderer::resize(int width, int height) { - m_glutScreenWidth = width; - m_glutScreenHeight = height; + m_screenWidth = width; + m_screenHeight = height; } void GLInstancingRenderer::updateCamera() @@ -1059,13 +1057,13 @@ void GLInstancingRenderer::updateCamera() m_data->m_cameraPosition[2] = eyePos.getZ(); m_data->m_cameraPosition += m_data->m_cameraTargetPosition; - if (m_glutScreenWidth == 0 && m_glutScreenHeight == 0) + if (m_screenWidth == 0 && m_screenHeight == 0) return; b3Scalar aspect; b3Vector3 extents; - aspect = m_glutScreenWidth / (b3Scalar)m_glutScreenHeight; + aspect = m_screenWidth / (b3Scalar)m_screenHeight; extents.setValue(aspect * 1.0f, 1.0f,0); @@ -1085,7 +1083,7 @@ void GLInstancingRenderer::updateCamera() } - if (m_glutScreenWidth > m_glutScreenHeight) + if (m_screenWidth > m_screenHeight) { b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); } else @@ -1171,14 +1169,14 @@ void GLInstancingRenderer::getMouseDirection(float* dir, int x, int y) b3Scalar aspect; - aspect = m_glutScreenWidth / (b3Scalar)m_glutScreenHeight; + aspect = m_screenWidth / (b3Scalar)m_screenHeight; hor*=aspect; b3Vector3 rayToCenter = rayFrom + rayForward; - b3Vector3 dHor = hor * 1.f/float(m_glutScreenWidth); - b3Vector3 dVert = vertical * 1.f/float(m_glutScreenHeight); + b3Vector3 dHor = hor * 1.f/float(m_screenWidth); + b3Vector3 dVert = vertical * 1.f/float(m_screenHeight); b3Vector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; @@ -1343,7 +1341,7 @@ void GLInstancingRenderer::RenderScene(void) glUseProgram(instancingShaderPointSprite); glUniformMatrix4fv(ProjectionMatrixPointSprite, 1, false, &projectionMatrix[0]); glUniformMatrix4fv(ModelViewMatrixPointSprite, 1, false, &modelviewMatrix[0]); - glUniform1f(screenWidthPointSprite,m_glutScreenWidth); + glUniform1f(screenWidthPointSprite,m_screenWidth); //glUniform1i(uniform_texture_diffusePointSprite, 0); err = glGetError(); diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.h b/btgui/OpenGLWindow/GLInstancingRenderer.h index 3e0e1d351..ba75d0e6a 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.h +++ b/btgui/OpenGLWindow/GLInstancingRenderer.h @@ -41,6 +41,8 @@ class GLInstancingRenderer bool m_textureenabled; bool m_textureinitialized; + int m_screenWidth; + int m_screenHeight; public: @@ -90,6 +92,14 @@ public: void resize(int width, int height); + int getScreenWidth() + { + return m_screenWidth; + } + int getScreenHeight() + { + return m_screenHeight; + } int getMaxShapeCapacity() const {