SimpleOpenGL2App.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "SimpleOpenGL2App.h"
  2. #define USE_OPENGL2
  3. #include "OpenGLInclude.h"
  4. #include "Bullet3Common/b3Logging.h"//b3Assert?
  5. #include "Bullet3Common/b3Scalar.h"
  6. #include "Bullet3Common/b3AlignedObjectArray.h"
  7. #include "Bullet3Common/b3Vector3.h"
  8. #include "../CommonInterfaces/CommonRenderInterface.h"
  9. #include "../OpenGLWindow/GLPrimitiveRenderer.h"
  10. #include "stdlib.h"
  11. #include "TwFonts.h"
  12. #ifdef __APPLE__
  13. #include "MacOpenGLWindow.h"
  14. #else
  15. //#include "GL/glew.h"
  16. #ifdef _WIN32
  17. #include "Win32OpenGLWindow.h"
  18. #else
  19. //let's cross the fingers it is Linux/X11
  20. #include "X11OpenGLWindow.h"
  21. #endif //_WIN32
  22. #endif//__APPLE__
  23. #include <stdio.h>
  24. #include "../CommonInterfaces/CommonRenderInterface.h"
  25. static SimpleOpenGL2App* gApp2=0;
  26. static void Simple2ResizeCallback( float widthf, float heightf)
  27. {
  28. int width = (int)widthf;
  29. int height = (int)heightf;
  30. if (gApp2->m_renderer)
  31. gApp2->m_renderer->resize(width,height);
  32. //gApp2->m_renderer->setScreenSize(width,height);
  33. }
  34. static void Simple2KeyboardCallback(int key, int state)
  35. {
  36. if (key==B3G_ESCAPE && gApp2 && gApp2->m_window)
  37. {
  38. gApp2->m_window->setRequestExit();
  39. } else
  40. {
  41. //gApp2->defaultKeyboardCallback(key,state);
  42. }
  43. }
  44. void Simple2MouseButtonCallback( int button, int state, float x, float y)
  45. {
  46. gApp2->defaultMouseButtonCallback(button,state,x,y);
  47. }
  48. void Simple2MouseMoveCallback( float x, float y)
  49. {
  50. gApp2->defaultMouseMoveCallback(x,y);
  51. }
  52. void Simple2WheelCallback( float deltax, float deltay)
  53. {
  54. gApp2->defaultWheelCallback(deltax,deltay);
  55. }
  56. struct SimpleOpenGL2AppInternalData
  57. {
  58. GLuint m_fontTextureId;
  59. GLuint m_largeFontTextureId;
  60. };
  61. static GLuint BindFont2(const CTexFont *_Font)
  62. {
  63. GLuint TexID = 0;
  64. glGenTextures(1, &TexID);
  65. glBindTexture(GL_TEXTURE_2D, TexID);
  66. glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  67. glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  68. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  69. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  70. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  71. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  72. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE , _Font->m_TexWidth, _Font->m_TexHeight, 0, GL_LUMINANCE , GL_UNSIGNED_BYTE, _Font->m_TexBytes);
  73. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  74. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  75. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  76. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  77. glBindTexture(GL_TEXTURE_2D, 0);
  78. return TexID;
  79. }
  80. SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
  81. {
  82. gApp2 = this;
  83. m_data = new SimpleOpenGL2AppInternalData;
  84. m_window = new b3gDefaultOpenGLWindow();
  85. b3gWindowConstructionInfo ci;
  86. ci.m_title = title;
  87. ci.m_openglVersion = 2;
  88. ci.m_width = width;
  89. ci.m_height = height;
  90. m_window->createWindow(ci);
  91. m_window->setWindowTitle(title);
  92. #ifndef __APPLE__
  93. #ifndef _WIN32
  94. //some Linux implementations need the 'glewExperimental' to be true
  95. glewExperimental = GL_TRUE;
  96. #endif
  97. if (glewInit() != GLEW_OK)
  98. {
  99. b3Error("glewInit failed");
  100. exit(1);
  101. }
  102. if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API.
  103. {
  104. b3Error("GLEW_VERSION_2_1 needs to support 2_1");
  105. exit(1); // or handle the error in a nicer way
  106. }
  107. #endif
  108. TwGenerateDefaultFonts();
  109. m_data->m_fontTextureId = BindFont2(g_DefaultNormalFont);
  110. m_data->m_largeFontTextureId = BindFont2(g_DefaultLargeFont);
  111. glGetError();//don't remove this call, it is needed for Ubuntu
  112. glClearColor( m_backgroundColorRGB[0],
  113. m_backgroundColorRGB[1],
  114. m_backgroundColorRGB[2],
  115. 1.f);
  116. b3Assert(glGetError() ==GL_NO_ERROR);
  117. //m_primRenderer = new GLPrimitiveRenderer(width,height);
  118. m_parameterInterface = 0;
  119. b3Assert(glGetError() ==GL_NO_ERROR);
  120. //m_instancingRenderer = new GLInstancingRenderer(128*1024,32*1024*1024);
  121. //m_instancingRenderer->init();
  122. //m_instancingRenderer->resize(width,height);
  123. b3Assert(glGetError() ==GL_NO_ERROR);
  124. //m_instancingRenderer->InitShaders();
  125. m_window->setMouseMoveCallback(Simple2MouseMoveCallback);
  126. m_window->setMouseButtonCallback(Simple2MouseButtonCallback);
  127. m_window->setKeyboardCallback(Simple2KeyboardCallback);
  128. m_window->setWheelCallback(Simple2WheelCallback);
  129. m_window->setResizeCallback(Simple2ResizeCallback);
  130. }
  131. SimpleOpenGL2App::~SimpleOpenGL2App()
  132. {
  133. gApp2 = 0;
  134. delete m_data;
  135. }
  136. void SimpleOpenGL2App::setBackgroundColor(float red, float green, float blue)
  137. {
  138. CommonGraphicsApp::setBackgroundColor(red,green,blue);
  139. glClearColor(m_backgroundColorRGB[0],m_backgroundColorRGB[1],m_backgroundColorRGB[2],1.f);
  140. }
  141. void SimpleOpenGL2App::drawGrid(DrawGridData data)
  142. {
  143. int gridSize = data.gridSize;
  144. float upOffset = data.upOffset;
  145. int upAxis = data.upAxis;
  146. float gridColor[4];
  147. gridColor[0] = data.gridColor[0];
  148. gridColor[1] = data.gridColor[1];
  149. gridColor[2] = data.gridColor[2];
  150. gridColor[3] = data.gridColor[3];
  151. int sideAxis=-1;
  152. int forwardAxis=-1;
  153. switch (upAxis)
  154. {
  155. case 1:
  156. forwardAxis=2;
  157. sideAxis=0;
  158. break;
  159. case 2:
  160. forwardAxis=1;
  161. sideAxis=0;
  162. break;
  163. default:
  164. b3Assert(0);
  165. };
  166. //b3Vector3 gridColor = b3MakeVector3(0.5,0.5,0.5);
  167. b3AlignedObjectArray<unsigned int> indices;
  168. b3AlignedObjectArray<b3Vector3> vertices;
  169. int lineIndex=0;
  170. for(int i=-gridSize;i<=gridSize;i++)
  171. {
  172. {
  173. b3Assert(glGetError() ==GL_NO_ERROR);
  174. b3Vector3 from = b3MakeVector3(0,0,0);
  175. from[sideAxis] = float(i);
  176. from[upAxis] = upOffset;
  177. from[forwardAxis] = float(-gridSize);
  178. b3Vector3 to=b3MakeVector3(0,0,0);
  179. to[sideAxis] = float(i);
  180. to[upAxis] = upOffset;
  181. to[forwardAxis] = float(gridSize);
  182. vertices.push_back(from);
  183. indices.push_back(lineIndex++);
  184. vertices.push_back(to);
  185. indices.push_back(lineIndex++);
  186. // m_renderer->drawLine(from,to,gridColor);
  187. }
  188. b3Assert(glGetError() ==GL_NO_ERROR);
  189. {
  190. b3Assert(glGetError() ==GL_NO_ERROR);
  191. b3Vector3 from=b3MakeVector3(0,0,0);
  192. from[sideAxis] = float(-gridSize);
  193. from[upAxis] = upOffset;
  194. from[forwardAxis] = float(i);
  195. b3Vector3 to=b3MakeVector3(0,0,0);
  196. to[sideAxis] = float(gridSize);
  197. to[upAxis] = upOffset;
  198. to[forwardAxis] = float(i);
  199. vertices.push_back(from);
  200. indices.push_back(lineIndex++);
  201. vertices.push_back(to);
  202. indices.push_back(lineIndex++);
  203. // m_renderer->drawLine(from,to,gridColor);
  204. }
  205. }
  206. m_renderer->drawLines(&vertices[0].x,
  207. gridColor,
  208. vertices.size(),sizeof(b3Vector3),&indices[0],indices.size(),1);
  209. m_renderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(1,0,0),b3MakeVector3(1,0,0),3);
  210. m_renderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,1,0),b3MakeVector3(0,1,0),3);
  211. m_renderer->drawLine(b3MakeVector3(0,0,0),b3MakeVector3(0,0,1),b3MakeVector3(0,0,1),3);
  212. // void GLInstancingRenderer::drawPoints(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, float pointDrawSize)
  213. //we don't use drawPoints because all points would have the same color
  214. // b3Vector3 points[3] = { b3MakeVector3(1, 0, 0), b3MakeVector3(0, 1, 0), b3MakeVector3(0, 0, 1) };
  215. // m_instancingRenderer->drawPoints(&points[0].x, b3MakeVector3(1, 0, 0), 3, sizeof(b3Vector3), 6);
  216. }
  217. void SimpleOpenGL2App::setUpAxis(int axis)
  218. {
  219. }
  220. int SimpleOpenGL2App::getUpAxis() const
  221. {
  222. return 1;
  223. }
  224. void SimpleOpenGL2App::swapBuffer()
  225. {
  226. m_window->endRendering();
  227. m_window->startRendering();
  228. }
  229. void SimpleOpenGL2App::drawText( const char* txt, int posX, int posY)
  230. {
  231. }
  232. static void restoreOpenGLState()
  233. {
  234. glPopClientAttrib();
  235. glPopAttrib();
  236. }
  237. static void saveOpenGLState(int screenWidth, int screenHeight)
  238. {
  239. glPushAttrib(GL_ALL_ATTRIB_BITS);
  240. glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
  241. glDisable(GL_TEXTURE_GEN_S);
  242. glDisable(GL_TEXTURE_GEN_T);
  243. glDisable(GL_TEXTURE_GEN_R);
  244. glDisable(GL_LINE_SMOOTH);
  245. // glDisable(GL_LINE_STIPPLE);
  246. glDisable(GL_CULL_FACE);
  247. glDisable(GL_DEPTH_TEST);
  248. glDisable(GL_LIGHTING);
  249. glEnable(GL_BLEND);
  250. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  251. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  252. glDisable(GL_TEXTURE_2D);
  253. }
  254. void SimpleOpenGL2App::drawText3D( const char* txt, float worldPosX, float worldPosY, float worldPosZ, float size1)
  255. {
  256. saveOpenGLState(gApp2->m_renderer->getScreenWidth(),gApp2->m_renderer->getScreenHeight());
  257. float viewMat[16];
  258. float projMat[16];
  259. CommonCameraInterface* cam = gApp2->m_renderer->getActiveCamera();
  260. cam->getCameraViewMatrix(viewMat);
  261. cam->getCameraProjectionMatrix(projMat);
  262. float camPos[4];
  263. cam->getCameraPosition(camPos);
  264. //b3Vector3 cp= b3MakeVector3(camPos[0],camPos[2],camPos[1]);
  265. // b3Vector3 p = b3MakeVector3(worldPosX,worldPosY,worldPosZ);
  266. glEnable(GL_BLEND);
  267. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  268. //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  269. glAlphaFunc( GL_GREATER, 1.0f );
  270. int viewport[4]={0,0,gApp2->m_renderer->getScreenWidth(),gApp2->m_renderer->getScreenHeight()};
  271. float posX = 450.f;
  272. float posY = 100.f;
  273. float winx,winy, winz;
  274. if (!projectWorldCoordToScreen(worldPosX, worldPosY, worldPosZ,viewMat,projMat,viewport,&winx, &winy, &winz))
  275. {
  276. return;
  277. }
  278. posX = winx;
  279. posY = gApp2->m_renderer->getScreenHeight()/2+(gApp2->m_renderer->getScreenHeight()/2)-winy;
  280. {
  281. //float width = 0.f;
  282. int pos=0;
  283. //float color[]={0.2f,0.2,0.2f,1.f};
  284. glActiveTexture(GL_TEXTURE0);
  285. glMatrixMode(GL_TEXTURE);
  286. glLoadIdentity();
  287. glMatrixMode(GL_PROJECTION);
  288. glLoadIdentity();
  289. glMatrixMode(GL_MODELVIEW);
  290. glLoadIdentity();
  291. glBindTexture(GL_TEXTURE_2D,m_data->m_largeFontTextureId);
  292. glEnable(GL_TEXTURE_2D);//BindTexture
  293. //float width = r.x;
  294. //float extraSpacing = 0.;
  295. float startX = posX;
  296. float startY = posY-g_DefaultLargeFont->m_CharHeight*size1;
  297. glEnable(GL_COLOR_MATERIAL);
  298. while (txt[pos])
  299. {
  300. int c = txt[pos];
  301. //r.h = g_DefaultNormalFont->m_CharHeight;
  302. //r.w = g_DefaultNormalFont->m_CharWidth[c]+extraSpacing;
  303. float endX = startX+g_DefaultLargeFont->m_CharWidth[c]*size1;
  304. float endY = posY;
  305. float currentColor[]={1.f,0.2,0.2f,1.f};
  306. float u0 = g_DefaultLargeFont->m_CharU0[c];
  307. float u1 = g_DefaultLargeFont->m_CharU1[c];
  308. float v0 = g_DefaultLargeFont->m_CharV0[c];
  309. float v1 = g_DefaultLargeFont->m_CharV1[c];
  310. float color[4] = {currentColor[0],currentColor[1],currentColor[2],currentColor[3]};
  311. float x0 = startX;
  312. float x1 = endX;
  313. float y0 = startY;
  314. float y1 = endY;
  315. int screenWidth = gApp2->m_renderer->getScreenWidth();
  316. int screenHeight = gApp2->m_renderer->getScreenHeight();
  317. float z = 2.f*winz-1.f;//*(far
  318. /*float identity[16]={1,0,0,0,
  319. 0,1,0,0,
  320. 0,0,1,0,
  321. 0,0,0,1};
  322. */
  323. PrimVertex vertexData[4] = {
  324. { PrimVec4(-1.f+2.f*x0/float(screenWidth), 1.f-2.f*y0/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v0)},
  325. { PrimVec4(-1.f+2.f*x0/float(screenWidth), 1.f-2.f*y1/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v1)},
  326. { PrimVec4( -1.f+2.f*x1/float(screenWidth), 1.f-2.f*y1/float(screenHeight), z, 1.f ), PrimVec4(color[0], color[1], color[2], color[3]) ,PrimVec2(u1,v1)},
  327. { PrimVec4( -1.f+2.f*x1/float(screenWidth), 1.f-2.f*y0/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u1,v0)}
  328. };
  329. glBegin(GL_TRIANGLES);
  330. //use red colored text for now
  331. glColor4f(1,0,0,1);
  332. float scaling = 1;
  333. glTexCoord2f(vertexData[0].uv.p[0],vertexData[0].uv.p[1]);
  334. glVertex3d(vertexData[0].position.p[0]*scaling, vertexData[0].position.p[1]*scaling,vertexData[0].position.p[2]*scaling);
  335. glTexCoord2f(vertexData[1].uv.p[0],vertexData[1].uv.p[1]);
  336. glVertex3d(vertexData[1].position.p[0]*scaling, vertexData[1].position.p[1]*scaling,vertexData[1].position.p[2]*scaling);
  337. glTexCoord2f(vertexData[2].uv.p[0],vertexData[2].uv.p[1]);
  338. glVertex3d(vertexData[2].position.p[0]*scaling, vertexData[2].position.p[1]*scaling,vertexData[2].position.p[2]*scaling);
  339. glTexCoord2f(vertexData[0].uv.p[0],vertexData[0].uv.p[1]);
  340. glVertex3d(vertexData[0].position.p[0]*scaling, vertexData[0].position.p[1]*scaling,vertexData[0].position.p[2]*scaling);
  341. glTexCoord2f(vertexData[2].uv.p[0],vertexData[2].uv.p[1]);
  342. glVertex3d(vertexData[2].position.p[0]*scaling, vertexData[2].position.p[1]*scaling,vertexData[2].position.p[2]*scaling);
  343. glTexCoord2f(vertexData[3].uv.p[0],vertexData[3].uv.p[1]);
  344. glVertex3d(vertexData[3].position.p[0]*scaling, vertexData[3].position.p[1]*scaling,vertexData[3].position.p[2]*scaling);
  345. glEnd();
  346. startX = endX;
  347. pos++;
  348. }
  349. }
  350. glBindTexture(GL_TEXTURE_2D,0);
  351. glDisable(GL_BLEND);
  352. glDisable(GL_TEXTURE_2D);
  353. restoreOpenGLState();
  354. }
  355. void SimpleOpenGL2App::registerGrid(int xres, int yres, float color0[4], float color1[4])
  356. {
  357. }