From f2cc840c3113fcbf94b0cee1788c0698c3726199 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Fri, 28 Jun 2013 14:10:23 -0700 Subject: [PATCH] move shaders to .glsl files and stringify to .h file. add crude screenshot facility (using F1 key), it can also be used for debugging start with shadows using shadowmap, not working yet add experimental 'ignore' body index in raycast, using b3HitInfo.m_m_hitResult2 --- Demos3/GpuDemos/GpuDemoInternalData.h | 6 +- Demos3/GpuDemos/ParticleDemo.cpp | 2 +- Demos3/GpuDemos/broadphase/PairBench.cpp | 2 +- Demos3/GpuDemos/main_opengl3core.cpp | 153 ++++- .../GpuDemos/raytrace/RaytracedShadowDemo.cpp | 444 ++++++++++++ .../GpuDemos/raytrace/RaytracedShadowDemo.h | 33 + Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp | 304 +-------- Demos3/GpuDemos/rigidbody/GpuConvexScene.h | 24 +- .../GpuDemos/rigidbody/GpuRigidBodyDemo.cpp | 5 +- Demos3/GpuDemos/shadows/ShadowMapDemo.cpp | 62 ++ Demos3/GpuDemos/shadows/ShadowMapDemo.h | 36 + .../shadows/ShadowMapDemoInternalData.h | 11 + Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp | 4 +- Demos3/GpuGuiInitialize/premake4.lua | 3 +- btgui/OpenGLWindow/GLInstancingRenderer.cpp | 630 ++++++++++-------- btgui/OpenGLWindow/GLInstancingRenderer.h | 10 +- btgui/OpenGLWindow/GLRenderToTexture.cpp | 72 +- btgui/OpenGLWindow/GLRenderToTexture.h | 9 +- .../Shaders/createShadowMapInstancingPS.glsl | 10 + .../Shaders/createShadowMapInstancingPS.h | 14 + .../Shaders/createShadowMapInstancingVS.h | 60 ++ .../Shaders/createshadowMapInstancingVS.glsl | 56 ++ btgui/OpenGLWindow/Shaders/instancingPS.glsl | 36 + btgui/OpenGLWindow/Shaders/instancingPS.h | 40 ++ btgui/OpenGLWindow/Shaders/instancingVS.glsl | 82 +++ btgui/OpenGLWindow/Shaders/instancingVS.h | 86 +++ btgui/OpenGLWindow/Shaders/pointSpritePS.glsl | 37 + btgui/OpenGLWindow/Shaders/pointSpritePS.h | 41 ++ btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl | 46 ++ btgui/OpenGLWindow/Shaders/pointSpriteVS.h | 50 ++ .../Shaders/useShadowMapInstancingPS.glsl | 40 ++ .../Shaders/useShadowMapInstancingPS.h | 44 ++ .../Shaders/useShadowMapInstancingVS.glsl | 86 +++ .../Shaders/useShadowMapInstancingVS.h | 90 +++ build3/stringify.bat | 18 + build3/stringifyShaders.bat | 17 + .../Initialize/b3OpenCLUtils.cpp | 1 + src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h | 11 +- src/Bullet3OpenCL/Raycast/b3GpuRaycast.cpp | 18 +- .../Raycast/kernels/rayCastKernels.cl | 6 +- .../Raycast/kernels/rayCastKernels.h | 6 +- 41 files changed, 2060 insertions(+), 645 deletions(-) create mode 100644 Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp create mode 100644 Demos3/GpuDemos/raytrace/RaytracedShadowDemo.h create mode 100644 Demos3/GpuDemos/shadows/ShadowMapDemo.cpp create mode 100644 Demos3/GpuDemos/shadows/ShadowMapDemo.h create mode 100644 Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h create mode 100644 btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h create mode 100644 btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h create mode 100644 btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/instancingPS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/instancingPS.h create mode 100644 btgui/OpenGLWindow/Shaders/instancingVS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/instancingVS.h create mode 100644 btgui/OpenGLWindow/Shaders/pointSpritePS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/pointSpritePS.h create mode 100644 btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/pointSpriteVS.h create mode 100644 btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h create mode 100644 btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl create mode 100644 btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h create mode 100644 build3/stringifyShaders.bat diff --git a/Demos3/GpuDemos/GpuDemoInternalData.h b/Demos3/GpuDemos/GpuDemoInternalData.h index 437cb82ed..be6584c35 100644 --- a/Demos3/GpuDemos/GpuDemoInternalData.h +++ b/Demos3/GpuDemos/GpuDemoInternalData.h @@ -13,7 +13,11 @@ struct GpuDemoInternalData char* m_clDeviceName; GpuDemoInternalData() - :m_clInitialized(false), + :m_platformId(0), + m_clContext(0), + m_clDevice(0), + m_clQueue(0), + m_clInitialized(false), m_clDeviceName(0) { diff --git a/Demos3/GpuDemos/ParticleDemo.cpp b/Demos3/GpuDemos/ParticleDemo.cpp index bbaaae7c7..a8234a9eb 100644 --- a/Demos3/GpuDemos/ParticleDemo.cpp +++ b/Demos3/GpuDemos/ParticleDemo.cpp @@ -276,7 +276,7 @@ void ParticleDemo::renderScene() if (m_instancingRenderer) { - m_instancingRenderer->RenderScene(); + m_instancingRenderer->renderScene(); } } diff --git a/Demos3/GpuDemos/broadphase/PairBench.cpp b/Demos3/GpuDemos/broadphase/PairBench.cpp index 2cae268e9..e14d18994 100644 --- a/Demos3/GpuDemos/broadphase/PairBench.cpp +++ b/Demos3/GpuDemos/broadphase/PairBench.cpp @@ -230,7 +230,7 @@ void PairBench::exitPhysics() void PairBench::renderScene() { - m_instancingRenderer->RenderScene(); + m_instancingRenderer->renderScene(); } void PairBench::clientMoveAndDisplay() diff --git a/Demos3/GpuDemos/main_opengl3core.cpp b/Demos3/GpuDemos/main_opengl3core.cpp index 336fb8611..61b5b4dbc 100644 --- a/Demos3/GpuDemos/main_opengl3core.cpp +++ b/Demos3/GpuDemos/main_opengl3core.cpp @@ -38,8 +38,14 @@ #include "softbody/GpuSoftBodyDemo.h" #include "../btgui/Timing/b3Quickprof.h" +#include "../btgui/OpenGLWindow/GLRenderToTexture.h" +#include "raytrace/RaytracedShadowDemo.h" +#include "shadows/ShadowMapDemo.h" +bool exportFrame=false; +int frameIndex = 0; +GLRenderToTexture* renderTexture =0; //#include "BroadphaseBenchmark.h" int g_OpenGLWidth=1024; @@ -81,11 +87,13 @@ GpuDemo::CreateFunc* allDemos[]= +// ConcaveSphereScene::MyCreateFunc, + +// ShadowMapDemo::MyCreateFunc, GpuBoxPlaneScene::MyCreateFunc, GpuConvexPlaneScene::MyCreateFunc, - ConcaveSphereScene::MyCreateFunc, GpuCompoundScene::MyCreateFunc, @@ -197,6 +205,10 @@ void MyKeyboardCallback(int key, int state) { window->setRequestExit(); } + if (key==B3G_F1) + { + exportFrame = true; + } b3DefaultKeyboardCallback(key,state); } @@ -410,6 +422,68 @@ void myprintf(const char* msg) fprintf(defaultOutput,msg); } + + + + +//#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "OpenGLTrueTypeFont/stb_image_write.h" +void writeTextureToPng(int textureWidth, int textureHeight, const char* fileName) +{ + int numComponents = 4; + //glPixelStorei(GL_PACK_ALIGNMENT,1); + GLuint err=glGetError(); + assert(err==GL_NO_ERROR); + glReadBuffer(GL_BACK);//COLOR_ATTACHMENT0); + err=glGetError(); + assert(err==GL_NO_ERROR); + float* orgPixels = (float*)malloc(textureWidth*textureHeight*numComponents*4); + glReadPixels(0,0,textureWidth, textureHeight, GL_RGBA, GL_FLOAT, orgPixels); + //it is useful to have the actual float values for debugging purposes + + //convert float->char + char* pixels = (char*)malloc(textureWidth*textureHeight*numComponents); + err=glGetError(); + assert(err==GL_NO_ERROR); + + for (int j=0;jendRendering(); @@ -621,6 +695,7 @@ int main(int argc, char* argv[]) ci.m_window = window; ci.m_gui = gui; ci.m_instancingRenderer->init(); + ci.m_instancingRenderer->resize(g_OpenGLWidth,g_OpenGLHeight); ci.m_instancingRenderer->InitShaders(); ci.m_primRenderer = &prim; // render.init(); @@ -709,15 +784,51 @@ int main(int argc, char* argv[]) fprintf(defaultOutput," Preferred cl_platform index%d\n", ci.preferredOpenCLPlatformIndex); fprintf(defaultOutput,"\n"); - b3OpenCLUtils::printPlatformInfo( demo->getInternalData()->m_platformId); - fprintf(defaultOutput,"\n"); - b3OpenCLUtils::printDeviceInfo( demo->getInternalData()->m_clDevice); - fprintf(defaultOutput,"\n"); + if (demo->getInternalData()->m_platformId) + { + b3OpenCLUtils::printPlatformInfo( demo->getInternalData()->m_platformId); + fprintf(defaultOutput,"\n"); + b3OpenCLUtils::printDeviceInfo( demo->getInternalData()->m_clDevice); + fprintf(defaultOutput,"\n"); + } do { + GLint err = glGetError(); assert(err==GL_NO_ERROR); + + if (exportFrame) + { + + if (!renderTexture) + { + renderTexture = new GLRenderToTexture(); + GLuint renderTextureId; + glGenTextures(1, &renderTextureId); + + // "Bind" the newly created texture : all future texture functions will modify this texture + glBindTexture(GL_TEXTURE_2D, renderTextureId); + + // Give an empty image to OpenGL ( the last "0" ) + //glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, g_OpenGLWidth,g_OpenGLHeight, 0,GL_RGBA, GL_UNSIGNED_BYTE, 0); + //glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, g_OpenGLWidth,g_OpenGLHeight, 0,GL_RGBA, GL_FLOAT, 0); + glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, g_OpenGLWidth,g_OpenGLHeight, 0,GL_RGBA, GL_FLOAT, 0); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + renderTexture->init(g_OpenGLWidth,g_OpenGLHeight,renderTextureId, false); + } + + bool result = renderTexture->enable(); + } + + err = glGetError(); + assert(err==GL_NO_ERROR); + b3ProfileManager::Reset(); b3ProfileManager::Increment_Frame_Counter(); @@ -725,12 +836,20 @@ int main(int argc, char* argv[]) ci.m_instancingRenderer->resize(g_OpenGLWidth,g_OpenGLHeight); prim.setScreenSize(g_OpenGLWidth,g_OpenGLHeight); + err = glGetError(); + assert(err==GL_NO_ERROR); + window->startRendering(); - glClearColor(0.6,0.6,0.6,1); - glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); + err = glGetError(); + assert(err==GL_NO_ERROR); + + glClearColor(0,0,0,0); + glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);//|GL_STENCIL_BUFFER_BIT); glEnable(GL_DEPTH_TEST); + err = glGetError(); + assert(err==GL_NO_ERROR); if (!gPause) { @@ -762,6 +881,19 @@ int main(int argc, char* argv[]) } */ + + + if (exportFrame) + { + + char fileName[1024]; + sprintf(fileName,"screenShot%d.png",frameIndex++); + writeTextureToPng(g_OpenGLWidth,g_OpenGLHeight,fileName); + exportFrame = false; + renderTexture->disable(); + } + + { B3_PROFILE("gui->draw"); gui->draw(g_OpenGLWidth,g_OpenGLHeight); @@ -769,10 +901,12 @@ int main(int argc, char* argv[]) err = glGetError(); assert(err==GL_NO_ERROR); + { B3_PROFILE("window->endRendering"); window->endRendering(); } + err = glGetError(); assert(err==GL_NO_ERROR); @@ -808,6 +942,7 @@ int main(int argc, char* argv[]) + } while (!window->requestedExit() && !gReset); diff --git a/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp new file mode 100644 index 000000000..913563eb3 --- /dev/null +++ b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp @@ -0,0 +1,444 @@ + +#include "RaytracedShadowDemo.h" + + + + +#include "OpenGLWindow/ShapeData.h" + +#include "OpenGLWindow/GLInstancingRenderer.h" +#include "Bullet3Common/b3Quaternion.h" +#include "OpenGLWindow/b3gWindowInterface.h" +#include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h" +#include "../GpuDemoInternalData.h" +#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h" +#include "OpenGLWindow/OpenGLInclude.h" +#include "OpenGLWindow/GLInstanceRendererInternalData.h" +#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h" +#include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h" +#include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h" +#include "Bullet3OpenCL/RigidBody/b3Config.h" +#include "../rigidbody/GpuRigidBodyDemoInternalData.h" +#include "../gwenUserInterface.h" +#include "Bullet3Dynamics/ConstraintSolver/b3Point2PointConstraint.h" +#include "OpenGLWindow/GLPrimitiveRenderer.h" +#include "Bullet3OpenCL/Raycast/b3GpuRaycast.h" +#include "Bullet3OpenCL/NarrowphaseCollision/b3ConvexUtility.h" + +#include "OpenGLWindow/GLRenderToTexture.h" + + + +struct GpuRaytraceInternalData +{ + GLuint* m_texId; + unsigned char* m_texels; + int textureWidth; + int textureHeight; + struct GLRenderToTexture* m_renderToTexture; + +}; +#include + +GpuRaytraceScene::GpuRaytraceScene() +{ + + m_raytraceData = new GpuRaytraceInternalData; + m_raytraceData->m_renderToTexture = 0;//new GLRenderToTexture(); + + m_raytraceData->m_texId = new GLuint; + m_raytraceData->textureWidth = 512;//1024;//1024; + m_raytraceData->textureHeight = 512;//1024; + + //create new texture + glGenTextures(1, m_raytraceData->m_texId); + GLenum err = glGetError(); + assert(err==GL_NO_ERROR); + + + + glBindTexture(GL_TEXTURE_2D, *m_raytraceData->m_texId); + m_raytraceData->m_texels = (unsigned char*)malloc(m_raytraceData->textureWidth*m_raytraceData->textureHeight*3); + memset(m_raytraceData->m_texels,0,m_raytraceData->textureWidth*m_raytraceData->textureHeight*3); + for (int i=0;itextureWidth;i++) + { + for (int y=0;ytextureHeight;y++) + { + int color = 0; + if (ytextureHeight-1 && (y>0) && (i>0 && itextureWidth-1)) + color = 255; + + m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+0] = color; + m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+1] = color; + m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+2] = color; + } + } + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_raytraceData->textureWidth, m_raytraceData->textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, m_raytraceData->m_texels); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + err = glGetError(); + assert(err==GL_NO_ERROR); + +} +GpuRaytraceScene::~GpuRaytraceScene() +{ + glDeleteTextures(1,m_raytraceData->m_texId); + delete[] m_raytraceData->m_texels; + delete m_raytraceData->m_renderToTexture; + delete m_raytraceData; +} + + + +int GpuRaytraceScene::createDynamicsObjects(const ConstructionInfo& ci2) +{ + //m_raytraceData->m_renderToTexture->init(ci2.m_instancingRenderer->getScreenWidth(),ci2.m_instancingRenderer->getScreenHeight()); + ConstructionInfo ci = ci2; + ci.arraySizeX = 2; + ci.arraySizeY = 50; + ci.arraySizeZ = 2; + + int strideInBytes = 9*sizeof(float); + int numVertices = sizeof(cube_vertices)/strideInBytes; + int numIndices = sizeof(cube_indices)/sizeof(int); + return createDynamicsObjects2(ci,cube_vertices,numVertices,cube_indices,numIndices); + + 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; + +} + + +//create primary rays + +void GpuRaytraceScene::renderScene() +{ + + renderScene2(); + return; + //m_raytraceData->m_renderToTexture->enable(); + m_instancingRenderer->renderScene(); + //m_raytraceData->m_renderToTexture->disable(); +} + +void GpuRaytraceScene::renderScene2() +{ + +// GpuBoxPlaneScene::renderScene(); +// return; + B3_PROFILE("raytrace"); + + //raytrace into the texels + { + B3_PROFILE("update camera"); + m_instancingRenderer->updateCamera(); + } + //generate primary rays + + { + B3_PROFILE("readbackAllBodiesToCpu"); + m_data->m_np->readbackAllBodiesToCpu(); + } + + + { + B3_PROFILE("Generate primary rays"); + 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. * atanf (tanFov); + float aspect = screenWidth / screenHeight; + + b3Vector3 rayFrom, camTarget; + m_instancingRenderer->getCameraPosition(rayFrom); + m_instancingRenderer->getCameraTargetPosition(camTarget); + b3Vector3 rayForward = camTarget-rayFrom; + rayForward.normalize(); + + rayForward*= farPlane; + + b3Vector3 rightOffset; + b3Vector3 vertical(0.f,1.f,0.f); + b3Vector3 hor; + hor = rayForward.cross(vertical); + hor.normalize(); + vertical = hor.cross(rayForward); + vertical.normalize(); + + float tanfov = tanf(0.5f*fov); + + 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./float(screenWidth); + b3Vector3 dVert = vertical * 1./float(screenHeight); + + b3Transform rayFromTrans; + rayFromTrans.setIdentity(); + rayFromTrans.setOrigin(rayFrom); + + b3Transform rayFromLocal; + b3Transform rayToLocal; + + + + + + //create primary rays + primaryRays.resize(m_raytraceData->textureWidth*m_raytraceData->textureHeight); + + b3Vector3 rayTo; + b3RayInfo ray; + + { + for (int x=0;xtextureWidth;x++) + { + for (int y=0;ytextureHeight;y++) + { + + rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; + rayTo += x * dHor*widthFactor; + rayTo -= y * dVert*heightFactor; + + ray.m_from = rayFrom; + ray.m_to = rayTo; + primaryRays[x+m_raytraceData->textureWidth*y] = ray; + } + } + } + } + + b3AlignedObjectArray hits; + { + B3_PROFILE("hits.resize"); + hits.resize(primaryRays.size()); + } + if (1) + { + B3_PROFILE("init hits"); + for (int i=0;icastRaysHost(primaryRays, hits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu(),m_data->m_np->getInternalData()); + m_raycaster->castRays(primaryRays, hits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu(), m_data->m_np->getInternalData()); + } + + + b3AlignedObjectArray shadowRays; + { + B3_PROFILE("shadowRays.resize"); + shadowRays.resize(primaryRays.size()); + } + b3AlignedObjectArray shadowHits; + { + B3_PROFILE("shadowHits.resize"); + shadowHits.resize(hits.size()); + } + + { + B3_PROFILE("init shadow rays"); + for (int i=0;icastRaysHost(primaryRays, hits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu()); + m_raycaster->castRays(shadowRays, shadowHits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu(), m_data->m_np->getInternalData()); + } + + { + B3_PROFILE("write texels"); + + for (int i=0;i0.f) + { + if (shadowHits[i].m_hitFraction<1.f) + { + dotje = -1.f; + } + } + + if (dotje>0.f) + { + m_raytraceData->m_texels[(i)*3+0] = 128+128.f*hits[i].m_hitNormal.x; + m_raytraceData->m_texels[(i)*3+1] = 128+128.f*hits[i].m_hitNormal.y; + m_raytraceData->m_texels[(i)*3+2] = 128+128.f*hits[i].m_hitNormal.z; + + if (hits[i].m_hitResult0==0) + { + m_raytraceData->m_texels[(i)*3+0] = 255; + m_raytraceData->m_texels[(i)*3+1] = 255; + m_raytraceData->m_texels[(i)*3+2] = 255; + } else + { + } + } else + { + if (dotje == -1.f) + { + m_raytraceData->m_texels[(i)*3+0] = 0; + m_raytraceData->m_texels[(i)*3+1] = 0; + m_raytraceData->m_texels[(i)*3+2] = 255; + } else + { + m_raytraceData->m_texels[(i)*3+0] = 255; + m_raytraceData->m_texels[(i)*3+1] = 0; + m_raytraceData->m_texels[(i)*3+2] = 0; + } + } + } else + { + m_raytraceData->m_texels[(i)*3+0] = 128; + m_raytraceData->m_texels[(i)*3+1] = 128; + m_raytraceData->m_texels[(i)*3+2] = 192; + } + } + } + GLint err; + + { + B3_PROFILE("get error"); + err = glGetError(); + assert(err==GL_NO_ERROR); + glActiveTexture(GL_TEXTURE0); + } + { + B3_PROFILE("glTexImage2D"); + glBindTexture(GL_TEXTURE_2D, *m_raytraceData->m_texId); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_raytraceData->textureWidth, m_raytraceData->textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, m_raytraceData->m_texels); + } + + { + B3_PROFILE("glGetError"); + 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 rect[4] = {0,0,m_instancingRenderer->getScreenWidth(),m_instancingRenderer->getScreenHeight()}; + float u[2] = {0,1}; + float v[2] = {0,1}; + int useRGBA = 1; + { + B3_PROFILE("drawTexturedRect"); + m_primRenderer->drawTexturedRect(rect[0],rect[1],rect[2],rect[3],color,u[0],v[0],u[1],v[1], useRGBA); + } + { + B3_PROFILE("glGetError"); + err = glGetError(); + assert(err==GL_NO_ERROR); + } +} diff --git a/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.h b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.h new file mode 100644 index 000000000..da39c5028 --- /dev/null +++ b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.h @@ -0,0 +1,33 @@ +#ifndef RAYTRACED_SHADOW_DEMO_H +#define RAYTRACED_SHADOW_DEMO_H + +#include "../rigidbody/GpuConvexScene.h" + +class GpuRaytraceScene : public GpuBoxPlaneScene +{ +protected: + b3AlignedObjectArray primaryRays; + + struct GpuRaytraceInternalData* m_raytraceData; + +public: + GpuRaytraceScene(); + virtual ~GpuRaytraceScene(); + virtual const char* getName() + { + return "GPURaytrace"; + } + + static GpuDemo* MyCreateFunc() + { + GpuDemo* demo = new GpuRaytraceScene; + return demo; + } + + virtual int createDynamicsObjects(const ConstructionInfo& ci); + + void renderScene(); + void renderScene2(); +}; + +#endif //RAYTRACED_SHADOW_DEMO_H diff --git a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp index 699e1ffcf..c50dcd52a 100644 --- a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp +++ b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp @@ -21,6 +21,7 @@ #include "Bullet3OpenCL/Raycast/b3GpuRaycast.h" #include "Bullet3OpenCL/NarrowphaseCollision/b3ConvexUtility.h" +#include "OpenGLWindow/GLRenderToTexture.h" void GpuConvexScene::setupScene(const ConstructionInfo& ci) { @@ -177,6 +178,7 @@ void GpuConvexScene::createStaticEnvironment(const ConstructionInfo& ci) int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int group=1; int mask=1; @@ -184,9 +186,9 @@ void GpuConvexScene::createStaticEnvironment(const ConstructionInfo& ci) { - b3Vector4 scaling(400,1,400,1); + b3Vector4 scaling(400,400,400,1); int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); - b3Vector3 position(0,0,0); + b3Vector3 position(0,-400,0); b3Quaternion orn(0,0,0,1); b3Vector4 color(0,0,1,1); @@ -199,308 +201,28 @@ void GpuConvexScene::createStaticEnvironment(const ConstructionInfo& ci) void GpuConvexPlaneScene::createStaticEnvironment(const ConstructionInfo& ci) { - int index=0; - b3Vector3 normal(0,1,0); - float constant=0.f; - int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); - b3Vector3 position(0,0,0); - b3Quaternion orn(0,0,0,1); - // b3Quaternion orn(b3Vector3(1,0,0),0.3); - b3Vector4 color(0,0,1,1); - b3Vector4 scaling(100,0.001,100,1); int strideInBytes = 9*sizeof(float); int numVertices = sizeof(cube_vertices)/strideInBytes; int numIndices = sizeof(cube_indices)/sizeof(int); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); - - - int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling); - int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false); - -} - -struct GpuRaytraceInternalData -{ - GLuint* m_texId; - unsigned char* m_texels; - int textureWidth; - int textureHeight; -}; -#include - -GpuRaytraceScene::GpuRaytraceScene() -{ - - m_raytraceData = new GpuRaytraceInternalData; - - m_raytraceData->m_texId = new GLuint; - m_raytraceData->textureWidth = 512;//1024;//1024; - m_raytraceData->textureHeight = 512;//1024; - - //create new texture - glGenTextures(1, m_raytraceData->m_texId); - GLenum err = glGetError(); - assert(err==GL_NO_ERROR); - - - - glBindTexture(GL_TEXTURE_2D, *m_raytraceData->m_texId); - m_raytraceData->m_texels = (unsigned char*)malloc(m_raytraceData->textureWidth*m_raytraceData->textureHeight*3); - memset(m_raytraceData->m_texels,0,m_raytraceData->textureWidth*m_raytraceData->textureHeight*3); - for (int i=0;itextureWidth;i++) - { - for (int y=0;ytextureHeight;y++) - { - int color = 0; - if (ytextureHeight-1 && (y>0) && (i>0 && itextureWidth-1)) - color = 255; - - m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+0] = color; - m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+1] = color; - m_raytraceData->m_texels[(i+m_raytraceData->textureWidth*y)*3+2] = color; - } - } - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_raytraceData->textureWidth, m_raytraceData->textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, m_raytraceData->m_texels); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - err = glGetError(); - assert(err==GL_NO_ERROR); - -} -GpuRaytraceScene::~GpuRaytraceScene() -{ - glDeleteTextures(1,m_raytraceData->m_texId); - delete[] m_raytraceData->m_texels; - delete m_raytraceData; -} - - - -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), - }; + b3Vector4 scaling(400,400,400,1); + int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling); + b3Vector3 position(0,-400,0); + b3Quaternion orn(0,0,0,1); - int curColor = 0; - float scaling[4] = {1,1,1,1}; - int prevBody = -1; - int insta = 0; + b3Vector4 color(0,0,1,1); - - //int colIndex = m_data->m_np->registerSphereShape(1); - for (int i=0;i<10;i++) - //for (int i=0;iregisterGraphicsInstance(shapeId,position,orn,color,scaling); + int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false); - b3Quaternion orn(0,0,0,1); - - b3Vector4 color = colors[curColor]; - curColor++; - curColor&=3; - b3Vector4 scaling(1,1,1,1); - int id = ci.m_instancingRenderer->registerGraphicsInstance(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; } - -//create primary rays -b3AlignedObjectArray rays; - -void GpuRaytraceScene::renderScene() -{ - - //GpuBoxPlaneScene::renderScene(); - - B3_PROFILE("raytrace"); - //raytrace into the texels - m_instancingRenderer->updateCamera(); - //generate primary rays - - - - { - B3_PROFILE("Generate primary rays"); - 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. * atanf (tanFov); - float aspect = screenWidth / screenHeight; - - b3Vector3 rayFrom, camTarget; - m_instancingRenderer->getCameraPosition(rayFrom); - m_instancingRenderer->getCameraTargetPosition(camTarget); - b3Vector3 rayForward = camTarget-rayFrom; - rayForward.normalize(); - - rayForward*= farPlane; - - b3Vector3 rightOffset; - b3Vector3 vertical(0.f,1.f,0.f); - b3Vector3 hor; - hor = rayForward.cross(vertical); - hor.normalize(); - vertical = hor.cross(rayForward); - vertical.normalize(); - - float tanfov = tanf(0.5f*fov); - - 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./float(screenWidth); - b3Vector3 dVert = vertical * 1./float(screenHeight); - - b3Transform rayFromTrans; - rayFromTrans.setIdentity(); - rayFromTrans.setOrigin(rayFrom); - - b3Transform rayFromLocal; - b3Transform rayToLocal; - - m_data->m_np->readbackAllBodiesToCpu(); - - - - //create primary rays - rays.resize(m_raytraceData->textureWidth*m_raytraceData->textureHeight); - - b3Vector3 rayTo; - b3RayInfo ray; - - { - for (int x=0;xtextureWidth;x++) - { - for (int y=0;ytextureHeight;y++) - { - - rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; - rayTo += x * dHor*widthFactor; - rayTo -= y * dVert*heightFactor; - - ray.m_from = rayFrom; - ray.m_to = rayTo; - rays[x+m_raytraceData->textureWidth*y] = ray; - } - } - } - } - - b3AlignedObjectArray hits; - hits.resize(rays.size()); - - { - B3_PROFILE("init hits"); - for (int i=0;icastRaysHost(rays, hits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu()); - m_raycaster->castRays(rays, hits, this->m_data->m_np->getNumRigidBodies(), m_data->m_np->getBodiesCpu(), m_data->m_np->getNumCollidablesGpu(), m_data->m_np->getCollidablesCpu(), m_data->m_np->getInternalData()); - - - - { - B3_PROFILE("write texels"); - - for (int i=0;im_texels[(i)*3+0] = 128+128.f*hits[i].m_hitNormal.x; - m_raytraceData->m_texels[(i)*3+1] = 128+128.f*hits[i].m_hitNormal.y; - m_raytraceData->m_texels[(i)*3+2] = 128+128.f*hits[i].m_hitNormal.z; - } else - { - m_raytraceData->m_texels[(i)*3+0] = 0; - m_raytraceData->m_texels[(i)*3+1] = 0; - m_raytraceData->m_texels[(i)*3+2] = 0; - } - } - } - GLint err; - - err = glGetError(); - assert(err==GL_NO_ERROR); - glActiveTexture(GL_TEXTURE0); - - glBindTexture(GL_TEXTURE_2D, *m_raytraceData->m_texId); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_raytraceData->textureWidth, m_raytraceData->textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, m_raytraceData->m_texels); - - err = glGetError(); - assert(err==GL_NO_ERROR); - b3Assert(m_primRenderer); - 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; - m_primRenderer->drawTexturedRect(rect[0],rect[1],rect[2],rect[3],color,u[0],v[0],u[1],v[1], useRGBA); - err = glGetError(); - assert(err==GL_NO_ERROR); -} diff --git a/Demos3/GpuDemos/rigidbody/GpuConvexScene.h b/Demos3/GpuDemos/rigidbody/GpuConvexScene.h index 07d5cab54..48e345d81 100644 --- a/Demos3/GpuDemos/rigidbody/GpuConvexScene.h +++ b/Demos3/GpuDemos/rigidbody/GpuConvexScene.h @@ -2,6 +2,8 @@ #define GPU_CONVEX_SCENE_H #include "GpuRigidBodyDemo.h" +#include "Bullet3Common/b3AlignedObjectArray.h" +#include "Bullet3OpenCL/Raycast/b3RaycastInfo.h" class GpuConvexScene : public GpuRigidBodyDemo { @@ -84,29 +86,7 @@ public: }; -class GpuRaytraceScene : public GpuBoxPlaneScene -{ -protected: - struct GpuRaytraceInternalData* m_raytraceData; -public: - GpuRaytraceScene(); - virtual ~GpuRaytraceScene(); - virtual const char* getName() - { - return "GPURaytrace"; - } - - static GpuDemo* MyCreateFunc() - { - GpuDemo* demo = new GpuRaytraceScene; - return demo; - } - - virtual int createDynamicsObjects(const ConstructionInfo& ci); - - void renderScene(); -}; #endif //GPU_CONVEX_SCENE_H diff --git a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp index 68e4be1c6..5ffe9e049 100644 --- a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp +++ b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp @@ -155,7 +155,7 @@ void GpuRigidBodyDemo::exitPhysics() void GpuRigidBodyDemo::renderScene() { - m_instancingRenderer->RenderScene(); + m_instancingRenderer->renderScene(); } void GpuRigidBodyDemo::clientMoveAndDisplay() @@ -174,6 +174,9 @@ void GpuRigidBodyDemo::clientMoveAndDisplay() numObjects = m_instancingRenderer->getInstanceCapacity(); } + GLint err = glGetError(); + assert(err==GL_NO_ERROR); + b3Vector4* positions = 0; if (animate && numObjects) { diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp b/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp new file mode 100644 index 000000000..7f5dca440 --- /dev/null +++ b/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp @@ -0,0 +1,62 @@ +#include "ShadowMapDemo.h" +#include "ShadowMapDemoInternalData.h" +#include "OpenGLWindow/GLInstancingRenderer.h" +#include "OpenGLWindow/ShapeData.h" + +ShadowMapDemo::ShadowMapDemo() +{ + m_shadowData = new ShadowMapDemoInternalData; +} + +ShadowMapDemo::~ShadowMapDemo() +{ + delete m_shadowData; +} + + +void ShadowMapDemo::initPhysics(const ConstructionInfo& ci) +{ + m_shadowData->m_instancingRenderer = ci.m_instancingRenderer; + + int sphereShape = registerGraphicsSphereShape(ci,0.1,false); + float pos[4]={0,3,0,0}; + float orn[4]={0,0,0,1}; + float color[4]={1,0,0,1}; + float scaling[4]={1,1,1,1}; + + ci.m_instancingRenderer->registerGraphicsInstance(sphereShape,pos,orn,color,scaling); + + + int strideInBytes = 9*sizeof(float); + int numVertices = sizeof(cube_vertices)/strideInBytes; + int numIndices = sizeof(cube_vertices)/sizeof(int); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + int boxShape = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + pos[1]=0.f; + scaling[0]=scaling[2]=10.f; + color[0]=1.f; + color[1]=1.f; + color[2]=1.f; + color[3]=1.f; + ci.m_instancingRenderer->registerGraphicsInstance(boxShape ,pos,orn,color,scaling); + + + m_shadowData->m_instancingRenderer->setCameraTargetPosition(pos); + m_shadowData->m_instancingRenderer->setCameraDistance(15); + ci.m_instancingRenderer->writeTransforms(); +} + +void ShadowMapDemo::exitPhysics() +{ +} + +void ShadowMapDemo::renderScene() +{ + m_shadowData->m_instancingRenderer->renderScene(B3_CREATE_SHADOWMAP_RENDERMODE); + //m_shadowData->m_instancingRenderer->renderScene(); + m_shadowData->m_instancingRenderer->renderScene(B3_USE_SHADOWMAP_RENDERMODE); +} + +void ShadowMapDemo::clientMoveAndDisplay() +{ +} diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemo.h b/Demos3/GpuDemos/shadows/ShadowMapDemo.h new file mode 100644 index 000000000..3f692e004 --- /dev/null +++ b/Demos3/GpuDemos/shadows/ShadowMapDemo.h @@ -0,0 +1,36 @@ +#ifndef SHADOW_MAP_DEMO_H +#define SHADOW_MAP_DEMO_H + +#include "../GpuDemo.h" + +class ShadowMapDemo : public GpuDemo +{ + struct ShadowMapDemoInternalData* m_shadowData; + +public: + + ShadowMapDemo(); + virtual ~ShadowMapDemo(); + + virtual const char* getName() + { + return "ShadowMapDemo"; + } + + virtual void initPhysics(const ConstructionInfo& ci); + + virtual void exitPhysics(); + + virtual void renderScene(); + + virtual void clientMoveAndDisplay(); + + static GpuDemo* MyCreateFunc() + { + GpuDemo* demo = new ShadowMapDemo; + return demo; + } + +}; + +#endif //SHADOW_MAP_DEMO_H diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h b/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h new file mode 100644 index 000000000..d8c9df62f --- /dev/null +++ b/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h @@ -0,0 +1,11 @@ +#ifndef SHADOW_MAP_INTERNAL_DATA_H +#define SHADOW_MAP_INTERNAL_DATA_H + + +struct ShadowMapDemoInternalData +{ + class GLInstancingRenderer* m_instancingRenderer; +}; + +#endif //SHADOW_MAP_INTERNAL_DATA_H + diff --git a/Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp b/Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp index 67304eaee..4b2df0948 100644 --- a/Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp +++ b/Demos3/GpuDemos/softbody/GpuSoftBodyDemo.cpp @@ -203,12 +203,12 @@ void GpuSoftClothDemo::renderScene() m_instancingRenderer->updateShape(m_data->m_clothShapeIndex,m_data->m_clothVertices); } - m_instancingRenderer->RenderScene(); + m_instancingRenderer->renderScene(); } void GpuSoftBodyDemo::renderScene() { - m_instancingRenderer->RenderScene(); + m_instancingRenderer->renderScene(); } void GpuSoftBodyDemo::clientMoveAndDisplay() diff --git a/Demos3/GpuGuiInitialize/premake4.lua b/Demos3/GpuGuiInitialize/premake4.lua index a170629e1..33cee5708 100644 --- a/Demos3/GpuGuiInitialize/premake4.lua +++ b/Demos3/GpuGuiInitialize/premake4.lua @@ -24,7 +24,6 @@ function createProject(vendor) "gwen" } - initOpenGL() initGlew() @@ -34,6 +33,8 @@ function createProject(vendor) "../../src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h", "../../btgui/OpenGLWindow/GLInstancingRenderer.cpp", "../../btgui/OpenGLWindow/GLInstancingRenderer.h", + "../../btgui/OpenGLWindow/GLRenderToTexture.cpp", + "../../btgui/OpenGLWindow/GLRenderToTexture.h", "../../btgui/OpenGLWindow/GLPrimitiveRenderer.h", "../../btgui/OpenGLWindow/GLPrimitiveRenderer.cpp", "../../btgui/OpenGLWindow/LoadShader.cpp", diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.cpp b/btgui/OpenGLWindow/GLInstancingRenderer.cpp index c69ef242e..e846f6670 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.cpp +++ b/btgui/OpenGLWindow/GLInstancingRenderer.cpp @@ -26,12 +26,27 @@ subject to the following restrictions: #include //#include "DemoSettings.h" #include -#include + #include "Bullet3Common/b3Vector3.h" #include "Bullet3Common/b3Quaternion.h" #include "Bullet3Common/b3Matrix3x3.h" #include "LoadShader.h" +#include "GLInstanceRendererInternalData.h" + +//GLSL shader strings, embedded using build3/stringify +#include "OpenGLWindow/Shaders/pointSpriteVS.h" +#include "OpenGLWindow/Shaders/pointSpritePS.h" +#include "OpenGLWindow/Shaders/instancingVS.h" +#include "OpenGLWindow/Shaders/instancingPS.h" +#include "OpenGLWindow/Shaders/createShadowMapInstancingVS.h" +#include "OpenGLWindow/Shaders/createShadowMapInstancingPS.h" +#include "OpenGLWindow/Shaders/useShadowMapInstancingVS.h" +#include "OpenGLWindow/Shaders/useShadowMapInstancingPS.h" + +#include "OpenGLWindow/GLRenderToTexture.h" + + //#include "../../opencl/gpu_rigidbody_pipeline/b3GpuNarrowphaseAndSolver.h"//for m_maxNumObjectCapacity @@ -67,6 +82,8 @@ bool m_ortho = false; static GLfloat projectionMatrix[16]; static GLfloat modelviewMatrix[16]; +static GLfloat depthLightModelviewMatrix[16]; + static void checkError(const char *functionName) { GLenum error; @@ -87,7 +104,7 @@ extern int gShapeIndex; -#include "GLInstanceRendererInternalData.h" + struct InternalDataRenderer : public GLInstanceRendererInternalData { @@ -108,6 +125,9 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData GLuint m_defaultTexturehandle; b3AlignedObjectArray m_textureHandles; + GLRenderToTexture* m_shadowMap; + GLuint m_shadowTexture; + InternalDataRenderer() : m_cameraPosition(b3Vector3(0,0,0)), m_cameraTargetPosition(b3Vector3(15,2,-24)), @@ -117,7 +137,9 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData //m_ele(25.f), m_ele(25.f), m_mouseInitialized(false), - m_mouseButton(0) + m_mouseButton(0), + m_shadowMap(0), + m_shadowTexture(0) { @@ -129,7 +151,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData { if (b3Fabs(deltax)>b3Fabs(deltay)) { - m_azi -= deltax*0.1; + m_azi -= deltax*0.1f; } else { @@ -163,10 +185,10 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData float yDelta = y-m_mouseYpos; // if (b3Fabs(xDelta)>b3Fabs(yDelta)) // { - m_azi += xDelta*0.1; + m_azi += xDelta*0.1f; // } else // { - m_ele += yDelta*0.1; + m_ele += yDelta*0.1f; // } } @@ -215,6 +237,8 @@ void b3DefaultKeyboardCallback(int key, int state) } +static GLuint useShadowMapInstancingShader; // The shadow instancing renderer +static GLuint createShadowMapInstancingShader; // The shadow instancing renderer static GLuint instancingShader; // The instancing renderer static GLuint instancingShaderPointSprite; // The point sprite instancing renderer @@ -222,9 +246,20 @@ static GLuint instancingShaderPointSprite; // The point spr static bool done = false; -static GLint angle_loc = 0; + +static GLint useShadow_ModelViewMatrix=0; +static GLint useShadow_ProjectionMatrix=0; +static GLint useShadow_DepthBiasModelViewMatrix=0; +static GLint useShadow_uniform_texture_diffuse = 0; +static GLint useShadow_shadowMap = 0; + +static GLint createShadow_ModelViewMatrix=0; +static GLint createShadow_ProjectionMatrix=0; +static GLint createShadow_uniform_texture_diffuse = 0; + static GLint ModelViewMatrix=0; static GLint ProjectionMatrix=0; +static GLint DepthModelViewMatrix=0; static GLint uniform_texture_diffuse = 0; static GLint screenWidthPointSprite=0; @@ -238,7 +273,9 @@ GLInstancingRenderer::GLInstancingRenderer(int maxNumObjectCapacity, int maxShap :m_maxNumObjectCapacity(maxNumObjectCapacity), m_maxShapeCapacityInBytes(maxShapeCapacityInBytes), m_textureenabled(true), - m_textureinitialized(false) + m_textureinitialized(false), + m_screenWidth(0), + m_screenHeight(0) { m_data = new InternalDataRenderer; @@ -277,227 +314,8 @@ GLInstancingRenderer::~GLInstancingRenderer() -//used for dynamic loading from disk (default switched off) -//#define MAX_SHADER_LENGTH 8192 -//static GLubyte shaderText[MAX_SHADER_LENGTH]; - -static const char* vertexShader= \ -"#version 330\n" -"precision highp float;\n" -"\n" -"\n" -"\n" -"layout (location = 0) in vec4 position;\n" -"layout (location = 1) in vec4 instance_position;\n" -"layout (location = 2) in vec4 instance_quaternion;\n" -"layout (location = 3) in vec2 uvcoords;\n" -"layout (location = 4) in vec3 vertexnormal;\n" -"layout (location = 5) in vec4 instance_color;\n" -"layout (location = 6) in vec3 instance_scale;\n" -"\n" -"\n" -"uniform float angle = 0.0;\n" -"uniform mat4 ModelViewMatrix;\n" -"uniform mat4 ProjectionMatrix;\n" -"\n" -"out Fragment\n" -"{\n" -" vec4 color;\n" -"} fragment;\n" -"\n" -"out Vert\n" -"{\n" -" vec2 texcoord;\n" -"} vert;\n" -"\n" -"\n" -"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" -"{\n" -" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" -" vec4 dt = q1 * q2;\n" -" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" -" return vec4 ( im, re );\n" -"}\n" -"\n" -"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" -"{\n" -" float cah = cos(angle*0.5);\n" -" float sah = sin(angle*0.5);\n" -" float d = inversesqrt(dot(axis,axis));\n" -" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" -" return q;\n" -"}\n" -"//\n" -"// vector rotation via quaternion\n" -"//\n" -"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" -"{\n" -" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" -" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" -"}\n" -"vec4 quatRotate ( in vec4 p, in vec4 q )\n" -"{\n" -" vec4 temp = quatMul ( q, p );\n" -" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" -"}\n" -"\n" -"out vec3 lightDir,normal,ambient;\n" -"\n" -"void main(void)\n" -"{\n" -" vec4 q = instance_quaternion;\n" -" ambient = vec3(0.3,.3,0.3);\n" -" \n" -" \n" -" vec4 local_normal = (quatRotate3( vertexnormal,q));\n" -" vec3 light_pos = vec3(-0.3,0.1,0.1);\n" -" normal = local_normal.xyz;\n"//normalize(ModelViewMatrix * local_normal).xyz;\n" -"\n" -" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n" -"// lightDir = normalize(vec3(gl_LightSource[0].position));\n" -" \n" -" vec4 axis = vec4(1,1,1,0);\n" -" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" -" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord);\n" -"\n" -" gl_Position = vertexPos;\n" -" \n" -" fragment.color = instance_color;\n" -" vert.texcoord = uvcoords;\n" -"}\n" -; -static const char* fragmentShader= \ -"#version 330\n" -"precision highp float;\n" -"\n" -"in Fragment\n" -"{\n" -" vec4 color;\n" -"} fragment;\n" -"\n" -"in Vert\n" -"{\n" -" vec2 texcoord;\n" -"} vert;\n" -"\n" -"uniform sampler2D Diffuse;\n" -"\n" -"in vec3 lightDir,normal,ambient;\n" -"\n" -"out vec4 color;\n" -"\n" -"void main_textured(void)\n" -"{\n" -" color = fragment.color;//texture2D(Diffuse,vert.texcoord);//fragment.color;\n" -"}\n" -"\n" -"void main(void)\n" -"{\n" -" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" -" vec3 ct,cf;\n" -" float intensity,at,af;\n" -" intensity = max(dot(lightDir,normalize(normal)),0);\n" -" cf = intensity*vec3(1.0,1.0,1.0)+ambient;" -" af = 1.0;\n" -" \n" -" ct = texel.rgb;\n" -" at = texel.a;\n" -" \n" -" color = vec4(ct * cf, at * af); \n" -"}\n" -; - - -static const char* vertexShaderPointSprite= \ -"#version 330\n" -"precision highp float;\n" -"\n" -"\n" -"\n" -"layout (location = 0) in vec4 position;\n" -"layout (location = 1) in vec4 instance_position;\n" -"layout (location = 3) in vec2 uvcoords;\n" -"layout (location = 4) in vec3 vertexnormal;\n" -"layout (location = 5) in vec4 instance_color;\n" -"layout (location = 6) in vec3 instance_scale;\n" -"\n" -"\n" -"uniform float screenWidth = 700.f;\n" -"uniform mat4 ModelViewMatrix;\n" -"uniform mat4 ProjectionMatrix;\n" -"\n" -"out Fragment\n" -"{\n" -" vec4 color;\n" -"} fragment;\n" -"\n" -"\n" -"\n" -"//\n" -"// vector rotation via quaternion\n" -"//\n" -"\n" -"out vec3 ambient;\n" -"\n" -"void main(void)\n" -"{\n" -" ambient = vec3(0.3,.3,0.3);\n" -" \n" -" \n" -" vec4 axis = vec4(1,1,1,0);\n" -" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position);\n" -" vec3 posEye = vec3(ModelViewMatrix * vec4(instance_position.xyz, 1.0));\n" -" float dist = length(posEye);\n" -" float pointRadius = 1.f;\n" -" gl_PointSize = instance_scale.x * pointRadius * (screenWidth / dist);\n" -"\n" -" gl_Position = vertexPos;\n" -" \n" -" fragment.color = instance_color;\n" -"}\n" -; - - -static const char* fragmentShaderPointSprite= \ -"#version 330\n" -"precision highp float;\n" -"\n" -"in Fragment\n" -"{\n" -" vec4 color;\n" -"} fragment;\n" -"\n" -"\n" -"in vec3 ambient;\n" -"\n" -"out vec4 color;\n" -"\n" -"void main_textured(void)\n" -"{\n" -" color = fragment.color;//texture2D(Diffuse,vert.texcoord);//fragment.color;\n" -"}\n" -"\n" -"void main(void)\n" -"{\n" -" vec3 N;\n" -" N.xy = gl_PointCoord.st*vec2(2.0, -2.0) + vec2(-1.0, 1.0);\n" -" float mag = dot(N.xy, N.xy);\n" -" if (mag > 1.0) discard; \n" -" vec4 texel = vec4(1,0,0,1);\n"//fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" -" vec3 ct;\n" -" float at,af;\n" -" af = 1.0;\n" -" \n" -" ct = texel.rgb;\n" -" at = texel.a;\n" -" \n" -" vec3 lightDir= vec3(1,0,0);\n" -" float diffuse = max(0.0, dot(lightDir, N));\n" -" color = vec4(ct * diffuse, at * af); \n" -"}\n" -; @@ -572,14 +390,14 @@ void GLInstancingRenderer::writeSingleInstanceTransformToGPU(float* position, fl void GLInstancingRenderer::writeTransforms() { GLint err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo); glFlush(); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); char* orgBase = (char*)glMapBuffer( GL_ARRAY_BUFFER,GL_READ_WRITE); if (orgBase) @@ -650,7 +468,7 @@ void GLInstancingRenderer::writeTransforms() b3Error("ERROR glMapBuffer failed\n"); } err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glUnmapBuffer( GL_ARRAY_BUFFER); //if this glFinish is removed, the animation is not always working/blocks @@ -659,7 +477,7 @@ void GLInstancingRenderer::writeTransforms() glBindBuffer(GL_ARRAY_BUFFER, 0);//m_data->m_vbo); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); } @@ -708,7 +526,7 @@ int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const float* int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height) { GLint err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); int textureIndex = m_data->m_textureHandles.size(); const GLubyte* image= (const GLubyte*)texels; @@ -717,18 +535,18 @@ int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width glBindTexture(GL_TEXTURE_2D,textureHandle); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glGenerateMipmap(GL_TEXTURE_2D); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); m_data->m_textureHandles.push_back(textureHandle); @@ -813,25 +631,41 @@ void GLInstancingRenderer::InitShaders() int SCALE_BUFFER_SIZE = (m_maxNumObjectCapacity*sizeof(float)*3); + useShadowMapInstancingShader = gltLoadShaderPair(useShadowMapInstancingVertexShader,useShadowMapInstancingFragmentShader); + glLinkProgram(useShadowMapInstancingShader); + glUseProgram(useShadowMapInstancingShader); + useShadow_ModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, "ModelViewMatrix"); + useShadow_ProjectionMatrix = glGetUniformLocation(useShadowMapInstancingShader, "ProjectionMatrix"); + useShadow_DepthBiasModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, " DepthBiasModelViewProjectionMatrix"); + useShadow_uniform_texture_diffuse = glGetUniformLocation(useShadowMapInstancingShader, "Diffuse"); + useShadow_shadowMap = glGetUniformLocation(useShadowMapInstancingShader,"shadowMap"); - instancingShaderPointSprite = gltLoadShaderPair(vertexShaderPointSprite,fragmentShaderPointSprite); - glUseProgram(instancingShaderPointSprite); - ModelViewMatrixPointSprite = glGetUniformLocation(instancingShaderPointSprite, "ModelViewMatrix"); - ProjectionMatrixPointSprite = glGetUniformLocation(instancingShaderPointSprite, "ProjectionMatrix"); - screenWidthPointSprite = glGetUniformLocation(instancingShaderPointSprite, "screenWidth"); + + createShadowMapInstancingShader = gltLoadShaderPair(createShadowMapInstancingVertexShader,createShadowMapInstancingFragmentShader); + glLinkProgram(createShadowMapInstancingShader); + glUseProgram(createShadowMapInstancingShader); + createShadow_ModelViewMatrix = glGetUniformLocation(createShadowMapInstancingShader, "ModelViewMatrix"); + createShadow_ProjectionMatrix = glGetUniformLocation(createShadowMapInstancingShader, "ProjectionMatrix"); + createShadow_uniform_texture_diffuse = glGetUniformLocation(createShadowMapInstancingShader, "Diffuse"); + + + glUseProgram(0); - - instancingShader = gltLoadShaderPair(vertexShader,fragmentShader); + instancingShader = gltLoadShaderPair(instancingVertexShader,instancingFragmentShader); glLinkProgram(instancingShader); glUseProgram(instancingShader); - angle_loc = glGetUniformLocation(instancingShader, "angle"); ModelViewMatrix = glGetUniformLocation(instancingShader, "ModelViewMatrix"); ProjectionMatrix = glGetUniformLocation(instancingShader, "ProjectionMatrix"); uniform_texture_diffuse = glGetUniformLocation(instancingShader, "Diffuse"); glUseProgram(0); + instancingShaderPointSprite = gltLoadShaderPair(pointSpriteVertexShader,pointSpriteFragmentShader); + glUseProgram(instancingShaderPointSprite); + ModelViewMatrixPointSprite = glGetUniformLocation(instancingShaderPointSprite, "ModelViewMatrix"); + ProjectionMatrixPointSprite = glGetUniformLocation(instancingShaderPointSprite, "ProjectionMatrix"); + screenWidthPointSprite = glGetUniformLocation(instancingShaderPointSprite, "screenWidth"); - + glUseProgram(0); //GLuint offset = 0; @@ -860,25 +694,25 @@ void GLInstancingRenderer::InitShaders() void GLInstancingRenderer::init() { GLint err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); - glClearColor(float(0.7),float(0.7),float(0.7),float(0)); + glClearColor(float(0.),float(0.),float(0.4),float(0)); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); { B3_PROFILE("texture"); @@ -923,62 +757,62 @@ void GLInstancingRenderer::init() glGenTextures(1,(GLuint*)&m_data->m_defaultTexturehandle); glBindTexture(GL_TEXTURE_2D,m_data->m_defaultTexturehandle); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); #if 0 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); #endif err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256,256,0,GL_RGB,GL_UNSIGNED_BYTE,image); glGenerateMipmap(GL_TEXTURE_2D); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); delete[] image; m_textureinitialized=true; } err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glBindTexture(GL_TEXTURE_2D,m_data->m_defaultTexturehandle); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); } else { glDisable(GL_TEXTURE_2D); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); } } //glEnable(GL_COLOR_MATERIAL); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); // glEnable(GL_CULL_FACE); // glCullFace(GL_BACK); @@ -1018,6 +852,41 @@ void b3CreateFrustum( } +void b3Matrix4x4Mul(GLfloat aIn[4][4], GLfloat bIn[4][4], GLfloat result[4][4]) +{ + for (int j=0;j<4;j++) + for (int i=0;i<4;i++) + result[j][i] = aIn[0][i] * bIn[j][0] + aIn[1][i] * bIn[j][1] + aIn[2][i] * bIn[j][2] + aIn[3][i] * bIn[j][3]; +} + +void b3CreateDiagonalMatrix(GLfloat value, GLfloat result[4][4]) +{ + for (int i=0;i<4;i++) + { + for (int j=0;j<4;j++) + { + if (i==j) + { + result[i][j] = value; + } else + { + result[i][j] = 0.f; + } + } + } +} + +void b3CreateOrtho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar, GLfloat result[4][4]) +{ + b3CreateDiagonalMatrix(1.f,result); + + result[0][0] = 2.f / (right - left); + result[1][1] = 2.f / (top - bottom); + result[2][2] = - 2.f / (zFar - zNear); + result[3][0] = - (right + left) / (right - left); + result[3][1] = - (top + bottom) / (top - bottom); + result[3][2] = - (zFar + zNear) / (zFar - zNear); +} void b3CreateLookAt(const b3Vector3& eye, const b3Vector3& center,const b3Vector3& up, GLfloat result[16]) { @@ -1029,13 +898,19 @@ void b3CreateLookAt(const b3Vector3& eye, const b3Vector3& center,const b3Vec result[0*4+0] = s.getX(); result[1*4+0] = s.getY(); result[2*4+0] = s.getZ(); - result[0*4+1] = u.getX(); + + result[0*4+1] = u.getX(); result[1*4+1] = u.getY(); result[2*4+1] = u.getZ(); + result[0*4+2] =-f.getX(); result[1*4+2] =-f.getY(); result[2*4+2] =-f.getZ(); + result[0*4+3] = 0.f; + result[1*4+3] = 0.f; + result[2*4+3] = 0.f; + result[3*4+0] = -s.dot(eye); result[3*4+1] = -u.dot(eye); result[3*4+2] = f.dot(eye); @@ -1053,7 +928,7 @@ void GLInstancingRenderer::updateCamera() { GLint err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); int m_forwardAxis(2); @@ -1221,8 +1096,82 @@ void GLInstancingRenderer::getMouseDirection(float* dir, int x, int y) } -void GLInstancingRenderer::RenderScene(void) + +//#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "OpenGLTrueTypeFont/stb_image_write.h" +void writeTextureToPng(int textureWidth, int textureHeight, const char* fileName, int numComponents) { + GLuint err; + err = glGetError(); + b3Assert(err==GL_NO_ERROR); + + glPixelStorei(GL_PACK_ALIGNMENT,4); + + glReadBuffer(GL_NONE); + float* orgPixels = (float*)malloc(textureWidth*textureHeight*numComponents*4); + char* pixels = (char*)malloc(textureWidth*textureHeight*numComponents*4); + glReadPixels(0,0,textureWidth, textureHeight, GL_DEPTH_COMPONENT, GL_FLOAT, orgPixels); + err = glGetError(); + b3Assert(err==GL_NO_ERROR); + + for (int j=0;jm_shadowMap) + { + glGenTextures(1,&m_data->m_shadowTexture); + glBindTexture(GL_TEXTURE_2D,m_data->m_shadowTexture); + //glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT16,m_screenWidth,m_screenHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); + glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT32,m_screenWidth,m_screenHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); + + m_data->m_shadowMap=new GLRenderToTexture(); + m_data->m_shadowMap->init(m_screenWidth,m_screenHeight,m_data->m_shadowTexture,RENDERTEXTURE_DEPTH); + } + m_data->m_shadowMap->enable(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + GLint err = glGetError(); + b3Assert(err==GL_NO_ERROR); + } + + b3Vector3 lightPos(0.5f,2,2);//20,15,10);//-13,6,2);// = b3Vector3(0.5f,2,2); + b3CreateOrtho(-10,10,-10,10,-10,200,depthProjectionMatrix);//-14,14,-14,14,1,200, depthProjectionMatrix); + float depthViewMatrix[4][4]; + b3Vector3 center(0,0,0); + b3Vector3 up(0,1,0); + b3CreateLookAt(lightPos,center,up,&depthViewMatrix[0][0]); + //b3CreateLookAt(lightPos,m_data->m_cameraTargetPosition,b3Vector3(0,1,0),(float*)depthModelViewMatrix2); + + GLfloat depthModelMatrix[4][4]; + b3CreateDiagonalMatrix(1.f,depthModelMatrix); + + b3Matrix4x4Mul(depthViewMatrix, depthModelMatrix, depthModelViewMatrix); + + GLfloat depthMVP[4][4]; + b3Matrix4x4Mul(depthProjectionMatrix,depthModelViewMatrix,depthMVP); + + GLfloat biasMatrix[4][4]={ + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 + }; + + GLfloat depthBiasMVP[4][4]; + b3Matrix4x4Mul(biasMatrix,depthMVP,depthBiasMVP); + + + float m_frustumZNear=1; + float m_frustumZFar=10000.f; + + + + //b3CreateFrustum(-m_frustumZNear, m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,(float*)depthProjectionMatrix); + + //b3CreateLookAt(lightPos,m_data->m_cameraTargetPosition,b3Vector3(0,0,1),(float*)depthModelViewMatrix); + { B3_PROFILE("updateCamera"); updateCamera(); } + err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); //render coordinate system #if 0 glBegin(GL_LINES); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glColor3f(1,0,0); glVertex3f(0,0,0); @@ -1273,7 +1291,7 @@ void GLInstancingRenderer::RenderScene(void) glFlush(); } err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); //updatePos(); @@ -1305,14 +1323,14 @@ void GLInstancingRenderer::RenderScene(void) else curBindTexture = m_data->m_defaultTexturehandle; - if (lastBindTexture != curBindTexture) + //if (lastBindTexture != curBindTexture) { glBindTexture(GL_TEXTURE_2D,curBindTexture); } lastBindTexture = curBindTexture; err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); // int myOffset = gfxObj->m_instanceOffset*4*sizeof(float); int POSITION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4); @@ -1376,7 +1394,7 @@ void GLInstancingRenderer::RenderScene(void) //glUniform1i(uniform_texture_diffusePointSprite, 0); err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); glPointSize(20); #ifndef __APPLE__ @@ -1388,12 +1406,50 @@ void GLInstancingRenderer::RenderScene(void) glDrawElementsInstanced(GL_POINTS, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); } else { - glUseProgram(instancingShader); - glUniform1f(angle_loc, 0); - glUniformMatrix4fv(ProjectionMatrix, 1, false, &projectionMatrix[0]); - glUniformMatrix4fv(ModelViewMatrix, 1, false, &modelviewMatrix[0]); - glUniform1i(uniform_texture_diffuse, 0); - glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + switch (renderMode) + { + + case B3_DEFAULT_RENDERMODE: + { + glUseProgram(instancingShader); + glUniformMatrix4fv(ProjectionMatrix, 1, false, &projectionMatrix[0]); + glUniformMatrix4fv(ModelViewMatrix, 1, false, &modelviewMatrix[0]); + glUniform1i(uniform_texture_diffuse, 0); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + break; + } + case B3_CREATE_SHADOWMAP_RENDERMODE: + { + glUseProgram(createShadowMapInstancingShader); + glUniformMatrix4fv(createShadow_ProjectionMatrix, 1, false, &depthProjectionMatrix[0][0]); + glUniformMatrix4fv(createShadow_ModelViewMatrix, 1, false, &depthModelViewMatrix[0][0]); + glUniform1i(createShadow_uniform_texture_diffuse, 0); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + break; + } + + case B3_USE_SHADOWMAP_RENDERMODE: + { + glUseProgram(useShadowMapInstancingShader); + glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &projectionMatrix[0]); + glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &modelviewMatrix[0]); + glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]); + + + glUniform1i(useShadow_uniform_texture_diffuse, 0); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); + glUniform1i(useShadow_shadowMap,1); + + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + break; + } + default: + { + + // b3Assert(0); + } + }; } @@ -1402,8 +1458,17 @@ void GLInstancingRenderer::RenderScene(void) } curOffset+= gfxObj->m_numGraphicsInstances; } + + if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE) + { + //writeTextureToPng(m_screenWidth,m_screenHeight,"shadowmap.png",4); + m_data->m_shadowMap->disable(); + + + } + err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); { B3_PROFILE("glUseProgram(0);"); glUseProgram(0); @@ -1411,8 +1476,11 @@ void GLInstancingRenderer::RenderScene(void) glBindVertexArray(0); } + + + err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(err==GL_NO_ERROR); } diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.h b/btgui/OpenGLWindow/GLInstancingRenderer.h index 199585d53..04ac19019 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.h +++ b/btgui/OpenGLWindow/GLInstancingRenderer.h @@ -29,6 +29,14 @@ enum B3_GL_POINTS }; +enum +{ + B3_DEFAULT_RENDERMODE=1, + //B3_WIREFRAME_RENDERMODE, + B3_CREATE_SHADOWMAP_RENDERMODE, + B3_USE_SHADOWMAP_RENDERMODE, +}; + class GLInstancingRenderer { @@ -52,7 +60,7 @@ public: void init(); void InitShaders(); - void RenderScene(void); + void renderScene(int renderMode=B3_DEFAULT_RENDERMODE); void CleanupShaders(); void updateShape(int shapeIndex, const float* vertices); diff --git a/btgui/OpenGLWindow/GLRenderToTexture.cpp b/btgui/OpenGLWindow/GLRenderToTexture.cpp index f7c2652af..acbf4c3e9 100644 --- a/btgui/OpenGLWindow/GLRenderToTexture.cpp +++ b/btgui/OpenGLWindow/GLRenderToTexture.cpp @@ -2,37 +2,47 @@ ///See http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/ #include "GLRenderToTexture.h" - +#include "Bullet3Common/b3Scalar.h" // for b3Assert GLRenderToTexture::GLRenderToTexture() :m_framebufferName(0) { } -void GLRenderToTexture::init(int width, int height) +void GLRenderToTexture::init(int width, int height, GLuint textureId, int renderTextureType) { + m_renderTextureType = renderTextureType; + glGenFramebuffers(1, &m_framebufferName); glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferName); - GLuint m_renderedTexture; - glGenTextures(1, &m_renderedTexture); - - // "Bind" the newly created texture : all future texture functions will modify this texture - glBindTexture(GL_TEXTURE_2D, m_renderedTexture); - - // Give an empty image to OpenGL ( the last "0" ) - glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, 0); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - + switch (m_renderTextureType) + { + case RENDERTEXTURE_COLOR: + { + glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureId, 0); + break; + } + case RENDERTEXTURE_DEPTH: + { + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureId, 0); + break; + } + default: + { + b3Assert(0); + } + }; + // The depth buffer glGenRenderbuffers(1, &m_depthrenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, m_depthrenderbuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthrenderbuffer); - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_renderedTexture, 0); + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); + } @@ -40,9 +50,29 @@ bool GLRenderToTexture::enable() { bool status = false; - // Set the list of draw buffers. - GLenum drawBuffers[2] = {GL_COLOR_ATTACHMENT0,0}; - glDrawBuffers(1, drawBuffers); + glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferName); + + + switch (m_renderTextureType) + { + case RENDERTEXTURE_COLOR: + { + // Set the list of draw buffers. + GLenum drawBuffers[2] = {GL_COLOR_ATTACHMENT0,0}; + glDrawBuffers(1, drawBuffers); + break; + } + case RENDERTEXTURE_DEPTH: + { + glDrawBuffer(GL_NONE); + break; + } + default: + { + b3Assert(0); + } + }; + // Always check that our framebuffer is ok if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) @@ -50,6 +80,7 @@ bool GLRenderToTexture::enable() status = true; } + return status; } @@ -57,7 +88,6 @@ bool GLRenderToTexture::enable() void GLRenderToTexture::disable() { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - glBindTexture(GL_TEXTURE_2D, 0); } GLRenderToTexture::~GLRenderToTexture() @@ -65,10 +95,10 @@ GLRenderToTexture::~GLRenderToTexture() glBindFramebuffer( GL_FRAMEBUFFER, 0 ); if (m_depthrenderbuffer) + { glDeleteRenderbuffers(1,&m_depthrenderbuffer); + } - if(m_renderedTexture) - glDeleteTextures(1, &m_renderedTexture); if( m_framebufferName) { diff --git a/btgui/OpenGLWindow/GLRenderToTexture.h b/btgui/OpenGLWindow/GLRenderToTexture.h index adc31a5f7..513f3e157 100644 --- a/btgui/OpenGLWindow/GLRenderToTexture.h +++ b/btgui/OpenGLWindow/GLRenderToTexture.h @@ -5,17 +5,22 @@ ///See http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/ #include "OpenGLInclude.h" +enum +{ + RENDERTEXTURE_COLOR=1, + RENDERTEXTURE_DEPTH, +}; struct GLRenderToTexture { GLuint m_framebufferName; - GLuint m_renderedTexture; GLuint m_depthrenderbuffer; bool m_initialized; + int m_renderTextureType; public: GLRenderToTexture(); - void init(int width, int height); + void init(int width, int height, GLuint textureId, int renderTextureType=RENDERTEXTURE_COLOR); bool enable(); void disable(); diff --git a/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl new file mode 100644 index 000000000..75f39082a --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl @@ -0,0 +1,10 @@ +#version 330 +precision highp float; + + +layout(location = 0) out float fragmentdepth; + +void main(void) +{ + fragmentdepth = gl_FragCoord.z; +} diff --git a/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h new file mode 100644 index 000000000..f9d23e438 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h @@ -0,0 +1,14 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* createShadowMapInstancingFragmentShader= \ +"#version 330\n" +"precision highp float;\n" +"\n" +"\n" +"layout(location = 0) out float fragmentdepth;\n" +"\n" +"void main(void)\n" +"{\n" +" fragmentdepth = gl_FragCoord.z;\n" +"}\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h new file mode 100644 index 000000000..de4f3c49e --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h @@ -0,0 +1,60 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* createShadowMapInstancingVertexShader= \ +"#version 330\n" +"precision highp float;\n" +"\n" +"\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 2) in vec4 instance_quaternion;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"\n" +"\n" +"uniform mat4 ModelViewMatrix;\n" +"uniform mat4 ProjectionMatrix;\n" +"\n" +"\n" +"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" +"{\n" +" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" +" vec4 dt = q1 * q2;\n" +" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" +" return vec4 ( im, re );\n" +"}\n" +"\n" +"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" +"{\n" +" float cah = cos(angle*0.5);\n" +" float sah = sin(angle*0.5);\n" +" float d = inversesqrt(dot(axis,axis));\n" +" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" +" return q;\n" +"}\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"vec4 quatRotate ( in vec4 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, p );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"\n" +"\n" +"void main(void)\n" +"{\n" +" vec4 q = instance_quaternion;\n" +" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" +" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1);\n" +" gl_Position = vertexPos;\n" +"}\n" +"\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl b/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl new file mode 100644 index 000000000..c8eb57fe7 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl @@ -0,0 +1,56 @@ +#version 330 +precision highp float; + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 2) in vec4 instance_quaternion; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; + + +vec4 quatMul ( in vec4 q1, in vec4 q2 ) +{ + vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz ); + vec4 dt = q1 * q2; + float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) ); + return vec4 ( im, re ); +} + +vec4 quatFromAxisAngle(vec4 axis, in float angle) +{ + float cah = cos(angle*0.5); + float sah = sin(angle*0.5); + float d = inversesqrt(dot(axis,axis)); + vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah); + return q; +} +// +// vector rotation via quaternion +// +vec4 quatRotate3 ( in vec3 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} +vec4 quatRotate ( in vec4 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, p ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} + + +void main(void) +{ + vec4 q = instance_quaternion; + vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); + vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1); + gl_Position = vertexPos; +} + diff --git a/btgui/OpenGLWindow/Shaders/instancingPS.glsl b/btgui/OpenGLWindow/Shaders/instancingPS.glsl new file mode 100644 index 000000000..edb5bd9b7 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/instancingPS.glsl @@ -0,0 +1,36 @@ +#version 330 +precision highp float; + +in Fragment +{ + vec4 color; +} fragment; + +in Vert +{ + vec2 texcoord; +} vert; + +uniform sampler2D Diffuse; +in vec3 lightDir,normal,ambient; +out vec4 color; + +void main_textured(void) +{ + color = vec4(0.1,0.2,0.3,0.3); +} + +void main(void) +{ + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color; + vec3 ct,cf; + float intensity,at,af; + intensity = max(dot(lightDir,normalize(normal)),0); + cf = intensity*vec3(1.0,1.0,1.0)+ambient; + af = 1.0; + + ct = texel.rgb; + at = texel.a; + + color = vec4(ct * cf, at * af); +} diff --git a/btgui/OpenGLWindow/Shaders/instancingPS.h b/btgui/OpenGLWindow/Shaders/instancingPS.h new file mode 100644 index 000000000..445337c58 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/instancingPS.h @@ -0,0 +1,40 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* instancingFragmentShader= \ +"#version 330\n" +"precision highp float;\n" +"\n" +"in Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"in Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"\n" +"uniform sampler2D Diffuse;\n" +"in vec3 lightDir,normal,ambient;\n" +"out vec4 color;\n" +"\n" +"void main_textured(void)\n" +"{\n" +" color = vec4(0.1,0.2,0.3,0.3);\n" +"}\n" +"\n" +"void main(void)\n" +"{\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" +" vec3 ct,cf;\n" +" float intensity,at,af;\n" +" intensity = max(dot(lightDir,normalize(normal)),0);\n" +" cf = intensity*vec3(1.0,1.0,1.0)+ambient;\n" +" af = 1.0;\n" +" \n" +" ct = texel.rgb;\n" +" at = texel.a;\n" +" \n" +" color = vec4(ct * cf, at * af); \n" +"}\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/instancingVS.glsl b/btgui/OpenGLWindow/Shaders/instancingVS.glsl new file mode 100644 index 000000000..3c0cbc683 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/instancingVS.glsl @@ -0,0 +1,82 @@ +#version 330 +precision highp float; + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 2) in vec4 instance_quaternion; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; + +out Fragment +{ + vec4 color; +} fragment; + +out Vert +{ + vec2 texcoord; +} vert; + + +vec4 quatMul ( in vec4 q1, in vec4 q2 ) +{ + vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz ); + vec4 dt = q1 * q2; + float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) ); + return vec4 ( im, re ); +} + +vec4 quatFromAxisAngle(vec4 axis, in float angle) +{ + float cah = cos(angle*0.5); + float sah = sin(angle*0.5); + float d = inversesqrt(dot(axis,axis)); + vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah); + return q; +} +// +// vector rotation via quaternion +// +vec4 quatRotate3 ( in vec3 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} +vec4 quatRotate ( in vec4 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, p ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} + +out vec3 lightDir,normal,ambient; + +void main(void) +{ + vec4 q = instance_quaternion; + ambient = vec3(0.3,.3,0.3); + + + vec4 local_normal = (quatRotate3( vertexnormal,q)); + vec3 light_pos = vec3(-0.3,0.1,0.1); + normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz; + + lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz)); +// lightDir = normalize(vec3(gl_LightSource[0].position)); + + vec4 axis = vec4(1,1,1,0); + vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); + vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord); + + gl_Position = vertexPos; + + fragment.color = instance_color; + vert.texcoord = uvcoords; +} + diff --git a/btgui/OpenGLWindow/Shaders/instancingVS.h b/btgui/OpenGLWindow/Shaders/instancingVS.h new file mode 100644 index 000000000..8dea34d91 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/instancingVS.h @@ -0,0 +1,86 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* instancingVertexShader= \ +"#version 330\n" +"precision highp float;\n" +"\n" +"\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 2) in vec4 instance_quaternion;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"\n" +"\n" +"uniform mat4 ModelViewMatrix;\n" +"uniform mat4 ProjectionMatrix;\n" +"\n" +"out Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"out Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"\n" +"\n" +"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" +"{\n" +" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" +" vec4 dt = q1 * q2;\n" +" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" +" return vec4 ( im, re );\n" +"}\n" +"\n" +"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" +"{\n" +" float cah = cos(angle*0.5);\n" +" float sah = sin(angle*0.5);\n" +" float d = inversesqrt(dot(axis,axis));\n" +" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" +" return q;\n" +"}\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"vec4 quatRotate ( in vec4 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, p );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"\n" +"out vec3 lightDir,normal,ambient;\n" +"\n" +"void main(void)\n" +"{\n" +" vec4 q = instance_quaternion;\n" +" ambient = vec3(0.3,.3,0.3);\n" +" \n" +" \n" +" vec4 local_normal = (quatRotate3( vertexnormal,q));\n" +" vec3 light_pos = vec3(-0.3,0.1,0.1);\n" +" normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;\n" +"\n" +" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n" +"// lightDir = normalize(vec3(gl_LightSource[0].position));\n" +" \n" +" vec4 axis = vec4(1,1,1,0);\n" +" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" +" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord);\n" +"\n" +" gl_Position = vertexPos;\n" +" \n" +" fragment.color = instance_color;\n" +" vert.texcoord = uvcoords;\n" +"}\n" +"\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/pointSpritePS.glsl b/btgui/OpenGLWindow/Shaders/pointSpritePS.glsl new file mode 100644 index 000000000..e69407485 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/pointSpritePS.glsl @@ -0,0 +1,37 @@ + +#version 330 +precision highp float; + +in Fragment +{ + vec4 color; +} fragment; + + +in vec3 ambient; + +out vec4 color; + +void main_textured(void) +{ + color = fragment.color;//texture2D(Diffuse,vert.texcoord);//fragment.color; +} + +void main(void) +{ + vec3 N; + N.xy = gl_PointCoord.st*vec2(2.0, -2.0) + vec2(-1.0, 1.0); + float mag = dot(N.xy, N.xy); + if (mag > 1.0) discard; + vec4 texel = vec4(1,0,0,1);//fragment.color*texture(Diffuse,vert.texcoord);//fragment.color; + vec3 ct; + float at,af; + af = 1.0; + + ct = texel.rgb; + at = texel.a; + + vec3 lightDir= vec3(1,0,0); + float diffuse = max(0.0, dot(lightDir, N)); + color = vec4(ct * diffuse, at * af); +} diff --git a/btgui/OpenGLWindow/Shaders/pointSpritePS.h b/btgui/OpenGLWindow/Shaders/pointSpritePS.h new file mode 100644 index 000000000..d5e62ba83 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/pointSpritePS.h @@ -0,0 +1,41 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* pointSpriteFragmentShader= \ +"\n" +"#version 330\n" +"precision highp float;\n" +"\n" +"in Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"\n" +"in vec3 ambient;\n" +"\n" +"out vec4 color;\n" +"\n" +"void main_textured(void)\n" +"{\n" +" color = fragment.color;//texture2D(Diffuse,vert.texcoord);//fragment.color;\n" +"}\n" +"\n" +"void main(void)\n" +"{\n" +" vec3 N;\n" +" N.xy = gl_PointCoord.st*vec2(2.0, -2.0) + vec2(-1.0, 1.0);\n" +" float mag = dot(N.xy, N.xy);\n" +" if (mag > 1.0) discard; \n" +" vec4 texel = vec4(1,0,0,1);//fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" +" vec3 ct;\n" +" float at,af;\n" +" af = 1.0;\n" +" \n" +" ct = texel.rgb;\n" +" at = texel.a;\n" +" \n" +" vec3 lightDir= vec3(1,0,0);\n" +" float diffuse = max(0.0, dot(lightDir, N));\n" +" color = vec4(ct * diffuse, at * af); \n" +"}\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl b/btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl new file mode 100644 index 000000000..27bfa33f5 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl @@ -0,0 +1,46 @@ +#version 330 +precision highp float; + + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform float screenWidth = 700.f; +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; + +out Fragment +{ + vec4 color; +} fragment; + + + +// +// vector rotation via quaternion +// + +out vec3 ambient; + +void main(void) +{ + ambient = vec3(0.3,.3,0.3); + + + vec4 axis = vec4(1,1,1,0); + vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position); + vec3 posEye = vec3(ModelViewMatrix * vec4(instance_position.xyz, 1.0)); + float dist = length(posEye); + float pointRadius = 1.f; + gl_PointSize = instance_scale.x * pointRadius * (screenWidth / dist); + + gl_Position = vertexPos; + + fragment.color = instance_color; +} diff --git a/btgui/OpenGLWindow/Shaders/pointSpriteVS.h b/btgui/OpenGLWindow/Shaders/pointSpriteVS.h new file mode 100644 index 000000000..8ca2a49a0 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/pointSpriteVS.h @@ -0,0 +1,50 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* pointSpriteVertexShader= \ +"#version 330\n" +"precision highp float;\n" +"\n" +"\n" +"\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"\n" +"\n" +"uniform float screenWidth = 700.f;\n" +"uniform mat4 ModelViewMatrix;\n" +"uniform mat4 ProjectionMatrix;\n" +"\n" +"out Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"\n" +"\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"\n" +"out vec3 ambient;\n" +"\n" +"void main(void)\n" +"{\n" +" ambient = vec3(0.3,.3,0.3);\n" +" \n" +" \n" +" vec4 axis = vec4(1,1,1,0);\n" +" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position);\n" +" vec3 posEye = vec3(ModelViewMatrix * vec4(instance_position.xyz, 1.0));\n" +" float dist = length(posEye);\n" +" float pointRadius = 1.f;\n" +" gl_PointSize = instance_scale.x * pointRadius * (screenWidth / dist);\n" +"\n" +" gl_Position = vertexPos;\n" +" \n" +" fragment.color = instance_color;\n" +"}\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl new file mode 100644 index 000000000..b213bdac5 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl @@ -0,0 +1,40 @@ +#version 330 core +//precision highp float; + +in Fragment +{ + vec4 color; +} fragment; + +in Vert +{ + vec2 texcoord; +} vert; + +uniform sampler2D Diffuse; +uniform sampler2DShadow shadowMap; + +in vec3 lightDir,normal,ambient; +in vec4 ShadowCoord; + +out vec4 color; + + + +void main(void) +{ + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color; + vec3 ct,cf; + float intensity,at,af; + intensity = max(dot(lightDir,normalize(normal)),0); + cf = intensity*vec3(1.0,1.0,1.0)+ambient; + af = 1.0; + + ct = texel.rgb; + at = texel.a; + + float bias = 0.005f; + float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w)); + + color = vec4(ct * cf * visibility, at * af); +} diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h new file mode 100644 index 000000000..ba1e5ebab --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h @@ -0,0 +1,44 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* useShadowMapInstancingFragmentShader= \ +"#version 330 core\n" +"//precision highp float;\n" +"\n" +"in Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"in Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"\n" +"uniform sampler2D Diffuse;\n" +"uniform sampler2DShadow shadowMap;\n" +"\n" +"in vec3 lightDir,normal,ambient;\n" +"in vec4 ShadowCoord;\n" +"\n" +"out vec4 color;\n" +"\n" +"\n" +"\n" +"void main(void)\n" +"{\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" +" vec3 ct,cf;\n" +" float intensity,at,af;\n" +" intensity = max(dot(lightDir,normalize(normal)),0);\n" +" cf = intensity*vec3(1.0,1.0,1.0)+ambient;\n" +" af = 1.0;\n" +" \n" +" ct = texel.rgb;\n" +" at = texel.a;\n" +" \n" +" float bias = 0.005f;\n" +" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));\n" +" \n" +" color = vec4(ct * cf * visibility, at * af); \n" +"}\n" +"\n" +; diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl new file mode 100644 index 000000000..3717b52b5 --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl @@ -0,0 +1,86 @@ +#version 330 core +//precision highp float; + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 2) in vec4 instance_quaternion; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; +uniform mat4 DepthBiasModelViewProjectionMatrix; + +out vec4 ShadowCoord; + +out Fragment +{ + vec4 color; +} fragment; + +out Vert +{ + vec2 texcoord; +} vert; + + +vec4 quatMul ( in vec4 q1, in vec4 q2 ) +{ + vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz ); + vec4 dt = q1 * q2; + float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) ); + return vec4 ( im, re ); +} + +vec4 quatFromAxisAngle(vec4 axis, in float angle) +{ + float cah = cos(angle*0.5); + float sah = sin(angle*0.5); + float d = inversesqrt(dot(axis,axis)); + vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah); + return q; +} +// +// vector rotation via quaternion +// +vec4 quatRotate3 ( in vec3 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} +vec4 quatRotate ( in vec4 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, p ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} + +out vec3 lightDir,normal,ambient; + +void main(void) +{ + vec4 q = instance_quaternion; + ambient = vec3(0.3,.3,0.3); + + + vec4 local_normal = (quatRotate3( vertexnormal,q)); + vec3 light_pos = vec3(-0.3,0.1,0.1); + normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz; + + lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz)); +// lightDir = normalize(vec3(gl_LightSource[0].position)); + + vec4 axis = vec4(1,1,1,0); + vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); + vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1); + + gl_Position = vertexPos; + ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); + + fragment.color = instance_color; + vert.texcoord = uvcoords; +} + diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h new file mode 100644 index 000000000..6ae70b98f --- /dev/null +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h @@ -0,0 +1,90 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* useShadowMapInstancingVertexShader= \ +"#version 330 core\n" +"//precision highp float;\n" +"\n" +"\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 2) in vec4 instance_quaternion;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"\n" +"\n" +"uniform mat4 ModelViewMatrix;\n" +"uniform mat4 ProjectionMatrix;\n" +"uniform mat4 DepthBiasModelViewProjectionMatrix;\n" +"\n" +"out vec4 ShadowCoord;\n" +"\n" +"out Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"\n" +"out Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"\n" +"\n" +"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" +"{\n" +" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" +" vec4 dt = q1 * q2;\n" +" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" +" return vec4 ( im, re );\n" +"}\n" +"\n" +"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" +"{\n" +" float cah = cos(angle*0.5);\n" +" float sah = sin(angle*0.5);\n" +" float d = inversesqrt(dot(axis,axis));\n" +" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" +" return q;\n" +"}\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"vec4 quatRotate ( in vec4 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, p );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"\n" +"out vec3 lightDir,normal,ambient;\n" +"\n" +"void main(void)\n" +"{\n" +" vec4 q = instance_quaternion;\n" +" ambient = vec3(0.3,.3,0.3);\n" +" \n" +" \n" +" vec4 local_normal = (quatRotate3( vertexnormal,q));\n" +" vec3 light_pos = vec3(-0.3,0.1,0.1);\n" +" normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;\n" +"\n" +" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n" +"// lightDir = normalize(vec3(gl_LightSource[0].position));\n" +" \n" +" vec4 axis = vec4(1,1,1,0);\n" +" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" +" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1);\n" +"\n" +" gl_Position = vertexPos;\n" +" ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" +"\n" +" fragment.color = instance_color;\n" +" vert.texcoord = uvcoords;\n" +"}\n" +"\n" +"\n" +; diff --git a/build3/stringify.bat b/build3/stringify.bat index 8b8eac2ab..027859ba5 100644 --- a/build3/stringify.bat +++ b/build3/stringify.bat @@ -28,4 +28,22 @@ premake4 --file=stringifyKernel.lua --kernelfile="../src/Bullet3OpenCL/RigidBody premake4 --file=stringifyKernel.lua --kernelfile="../src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.cl" --headerfile="../src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.h" --stringname="rayCastKernelCL" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/instancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/instancingVS.h" --stringname="instancingVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/instancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/instancingPS.h" --stringname="instancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/pointSpriteVS.h" --stringname="pointSpriteVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/pointSpritePS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/pointSpritePS.h" --stringname="pointSpriteFragmentShader" stringify + + +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h" --stringname="createShadowMapInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h" --stringname="createShadowMapInstancingVertexShader" stringify + +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h" --stringname="useShadowMapInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h" --stringname="useShadowMapInstancingVertexShader" stringify + + + + + + + pause \ No newline at end of file diff --git a/build3/stringifyShaders.bat b/build3/stringifyShaders.bat new file mode 100644 index 000000000..9ff852b9a --- /dev/null +++ b/build3/stringifyShaders.bat @@ -0,0 +1,17 @@ + +rem @echo off + + +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/instancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/instancingVS.h" --stringname="instancingVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/instancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/instancingPS.h" --stringname="instancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/pointSpriteVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/pointSpriteVS.h" --stringname="pointSpriteVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/pointSpritePS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/pointSpritePS.h" --stringname="pointSpriteFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingPS.h" --stringname="createShadowMapInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h" --stringname="createShadowMapInstancingVertexShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h" --stringname="useShadowMapInstancingFragmentShader" stringify +premake4 --file=stringifyKernel.lua --kernelfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl" --headerfile="../btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h" --stringname="useShadowMapInstancingVertexShader" stringify + + + + +pause \ No newline at end of file diff --git a/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.cpp b/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.cpp index 1d305544f..8ddaa8e82 100644 --- a/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.cpp +++ b/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.cpp @@ -194,6 +194,7 @@ cl_platform_id b3OpenCLUtils_getPlatform(int platformIndex0, cl_int* pErrNum) void b3OpenCLUtils::getPlatformInfo(cl_platform_id platform, b3OpenCLPlatformInfo* platformInfo) { + b3Assert(platform); cl_int ciErrNum; ciErrNum = clGetPlatformInfo( platform,CL_PLATFORM_VENDOR,B3_MAX_STRING_LENGTH,platformInfo->m_platformVendor,NULL); oclCHECKERROR(ciErrNum,CL_SUCCESS); diff --git a/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h b/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h index 6937da3a6..db6466e76 100644 --- a/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h +++ b/src/Bullet3OpenCL/Initialize/b3OpenCLUtils.h @@ -105,12 +105,19 @@ typedef struct } b3OpenCLDeviceInfo; -typedef struct +struct b3OpenCLPlatformInfo { char m_platformVendor[B3_MAX_STRING_LENGTH]; char m_platformName[B3_MAX_STRING_LENGTH]; char m_platformVersion[B3_MAX_STRING_LENGTH]; -} b3OpenCLPlatformInfo; + + b3OpenCLPlatformInfo() + { + m_platformVendor[0]=0; + m_platformName[0]=0; + m_platformVersion[0]=0; + } +}; ///C++ API for OpenCL utilities: convenience functions diff --git a/src/Bullet3OpenCL/Raycast/b3GpuRaycast.cpp b/src/Bullet3OpenCL/Raycast/b3GpuRaycast.cpp index f71aa7795..3808b863c 100644 --- a/src/Bullet3OpenCL/Raycast/b3GpuRaycast.cpp +++ b/src/Bullet3OpenCL/Raycast/b3GpuRaycast.cpp @@ -214,11 +214,16 @@ void b3GpuRaycast::castRays(const b3AlignedObjectArray& rays, b3Align B3_PROFILE("castRaysGPU"); b3OpenCLArray gpuRays(m_data->m_context,m_data->m_q); - gpuRays.copyFromHost(rays); - b3OpenCLArray gpuHitResults(m_data->m_context,m_data->m_q); - gpuHitResults.resize(hitResults.size()); - gpuHitResults.copyFromHost(hitResults); + + { + B3_PROFILE("raycast copyFromHost"); + gpuRays.copyFromHost(rays); + + + gpuHitResults.resize(hitResults.size()); + gpuHitResults.copyFromHost(hitResults); + } //run kernel @@ -243,6 +248,9 @@ void b3GpuRaycast::castRays(const b3AlignedObjectArray& rays, b3Align } //copy results - gpuHitResults.copyToHost(hitResults); + { + B3_PROFILE("raycast copyToHost"); + gpuHitResults.copyToHost(hitResults); + } } \ No newline at end of file diff --git a/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.cl b/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.cl index 95a3f21dc..acf0bdd32 100644 --- a/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.cl +++ b/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.cl @@ -281,7 +281,8 @@ __kernel void rayCastKernel( for (int b=0;b=0) { + hitPoint = setInterpolate3(rayFrom, rayTo,hitFraction); hitResults[i].m_hitFraction = hitFraction; hitResults[i].m_hitPoint = hitPoint; hitResults[i].m_hitNormal = normalize(hitNormal); diff --git a/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.h b/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.h index f61793f01..ea14e28e4 100644 --- a/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.h +++ b/src/Bullet3OpenCL/Raycast/kernels/rayCastKernels.h @@ -283,7 +283,8 @@ static const char* rayCastKernelCL= \ "\n" " for (int b=0;b=0)\n" " {\n" +" hitPoint = setInterpolate3(rayFrom, rayTo,hitFraction);\n" " hitResults[i].m_hitFraction = hitFraction;\n" " hitResults[i].m_hitPoint = hitPoint;\n" " hitResults[i].m_hitNormal = normalize(hitNormal);\n"