Merge pull request #605 from erwincoumans/master

fix text rendering (uninitialized uniforms)
This commit is contained in:
erwincoumans
2016-04-23 15:38:22 -07:00
7 changed files with 97 additions and 21 deletions

View File

@@ -43,7 +43,11 @@ struct CommonRenderInterface
virtual void drawPoint(const double* position, const double color[4], double pointDrawSize)=0; virtual void drawPoint(const double* position, const double color[4], double pointDrawSize)=0;
virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices,int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1)=0; virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices,int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1)=0;
virtual void updateShape(int shapeIndex, const float* vertices)=0; virtual void updateShape(int shapeIndex, const float* vertices)=0;
virtual int registerTexture(const unsigned char* texels, int width, int height)=0;
virtual int registerTexture(const unsigned char* texels, int width, int height)=0;
virtual void updateTexture(int textureIndex, const unsigned char* texels)=0;
virtual void activateTexture(int textureIndex)=0;
virtual void writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex)=0; virtual void writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex)=0;
virtual void writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex)=0; virtual void writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex)=0;
virtual void writeSingleInstanceColorToCPU(float* color, int srcIndex)=0; virtual void writeSingleInstanceColorToCPU(float* color, int srcIndex)=0;

View File

@@ -9,9 +9,7 @@
#include "../BasicDemo/BasicExample.h" #include "../BasicDemo/BasicExample.h"
#include "../Planar2D/Planar2D.h" #include "../Planar2D/Planar2D.h"
#include "../Benchmarks/BenchmarkDemo.h" #include "../Benchmarks/BenchmarkDemo.h"
#ifdef ENABLE_URDF_OBJ
#include "../Importers/ImportObjDemo/ImportObjExample.h" #include "../Importers/ImportObjDemo/ImportObjExample.h"
#endif
#include "../Importers/ImportBsp/ImportBspExample.h" #include "../Importers/ImportBsp/ImportBspExample.h"
#include "../Importers/ImportColladaDemo/ImportColladaSetup.h" #include "../Importers/ImportColladaDemo/ImportColladaSetup.h"
#include "../Importers/ImportSTLDemo/ImportSTLSetup.h" #include "../Importers/ImportSTLDemo/ImportSTLSetup.h"
@@ -194,9 +192,7 @@ static ExampleEntry gDefaultExamples[]=
ExampleEntry(0,"Importers"), ExampleEntry(0,"Importers"),
ExampleEntry(1,"Import .bullet", "Load a binary .bullet file. The serialization mechanism can deal with versioning, differences in endianess, 32 and 64bit, double/single precision. It is easy to save a .bullet file, see the examples/Importers/ImportBullet/SerializeDemo.cpp for a code example how to export a .bullet file.", SerializeBulletCreateFunc), ExampleEntry(1,"Import .bullet", "Load a binary .bullet file. The serialization mechanism can deal with versioning, differences in endianess, 32 and 64bit, double/single precision. It is easy to save a .bullet file, see the examples/Importers/ImportBullet/SerializeDemo.cpp for a code example how to export a .bullet file.", SerializeBulletCreateFunc),
#ifdef ENABLE_URDF_OBJ
ExampleEntry(1,"Wavefront Obj", "Import a Wavefront .obj file", ImportObjCreateFunc, 0), ExampleEntry(1,"Wavefront Obj", "Import a Wavefront .obj file", ImportObjCreateFunc, 0),
#endif
ExampleEntry(1,"Quake BSP", "Import a Quake .bsp file", ImportBspCreateFunc, 0), ExampleEntry(1,"Quake BSP", "Import a Quake .bsp file", ImportBspCreateFunc, 0),
ExampleEntry(1,"COLLADA dae", "Import the geometric mesh data from a COLLADA file. This is used as part of the URDF importer. This loader can also be used to import collision geometry in general. ", ExampleEntry(1,"COLLADA dae", "Import the geometric mesh data from a COLLADA file. This is used as part of the URDF importer. This loader can also be used to import collision geometry in general. ",
ImportColladaCreateFunc, 0), ImportColladaCreateFunc, 0),

View File

@@ -78,7 +78,6 @@ project "BulletExampleBrowserLib"
"../../src", "../../src",
"../ThirdPartyLibs", "../ThirdPartyLibs",
} }
defines {"ENABLE_URDF_OBJ"}
if _OPTIONS["lua"] then if _OPTIONS["lua"] then
includedirs{"../ThirdPartyLibs/lua-5.2.3/src"} includedirs{"../ThirdPartyLibs/lua-5.2.3/src"}

View File

@@ -134,7 +134,12 @@ extern int gShapeIndex;
struct InternalTextureHandle
{
GLuint m_glTexture;
int m_width;
int m_height;
};
@@ -148,7 +153,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
GLuint m_defaultTexturehandle; GLuint m_defaultTexturehandle;
b3AlignedObjectArray<GLuint> m_textureHandles; b3AlignedObjectArray<InternalTextureHandle> m_textureHandles;
GLRenderToTexture* m_shadowMap; GLRenderToTexture* m_shadowMap;
GLuint m_shadowTexture; GLuint m_shadowTexture;
@@ -518,9 +523,9 @@ int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const float*
int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height) int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height)
{ {
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
glActiveTexture(GL_TEXTURE0);
int textureIndex = m_data->m_textureHandles.size(); int textureIndex = m_data->m_textureHandles.size();
const GLubyte* image= (const GLubyte*)texels; const GLubyte* image= (const GLubyte*)texels;
GLuint textureHandle; GLuint textureHandle;
glGenTextures(1,(GLuint*)&textureHandle); glGenTextures(1,(GLuint*)&textureHandle);
glBindTexture(GL_TEXTURE_2D,textureHandle); glBindTexture(GL_TEXTURE_2D,textureHandle);
@@ -528,18 +533,51 @@ int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
InternalTextureHandle h;
h.m_glTexture = textureHandle;
h.m_width = width;
h.m_height = height;
m_data->m_textureHandles.push_back(textureHandle); m_data->m_textureHandles.push_back(h);
return textureIndex; return textureIndex;
} }
void GLInstancingRenderer::updateTexture(int textureIndex, const unsigned char* texels)
{
if (textureIndex>=0)
{
glActiveTexture(GL_TEXTURE0);
b3Assert(glGetError() ==GL_NO_ERROR);
InternalTextureHandle& h = m_data->m_textureHandles[textureIndex];
glBindTexture(GL_TEXTURE_2D,h.m_glTexture);
b3Assert(glGetError() ==GL_NO_ERROR);
const GLubyte* image= (const GLubyte*)texels;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, h.m_width,h.m_height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
b3Assert(glGetError() ==GL_NO_ERROR);
glGenerateMipmap(GL_TEXTURE_2D);
b3Assert(glGetError() ==GL_NO_ERROR);
}
}
void GLInstancingRenderer::activateTexture(int textureIndex)
{
glActiveTexture(GL_TEXTURE0);
if (textureIndex>=0)
{
glBindTexture(GL_TEXTURE_2D,m_data->m_textureHandles[textureIndex].m_glTexture);
} else
{
glBindTexture(GL_TEXTURE_2D,0);
}
}
void GLInstancingRenderer::updateShape(int shapeIndex, const float* vertices) void GLInstancingRenderer::updateShape(int shapeIndex, const float* vertices)
{ {
b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex]; b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex];
@@ -559,7 +597,7 @@ int GLInstancingRenderer::registerShape(const float* vertices, int numvertices,
if (textureId>=0) if (textureId>=0)
{ {
gfxObj->m_texturehandle = m_data->m_textureHandles[textureId]; gfxObj->m_texturehandle = m_data->m_textureHandles[textureId].m_glTexture;
} }
gfxObj->m_primitiveType = primitiveType; gfxObj->m_primitiveType = primitiveType;

View File

@@ -63,6 +63,9 @@ public:
virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1); virtual int registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType=B3_GL_TRIANGLES, int textureIndex=-1);
virtual int registerTexture(const unsigned char* texels, int width, int height); virtual int registerTexture(const unsigned char* texels, int width, int height);
virtual void updateTexture(int textureIndex, const unsigned char* texels);
virtual void activateTexture(int textureIndex);
///position x,y,z, quaternion x,y,z,w, color r,g,b,a, scaling x,y,z ///position x,y,z, quaternion x,y,z,w, color r,g,b,a, scaling x,y,z
virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling); virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);

View File

@@ -50,6 +50,10 @@ struct SimpleOpenGL2Renderer : public CommonRenderInterface
{ {
return -1; return -1;
} }
virtual void updateTexture(int textureIndex, const unsigned char* texels) {}
virtual void activateTexture(int textureIndex) {}
virtual int registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling); virtual int registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling);
virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling); virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);

View File

@@ -1,10 +1,8 @@
#include "OpenGLWindow/SimpleOpenGL3App.h" #include "OpenGLWindow/SimpleOpenGL3App.h"
#include "Bullet3Common/b3Vector3.h" #include "Bullet3Common/b3Vector3.h"
#include "Bullet3Common/b3CommandLineArgs.h" #include "Bullet3Common/b3CommandLineArgs.h"
#include "assert.h" #include "assert.h"
#include <stdio.h> #include <stdio.h>
#include "OpenGLWindow/OpenGLInclude.h"
char* gVideoFileName = 0; char* gVideoFileName = 0;
char* gPngFileName = 0; char* gPngFileName = 0;
@@ -16,6 +14,8 @@ static b3MouseButtonCallback sOldMouseButtonCB = 0;
static b3KeyboardCallback sOldKeyboardCB = 0; static b3KeyboardCallback sOldKeyboardCB = 0;
//static b3RenderCallback sOldRenderCB = 0; //static b3RenderCallback sOldRenderCB = 0;
float gWidth = 0 ;
float gHeight = 0;
void MyWheelCallback(float deltax, float deltay) void MyWheelCallback(float deltax, float deltay)
{ {
@@ -24,6 +24,9 @@ void MyWheelCallback(float deltax, float deltay)
} }
void MyResizeCallback( float width, float height) void MyResizeCallback( float width, float height)
{ {
gWidth = width;
gHeight = height;
if (sOldResizeCB) if (sOldResizeCB)
sOldResizeCB(width,height); sOldResizeCB(width,height);
} }
@@ -59,6 +62,7 @@ int main(int argc, char* argv[])
SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",1024,768,true); SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",1024,768,true);
app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13); app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13);
app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0); app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0);
app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0,0,0); app->m_instancingRenderer->getActiveCamera()->setCameraTargetPosition(0,0,0);
@@ -74,16 +78,22 @@ int main(int argc, char* argv[])
app->m_window->setResizeCallback(MyResizeCallback); app->m_window->setResizeCallback(MyResizeCallback);
assert(glGetError()==GL_NO_ERROR);
myArgs.GetCmdLineArgument("mp4_file",gVideoFileName); myArgs.GetCmdLineArgument("mp4_file",gVideoFileName);
if (gVideoFileName) if (gVideoFileName)
app->dumpFramesToVideo(gVideoFileName); app->dumpFramesToVideo(gVideoFileName);
myArgs.GetCmdLineArgument("png_file",gPngFileName); myArgs.GetCmdLineArgument("png_file",gPngFileName);
char fileName[1024]; char fileName[1024];
int textureIndex = -1;
int textureWidth = 128;
int textureHeight = 128;
unsigned char* image=new unsigned char[textureWidth*textureHeight*4];
int textureHandle = app->m_renderer->registerTexture(image,textureWidth,textureHeight);
do do
{ {
static int frameCount = 0; static int frameCount = 0;
@@ -96,7 +106,29 @@ int main(int argc, char* argv[])
app->dumpNextFrameToPng(fileName); app->dumpNextFrameToPng(fileName);
} }
assert(glGetError()==GL_NO_ERROR);
//update the texels of the texture using a simple pattern, animated using frame index
for(int y=0;y<textureHeight;++y)
{
const int t=(y+frameCount)>>4;
unsigned char* pi=image+y*textureWidth*3;
for(int x=0;x<textureWidth;++x)
{
const int s=x>>4;
const unsigned char b=180;
unsigned char c=b+((s+(t&1))&1)*(255-b);
pi[0]=pi[1]=pi[2]=pi[3]=c;pi+=3;
}
}
app->m_renderer->activateTexture(textureHandle);
app->m_renderer->updateTexture(textureHandle,image);
float color[4] = {255,1,1,1};
app->m_primRenderer->drawTexturedRect(100,200,gWidth/2-50,gHeight/2-50,color,0,0,1,1,true);
app->m_instancingRenderer->init(); app->m_instancingRenderer->init();
app->m_instancingRenderer->updateCamera(); app->m_instancingRenderer->updateCamera();