SimpleOpenGL2App.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #include "SimpleOpenGL2App.h"
  2. #define USE_OPENGL2
  3. #include "OpenGLInclude.h"
  4. #include "ShapeData.h"
  5. #include "Bullet3Common/b3Logging.h" //b3Assert?
  6. #include "Bullet3Common/b3Scalar.h"
  7. #include "Bullet3Common/b3AlignedObjectArray.h"
  8. #include "Bullet3Common/b3Vector3.h"
  9. #include "Bullet3Common/b3Quaternion.h"
  10. #include "../CommonInterfaces/CommonRenderInterface.h"
  11. #include "../OpenGLWindow/GLPrimitiveRenderer.h"
  12. #include "GLInstanceGraphicsShape.h"
  13. #include "stdlib.h"
  14. #include "TwFonts.h"
  15. #include "SimpleOpenGL2Renderer.h"
  16. #ifdef B3_USE_GLFW
  17. #include "GLFWOpenGLWindow.h"
  18. #else
  19. #ifdef __APPLE__
  20. #include "MacOpenGLWindow.h"
  21. #else
  22. //#include "GL/glew.h"
  23. #ifdef _WIN32
  24. #include "Win32OpenGLWindow.h"
  25. #else
  26. //let's cross the fingers it is Linux/X11
  27. #include "X11OpenGLWindow.h"
  28. #ifdef BT_USE_EGL
  29. #include "EGLOpenGLWindow.h"
  30. #else
  31. #endif //BT_USE_EGL
  32. #endif //_WIN32
  33. #endif //__APPLE__
  34. #endif //#ifdef B3_USE_GLFW
  35. #include <stdio.h>
  36. #include "../CommonInterfaces/CommonRenderInterface.h"
  37. static SimpleOpenGL2App* gApp2 = 0;
  38. static void Simple2ResizeCallback(float widthf, float heightf)
  39. {
  40. int width = (int)widthf;
  41. int height = (int)heightf;
  42. if (gApp2->m_renderer && gApp2->m_window)
  43. gApp2->m_renderer->resize(width, height); //*gApp2->m_window->getRetinaScale(),height*gApp2->m_window->getRetinaScale());
  44. }
  45. static void Simple2KeyboardCallback(int key, int state)
  46. {
  47. if (key == B3G_ESCAPE && gApp2 && gApp2->m_window)
  48. {
  49. gApp2->m_window->setRequestExit();
  50. }
  51. else
  52. {
  53. //gApp2->defaultKeyboardCallback(key,state);
  54. }
  55. }
  56. void Simple2MouseButtonCallback(int button, int state, float x, float y)
  57. {
  58. if (gApp2 && gApp2->m_window)
  59. {
  60. gApp2->defaultMouseButtonCallback(button, state, x, y);
  61. }
  62. }
  63. void Simple2MouseMoveCallback(float x, float y)
  64. {
  65. if (gApp2 && gApp2->m_window)
  66. {
  67. gApp2->defaultMouseMoveCallback(x, y);
  68. }
  69. }
  70. void Simple2WheelCallback(float deltax, float deltay)
  71. {
  72. gApp2->defaultWheelCallback(deltax, deltay);
  73. }
  74. struct SimpleOpenGL2AppInternalData
  75. {
  76. GLuint m_fontTextureId;
  77. GLuint m_largeFontTextureId;
  78. int m_upAxis;
  79. SimpleOpenGL2AppInternalData()
  80. : m_upAxis(1)
  81. {
  82. }
  83. };
  84. static GLuint BindFont2(const CTexFont* _Font)
  85. {
  86. GLuint TexID = 0;
  87. glGenTextures(1, &TexID);
  88. glBindTexture(GL_TEXTURE_2D, TexID);
  89. glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  90. glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  91. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  92. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  93. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  94. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  95. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, _Font->m_TexWidth, _Font->m_TexHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, _Font->m_TexBytes);
  96. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  97. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  98. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  99. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  100. glBindTexture(GL_TEXTURE_2D, 0);
  101. return TexID;
  102. }
  103. SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
  104. {
  105. gApp2 = this;
  106. m_data = new SimpleOpenGL2AppInternalData;
  107. m_window = new b3gDefaultOpenGLWindow();
  108. b3gWindowConstructionInfo ci;
  109. ci.m_title = title;
  110. ci.m_openglVersion = 2;
  111. ci.m_width = width;
  112. ci.m_height = height;
  113. m_window->createWindow(ci);
  114. m_window->setWindowTitle(title);
  115. #ifndef NO_GLEW
  116. #ifndef __APPLE__
  117. #ifndef _WIN32
  118. #ifndef B3_USE_GLFW
  119. //some Linux implementations need the 'glewExperimental' to be true
  120. #endif //B3_USE_GLFW
  121. #endif //_WIN32
  122. #ifndef B3_USE_GLFW
  123. //gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  124. #if 0
  125. if (glewInit() != GLEW_OK)
  126. {
  127. b3Error("glewInit failed");
  128. exit(1);
  129. }
  130. if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API.
  131. {
  132. b3Error("GLEW_VERSION_2_1 needs to support 2_1");
  133. exit(1); // or handle the error in a nicer way
  134. }
  135. #endif
  136. #endif //B3_USE_GLFW
  137. #endif //__APPLE__
  138. #endif //NO_GLEW
  139. TwGenerateDefaultFonts();
  140. m_data->m_fontTextureId = BindFont2(g_DefaultNormalFont);
  141. m_data->m_largeFontTextureId = BindFont2(g_DefaultLargeFont);
  142. glGetError(); //don't remove this call, it is needed for Ubuntu
  143. glClearColor(m_backgroundColorRGB[0],
  144. m_backgroundColorRGB[1],
  145. m_backgroundColorRGB[2],
  146. 1.f);
  147. b3Assert(glGetError() == GL_NO_ERROR);
  148. //m_primRenderer = new GLPrimitiveRenderer(width,height);
  149. m_parameterInterface = 0;
  150. b3Assert(glGetError() == GL_NO_ERROR);
  151. //m_renderer = new GLInstancingRenderer(128*1024,32*1024*1024);
  152. //m_renderer->init();
  153. //m_renderer->resize(width,height);
  154. b3Assert(glGetError() == GL_NO_ERROR);
  155. //m_renderer->InitShaders();
  156. m_window->setMouseMoveCallback(Simple2MouseMoveCallback);
  157. m_window->setMouseButtonCallback(Simple2MouseButtonCallback);
  158. m_window->setKeyboardCallback(Simple2KeyboardCallback);
  159. m_window->setWheelCallback(Simple2WheelCallback);
  160. m_window->setResizeCallback(Simple2ResizeCallback);
  161. m_renderer = new SimpleOpenGL2Renderer(width, height);
  162. }
  163. SimpleOpenGL2App::~SimpleOpenGL2App()
  164. {
  165. gApp2 = 0;
  166. delete m_data;
  167. }
  168. void SimpleOpenGL2App::setBackgroundColor(float red, float green, float blue)
  169. {
  170. CommonGraphicsApp::setBackgroundColor(red, green, blue);
  171. glClearColor(m_backgroundColorRGB[0], m_backgroundColorRGB[1], m_backgroundColorRGB[2], 1.f);
  172. }
  173. void SimpleOpenGL2App::drawGrid(DrawGridData data)
  174. {
  175. glEnable(GL_COLOR_MATERIAL);
  176. int gridSize = data.gridSize;
  177. float upOffset = data.upOffset;
  178. int upAxis = data.upAxis;
  179. float gridColor[4];
  180. gridColor[0] = data.gridColor[0];
  181. gridColor[1] = data.gridColor[1];
  182. gridColor[2] = data.gridColor[2];
  183. gridColor[3] = data.gridColor[3];
  184. int sideAxis = -1;
  185. int forwardAxis = -1;
  186. switch (upAxis)
  187. {
  188. case 1:
  189. forwardAxis = 2;
  190. sideAxis = 0;
  191. break;
  192. case 2:
  193. forwardAxis = 1;
  194. sideAxis = 0;
  195. break;
  196. default:
  197. b3Assert(0);
  198. };
  199. //b3Vector3 gridColor = b3MakeVector3(0.5,0.5,0.5);
  200. b3AlignedObjectArray<unsigned int> indices;
  201. b3AlignedObjectArray<b3Vector3> vertices;
  202. int lineIndex = 0;
  203. for (int i = -gridSize; i <= gridSize; i++)
  204. {
  205. {
  206. b3Assert(glGetError() == GL_NO_ERROR);
  207. b3Vector3 from = b3MakeVector3(0, 0, 0);
  208. from[sideAxis] = float(i);
  209. from[upAxis] = upOffset;
  210. from[forwardAxis] = float(-gridSize);
  211. b3Vector3 to = b3MakeVector3(0, 0, 0);
  212. to[sideAxis] = float(i);
  213. to[upAxis] = upOffset;
  214. to[forwardAxis] = float(gridSize);
  215. vertices.push_back(from);
  216. indices.push_back(lineIndex++);
  217. vertices.push_back(to);
  218. indices.push_back(lineIndex++);
  219. // m_renderer->drawLine(from,to,gridColor);
  220. }
  221. b3Assert(glGetError() == GL_NO_ERROR);
  222. {
  223. b3Assert(glGetError() == GL_NO_ERROR);
  224. b3Vector3 from = b3MakeVector3(0, 0, 0);
  225. from[sideAxis] = float(-gridSize);
  226. from[upAxis] = upOffset;
  227. from[forwardAxis] = float(i);
  228. b3Vector3 to = b3MakeVector3(0, 0, 0);
  229. to[sideAxis] = float(gridSize);
  230. to[upAxis] = upOffset;
  231. to[forwardAxis] = float(i);
  232. vertices.push_back(from);
  233. indices.push_back(lineIndex++);
  234. vertices.push_back(to);
  235. indices.push_back(lineIndex++);
  236. // m_renderer->drawLine(from,to,gridColor);
  237. }
  238. }
  239. m_renderer->drawLines(&vertices[0].x,
  240. gridColor,
  241. vertices.size(), sizeof(b3Vector3), &indices[0], indices.size(), 1);
  242. m_renderer->drawLine(b3MakeVector3(0, 0, 0), b3MakeVector3(1, 0, 0), b3MakeVector3(1, 0, 0), 3);
  243. m_renderer->drawLine(b3MakeVector3(0, 0, 0), b3MakeVector3(0, 1, 0), b3MakeVector3(0, 1, 0), 3);
  244. m_renderer->drawLine(b3MakeVector3(0, 0, 0), b3MakeVector3(0, 0, 1), b3MakeVector3(0, 0, 1), 3);
  245. // void GLInstancingRenderer::drawPoints(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, float pointDrawSize)
  246. //we don't use drawPoints because all points would have the same color
  247. // b3Vector3 points[3] = { b3MakeVector3(1, 0, 0), b3MakeVector3(0, 1, 0), b3MakeVector3(0, 0, 1) };
  248. // m_renderer->drawPoints(&points[0].x, b3MakeVector3(1, 0, 0), 3, sizeof(b3Vector3), 6);
  249. }
  250. void SimpleOpenGL2App::setUpAxis(int axis)
  251. {
  252. this->m_data->m_upAxis = axis;
  253. }
  254. int SimpleOpenGL2App::getUpAxis() const
  255. {
  256. return this->m_data->m_upAxis;
  257. }
  258. void SimpleOpenGL2App::swapBuffer()
  259. {
  260. m_window->endRendering();
  261. m_window->startRendering();
  262. }
  263. void SimpleOpenGL2App::drawText(const char* txt, int posXi, int posYi, float size, float colorRGBA[4])
  264. {
  265. }
  266. static void restoreOpenGLState()
  267. {
  268. glPopClientAttrib();
  269. glPopAttrib();
  270. }
  271. static void saveOpenGLState(int screenWidth, int screenHeight)
  272. {
  273. glPushAttrib(GL_ALL_ATTRIB_BITS);
  274. glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
  275. glDisable(GL_TEXTURE_GEN_S);
  276. glDisable(GL_TEXTURE_GEN_T);
  277. glDisable(GL_TEXTURE_GEN_R);
  278. glDisable(GL_LINE_SMOOTH);
  279. // glDisable(GL_LINE_STIPPLE);
  280. glDisable(GL_CULL_FACE);
  281. glDisable(GL_DEPTH_TEST);
  282. glDisable(GL_LIGHTING);
  283. glEnable(GL_BLEND);
  284. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  285. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  286. glDisable(GL_TEXTURE_2D);
  287. }
  288. void SimpleOpenGL2App::drawText3D(const char* txt, float position[3], float orientation[4], float color[4], float size, int optionFlag)
  289. {
  290. }
  291. void SimpleOpenGL2App::drawText3D(const char* txt, float worldPosX, float worldPosY, float worldPosZ, float size1)
  292. {
  293. saveOpenGLState(gApp2->m_renderer->getScreenWidth(), gApp2->m_renderer->getScreenHeight());
  294. float viewMat[16];
  295. float projMat[16];
  296. CommonCameraInterface* cam = gApp2->m_renderer->getActiveCamera();
  297. cam->getCameraViewMatrix(viewMat);
  298. cam->getCameraProjectionMatrix(projMat);
  299. float camPos[4];
  300. cam->getCameraPosition(camPos);
  301. //b3Vector3 cp= b3MakeVector3(camPos[0],camPos[2],camPos[1]);
  302. // b3Vector3 p = b3MakeVector3(worldPosX,worldPosY,worldPosZ);
  303. glEnable(GL_BLEND);
  304. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  305. //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  306. glAlphaFunc(GL_GREATER, 1.0f);
  307. int viewport[4] = {0, 0, gApp2->m_renderer->getScreenWidth(), gApp2->m_renderer->getScreenHeight()};
  308. float posX = 450.f;
  309. float posY = 100.f;
  310. float winx, winy, winz;
  311. if (!projectWorldCoordToScreen(worldPosX, worldPosY, worldPosZ, viewMat, projMat, viewport, &winx, &winy, &winz))
  312. {
  313. return;
  314. }
  315. posX = winx;
  316. posY = gApp2->m_renderer->getScreenHeight() / 2 + (gApp2->m_renderer->getScreenHeight() / 2) - winy;
  317. {
  318. //float width = 0.f;
  319. int pos = 0;
  320. //float color[]={0.2f,0.2,0.2f,1.f};
  321. glActiveTexture(GL_TEXTURE0);
  322. glMatrixMode(GL_TEXTURE);
  323. glLoadIdentity();
  324. glMatrixMode(GL_PROJECTION);
  325. glLoadIdentity();
  326. glMatrixMode(GL_MODELVIEW);
  327. glLoadIdentity();
  328. glBindTexture(GL_TEXTURE_2D, m_data->m_largeFontTextureId);
  329. glEnable(GL_TEXTURE_2D); //BindTexture
  330. //float width = r.x;
  331. //float extraSpacing = 0.;
  332. float startX = posX;
  333. float startY = posY - g_DefaultLargeFont->m_CharHeight * size1;
  334. glEnable(GL_COLOR_MATERIAL);
  335. while (txt[pos])
  336. {
  337. int c = txt[pos];
  338. //r.h = g_DefaultNormalFont->m_CharHeight;
  339. //r.w = g_DefaultNormalFont->m_CharWidth[c]+extraSpacing;
  340. float endX = startX + g_DefaultLargeFont->m_CharWidth[c] * size1;
  341. float endY = posY;
  342. float currentColor[] = {1.f, 0.2, 0.2f, 1.f};
  343. float u0 = g_DefaultLargeFont->m_CharU0[c];
  344. float u1 = g_DefaultLargeFont->m_CharU1[c];
  345. float v0 = g_DefaultLargeFont->m_CharV0[c];
  346. float v1 = g_DefaultLargeFont->m_CharV1[c];
  347. float color[4] = {currentColor[0], currentColor[1], currentColor[2], currentColor[3]};
  348. float x0 = startX;
  349. float x1 = endX;
  350. float y0 = startY;
  351. float y1 = endY;
  352. int screenWidth = gApp2->m_renderer->getScreenWidth();
  353. int screenHeight = gApp2->m_renderer->getScreenHeight();
  354. float z = 2.f * winz - 1.f; //*(far
  355. /*float identity[16]={1,0,0,0,
  356. 0,1,0,0,
  357. 0,0,1,0,
  358. 0,0,0,1};
  359. */
  360. PrimVertex vertexData[4] = {
  361. PrimVertex(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)),
  362. PrimVertex(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)),
  363. PrimVertex(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)),
  364. PrimVertex(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))};
  365. glBegin(GL_TRIANGLES);
  366. //use red colored text for now
  367. glColor4f(1, 0, 0, 1);
  368. float scaling = 1;
  369. glTexCoord2f(vertexData[0].uv.p[0], vertexData[0].uv.p[1]);
  370. glVertex3d(vertexData[0].position.p[0] * scaling, vertexData[0].position.p[1] * scaling, vertexData[0].position.p[2] * scaling);
  371. glTexCoord2f(vertexData[1].uv.p[0], vertexData[1].uv.p[1]);
  372. glVertex3d(vertexData[1].position.p[0] * scaling, vertexData[1].position.p[1] * scaling, vertexData[1].position.p[2] * scaling);
  373. glTexCoord2f(vertexData[2].uv.p[0], vertexData[2].uv.p[1]);
  374. glVertex3d(vertexData[2].position.p[0] * scaling, vertexData[2].position.p[1] * scaling, vertexData[2].position.p[2] * scaling);
  375. glTexCoord2f(vertexData[0].uv.p[0], vertexData[0].uv.p[1]);
  376. glVertex3d(vertexData[0].position.p[0] * scaling, vertexData[0].position.p[1] * scaling, vertexData[0].position.p[2] * scaling);
  377. glTexCoord2f(vertexData[2].uv.p[0], vertexData[2].uv.p[1]);
  378. glVertex3d(vertexData[2].position.p[0] * scaling, vertexData[2].position.p[1] * scaling, vertexData[2].position.p[2] * scaling);
  379. glTexCoord2f(vertexData[3].uv.p[0], vertexData[3].uv.p[1]);
  380. glVertex3d(vertexData[3].position.p[0] * scaling, vertexData[3].position.p[1] * scaling, vertexData[3].position.p[2] * scaling);
  381. glEnd();
  382. startX = endX;
  383. pos++;
  384. }
  385. }
  386. glBindTexture(GL_TEXTURE_2D, 0);
  387. glDisable(GL_BLEND);
  388. glDisable(GL_TEXTURE_2D);
  389. restoreOpenGLState();
  390. }
  391. void SimpleOpenGL2App::registerGrid(int cells_x, int cells_z, float color0[4], float color1[4])
  392. {
  393. b3Vector3 cubeExtents = b3MakeVector3(0.5, 0.5, 0.5);
  394. double halfHeight = 0.1;
  395. cubeExtents[m_data->m_upAxis] = halfHeight;
  396. int cubeId = registerCubeShape(cubeExtents[0], cubeExtents[1], cubeExtents[2]);
  397. b3Quaternion orn(0, 0, 0, 1);
  398. b3Vector3 center = b3MakeVector3(0, 0, 0, 1);
  399. b3Vector3 scaling = b3MakeVector3(1, 1, 1, 1);
  400. for (int i = 0; i < cells_x; i++)
  401. {
  402. for (int j = 0; j < cells_z; j++)
  403. {
  404. float* color = 0;
  405. if ((i + j) % 2 == 0)
  406. {
  407. color = (float*)color0;
  408. }
  409. else
  410. {
  411. color = (float*)color1;
  412. }
  413. if (this->m_data->m_upAxis == 1)
  414. {
  415. center = b3MakeVector3((i + 0.5f) - cells_x * 0.5f, -halfHeight, (j + 0.5f) - cells_z * 0.5f);
  416. }
  417. else
  418. {
  419. center = b3MakeVector3((i + 0.5f) - cells_x * 0.5f, (j + 0.5f) - cells_z * 0.5f, -halfHeight);
  420. }
  421. m_renderer->registerGraphicsInstance(cubeId, center, orn, color, scaling);
  422. }
  423. }
  424. }
  425. int SimpleOpenGL2App::registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId)
  426. {
  427. int strideInBytes = 9 * sizeof(float);
  428. int graphicsShapeIndex = -1;
  429. switch (lod)
  430. {
  431. case SPHERE_LOD_POINT_SPRITE:
  432. {
  433. int numVertices = sizeof(point_sphere_vertices) / strideInBytes;
  434. int numIndices = sizeof(point_sphere_indices) / sizeof(int);
  435. graphicsShapeIndex = m_renderer->registerShape(&point_sphere_vertices[0], numVertices, point_sphere_indices, numIndices, B3_GL_POINTS, textureId);
  436. break;
  437. }
  438. case SPHERE_LOD_LOW:
  439. {
  440. int numVertices = sizeof(low_sphere_vertices) / strideInBytes;
  441. int numIndices = sizeof(low_sphere_indices) / sizeof(int);
  442. graphicsShapeIndex = m_renderer->registerShape(&low_sphere_vertices[0], numVertices, low_sphere_indices, numIndices, B3_GL_TRIANGLES, textureId);
  443. break;
  444. }
  445. case SPHERE_LOD_MEDIUM:
  446. {
  447. int numVertices = sizeof(medium_sphere_vertices) / strideInBytes;
  448. int numIndices = sizeof(medium_sphere_indices) / sizeof(int);
  449. graphicsShapeIndex = m_renderer->registerShape(&medium_sphere_vertices[0], numVertices, medium_sphere_indices, numIndices, B3_GL_TRIANGLES, textureId);
  450. break;
  451. }
  452. case SPHERE_LOD_HIGH:
  453. default:
  454. {
  455. int numVertices = sizeof(detailed_sphere_vertices) / strideInBytes;
  456. int numIndices = sizeof(detailed_sphere_indices) / sizeof(int);
  457. graphicsShapeIndex = m_renderer->registerShape(&detailed_sphere_vertices[0], numVertices, detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, textureId);
  458. break;
  459. }
  460. };
  461. return graphicsShapeIndex;
  462. }
  463. int SimpleOpenGL2App::registerCubeShape(float halfExtentsX, float halfExtentsY, float halfExtentsZ, int textureIndex, float textureScaling)
  464. {
  465. int strideInBytes = 9 * sizeof(float);
  466. int numVertices = sizeof(cube_vertices_textured) / strideInBytes;
  467. int numIndices = sizeof(cube_indices) / sizeof(int);
  468. b3AlignedObjectArray<GLInstanceVertex> verts;
  469. verts.resize(numVertices);
  470. for (int i = 0; i < numVertices; i++)
  471. {
  472. verts[i].xyzw[0] = halfExtentsX * cube_vertices_textured[i * 9];
  473. verts[i].xyzw[1] = halfExtentsY * cube_vertices_textured[i * 9 + 1];
  474. verts[i].xyzw[2] = halfExtentsZ * cube_vertices_textured[i * 9 + 2];
  475. verts[i].xyzw[3] = cube_vertices_textured[i * 9 + 3];
  476. verts[i].normal[0] = cube_vertices_textured[i * 9 + 4];
  477. verts[i].normal[1] = cube_vertices_textured[i * 9 + 5];
  478. verts[i].normal[2] = cube_vertices_textured[i * 9 + 6];
  479. verts[i].uv[0] = cube_vertices_textured[i * 9 + 7] * textureScaling;
  480. verts[i].uv[1] = cube_vertices_textured[i * 9 + 8] * textureScaling;
  481. }
  482. int shapeId = m_renderer->registerShape(&verts[0].xyzw[0], numVertices, cube_indices, numIndices, B3_GL_TRIANGLES, textureIndex);
  483. return shapeId;
  484. }