From 0b40e1a899307aeec574eff668c42c6fb5823525 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sun, 27 Jul 2014 10:29:10 -0700 Subject: [PATCH] add upAxis option for Y or Z up remove some warnings --- btgui/OpenGLWindow/GLInstancingRenderer.cpp | 341 ++++++++---------- btgui/OpenGLWindow/GLInstancingRenderer.h | 2 +- btgui/OpenGLWindow/GwenOpenGL3CoreRenderer.h | 10 +- .../Shaders/useShadowMapInstancingVS.glsl | 8 +- .../Shaders/useShadowMapInstancingVS.h | 8 +- btgui/OpenGLWindow/ShapeData.h | 3 +- btgui/OpenGLWindow/SimpleOpenGL3App.cpp | 123 +++++-- btgui/OpenGLWindow/SimpleOpenGL3App.h | 2 +- btgui/OpenGLWindow/X11OpenGLWindow.cpp | 11 +- btgui/OpenGLWindow/premake4.lua | 1 - 10 files changed, 255 insertions(+), 254 deletions(-) diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.cpp b/btgui/OpenGLWindow/GLInstancingRenderer.cpp index d8789ff08..8d2e05a4f 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.cpp +++ b/btgui/OpenGLWindow/GLInstancingRenderer.cpp @@ -18,7 +18,7 @@ subject to the following restrictions: bool useShadowMap=true;//false;//true; float shadowMapWidth=8192; float shadowMapHeight=8192; -float shadowMapWorldSize=200; +float shadowMapWorldSize=100; float WHEEL_MULTIPLIER=0.01f; float MOUSE_MOVE_MULTIPLIER = 0.4f; #define MAX_POINTS_IN_BATCH 1024 @@ -68,6 +68,7 @@ float MOUSE_MOVE_MULTIPLIER = 0.4f; static InternalDataRenderer* sData2; GLint lineWidthRange[2]={1,1}; +static b3Vector3 gLightPos=b3MakeVector3(-5,200,-40); struct b3GraphicsInstance { @@ -84,7 +85,16 @@ struct b3GraphicsInstance int m_vertexArrayOffset; int m_primitiveType; - b3GraphicsInstance() :m_cube_vao(-1),m_index_vbo(-1),m_numIndices(-1),m_numVertices(-1),m_numGraphicsInstances(0),m_instanceOffset(0),m_vertexArrayOffset(0),m_primitiveType(B3_GL_TRIANGLES),m_texturehandle(0) + b3GraphicsInstance() + :m_cube_vao(-1), + m_index_vbo(-1), + m_texturehandle(0), + m_numIndices(-1), + m_numVertices(-1), + m_numGraphicsInstances(0), + m_instanceOffset(0), + m_vertexArrayOffset(0), + m_primitiveType(B3_GL_TRIANGLES) { } @@ -98,7 +108,7 @@ bool m_ortho = false; static GLfloat projectionMatrix[16]; static GLfloat modelviewMatrix[16]; -static GLfloat depthLightModelviewMatrix[16]; +//static GLfloat depthLightModelviewMatrix[16]; static void checkError(const char *functionName) { @@ -141,14 +151,15 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData int m_rightMouseButton; - int m_altPressed; - int m_controlPressed; + GLuint m_defaultTexturehandle; b3AlignedObjectArray m_textureHandles; GLRenderToTexture* m_shadowMap; GLuint m_shadowTexture; + int m_altPressed; + int m_controlPressed; InternalDataRenderer() : m_cameraPosition(b3MakeVector3(0,0,0)), @@ -317,7 +328,7 @@ static GLuint instancingShaderPointSprite; // The point spr -static bool done = false; +//static bool done = false; static GLint lines_ModelViewMatrix=0; static GLint lines_ProjectionMatrix=0; @@ -334,6 +345,7 @@ GLuint linesIndexVbo = 0; static GLint useShadow_ModelViewMatrix=0; static GLint useShadow_MVP=0; +static GLint useShadow_lightDirIn=0; static GLint useShadow_ProjectionMatrix=0; static GLint useShadow_DepthBiasModelViewMatrix=0; @@ -344,13 +356,13 @@ static GLint createShadow_depthMVP=0; static GLint ModelViewMatrix=0; static GLint ProjectionMatrix=0; -static GLint DepthModelViewMatrix=0; + static GLint uniform_texture_diffuse = 0; static GLint screenWidthPointSprite=0; static GLint ModelViewMatrixPointSprite=0; static GLint ProjectionMatrixPointSprite=0; -static GLint uniform_texture_diffusePointSprite= 0; +//static GLint uniform_texture_diffusePointSprite= 0; @@ -490,15 +502,15 @@ void GLInstancingRenderer::writeSingleInstanceTransformToGPU(float* position, fl void GLInstancingRenderer::writeTransforms() { - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); + + b3Assert(glGetError() ==GL_NO_ERROR); glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo); glFlush(); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); + char* orgBase = (char*)glMapBuffer( GL_ARRAY_BUFFER,GL_READ_WRITE); if (orgBase) @@ -568,8 +580,7 @@ void GLInstancingRenderer::writeTransforms() { b3Error("ERROR glMapBuffer failed\n"); } - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); glUnmapBuffer( GL_ARRAY_BUFFER); //if this glFinish is removed, the animation is not always working/blocks @@ -577,17 +588,16 @@ void GLInstancingRenderer::writeTransforms() glFlush(); glBindBuffer(GL_ARRAY_BUFFER, 0);//m_data->m_vbo); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); } int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const double* pos1, const double* orn1, const double* color1, const double* scaling1) { - float pos[4] = {pos1[0],pos1[1],pos1[2],pos1[3]}; - float orn[4] = {orn1[0],orn1[1],orn1[2],orn1[3]}; - float color[4] = {color1[0],color1[1],color1[2],color1[3]}; - float scaling[4] = {scaling1[0],scaling1[1],scaling1[2],scaling1[3]}; + float pos[4] = {(float)pos1[0],(float)pos1[1],(float)pos1[2],(float)pos1[3]}; + float orn[4] = {(float)orn1[0],(float)orn1[1],(float)orn1[2],(float)orn1[3]}; + float color[4] = {(float)color1[0],(float)color1[1],(float)color1[2],(float)color1[3]}; + float scaling[4] = {(float)scaling1[0],(float)scaling1[1],(float)scaling1[2],(float)scaling1[3]}; return registerGraphicsInstance(shapeIndex,pos,orn,color,scaling); } @@ -635,8 +645,7 @@ int GLInstancingRenderer::registerGraphicsInstance(int shapeIndex, const float* int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width, int height) { - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); int textureIndex = m_data->m_textureHandles.size(); const GLubyte* image= (const GLubyte*)texels; @@ -644,19 +653,15 @@ int GLInstancingRenderer::registerTexture(const unsigned char* texels, int width glGenTextures(1,(GLuint*)&textureHandle); glBindTexture(GL_TEXTURE_2D,textureHandle); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); glGenerateMipmap(GL_TEXTURE_2D); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); m_data->m_textureHandles.push_back(textureHandle); @@ -706,8 +711,10 @@ int GLInstancingRenderer::registerShape(const float* vertices, int numvertices, char* dest= (char*)glMapBuffer( GL_ARRAY_BUFFER,GL_WRITE_ONLY);//GL_WRITE_ONLY int vertexStrideInBytes = 9*sizeof(float); int sz = numvertices*vertexStrideInBytes; +#ifdef B3_DEBUG int totalUsed = vertexStrideInBytes*gfxObj->m_vertexArrayOffset+sz; b3Assert(totalUsedm_maxShapeCapacityInBytes); +#endif//B3_DEBUG memcpy(dest+vertexStrideInBytes*gfxObj->m_vertexArrayOffset,vertices,sz); glUnmapBuffer( GL_ARRAY_BUFFER); @@ -796,7 +803,7 @@ void GLInstancingRenderer::InitShaders() useShadow_DepthBiasModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, "DepthBiasModelViewProjectionMatrix"); useShadow_uniform_texture_diffuse = glGetUniformLocation(useShadowMapInstancingShader, "Diffuse"); useShadow_shadowMap = glGetUniformLocation(useShadowMapInstancingShader,"shadowMap"); - + useShadow_lightDirIn = glGetUniformLocation(useShadowMapInstancingShader,"lightDirIn"); createShadowMapInstancingShader = gltLoadShaderPair(createShadowMapInstancingVertexShader,createShadowMapInstancingFragmentShader); glLinkProgram(createShadowMapInstancingShader); @@ -847,26 +854,20 @@ void GLInstancingRenderer::InitShaders() void GLInstancingRenderer::init() { - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); + glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); // glClearColor(float(0.),float(0.),float(0.4),float(0)); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); { B3_PROFILE("texture"); @@ -910,63 +911,32 @@ void GLInstancingRenderer::init() glGenTextures(1,(GLuint*)&m_data->m_defaultTexturehandle); glBindTexture(GL_TEXTURE_2D,m_data->m_defaultTexturehandle); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - #if 0 - - glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - - #endif - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==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(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); delete[] image; m_textureinitialized=true; } - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); glBindTexture(GL_TEXTURE_2D,m_data->m_defaultTexturehandle); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); } else { glDisable(GL_TEXTURE_2D); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); } } //glEnable(GL_COLOR_MATERIAL); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); // glEnable(GL_CULL_FACE); // glCullFace(GL_BACK); @@ -1086,14 +1056,28 @@ void GLInstancingRenderer::resize(int width, int height) m_screenHeight = height; } -void GLInstancingRenderer::updateCamera() +void GLInstancingRenderer::updateCamera(int upAxis) { - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); - - int m_forwardAxis(2); + int m_forwardAxis(-1); + switch (upAxis) + { + case 1: + m_forwardAxis = 2; + m_data->m_cameraUp = b3MakeVector3(0,1,0); + gLightPos = b3MakeVector3(-50.f,100,30); + break; + case 2: + m_forwardAxis = 1; + m_data->m_cameraUp = b3MakeVector3(0,0,1); + gLightPos = b3MakeVector3(-50.f,30,100); + break; + default: + b3Assert(0); + }; + float m_frustumZNear=1; float m_frustumZFar=10000.f; @@ -1106,8 +1090,7 @@ void GLInstancingRenderer::updateCamera() b3Quaternion rot(m_data->m_cameraUp,razi); - - b3Vector3 eyePos = b3MakeVector3(0,0,0); + b3Vector3 eyePos = b3MakeVector3(0,0,0); eyePos[m_forwardAxis] = -m_data->m_cameraDistance; b3Vector3 forward = b3MakeVector3(eyePos[0],eyePos[1],eyePos[2]); @@ -1256,19 +1239,15 @@ void GLInstancingRenderer::getMouseDirection(float* dir, int x, int y) #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); - + + b3Assert(glGetError() ==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); - + b3Assert(glGetError() ==GL_NO_ERROR); for (int j=0;jm_vbo); - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,0); - int curOffset = 0; - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - glUseProgram(linesShader); + + b3Assert(glGetError() ==GL_NO_ERROR);glUseProgram(linesShader); glUniformMatrix4fv(lines_ProjectionMatrix, 1, false, &projectionMatrix[0]); glUniformMatrix4fv(lines_ModelViewMatrix, 1, false, &modelviewMatrix[0]); glUniform4f(lines_colour,color[0],color[1],color[2],color[3]); @@ -1412,9 +1385,7 @@ void GLInstancingRenderer::drawLines(const float* positions, const float color[4 // glPointSize(pointDrawSize); glBindVertexArray(linesVertexArrayObject); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); glBindBuffer(GL_ARRAY_BUFFER, linesVertexBufferObject); { @@ -1422,21 +1393,15 @@ void GLInstancingRenderer::drawLines(const float* positions, const float color[4 glBufferData(GL_ARRAY_BUFFER, numPoints*pointStrideInBytes, 0,GL_DYNAMIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, numPoints*pointStrideInBytes, positions); - err = glGetError(); - b3Assert(err == GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, linesVertexBufferObject); glEnableVertexAttribArray(0); - err = glGetError(); - b3Assert(err == GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); int numFloats = pointStrideInBytes / sizeof(float); glVertexAttribPointer(0, numFloats, GL_FLOAT, GL_FALSE, 0, 0); - err = glGetError(); - b3Assert(err == GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, linesIndexVbo); int indexBufferSizeInBytes = numIndices*sizeof(int); @@ -1451,68 +1416,54 @@ void GLInstancingRenderer::drawLines(const float* positions, const float color[4 glBindBuffer(GL_ARRAY_BUFFER, 0); // for (int i=0;i draw only front-facing triangles - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); } else { glDisable(GL_CULL_FACE); //glCullFace(GL_BACK); } - static b3Vector3 lightPos = b3MakeVector3(-5.f,200,-40);//20,15,10);//-13,6,2);// = b3Vector3(0.5f,2,2); -// lightPos.y+=0.1f; + b3CreateOrtho(-shadowMapWorldSize,shadowMapWorldSize,-shadowMapWorldSize,shadowMapWorldSize,1,300,depthProjectionMatrix);//-14,14,-14,14,1,200, depthProjectionMatrix); float depthViewMatrix[4][4]; b3Vector3 center = b3MakeVector3(0,0,0); b3Vector3 up =b3MakeVector3(0,1,0); - b3CreateLookAt(lightPos,center,up,&depthViewMatrix[0][0]); + b3CreateLookAt(gLightPos,center,up,&depthViewMatrix[0][0]); //b3CreateLookAt(lightPos,m_data->m_cameraTargetPosition,b3Vector3(0,1,0),(float*)depthModelViewMatrix2); GLfloat depthModelMatrix[4][4]; @@ -1669,12 +1625,10 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) { B3_PROFILE("updateCamera"); - updateCamera(); + // updateCamera(); } - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - +b3Assert(glGetError() ==GL_NO_ERROR); // glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -1684,9 +1638,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo); glFlush(); } - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - +b3Assert(glGetError() ==GL_NO_ERROR); int totalNumInstances = 0; @@ -1696,7 +1648,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) } int curOffset = 0; - GLuint lastBindTexture = 0; + //GLuint lastBindTexture = 0; for (int i=0;im_defaultTexturehandle; +//disable lazy evaluation, it just leads to bugs //if (lastBindTexture != curBindTexture) { glBindTexture(GL_TEXTURE_2D,curBindTexture); } - lastBindTexture = curBindTexture; + //lastBindTexture = curBindTexture; - err = glGetError(); - b3Assert(err==GL_NO_ERROR); +b3Assert(glGetError() ==GL_NO_ERROR); // int myOffset = gfxObj->m_instanceOffset*4*sizeof(float); int POSITION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4); @@ -1731,16 +1683,22 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) int vertexStride = 9*sizeof(float); - int vertexBase = gfxObj->m_vertexArrayOffset*vertexStride; + PointerCaster vertex; + vertex.m_baseIndex = gfxObj->m_vertexArrayOffset*vertexStride; + - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 9*sizeof(float), (GLvoid*)vertexBase); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 9*sizeof(float), vertex.m_pointer); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_maxShapeCapacityInBytes)); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE)); - int uvoffset = 7*sizeof(float)+vertexBase; - int normaloffset = 4*sizeof(float)+vertexBase; + + PointerCaster uv; + uv.m_baseIndex = 7*sizeof(float)+vertex.m_baseIndex; - glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 9*sizeof(float), (GLvoid *)uvoffset); - glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 9*sizeof(float), (GLvoid *)normaloffset); + PointerCaster normal; + normal.m_baseIndex = 4*sizeof(float)+vertex.m_baseIndex; + + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 9*sizeof(float), uv.m_pointer); + glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 9*sizeof(float), normal.m_pointer); glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE)); glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*3*sizeof(float)+m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE)); @@ -1765,7 +1723,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) int indexCount = gfxObj->m_numIndices; - int indexOffset = 0; + GLvoid* indexOffset = 0; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gfxObj->m_index_vbo); { @@ -1779,9 +1737,8 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) glUniform1f(screenWidthPointSprite,m_screenWidth); //glUniform1i(uniform_texture_diffusePointSprite, 0); - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - glPointSize(20); + b3Assert(glGetError() ==GL_NO_ERROR); + glPointSize(20); #ifndef __APPLE__ glEnable(GL_POINT_SPRITE_ARB); @@ -1789,7 +1746,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) #endif glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); - glDrawElementsInstanced(GL_POINTS, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + glDrawElementsInstanced(GL_POINTS, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); } else { switch (renderMode) @@ -1801,7 +1758,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) 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); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); break; } case B3_CREATE_SHADOWMAP_RENDERMODE: @@ -1814,7 +1771,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) */ glUseProgram(createShadowMapInstancingShader); glUniformMatrix4fv(createShadow_depthMVP, 1, false, &depthMVP[0][0]); - glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); break; } @@ -1826,11 +1783,14 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) float MVP[16]; b3Matrix4x4Mul16(projectionMatrix,modelviewMatrix,MVP); glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]); + b3Vector3 gLightDir = gLightPos; + gLightDir.normalize(); + glUniform3f(useShadow_lightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]); glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][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); + glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); break; } default: @@ -1859,8 +1819,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) } - err = glGetError(); - b3Assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); { B3_PROFILE("glUseProgram(0);"); glUseProgram(0); @@ -1871,9 +1830,7 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - + b3Assert(glGetError() ==GL_NO_ERROR); } diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.h b/btgui/OpenGLWindow/GLInstancingRenderer.h index f5faeea03..4c8ac49af 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.h +++ b/btgui/OpenGLWindow/GLInstancingRenderer.h @@ -110,7 +110,7 @@ public: void drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize); void drawPoints(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, float pointDrawSize); void drawPoint(const float* position, const float color[4], float pointSize=1); - void updateCamera(); + void updateCamera(int upAxis=1); void getCameraPosition(float cameraPos[4]); void getCameraPosition(double cameraPos[4]) diff --git a/btgui/OpenGLWindow/GwenOpenGL3CoreRenderer.h b/btgui/OpenGLWindow/GwenOpenGL3CoreRenderer.h index 4dcb12d12..cb3f06e20 100644 --- a/btgui/OpenGLWindow/GwenOpenGL3CoreRenderer.h +++ b/btgui/OpenGLWindow/GwenOpenGL3CoreRenderer.h @@ -208,8 +208,8 @@ public: // //printf("str = %s\n",unicodeText); - int xpos=0; - int ypos=0; + //int xpos=0; + //int ypos=0; float dx; int measureOnly=0; @@ -232,7 +232,7 @@ public: { //float width = 0.f; int pos=0; - float color[]={0.2f,0.2,0.2f,1.f}; + //float color[]={0.2f,0.2,0.2f,1.f}; glBindTexture(GL_TEXTURE_2D,m_fontTextureId); float width = r.x; @@ -299,7 +299,7 @@ public: int pos=0; while (unicodeText[pos]) { - width += m_currentFont->m_CharWidth[unicodeText[pos]]+extraSpacing; + width += m_currentFont->m_CharWidth[(int)unicodeText[pos]]+extraSpacing; pos++; } Gwen::Point pt; @@ -318,4 +318,4 @@ public: }; -#endif //__GWEN_OPENGL3_CORE_RENDERER_H \ No newline at end of file +#endif //__GWEN_OPENGL3_CORE_RENDERER_H diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl index 7d175fb57..fe16aaf4c 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl @@ -15,6 +15,7 @@ uniform mat4 ModelViewMatrix; uniform mat4 ProjectionMatrix; uniform mat4 DepthBiasModelViewProjectionMatrix; uniform mat4 MVP; +uniform vec3 lightDirIn; out vec4 ShadowCoord; @@ -65,13 +66,12 @@ void main(void) { vec4 q = instance_quaternion; ambient = vec3(0.3,.3,0.3); - - + vec4 worldNormal = (quatRotate3( vertexnormal,q)); - vec3 light_pos = vec3(-5.f,100,-40); + normal = normalize(worldNormal).xyz; - lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz)); + lightDir = lightDirIn; vec4 axis = vec4(1,1,1,0); vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h index f8ed25a2d..8c2ee8765 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h @@ -13,6 +13,7 @@ static const char* useShadowMapInstancingVertexShader= \ "uniform mat4 ProjectionMatrix;\n" "uniform mat4 DepthBiasModelViewProjectionMatrix;\n" "uniform mat4 MVP;\n" +"uniform vec3 lightDirIn;\n" "out vec4 ShadowCoord;\n" "out Fragment\n" "{\n" @@ -55,12 +56,11 @@ static const char* useShadowMapInstancingVertexShader= \ "{\n" " vec4 q = instance_quaternion;\n" " ambient = vec3(0.3,.3,0.3);\n" -" \n" -" \n" +" \n" " vec4 worldNormal = (quatRotate3( vertexnormal,q));\n" -" vec3 light_pos = vec3(-5.f,100,-40);\n" +" \n" " normal = normalize(worldNormal).xyz;\n" -" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n" +" lightDir = lightDirIn;\n" " \n" " vec4 axis = vec4(1,1,1,0);\n" " vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" diff --git a/btgui/OpenGLWindow/ShapeData.h b/btgui/OpenGLWindow/ShapeData.h index 1325fbf2b..337345e6b 100644 --- a/btgui/OpenGLWindow/ShapeData.h +++ b/btgui/OpenGLWindow/ShapeData.h @@ -1,6 +1,7 @@ #ifndef SHAPE_DATA_H #define SHAPE_DATA_H +#if USE_BARREL_VERTICES static float barrel_vertices[] = { 0.0f,-0.5f,0.0f, 1.0f, 0.0f,-1.0f,0.0f, 0.5f, 0.5f, 0.282362f,-0.5f,-0.205148f, 1.0f, 0.0f,-1.0f,0.0f, 0.5f, 0.5f, @@ -125,7 +126,7 @@ static int barrel_indices[] = { 44,47,56, 44,26,47, }; - +#endif //USE_BARREL_VERTICES ///position xyz, unused w, normal, uv static const float cube_vertices[] = diff --git a/btgui/OpenGLWindow/SimpleOpenGL3App.cpp b/btgui/OpenGLWindow/SimpleOpenGL3App.cpp index 2be04937b..ace8275fb 100644 --- a/btgui/OpenGLWindow/SimpleOpenGL3App.cpp +++ b/btgui/OpenGLWindow/SimpleOpenGL3App.cpp @@ -4,7 +4,8 @@ #include "OpenGLWindow/MacOpenGLWindow.h" #else -#include "GL/glew.h" +#include "OpenGLWindow/GlewWindows/GL/glew.h" +//#include "GL/glew.h" #ifdef _WIN32 #include "OpenGLWindow/Win32OpenGLWindow.h" #else @@ -19,8 +20,8 @@ #include "Bullet3Common/b3Vector3.h" #include "Bullet3Common/b3Logging.h" -#include "../btgui/OpenGLTrueTypeFont/fontstash.h" -#include "../btgui/OpenGLWindow/TwFonts.h" +#include "OpenGLTrueTypeFont/fontstash.h" +#include "OpenGLWindow/TwFonts.h" #include "OpenGLTrueTypeFont/opengl_fontstashcallbacks.h" #include @@ -78,13 +79,11 @@ SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height) m_window->setWindowTitle(title); - GLuint err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); glClearColor(1,1,1,1); m_window->startRendering(); - err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); #ifndef __APPLE__ #ifndef _WIN32 @@ -92,28 +91,26 @@ SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height) glewExperimental = GL_TRUE; #endif - err = glewInit(); - if (err != GLEW_OK) + + if (glewInit() != GLEW_OK) exit(1); // or handle the error in a nicer way if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API. exit(1); // or handle the error in a nicer way #endif - err = glGetError(); - err = glGetError(); - assert(err==GL_NO_ERROR); + glGetError();//don't remove this call, it is needed for Ubuntu + + b3Assert(glGetError() ==GL_NO_ERROR); m_primRenderer = new GLPrimitiveRenderer(width,height); - err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); m_instancingRenderer = new GLInstancingRenderer(128*1024,4*1024*1024); m_instancingRenderer->init(); m_instancingRenderer->resize(width,height); - err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); m_instancingRenderer->InitShaders(); @@ -128,23 +125,19 @@ SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height) { - GLint err; + - int datasize; - - float sx,sy,dx,dy,lh; - GLuint texture; + m_data->m_renderCallbacks = new OpenGL2RenderCallbacks(m_primRenderer); m_data->m_fontStash = sth_create(512,512,m_data->m_renderCallbacks);//256,256);//,1024);//512,512); - err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); if (!m_data->m_fontStash) { b3Warning("Could not create stash"); //fprintf(stderr, "Could not create stash.\n"); } - int droidRegular=0; + char* data2 = OpenSansData; unsigned char* data = (unsigned char*) data2; @@ -152,8 +145,7 @@ SimpleOpenGL3App::SimpleOpenGL3App( const char* title, int width,int height) { b3Warning("error!\n"); } - err = glGetError(); - assert(err==GL_NO_ERROR); + b3Assert(glGetError() ==GL_NO_ERROR); } } @@ -176,7 +168,7 @@ void SimpleOpenGL3App::drawText( const char* txt, int posX, int posY) float dx=0; - int measureOnly=0; + //int measureOnly=0; glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -196,12 +188,12 @@ void SimpleOpenGL3App::drawText( const char* txt, int posX, int posY) { //float width = 0.f; int pos=0; - float color[]={0.2f,0.2,0.2f,1.f}; + //float color[]={0.2f,0.2,0.2f,1.f}; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,m_data->m_fontTextureId); //float width = r.x; - float extraSpacing = 0.; + //float extraSpacing = 0.; int startX = posX; int startY = posY; @@ -310,24 +302,75 @@ int SimpleOpenGL3App::registerGraphicsSphereShape(float radius, bool usePointSpr return graphicsShapeIndex; } -void SimpleOpenGL3App::drawGrid(int gridSize, float yOffset) +void SimpleOpenGL3App::drawGrid(int gridSize, float upOffset, int upAxis) { - + int sideAxis=-1; + int forwardAxis=-1; + + switch (upAxis) + { + case 1: + forwardAxis=2; + sideAxis=0; + break; + case 2: + forwardAxis=1; + sideAxis=0; + break; + default: + b3Assert(0); + }; b3Vector3 gridColor = b3MakeVector3(0.5,0.5,0.5); + + b3AlignedObjectArray indices; + b3AlignedObjectArray vertices; + int lineIndex=0; for(int i=-gridSize;i<=gridSize;i++) { + { + b3Assert(glGetError() ==GL_NO_ERROR); + b3Vector3 from = b3MakeVector3(0,0,0); + from[sideAxis] = float(i); + from[upAxis] = upOffset; + from[forwardAxis] = float(-gridSize); + b3Vector3 to=b3MakeVector3(0,0,0); + to[sideAxis] = float(i); + to[upAxis] = upOffset; + to[forwardAxis] = float(gridSize); + vertices.push_back(from); + indices.push_back(lineIndex++); + vertices.push_back(to); + indices.push_back(lineIndex++); + m_instancingRenderer->drawLine(from,to,gridColor); + } - GLint err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - m_instancingRenderer->drawLine(b3MakeVector3(float(i),yOffset,float(-gridSize)),b3MakeVector3(float(i),yOffset,float(gridSize)),gridColor); - - err = glGetError(); - b3Assert(err==GL_NO_ERROR); - - m_instancingRenderer->drawLine(b3MakeVector3(float(-gridSize),yOffset,float(i)),b3MakeVector3(float(gridSize),yOffset,float(i)),gridColor); + b3Assert(glGetError() ==GL_NO_ERROR); + { + + b3Assert(glGetError() ==GL_NO_ERROR); + b3Vector3 from=b3MakeVector3(0,0,0); + from[sideAxis] = float(-gridSize); + from[upAxis] = upOffset; + from[forwardAxis] = float(i); + b3Vector3 to=b3MakeVector3(0,0,0); + to[sideAxis] = float(gridSize); + to[upAxis] = upOffset; + to[forwardAxis] = float(i); + vertices.push_back(from); + indices.push_back(lineIndex++); + vertices.push_back(to); + indices.push_back(lineIndex++); + m_instancingRenderer->drawLine(from,to,gridColor); + } + } + + /*m_instancingRenderer->drawLines(&vertices[0].x, + gridColor, + vertices.size(),sizeof(b3Vector3),&indices[0],indices.size(),1); + */ + m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(1,0,0),b3MakeVector3(1,0,0),3); m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,1,0),b3MakeVector3(0,1,0),3); m_instancingRenderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,0,1),b3MakeVector3(0,0,1),3); diff --git a/btgui/OpenGLWindow/SimpleOpenGL3App.h b/btgui/OpenGLWindow/SimpleOpenGL3App.h index 7bad61d70..4135dfa38 100644 --- a/btgui/OpenGLWindow/SimpleOpenGL3App.h +++ b/btgui/OpenGLWindow/SimpleOpenGL3App.h @@ -19,7 +19,7 @@ struct SimpleOpenGL3App int registerCubeShape(float halfExtentsX=1.f,float halfExtentsY=1.f, float halfExtentsZ = 1.f); int registerGraphicsSphereShape(float radius, bool usePointSprites=true, int largeSphereThreshold=100, int mediumSphereThreshold=10); - void drawGrid(int gridSize=10, float yOffset=0.001); + void drawGrid(int gridSize=10, float upOffset=0.001, int upAxis=1); void swapBuffer(); void drawText( const char* txt, int posX, int posY); struct sth_stash* getFontStash(); diff --git a/btgui/OpenGLWindow/X11OpenGLWindow.cpp b/btgui/OpenGLWindow/X11OpenGLWindow.cpp index 78e14c717..64211f8de 100644 --- a/btgui/OpenGLWindow/X11OpenGLWindow.cpp +++ b/btgui/OpenGLWindow/X11OpenGLWindow.cpp @@ -530,9 +530,9 @@ if (res==0) // " SAMPLES = %d\n", // i, vi -> visualid, samp_buf, samples ); - if ( best_fbc < 0 || samp_buf && samples > best_num_samp ) + if ( best_fbc < 0 || (samp_buf && (samples > best_num_samp)) ) best_fbc = i, best_num_samp = samples; - if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp ) + if ( worst_fbc < 0 || (!samp_buf || (samples < worst_num_samp)) ) worst_fbc = i, worst_num_samp = samples; } MyXFree( vi ); @@ -652,9 +652,9 @@ int X11OpenGLWindow::getAsciiCodeFromVirtualKeycode(int keycode) case XK_F14: return B3G_F14; case XK_F15: return B3G_F15; default: - // Make uppercase + // Make lowercase MyXConvertCase( key, &key_lc, &key_uc ); - key = key_uc; + key = key_lc; // Valid ISO 8859-1 character? if( (key >= 32 && key <= 126) ||(key >= 160 && key <= 255) ) { @@ -698,7 +698,7 @@ void X11OpenGLWindow::pumpMessage() if (m_data->m_keyboardCallback) { - +#if 0 unsigned short is_retriggered = 0; ///filter out keyboard repeat //see http://stackoverflow.com/questions/2100654/ignore-auto-repeat-in-x11-applications @@ -718,6 +718,7 @@ void X11OpenGLWindow::pumpMessage() is_retriggered = 1; } } +#endif int keycode = getAsciiCodeFromVirtualKeycode( m_data->m_xev.xkey.keycode); int state = 0; (*m_data->m_keyboardCallback)(keycode,state); diff --git a/btgui/OpenGLWindow/premake4.lua b/btgui/OpenGLWindow/premake4.lua index e98cb5105..100379855 100644 --- a/btgui/OpenGLWindow/premake4.lua +++ b/btgui/OpenGLWindow/premake4.lua @@ -4,7 +4,6 @@ language "C++" kind "StaticLib" - targetdir "../../bin" initOpenGL() initGlew()