From bae303c5da3fcfd35677da7759515f43541e2fff Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Thu, 3 Jul 2014 16:42:50 -0700 Subject: [PATCH] add preliminary glut replacement test for Bullet 2.x demos --- Demos/OpenGL/DemoApplication.cpp | 3 + Demos/OpenGL/DemoApplication.h | 6 + Demos/OpenGL/GLDebugFont.cpp | 15 +- Demos/OpenGL/GlutStuff.cpp | 5 +- Demos/OpenGL/GlutStuff.h | 81 ++++--- Demos/OpenGL/RenderTexture.h | 5 + Demos3/BasicDemoConsole/main.cpp | 14 ++ Demos3/BasicDemoConsole/premake4.lua | 26 +++ Demos3/BasicDemoCustomOpenGL2/main.cpp | 255 +++++++++++++++++++++ Demos3/BasicDemoCustomOpenGL2/premake4.lua | 34 +++ btgui/OpenGLWindow/Win32Window.cpp | 4 +- build3/premake4.lua | 5 + 12 files changed, 416 insertions(+), 37 deletions(-) create mode 100644 Demos3/BasicDemoConsole/main.cpp create mode 100644 Demos3/BasicDemoConsole/premake4.lua create mode 100644 Demos3/BasicDemoCustomOpenGL2/main.cpp create mode 100644 Demos3/BasicDemoCustomOpenGL2/premake4.lua diff --git a/Demos/OpenGL/DemoApplication.cpp b/Demos/OpenGL/DemoApplication.cpp index e373a08e3..a231c8789 100644 --- a/Demos/OpenGL/DemoApplication.cpp +++ b/Demos/OpenGL/DemoApplication.cpp @@ -14,6 +14,7 @@ subject to the following restrictions: */ + #include "DemoApplication.h" #include "LinearMath/btIDebugDraw.h" #include "BulletDynamics/Dynamics/btDynamicsWorld.h" @@ -112,6 +113,8 @@ DemoApplication::~DemoApplication() if (m_shapeDrawer) delete m_shapeDrawer; + + //GLDebugResetFont(0,0); } diff --git a/Demos/OpenGL/DemoApplication.h b/Demos/OpenGL/DemoApplication.h index a86ff8e79..acbcc80e7 100644 --- a/Demos/OpenGL/DemoApplication.h +++ b/Demos/OpenGL/DemoApplication.h @@ -112,6 +112,12 @@ public: virtual ~DemoApplication(); + void setDynamicsWorld(btDynamicsWorld* world) + { + m_dynamicsWorld=world; + } + + btDynamicsWorld* getDynamicsWorld() { return m_dynamicsWorld; diff --git a/Demos/OpenGL/GLDebugFont.cpp b/Demos/OpenGL/GLDebugFont.cpp index 616006810..68ee650e9 100644 --- a/Demos/OpenGL/GLDebugFont.cpp +++ b/Demos/OpenGL/GLDebugFont.cpp @@ -41,7 +41,7 @@ subject to the following restrictions: #include #else #include -#include +#include #endif #endif @@ -59,6 +59,19 @@ static int sScreenHeight = -1; void GLDebugResetFont(int screenWidth,int screenHeight) { + if (screenWidth==0 && screenHeight==0) + { + sScreenWidth = screenWidth; + sScreenHeight = screenHeight; + + if (sTexturesInitialized) + { + glDeleteTextures(1, &sTexture); + glBindTexture(GL_TEXTURE_2D,0); + sTexturesInitialized = false; + } + return; + } if ((sScreenWidth == screenWidth) && (sScreenHeight == screenHeight)) return; diff --git a/Demos/OpenGL/GlutStuff.cpp b/Demos/OpenGL/GlutStuff.cpp index 95658f1f2..b9d5496b9 100644 --- a/Demos/OpenGL/GlutStuff.cpp +++ b/Demos/OpenGL/GlutStuff.cpp @@ -13,7 +13,7 @@ subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ -#ifndef _WINDOWS +#ifndef DONT_USE_GLUT #include "DemoApplication.h" @@ -71,6 +71,7 @@ static void glutDisplayCallback(void) gDemoApplication->displayCallback(); } +#include int glutmain(int argc, char **argv,int width,int height,const char* title,DemoApplication* demoApp) { @@ -117,4 +118,4 @@ CGLSetParameter(cgl_context, kCGLCPSwapInterval, &swap_interval); } -#endif //_WINDOWS +#endif //DONT_USE_GLUT diff --git a/Demos/OpenGL/GlutStuff.h b/Demos/OpenGL/GlutStuff.h index 76435ceeb..49631b17f 100644 --- a/Demos/OpenGL/GlutStuff.h +++ b/Demos/OpenGL/GlutStuff.h @@ -15,51 +15,68 @@ subject to the following restrictions: #ifndef GLUT_STUFF_H #define GLUT_STUFF_H +#ifdef DONT_USE_GLUT #ifdef _WIN32//for glut.h #include #endif +#endif //DONT_USE_GLUT //think different #if defined(__APPLE__) && !defined (VMDMESA) #include #include #include -#include -#else +#ifndef DONT_USE_GLUT + #include +#endif//DONT_USE_GLUT +#else//(__APPLE__) && !defined (VMDMESA) + #ifdef _WINDOWS -#include -#include -#include -#else -#include -#include -#endif //_WINDOWS -#endif //APPLE + #include + #include + #include + #define BT_ACTIVE_ALT VK_LMENU + #define BT_ACTIVE_SHIFT VK_LSHIFT + #define BT_ACTIVE_CTRL VK_LCONTROL +#else //_WINDOWS + +#ifdef DONT_USE_GLUT + #include + #include + #define BT_ACTIVE_ALT 8192 + #define BT_ACTIVE_SHIFT 8193 + #define BT_ACTIVE_CTRL 8194 +#else//DONT_USE_GLUT + #ifdef _WIN32 + #include + #endif//_WIN32 + #include + #include + #define BT_KEY_K 'k' + #define BT_KEY_LEFT GLUT_KEY_LEFT + #define BT_KEY_RIGHT GLUT_KEY_RIGHT + #define BT_KEY_UP GLUT_KEY_UP + #define BT_KEY_DOWN GLUT_KEY_DOWN + #define BT_KEY_F1 GLUT_KEY_F1 + #define BT_KEY_F2 GLUT_KEY_F2 + #define BT_KEY_F3 GLUT_KEY_F3 + #define BT_KEY_F4 GLUT_KEY_F4 + #define BT_KEY_F5 GLUT_KEY_F5 + #define BT_KEY_PAGEUP GLUT_KEY_PAGE_UP + #define BT_KEY_PAGEDOWN GLUT_KEY_PAGE_DOWN + #define BT_KEY_END GLUT_KEY_END + #define BT_KEY_HOME GLUT_KEY_HOME + #define BT_ACTIVE_ALT GLUT_ACTIVE_ALT + #define BT_ACTIVE_CTRL GLUT_ACTIVE_ALT + #define BT_ACTIVE_SHIFT GLUT_ACTIVE_SHIFT + +#endif//DONT_USE_GLUT +#endif//_WINDOWS +#endif //(__APPLE__) && !defined (VMDMESA) + -#ifdef _WINDOWS -#define BT_ACTIVE_ALT VK_LMENU -#define BT_ACTIVE_SHIFT VK_LSHIFT -#else -#define BT_KEY_K 'k' -#define BT_KEY_LEFT GLUT_KEY_LEFT -#define BT_KEY_RIGHT GLUT_KEY_RIGHT -#define BT_KEY_UP GLUT_KEY_UP -#define BT_KEY_DOWN GLUT_KEY_DOWN -#define BT_KEY_F1 GLUT_KEY_F1 -#define BT_KEY_F2 GLUT_KEY_F2 -#define BT_KEY_F3 GLUT_KEY_F3 -#define BT_KEY_F4 GLUT_KEY_F4 -#define BT_KEY_F5 GLUT_KEY_F5 -#define BT_KEY_PAGEUP GLUT_KEY_PAGE_UP -#define BT_KEY_PAGEDOWN GLUT_KEY_PAGE_DOWN -#define BT_KEY_END GLUT_KEY_END -#define BT_KEY_HOME GLUT_KEY_HOME -#define BT_ACTIVE_ALT GLUT_ACTIVE_ALT -#define BT_ACTIVE_CTRL GLUT_ACTIVE_ALT -#define BT_ACTIVE_SHIFT GLUT_ACTIVE_SHIFT -#endif #if BT_USE_FREEGLUT #include "GL/freeglut_ext.h" //to be able to return from glutMainLoop() diff --git a/Demos/OpenGL/RenderTexture.h b/Demos/OpenGL/RenderTexture.h index 1aee51d79..1153018a1 100644 --- a/Demos/OpenGL/RenderTexture.h +++ b/Demos/OpenGL/RenderTexture.h @@ -36,6 +36,11 @@ public: ///rgba input is in range [0..1] for each component inline void setPixel(int x,int y,const btVector4& rgba) { + btAssert(x>=0); + btAssert(y>=0); + btAssert(x +#include "../../Demos/BasicDemo/BasicDemoPhysicsSetup.h" + +int main(int argc, char* argv[]) +{ + BasicDemoPhysicsSetup physicsSetup; + GraphicsPhysicsBridge br; + physicsSetup.initPhysics(br); + physicsSetup.stepSimulation(1./60.); + physicsSetup.exitPhysics(); + + printf("hello\n"); +} \ No newline at end of file diff --git a/Demos3/BasicDemoConsole/premake4.lua b/Demos3/BasicDemoConsole/premake4.lua new file mode 100644 index 000000000..330128b24 --- /dev/null +++ b/Demos3/BasicDemoConsole/premake4.lua @@ -0,0 +1,26 @@ +project "App_BasicDemoConsole" + + kind "ConsoleApp" + targetdir "../../bin" + + includedirs {"../../src"} + configuration { "Windows" } +-- links { "opengl32","glu32","gdi32","winmm", "user32" } + files { "../../build3/bullet.rc" } +-- configuration {"MacOSX"} + --print "hello" + -- linkoptions { "-framework Carbon -framework OpenGL -framework AGL -framework Glut" } +-- configuration {"not Windows", "not MacOSX"} +-- links {"GL","GLU","glut"} + configuration{} + + links { + "BulletDynamics", "BulletCollision", "LinearMath" + } + + files { + "*.cpp" , + "*.h", + "../../Demos/BasicDemo/BasicDemoPhysicsSetup.cpp", + "../../Demos/BasicDemo/BasicDemoPhysicsSetup.h" + } diff --git a/Demos3/BasicDemoCustomOpenGL2/main.cpp b/Demos3/BasicDemoCustomOpenGL2/main.cpp new file mode 100644 index 000000000..aabb85135 --- /dev/null +++ b/Demos3/BasicDemoCustomOpenGL2/main.cpp @@ -0,0 +1,255 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2014 Erwin Coumans http://bulletphysics.org + +//This file is Copyright (c) 2014 Google Inc. + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + +///This main.cpp replaces glut by Bullet 3 own platform management for OpenGL and input devices + +#include +#include "../../Demos/BasicDemo/BasicDemoPhysicsSetup.h" + + +#ifdef __APPLE__ +#include "OpenGLWindow/MacOpenGLWindow.h" +#else + +#include "GL/glew.h" +#ifdef _WIN32 +#include "OpenGLWindow/Win32OpenGLWindow.h" +#else +//let's cross the fingers it is Linux/X11 +#include "OpenGLWindow/X11OpenGLWindow.h" +#endif //_WIN32 +#endif//__APPLE__ + +#include "../../Demos/OpenGL/DemoApplication.h" +#include "../../Demos/OpenGL/GLDebugDrawer.h" + +int sWidth = 800; +int sHeight = 600; +DemoApplication* gApp = 0; +bool isShiftPressed = false; +bool isCtrlPressed = false; +bool isAltPressed = false; + +/* +todo: add wheel callback +typedef void (*b3WheelCallback)(float deltax, float deltay); +*/ + +void MyKeyboardCallback(int orgKeycode, int state) +{ + int keycode = orgKeycode; + + if (gApp) + { + + switch (orgKeycode) + { + case B3G_SHIFT: + { + isShiftPressed = (state==1); + break; + } + case B3G_ALT: + { + isAltPressed = (state==1); + break; + } + case B3G_CONTROL: + { + isCtrlPressed = (state==1); + break; + } + default: + { + } + }; + + if (state) + { + gApp->keyboardCallback(keycode,0,0); + } else + { + gApp->keyboardUpCallback(keycode,0,0); + } + } +} +void MyMouseMoveCallback( float x, float y) +{ + if (gApp) + { + //printf("mouseMotionFunc %f,%f\n",x,y); + gApp->mouseMotionFunc(x,y); + } +} + +void MyMouseButtonCallback(int button, int state, float x, float y) +{ + if (gApp) + { + //printf("mouseFunc %d,%d, %f,%f\n",button,state,x,y); + gApp->mouseFunc(button,1-state,x,y); + } +} + +void MyResizeCallback(float width, float height) +{ + sWidth = width; + sHeight = height; + if (gApp) + { + gApp->reshape(width,height); + } +} + +int main(int argc, char* argv[]) +{ + + b3gDefaultOpenGLWindow* window = new b3gDefaultOpenGLWindow(); + //window->setKeyboardCallback(keyCallback); + b3gWindowConstructionInfo wci; + wci.m_openglVersion = 2; + wci.m_width = sWidth; + wci.m_height = sHeight; + // wci.m_resizeCallback = MyResizeCallback; + + window->createWindow(wci); + window->setResizeCallback(MyResizeCallback); + window->setMouseButtonCallback(MyMouseButtonCallback); + + window->setMouseMoveCallback(MyMouseMoveCallback); + window->setKeyboardCallback(MyKeyboardCallback); + + //window->setWindowTitle("render test"); + + int majorGlVersion, minorGlVersion; + + if (!sscanf((const char*)glGetString(GL_VERSION), "%d.%d", &majorGlVersion, &minorGlVersion)==2) + { + printf("Exit: Error cannot extract OpenGL version from GL_VERSION string\n"); + exit(0); + } + if (majorGlVersion>=3 && wci.m_openglVersion>=3) + { + float retinaScale = 1.f; + +#ifndef __APPLE__ +#ifndef _WIN32 + //we need glewExperimental on Linux + glewExperimental = GL_TRUE; +#endif // _WIN32 + glewInit(); +#endif + + //we ned to call glGetError twice, because of some Ubuntu/Intel/OpenGL issue + + GLuint err = glGetError(); + err = glGetError(); + btAssert(err==GL_NO_ERROR); + + + retinaScale = window->getRetinaScale(); + + //primRenderer = new GLPrimitiveRenderer(sWidth,sHeight); + //sth_stash* font = initFont(primRenderer ); + //gwenRenderer = new GwenOpenGL3CoreRenderer(primRenderer,font,sWidth,sHeight,retinaScale); + + } else + { + //OpenGL 2.x + /*gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont(); + skin.SetRender( gwenRenderer ); + + pCanvas = new Gwen::Controls::Canvas( &skin ); + pCanvas->SetSize( sWidth, sHeight); + pCanvas->SetDrawBackground( true ); + pCanvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) ); + */ + + } + + +// glClearColor(0.2,0.2,0.2,1); + + + + BasicDemoPhysicsSetup physicsSetup; + GraphicsPhysicsBridge br; + physicsSetup.initPhysics(br); + + + struct MyAppie : public DemoApplication + { + virtual void initPhysics() + { + } + virtual void clientMoveAndDisplay() + { + } + virtual void swapBuffers() + { + } + virtual void updateModifierKeys() + { + m_modifierKeys = 0; + if (isAltPressed) + m_modifierKeys |= BT_ACTIVE_ALT; + + if (isCtrlPressed) + m_modifierKeys |= BT_ACTIVE_CTRL; + + if (isShiftPressed) + m_modifierKeys |= BT_ACTIVE_SHIFT; + + } + + }; + + { + MyAppie appie; + appie.setDynamicsWorld(physicsSetup.m_dynamicsWorld); + appie.reshape(sWidth,sHeight); + appie.setShadows(true); + gApp = &appie; + GLDebugDrawer draw; + physicsSetup.m_dynamicsWorld->setDebugDrawer(&draw); + btClock timer; + unsigned long prevTime = timer.getTimeMicroseconds(); + + do + { + unsigned long curTime = timer.getTimeMicroseconds(); + if (!appie.isIdle()) + { + physicsSetup.stepSimulation((curTime-prevTime)*(1./1000000.)); + } + prevTime = curTime; + window->startRendering(); + br.syncPhysicsToGraphics(physicsSetup.m_dynamicsWorld); + appie.renderme(); + physicsSetup.m_dynamicsWorld->debugDrawWorld(); + window->endRendering(); + + } while (!window->requestedExit()); + } + window->closeWindow(); + delete window; + + physicsSetup.exitPhysics(); + + + printf("hello\n"); +} \ No newline at end of file diff --git a/Demos3/BasicDemoCustomOpenGL2/premake4.lua b/Demos3/BasicDemoCustomOpenGL2/premake4.lua new file mode 100644 index 000000000..c2cd23b10 --- /dev/null +++ b/Demos3/BasicDemoCustomOpenGL2/premake4.lua @@ -0,0 +1,34 @@ +project "App_BasicDemoCustomOpenGL2" + + kind "ConsoleApp" + targetdir "../../bin" + + includedirs { + "../../src", + "../../btgui" + } + + defines { "DONT_USE_GLUT"} + + initOpenGL() + initGlew() + + links { + "OpenGL_Window","BulletDynamics", "BulletCollision", "LinearMath" + } + + files { + "*.cpp" , + "*.h", + "../../Demos/BasicDemo/BasicDemoPhysicsSetup.cpp", + "../../Demos/BasicDemo/BasicDemoPhysicsSetup.h", + "../../Demos/OpenGL/DemoApplication.cpp", + "../../Demos/OpenGL/DemoApplication.h", + "../../Demos/OpenGL/GL_ShapeDrawer.cpp", + "../../Demos/OpenGL/GL_ShapeDrawer.h", + "../../Demos/OpenGL/GL_ShapeDrawer.cpp", + "../../Demos/OpenGL/GLDebugDrawer.cpp", + "../../Demos/OpenGL/GLDebugDrawer.h", + "../../Demos/OpenGL/GLDebugFont.cpp", + "../../Demos/OpenGL/GLDebugFont.h" + } diff --git a/btgui/OpenGLWindow/Win32Window.cpp b/btgui/OpenGLWindow/Win32Window.cpp index 1d94ae3c4..19615e18c 100644 --- a/btgui/OpenGLWindow/Win32Window.cpp +++ b/btgui/OpenGLWindow/Win32Window.cpp @@ -65,7 +65,7 @@ int getAsciiCodeFromVirtualKeycode(int virtualKeyCode) } if (virtualKeyCode >= 'A' && virtualKeyCode <= 'Z') { - return virtualKeyCode; + return virtualKeyCode+32;//todo: fix the ascii A vs a input } switch (virtualKeyCode) { @@ -170,7 +170,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int keycode = getAsciiCodeFromVirtualKeycode(wParam); - if (keycode>=0 && sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0)) + if (keycode>=0 && sData && sData->m_keyboardCallback)// && ((HIWORD(lParam) & KF_REPEAT) == 0)) { int state = 1; (*sData->m_keyboardCallback)(keycode,state); diff --git a/build3/premake4.lua b/build3/premake4.lua index 6b679d976..d6a880a22 100644 --- a/build3/premake4.lua +++ b/build3/premake4.lua @@ -104,6 +104,11 @@ if findOpenGL() then include "../Demos3/AllBullet2Demos" include "../Demos3/GpuDemos" + --include"../Demos3/BasicDemoConsole" + --include"../Demos3/BasicDemoCustomOpenGL2" + + + -- include "../Demos3/CpuDemos" -- include "../Demos3/Wavefront" -- include "../btgui/MultiThreading"