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
This commit is contained in:
erwin coumans
2013-06-28 14:10:23 -07:00
parent bb723f9fd1
commit f2cc840c31
41 changed files with 2060 additions and 645 deletions

View File

@@ -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)
{

View File

@@ -276,7 +276,7 @@ void ParticleDemo::renderScene()
if (m_instancingRenderer)
{
m_instancingRenderer->RenderScene();
m_instancingRenderer->renderScene();
}
}

View File

@@ -230,7 +230,7 @@ void PairBench::exitPhysics()
void PairBench::renderScene()
{
m_instancingRenderer->RenderScene();
m_instancingRenderer->renderScene();
}
void PairBench::clientMoveAndDisplay()

View File

@@ -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;j<textureHeight;j++)
{
for (int i=0;i<textureWidth;i++)
{
pixels[(j*textureWidth+i)*numComponents] = orgPixels[(j*textureWidth+i)*numComponents]*255.f;
pixels[(j*textureWidth+i)*numComponents+1]=orgPixels[(j*textureWidth+i)*numComponents+1]*255.f;
pixels[(j*textureWidth+i)*numComponents+2]=orgPixels[(j*textureWidth+i)*numComponents+2]*255.f;
pixels[(j*textureWidth+i)*numComponents+3]=orgPixels[(j*textureWidth+i)*numComponents+3]*255.f;
}
}
if (1)
{
//swap the pixels
unsigned char tmp;
for (int j=0;j<textureHeight/2;j++)
{
for (int i=0;i<textureWidth;i++)
{
for (int c=0;c<numComponents;c++)
{
tmp = pixels[(j*textureWidth+i)*numComponents+c];
pixels[(j*textureWidth+i)*numComponents+c]=
pixels[((textureHeight-j-1)*textureWidth+i)*numComponents+c];
pixels[((textureHeight-j-1)*textureWidth+i)*numComponents+c] = tmp;
}
}
}
}
stbi_write_png(fileName, textureWidth,textureHeight, numComponents, pixels, textureWidth*numComponents);
free(pixels);
free(orgPixels);
}
int main(int argc, char* argv[])
{
//b3OpenCLUtils::setCachePath("/Users/erwincoumans/develop/mycache");
@@ -539,7 +613,7 @@ int main(int argc, char* argv[])
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
{
@@ -594,7 +668,7 @@ int main(int argc, char* argv[])
// OpenGL3CoreRenderer render;
glClearColor(0,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
window->endRendering();
@@ -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);

View File

@@ -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 <string.h>
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;i<m_raytraceData->textureWidth;i++)
{
for (int y=0;y<m_raytraceData->textureHeight;y++)
{
int color = 0;
if (y<m_raytraceData->textureHeight-1 && (y>0) && (i>0 && i<m_raytraceData->textureWidth-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;i<ci.arraySizeX;i++)
{
//for (int j=0;j<ci.arraySizeY;j++)
for (int j=0;j<10;j++)
{
// for (int k=0;k<ci.arraySizeZ;k++)
for (int k=0;k<1;k++)
{
float mass = 1.f;
if (j==0)//ci.arraySizeY-1)
{
//mass=0.f;
}
b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
//b3Vector3 position(i*2.2,10+j*1.9,k*2.2);
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
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;x<m_raytraceData->textureWidth;x++)
{
for (int y=0;y<m_raytraceData->textureHeight;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<b3RayHit> hits;
{
B3_PROFILE("hits.resize");
hits.resize(primaryRays.size());
}
if (1)
{
B3_PROFILE("init hits");
for (int i=0;i<hits.size();i++)
{
hits[i].m_hitFraction = 1.f;
hits[i].m_hitResult2 = -1;
}
}
b3Vector3 lightPos(1000,1000,100);
{
B3_PROFILE("cast primary rays");
//m_raycaster->castRaysHost(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<b3RayInfo> shadowRays;
{
B3_PROFILE("shadowRays.resize");
shadowRays.resize(primaryRays.size());
}
b3AlignedObjectArray<b3RayHit> shadowHits;
{
B3_PROFILE("shadowHits.resize");
shadowHits.resize(hits.size());
}
{
B3_PROFILE("init shadow rays");
for (int i=0;i<hits.size();i++)
{
if(hits[i].m_hitFraction<1.f)
{
//hits[i].m_hitPoint.setInterpolate3(primaryRays[i].m_from,primaryRays[i].m_to,hits[i].m_hitFraction);
//b3Vector3 shift = (lightPos-hits[i].m_hitPoint).normalize()*0.001f;
shadowRays[i].m_from = hits[i].m_hitPoint;
shadowRays[i].m_to = lightPos;
shadowHits[i].m_hitFraction=1.f;
shadowHits[i].m_hitResult2 = hits[i].m_hitResult0;
} else
{
shadowRays[i].m_from.setValue(0,0,0);
shadowRays[i].m_to.setValue(0,0,0);
shadowHits[i].m_hitFraction=1.f;
shadowHits[i].m_hitResult2 = -2;
}
}
}
{
B3_PROFILE("cast shadow rays");
//m_raycaster->castRaysHost(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;i<shadowHits.size();i++)
{
bool hit = hits[i].m_hitFraction < 1.f;
if (hit)
{
float dotje = hits[i].m_hitNormal.dot(lightPos);
if (dotje>0.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);
}
}

View File

@@ -0,0 +1,33 @@
#ifndef RAYTRACED_SHADOW_DEMO_H
#define RAYTRACED_SHADOW_DEMO_H
#include "../rigidbody/GpuConvexScene.h"
class GpuRaytraceScene : public GpuBoxPlaneScene
{
protected:
b3AlignedObjectArray<b3RayInfo> 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

View File

@@ -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 <string.h>
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;i<m_raytraceData->textureWidth;i++)
{
for (int y=0;y<m_raytraceData->textureHeight;y++)
{
int color = 0;
if (y<m_raytraceData->textureHeight-1 && (y>0) && (i>0 && i<m_raytraceData->textureWidth-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;i<ci.arraySizeX;i++)
{
//for (int j=0;j<ci.arraySizeY;j++)
for (int j=0;j<10;j++)
{
// for (int k=0;k<ci.arraySizeZ;k++)
for (int k=0;k<10;k++)
{
float mass = 1.f;
if (j==0)//ci.arraySizeY-1)
{
//mass=0.f;
}
b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
//b3Vector3 position(i*2.2,10+j*1.9,k*2.2);
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);
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<b3RayInfo> 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;x<m_raytraceData->textureWidth;x++)
{
for (int y=0;y<m_raytraceData->textureHeight;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<b3RayHit> hits;
hits.resize(rays.size());
{
B3_PROFILE("init hits");
for (int i=0;i<hits.size();i++)
{
hits[i].m_hitFraction = 1.f;
}
}
//m_raycaster->castRaysHost(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;i<hits.size();i++)
{
bool hit = hits[i].m_hitFraction < 1.f;
if (hit)
{
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;
} 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);
}

View File

@@ -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

View File

@@ -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)
{

View File

@@ -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()
{
}

View File

@@ -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

View File

@@ -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

View File

@@ -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()