SimpleOpenGL2Renderer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #include "SimpleOpenGL2Renderer.h"
  2. #include "OpenGL2Include.h"
  3. #include "Bullet3Common/b3Vector3.h"
  4. #include "Bullet3Common/b3AlignedObjectArray.h"
  5. #include "GLInstanceGraphicsShape.h"
  6. #include "Bullet3Common/b3Quaternion.h"
  7. #include "Bullet3Common/b3Transform.h"
  8. #include "Bullet3Common/b3ResizablePool.h"
  9. B3_ATTRIBUTE_ALIGNED16(struct)
  10. SimpleGL2Shape
  11. {
  12. B3_DECLARE_ALIGNED_ALLOCATOR();
  13. int m_textureIndex;
  14. int m_primitiveType;
  15. b3AlignedObjectArray<int> m_indices;
  16. b3AlignedObjectArray<GLInstanceVertex> m_vertices;
  17. b3Vector3 m_scaling;
  18. };
  19. B3_ATTRIBUTE_ALIGNED16(struct)
  20. SimpleGL2Instance
  21. {
  22. B3_DECLARE_ALIGNED_ALLOCATOR();
  23. int m_shapeIndex;
  24. b3Vector3 m_position;
  25. b3Quaternion orn;
  26. b3Vector4 m_rgbColor;
  27. b3Vector3 m_scaling;
  28. void clear()
  29. {
  30. }
  31. };
  32. struct InternalTextureHandle2
  33. {
  34. GLuint m_glTexture;
  35. int m_width;
  36. int m_height;
  37. };
  38. typedef b3PoolBodyHandle<SimpleGL2Instance> SimpleGL2InstanceHandle;
  39. struct SimpleOpenGL2RendererInternalData
  40. {
  41. int m_width;
  42. int m_height;
  43. SimpleCamera m_camera;
  44. b3AlignedObjectArray<SimpleGL2Shape*> m_shapes;
  45. //b3AlignedObjectArray<SimpleGL2Instance> m_graphicsInstances1;
  46. b3ResizablePool<SimpleGL2InstanceHandle> m_graphicsInstancesPool;
  47. b3AlignedObjectArray<InternalTextureHandle2> m_textureHandles;
  48. };
  49. SimpleOpenGL2Renderer::SimpleOpenGL2Renderer(int width, int height)
  50. {
  51. m_data = new SimpleOpenGL2RendererInternalData;
  52. m_data->m_width = width;
  53. m_data->m_height = height;
  54. }
  55. SimpleOpenGL2Renderer::~SimpleOpenGL2Renderer()
  56. {
  57. delete m_data;
  58. }
  59. void SimpleOpenGL2Renderer::init()
  60. {
  61. }
  62. const CommonCameraInterface* SimpleOpenGL2Renderer::getActiveCamera() const
  63. {
  64. return &m_data->m_camera;
  65. }
  66. CommonCameraInterface* SimpleOpenGL2Renderer::getActiveCamera()
  67. {
  68. return &m_data->m_camera;
  69. }
  70. void SimpleOpenGL2Renderer::setActiveCamera(CommonCameraInterface* cam)
  71. {
  72. b3Assert(0); //not supported yet
  73. }
  74. void SimpleOpenGL2Renderer::setLightPosition(const float lightPos[3])
  75. {
  76. }
  77. void SimpleOpenGL2Renderer::setLightPosition(const double lightPos[3])
  78. {
  79. }
  80. void SimpleOpenGL2Renderer::updateCamera(int upAxis)
  81. {
  82. float projection[16];
  83. float view[16];
  84. getActiveCamera()->setAspectRatio((float)m_data->m_width / (float)m_data->m_height);
  85. getActiveCamera()->setCameraUpAxis(upAxis);
  86. m_data->m_camera.update(); //??
  87. getActiveCamera()->getCameraProjectionMatrix(projection);
  88. getActiveCamera()->getCameraViewMatrix(view);
  89. GLfloat projMat[16];
  90. GLfloat viewMat[16];
  91. for (int i = 0; i < 16; i++)
  92. {
  93. viewMat[i] = view[i];
  94. projMat[i] = projection[i];
  95. }
  96. glMatrixMode(GL_PROJECTION);
  97. glLoadIdentity();
  98. glMultMatrixf(projMat);
  99. glMatrixMode(GL_MODELVIEW);
  100. glLoadIdentity();
  101. glMultMatrixf(viewMat);
  102. }
  103. void SimpleOpenGL2Renderer::removeAllInstances()
  104. {
  105. for (int i = 0; i < m_data->m_shapes.size(); i++)
  106. {
  107. delete m_data->m_shapes[i];
  108. }
  109. m_data->m_shapes.clear();
  110. m_data->m_graphicsInstancesPool.exitHandles();
  111. m_data->m_graphicsInstancesPool.initHandles();
  112. //also destroy textures?
  113. m_data->m_textureHandles.clear();
  114. }
  115. void SimpleOpenGL2Renderer::removeGraphicsInstance(int instanceUid)
  116. {
  117. m_data->m_graphicsInstancesPool.freeHandle(instanceUid);
  118. }
  119. bool SimpleOpenGL2Renderer::readSingleInstanceTransformToCPU(float* position, float* orientation, int srcIndex)
  120. {
  121. return false;
  122. }
  123. void SimpleOpenGL2Renderer::writeSingleInstanceColorToCPU(const float* color, int srcIndex)
  124. {
  125. }
  126. void SimpleOpenGL2Renderer::writeSingleInstanceColorToCPU(const double* color, int srcIndex)
  127. {
  128. }
  129. void SimpleOpenGL2Renderer::writeSingleInstanceScaleToCPU(const float* scale, int srcIndex)
  130. {
  131. }
  132. void SimpleOpenGL2Renderer::writeSingleInstanceScaleToCPU(const double* scale, int srcIndex)
  133. {
  134. }
  135. int SimpleOpenGL2Renderer::getTotalNumInstances() const
  136. {
  137. return m_data->m_graphicsInstancesPool.getNumHandles();
  138. }
  139. void SimpleOpenGL2Renderer::getCameraViewMatrix(float viewMat[16]) const
  140. {
  141. b3Assert(0);
  142. }
  143. void SimpleOpenGL2Renderer::getCameraProjectionMatrix(float projMat[16]) const
  144. {
  145. b3Assert(0);
  146. }
  147. void SimpleOpenGL2Renderer::drawOpenGL(int instanceIndex)
  148. {
  149. const SimpleGL2Instance* instPtr = m_data->m_graphicsInstancesPool.getHandle(instanceIndex);
  150. if (0 == instPtr)
  151. {
  152. b3Assert(0);
  153. return;
  154. }
  155. const SimpleGL2Instance& inst = *instPtr;
  156. const SimpleGL2Shape* shape = m_data->m_shapes[inst.m_shapeIndex];
  157. if (inst.m_rgbColor[3] == 0)
  158. {
  159. return;
  160. }
  161. glPushMatrix();
  162. b3Transform tr;
  163. tr.setOrigin(b3MakeVector3(inst.m_position[0], inst.m_position[1], inst.m_position[2]));
  164. tr.setRotation(b3Quaternion(inst.orn[0], inst.orn[1], inst.orn[2], inst.orn[3]));
  165. b3Scalar m[16];
  166. tr.getOpenGLMatrix(m);
  167. #ifdef B3_USE_DOUBLE_PRECISION
  168. glMultMatrixd(m);
  169. #else
  170. glMultMatrixf(m);
  171. #endif
  172. #if 0
  173. glMatrixMode(GL_TEXTURE);
  174. glLoadIdentity();
  175. glScalef(0.025f,0.025f,0.025f);
  176. glMatrixMode(GL_MODELVIEW);
  177. static const GLfloat planex[]={1,0,0,0};
  178. // static const GLfloat planey[]={0,1,0,0};
  179. static const GLfloat planez[]={0,0,1,0};
  180. glTexGenfv(GL_S,GL_OBJECT_PLANE,planex);
  181. glTexGenfv(GL_T,GL_OBJECT_PLANE,planez);
  182. glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
  183. glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
  184. glEnable(GL_TEXTURE_GEN_S);
  185. glEnable(GL_TEXTURE_GEN_T);
  186. glEnable(GL_TEXTURE_GEN_R);
  187. m_textureinitialized=true;
  188. #endif
  189. //drawCoordSystem();
  190. //glPushMatrix();
  191. // glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  192. // glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  193. // glMatrixMode(GL_TEXTURE);
  194. // glLoadIdentity();
  195. glMatrixMode(GL_MODELVIEW);
  196. glEnable(GL_COLOR_MATERIAL);
  197. if (shape->m_textureIndex >= 0)
  198. {
  199. glEnable(GL_TEXTURE_2D);
  200. activateTexture(shape->m_textureIndex);
  201. }
  202. else
  203. {
  204. glDisable(GL_TEXTURE_2D);
  205. }
  206. glColor3f(inst.m_rgbColor[0], inst.m_rgbColor[1], inst.m_rgbColor[2]);
  207. glScalef(inst.m_scaling[0], inst.m_scaling[1], inst.m_scaling[2]);
  208. glShadeModel(GL_SMOOTH);
  209. glBegin(GL_TRIANGLES);
  210. for (int i = 0; i < shape->m_indices.size(); i += 3)
  211. {
  212. for (int v = 0; v < 3; v++)
  213. {
  214. const GLInstanceVertex& vtx0 = shape->m_vertices[shape->m_indices[i + v]];
  215. glNormal3f(vtx0.normal[0], vtx0.normal[1], vtx0.normal[2]);
  216. glTexCoord2f(vtx0.uv[0], vtx0.uv[1]);
  217. glVertex3f(vtx0.xyzw[0], vtx0.xyzw[1], vtx0.xyzw[2]);
  218. }
  219. }
  220. glEnd();
  221. glPopMatrix();
  222. }
  223. void SimpleOpenGL2Renderer::drawSceneInternal(int pass, int cameraUpAxis)
  224. {
  225. b3AlignedObjectArray<int> usedHandles;
  226. m_data->m_graphicsInstancesPool.getUsedHandles(usedHandles);
  227. for (int i = 0; i < usedHandles.size(); i++)
  228. {
  229. drawOpenGL(usedHandles[i]);
  230. }
  231. #if 0
  232. b3Scalar m[16];
  233. b3Matrix3x3 rot;rot.setIdentity();
  234. const int numObjects=dynamicsWorld->getNumCollisionObjects();
  235. btVector3 wireColor(1,0,0);
  236. //glDisable(GL_CULL_FACE);
  237. for(int i=0;i<numObjects;i++)
  238. {
  239. const btCollisionObject* colObj=dynamicsWorld->getCollisionObjectArray()[i];
  240. const btRigidBody* body=btRigidBody::upcast(colObj);
  241. if(body&&body->getMotionState())
  242. {
  243. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  244. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(m);
  245. rot=myMotionState->m_graphicsWorldTrans.getBasis();
  246. }
  247. else
  248. {
  249. colObj->getWorldTransform().getOpenGLMatrix(m);
  250. rot=colObj->getWorldTransform().getBasis();
  251. }
  252. btVector3 wireColor(1.f,1.0f,0.5f); //wants deactivation
  253. if(i&1) wireColor=btVector3(0.f,0.0f,1.f);
  254. ///color differently for active, sleeping, wantsdeactivation states
  255. if (colObj->getActivationState() == 1) //active
  256. {
  257. if (i & 1)
  258. {
  259. wireColor += btVector3 (1.f,0.f,0.f);
  260. }
  261. else
  262. {
  263. wireColor += btVector3 (.5f,0.f,0.f);
  264. }
  265. }
  266. if(colObj->getActivationState()==2) //ISLAND_SLEEPING
  267. {
  268. if(i&1)
  269. {
  270. wireColor += btVector3 (0.f,1.f, 0.f);
  271. }
  272. else
  273. {
  274. wireColor += btVector3 (0.f,0.5f,0.f);
  275. }
  276. }
  277. btVector3 aabbMin(0,0,0),aabbMax(0,0,0);
  278. //m_dynamicsWorld->getBroadphase()->getBroadphaseAabb(aabbMin,aabbMax);
  279. aabbMin-=btVector3(BT_LARGE_FLOAT,BT_LARGE_FLOAT,BT_LARGE_FLOAT);
  280. aabbMax+=btVector3(BT_LARGE_FLOAT,BT_LARGE_FLOAT,BT_LARGE_FLOAT);
  281. // printf("aabbMin=(%f,%f,%f)\n",aabbMin.getX(),aabbMin.getY(),aabbMin.getZ());
  282. // printf("aabbMax=(%f,%f,%f)\n",aabbMax.getX(),aabbMax.getY(),aabbMax.getZ());
  283. // m_dynamicsWorld->getDebugDrawer()->drawAabb(aabbMin,aabbMax,btVector3(1,1,1));
  284. //switch(pass)
  285. //if (!(getDebugMode()& btIDebugDraw::DBG_DrawWireframe))
  286. int debugMode = 0;//getDebugMode()
  287. //btVector3 m_sundirection(-1,-1,-1);
  288. btVector3 m_sundirection(btVector3(1,-2,1)*1000);
  289. if (cameraUpAxis==2)
  290. {
  291. m_sundirection = btVector3(1,1,-2)*1000;
  292. }
  293. switch(pass)
  294. {
  295. case 0: drawOpenGL(m,colObj->getCollisionShape(),wireColor,debugMode,aabbMin,aabbMax);break;
  296. case 1: drawShadow(m,m_sundirection*rot,colObj->getCollisionShape(),aabbMin,aabbMax);break;
  297. case 2: drawOpenGL(m,colObj->getCollisionShape(),wireColor*b3Scalar(0.3),0,aabbMin,aabbMax);break;
  298. }
  299. }
  300. #endif
  301. }
  302. void SimpleOpenGL2Renderer::renderScene()
  303. {
  304. GLfloat light_ambient[] = {b3Scalar(0.2), b3Scalar(0.2), b3Scalar(0.2), b3Scalar(1.0)};
  305. GLfloat light_diffuse[] = {b3Scalar(1.0), b3Scalar(1.0), b3Scalar(1.0), b3Scalar(1.0)};
  306. GLfloat light_specular[] = {b3Scalar(1.0), b3Scalar(1.0), b3Scalar(1.0), b3Scalar(1.0)};
  307. /* light_position is NOT default value */
  308. GLfloat light_position0[] = {b3Scalar(1.0), b3Scalar(10.0), b3Scalar(1.0), b3Scalar(0.0)};
  309. GLfloat light_position1[] = {b3Scalar(-1.0), b3Scalar(-10.0), b3Scalar(-1.0), b3Scalar(0.0)};
  310. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  311. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  312. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  313. glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
  314. glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
  315. glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
  316. glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
  317. glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
  318. glEnable(GL_LIGHTING);
  319. glEnable(GL_LIGHT0);
  320. glEnable(GL_LIGHT1);
  321. glShadeModel(GL_SMOOTH);
  322. glEnable(GL_DEPTH_TEST);
  323. glDepthFunc(GL_LESS);
  324. drawSceneInternal(0, 0);
  325. }
  326. int SimpleOpenGL2Renderer::registerTexture(const unsigned char* texels, int width, int height, bool flipTexelsY)
  327. {
  328. b3Assert(glGetError() == GL_NO_ERROR);
  329. glActiveTexture(GL_TEXTURE0);
  330. int textureIndex = m_data->m_textureHandles.size();
  331. // const GLubyte* image= (const GLubyte*)texels;
  332. GLuint textureHandle;
  333. glGenTextures(1, (GLuint*)&textureHandle);
  334. glBindTexture(GL_TEXTURE_2D, textureHandle);
  335. b3Assert(glGetError() == GL_NO_ERROR);
  336. InternalTextureHandle2 h;
  337. h.m_glTexture = textureHandle;
  338. h.m_width = width;
  339. h.m_height = height;
  340. m_data->m_textureHandles.push_back(h);
  341. updateTexture(textureIndex, texels, flipTexelsY);
  342. return textureIndex;
  343. }
  344. void SimpleOpenGL2Renderer::updateTexture(int textureIndex, const unsigned char* texels, bool flipTexelsY)
  345. {
  346. if (textureIndex >= 0)
  347. {
  348. glActiveTexture(GL_TEXTURE0);
  349. b3Assert(glGetError() == GL_NO_ERROR);
  350. InternalTextureHandle2& h = m_data->m_textureHandles[textureIndex];
  351. glBindTexture(GL_TEXTURE_2D, h.m_glTexture);
  352. b3Assert(glGetError() == GL_NO_ERROR);
  353. if (flipTexelsY)
  354. {
  355. //textures need to be flipped for OpenGL...
  356. b3AlignedObjectArray<unsigned char> flippedTexels;
  357. flippedTexels.resize(h.m_width * h.m_height * 3);
  358. for (int i = 0; i < h.m_width; i++)
  359. {
  360. for (int j = 0; j < h.m_height; j++)
  361. {
  362. flippedTexels[(i + j * h.m_width) * 3] = texels[(i + (h.m_height - 1 - j) * h.m_width) * 3];
  363. flippedTexels[(i + j * h.m_width) * 3 + 1] = texels[(i + (h.m_height - 1 - j) * h.m_width) * 3 + 1];
  364. flippedTexels[(i + j * h.m_width) * 3 + 2] = texels[(i + (h.m_height - 1 - j) * h.m_width) * 3 + 2];
  365. }
  366. }
  367. // const GLubyte* image= (const GLubyte*)texels;
  368. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, h.m_width, h.m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, &flippedTexels[0]);
  369. }
  370. else
  371. {
  372. // const GLubyte* image= (const GLubyte*)texels;
  373. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, h.m_width, h.m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, &texels[0]);
  374. }
  375. b3Assert(glGetError() == GL_NO_ERROR);
  376. //glGenerateMipmap(GL_TEXTURE_2D);
  377. b3Assert(glGetError() == GL_NO_ERROR);
  378. }
  379. }
  380. void SimpleOpenGL2Renderer::removeTexture(int textureIndex)
  381. {
  382. if ((textureIndex >= 0) && (textureIndex < m_data->m_textureHandles.size()))
  383. {
  384. glDeleteTextures(1, &m_data->m_textureHandles[textureIndex].m_glTexture);
  385. }
  386. }
  387. void SimpleOpenGL2Renderer::activateTexture(int textureIndex)
  388. {
  389. glActiveTexture(GL_TEXTURE0);
  390. if (textureIndex >= 0)
  391. {
  392. glBindTexture(GL_TEXTURE_2D, m_data->m_textureHandles[textureIndex].m_glTexture);
  393. }
  394. else
  395. {
  396. glBindTexture(GL_TEXTURE_2D, 0);
  397. }
  398. }
  399. int SimpleOpenGL2Renderer::registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling)
  400. {
  401. int newHandle = m_data->m_graphicsInstancesPool.allocHandle();
  402. // int sz = m_data->m_graphicsInstances.size();
  403. SimpleGL2Instance& instance = *m_data->m_graphicsInstancesPool.getHandle(newHandle);
  404. instance.m_shapeIndex = shapeIndex;
  405. instance.m_position[0] = position[0];
  406. instance.m_position[1] = position[1];
  407. instance.m_position[2] = position[2];
  408. instance.orn[0] = quaternion[0];
  409. instance.orn[1] = quaternion[1];
  410. instance.orn[2] = quaternion[2];
  411. instance.orn[3] = quaternion[3];
  412. instance.m_rgbColor[0] = color[0];
  413. instance.m_rgbColor[1] = color[1];
  414. instance.m_rgbColor[2] = color[2];
  415. instance.m_rgbColor[3] = color[3];
  416. instance.m_scaling[0] = scaling[0];
  417. instance.m_scaling[1] = scaling[1];
  418. instance.m_scaling[2] = scaling[2];
  419. return newHandle;
  420. }
  421. int SimpleOpenGL2Renderer::registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling)
  422. {
  423. int newHandle = m_data->m_graphicsInstancesPool.allocHandle();
  424. SimpleGL2Instance& instance = *m_data->m_graphicsInstancesPool.getHandle(newHandle);
  425. instance.m_shapeIndex = shapeIndex;
  426. instance.m_position[0] = position[0];
  427. instance.m_position[1] = position[1];
  428. instance.m_position[2] = position[2];
  429. instance.orn[0] = quaternion[0];
  430. instance.orn[1] = quaternion[1];
  431. instance.orn[2] = quaternion[2];
  432. instance.orn[3] = quaternion[3];
  433. instance.m_rgbColor[0] = color[0];
  434. instance.m_rgbColor[1] = color[1];
  435. instance.m_rgbColor[2] = color[2];
  436. instance.m_rgbColor[3] = color[3];
  437. instance.m_scaling[0] = scaling[0];
  438. instance.m_scaling[1] = scaling[1];
  439. instance.m_scaling[2] = scaling[2];
  440. return newHandle;
  441. }
  442. void SimpleOpenGL2Renderer::drawLines(const float* positions, const float color[4], int numPoints, int pointStrideInBytes, const unsigned int* indices, int numIndices, float pointDrawSize)
  443. {
  444. int pointStrideInFloats = pointStrideInBytes / 4;
  445. glLineWidth(pointDrawSize);
  446. for (int i = 0; i < numIndices; i += 2)
  447. {
  448. int index0 = indices[i];
  449. int index1 = indices[i + 1];
  450. b3Vector3 fromColor = b3MakeVector3(color[0], color[1], color[2]);
  451. b3Vector3 toColor = b3MakeVector3(color[0], color[1], color[2]);
  452. b3Vector3 from = b3MakeVector3(positions[index0 * pointStrideInFloats], positions[index0 * pointStrideInFloats + 1], positions[index0 * pointStrideInFloats + 2]);
  453. b3Vector3 to = b3MakeVector3(positions[index1 * pointStrideInFloats], positions[index1 * pointStrideInFloats + 1], positions[index1 * pointStrideInFloats + 2]);
  454. glBegin(GL_LINES);
  455. glColor3f(fromColor.getX(), fromColor.getY(), fromColor.getZ());
  456. glVertex3d(from.getX(), from.getY(), from.getZ());
  457. glColor3f(toColor.getX(), toColor.getY(), toColor.getZ());
  458. glVertex3d(to.getX(), to.getY(), to.getZ());
  459. glEnd();
  460. }
  461. }
  462. void SimpleOpenGL2Renderer::drawLine(const float from[4], const float to[4], const float color[4], float lineWidth)
  463. {
  464. glLineWidth(lineWidth);
  465. glBegin(GL_LINES);
  466. glColor3f(color[0], color[1], color[2]);
  467. glVertex3d(from[0], from[1], from[2]);
  468. glVertex3d(to[0], to[1], to[2]);
  469. glEnd();
  470. }
  471. int SimpleOpenGL2Renderer::registerShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType, int textureIndex)
  472. {
  473. SimpleGL2Shape* shape = new SimpleGL2Shape();
  474. shape->m_textureIndex = textureIndex;
  475. shape->m_indices.resize(numIndices);
  476. for (int i = 0; i < numIndices; i++)
  477. {
  478. shape->m_indices[i] = indices[i];
  479. }
  480. shape->m_vertices.resize(numvertices);
  481. for (int v = 0; v < numvertices; v++)
  482. {
  483. GLInstanceVertex& vtx = shape->m_vertices[v];
  484. vtx.xyzw[0] = vertices[9 * v + 0];
  485. vtx.xyzw[1] = vertices[9 * v + 1];
  486. vtx.xyzw[2] = vertices[9 * v + 2];
  487. vtx.xyzw[3] = vertices[9 * v + 3];
  488. vtx.normal[0] = vertices[9 * v + 4];
  489. vtx.normal[1] = vertices[9 * v + 5];
  490. vtx.normal[2] = vertices[9 * v + 6];
  491. vtx.uv[0] = vertices[9 * v + 7];
  492. vtx.uv[1] = vertices[9 * v + 8];
  493. }
  494. int sz = m_data->m_shapes.size();
  495. m_data->m_shapes.push_back(shape);
  496. return sz;
  497. }
  498. void SimpleOpenGL2Renderer::writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex)
  499. {
  500. SimpleGL2Instance& graphicsInstance = *m_data->m_graphicsInstancesPool.getHandle(srcIndex);
  501. graphicsInstance.m_position[0] = position[0];
  502. graphicsInstance.m_position[1] = position[1];
  503. graphicsInstance.m_position[2] = position[2];
  504. graphicsInstance.orn[0] = orientation[0];
  505. graphicsInstance.orn[1] = orientation[1];
  506. graphicsInstance.orn[2] = orientation[2];
  507. graphicsInstance.orn[3] = orientation[3];
  508. }
  509. void SimpleOpenGL2Renderer::writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex)
  510. {
  511. SimpleGL2Instance& graphicsInstance = *m_data->m_graphicsInstancesPool.getHandle(srcIndex);
  512. graphicsInstance.m_position[0] = position[0];
  513. graphicsInstance.m_position[1] = position[1];
  514. graphicsInstance.m_position[2] = position[2];
  515. graphicsInstance.orn[0] = orientation[0];
  516. graphicsInstance.orn[1] = orientation[1];
  517. graphicsInstance.orn[2] = orientation[2];
  518. graphicsInstance.orn[3] = orientation[3];
  519. }
  520. void SimpleOpenGL2Renderer::writeTransforms()
  521. {
  522. }
  523. void SimpleOpenGL2Renderer::resize(int width, int height)
  524. {
  525. m_data->m_width = width;
  526. m_data->m_height = height;
  527. }
  528. int SimpleOpenGL2Renderer::getScreenWidth()
  529. {
  530. return m_data->m_width;
  531. }
  532. int SimpleOpenGL2Renderer::getScreenHeight()
  533. {
  534. return m_data->m_height;
  535. }
  536. void SimpleOpenGL2Renderer::drawLine(const double from[4], const double to[4], const double color[4], double lineWidth)
  537. {
  538. glLineWidth(lineWidth);
  539. glBegin(GL_LINES);
  540. glColor3f(color[0], color[1], color[2]);
  541. glVertex3d(from[0], from[1], from[2]);
  542. glVertex3d(to[0], to[1], to[2]);
  543. glEnd();
  544. }
  545. void SimpleOpenGL2Renderer::drawPoint(const float* position, const float color[4], float pointDrawSize)
  546. {
  547. }
  548. void SimpleOpenGL2Renderer::drawPoint(const double* position, const double color[4], double pointDrawSize)
  549. {
  550. }
  551. void SimpleOpenGL2Renderer::drawPoints(const float* positions, const float* colors, int numPoints, int pointStrideInBytes, float pointDrawSize)
  552. {
  553. }
  554. void SimpleOpenGL2Renderer::updateShape(int shapeIndex, const float* vertices, int numVertices)
  555. {
  556. SimpleGL2Shape* shape = m_data->m_shapes[shapeIndex];
  557. int numvertices = shape->m_vertices.size();
  558. b3Assert(numVertices = numvertices);
  559. if (numVertices != numvertices)
  560. return;
  561. for (int i = 0; i < numvertices; i++)
  562. {
  563. shape->m_vertices[i].xyzw[0] = vertices[9 * i + 0];
  564. shape->m_vertices[i].xyzw[1] = vertices[9 * i + 1];
  565. shape->m_vertices[i].xyzw[2] = vertices[9 * i + 2];
  566. shape->m_vertices[i].xyzw[3] = vertices[9 * i + 3];
  567. shape->m_vertices[i].normal[0] = vertices[9 * i + 4];
  568. shape->m_vertices[i].normal[1] = vertices[9 * i + 5];
  569. shape->m_vertices[i].normal[2] = vertices[9 * i + 6];
  570. shape->m_vertices[i].uv[0] = vertices[9 * i + 7];
  571. shape->m_vertices[i].uv[1] = vertices[9 * i + 8];
  572. }
  573. }
  574. void SimpleOpenGL2Renderer::clearZBuffer()
  575. {
  576. glClear(GL_DEPTH_BUFFER_BIT);
  577. }