TinyVRGui.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "TinyVRGui.h"
  2. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  3. #include "../ExampleBrowser/GwenGUISupport/GraphingTexture.h"
  4. #include "../CommonInterfaces/Common2dCanvasInterface.h"
  5. #include "../RenderingExamples/TimeSeriesCanvas.h"
  6. #include "../RenderingExamples/TimeSeriesFontData.h"
  7. #include "../Importers/ImportMeshUtility/b3ImportMeshUtility.h"
  8. #include "../OpenGLWindow/GLInstanceGraphicsShape.h"
  9. #include "../Utils/b3BulletDefaultFileIO.h"
  10. #include "../CommonInterfaces/CommonRenderInterface.h"
  11. #include "../CommonInterfaces/CommonParameterInterface.h"
  12. struct TestCanvasInterface2 : public Common2dCanvasInterface
  13. {
  14. b3AlignedObjectArray<unsigned char>& m_texelsRGB;
  15. int m_width;
  16. int m_height;
  17. TestCanvasInterface2(b3AlignedObjectArray<unsigned char>& texelsRGB, int width, int height)
  18. : m_texelsRGB(texelsRGB),
  19. m_width(width),
  20. m_height(height)
  21. {
  22. }
  23. virtual ~TestCanvasInterface2()
  24. {
  25. }
  26. virtual int createCanvas(const char* canvasName, int width, int height, int posX, int posY)
  27. {
  28. return 0;
  29. }
  30. virtual void destroyCanvas(int canvasId)
  31. {
  32. }
  33. virtual void setPixel(int canvasId, int x, int y, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
  34. {
  35. if (x >= 0 && x < m_width && y >= 0 && y < m_height)
  36. {
  37. m_texelsRGB[(x + y * m_width) * 3 + 0] = red;
  38. m_texelsRGB[(x + y * m_width) * 3 + 1] = green;
  39. m_texelsRGB[(x + y * m_width) * 3 + 2] = blue;
  40. }
  41. }
  42. virtual void getPixel(int canvasId, int x, int y, unsigned char& red, unsigned char& green, unsigned char& blue, unsigned char& alpha)
  43. {
  44. if (x >= 0 && x < m_width && y >= 0 && y < m_height)
  45. {
  46. red = m_texelsRGB[(x + y * m_width) * 3 + 0];
  47. green = m_texelsRGB[(x + y * m_width) * 3 + 1];
  48. blue = m_texelsRGB[(x + y * m_width) * 3 + 2];
  49. }
  50. }
  51. virtual void refreshImageData(int canvasId)
  52. {
  53. }
  54. };
  55. struct TinyVRGuiInternalData
  56. {
  57. CommonRenderInterface* m_renderer;
  58. b3AlignedObjectArray<unsigned char> m_texelsRGB;
  59. TestCanvasInterface2* m_testCanvas;
  60. TimeSeriesCanvas* m_timeSeries;
  61. int m_src;
  62. int m_textureId;
  63. int m_gfxObjectId;
  64. TinyVRGuiInternalData()
  65. : m_renderer(0),
  66. m_testCanvas(0),
  67. m_timeSeries(0),
  68. m_src(-1),
  69. m_textureId(-1),
  70. m_gfxObjectId(-1)
  71. {
  72. }
  73. };
  74. TinyVRGui::TinyVRGui(struct ComboBoxParams& params, struct CommonRenderInterface* renderer)
  75. {
  76. m_data = new TinyVRGuiInternalData;
  77. m_data->m_renderer = renderer;
  78. }
  79. TinyVRGui::~TinyVRGui()
  80. {
  81. delete m_data->m_timeSeries;
  82. delete m_data->m_testCanvas;
  83. delete m_data;
  84. }
  85. bool TinyVRGui::init()
  86. {
  87. {
  88. int width = 256;
  89. int height = 256;
  90. m_data->m_texelsRGB.resize(width * height * 3);
  91. for (int i = 0; i < width; i++)
  92. for (int j = 0; j < height; j++)
  93. {
  94. m_data->m_texelsRGB[(i + j * width) * 3 + 0] = 155;
  95. m_data->m_texelsRGB[(i + j * width) * 3 + 1] = 155;
  96. m_data->m_texelsRGB[(i + j * width) * 3 + 2] = 255;
  97. }
  98. m_data->m_testCanvas = new TestCanvasInterface2(m_data->m_texelsRGB, width, height);
  99. m_data->m_timeSeries = new TimeSeriesCanvas(m_data->m_testCanvas, width, height, "time series");
  100. bool clearCanvas = false;
  101. m_data->m_timeSeries->setupTimeSeries(3, 100, 0, clearCanvas);
  102. m_data->m_timeSeries->addDataSource("Some sine wave", 255, 0, 0);
  103. m_data->m_timeSeries->addDataSource("Some cosine wave", 0, 255, 0);
  104. m_data->m_timeSeries->addDataSource("Delta Time (*10)", 0, 0, 255);
  105. m_data->m_timeSeries->addDataSource("Tan", 255, 0, 255);
  106. m_data->m_timeSeries->addDataSource("Some cosine wave2", 255, 255, 0);
  107. m_data->m_timeSeries->addDataSource("Empty source2", 255, 0, 255);
  108. m_data->m_textureId = m_data->m_renderer->registerTexture(&m_data->m_texelsRGB[0], width, height);
  109. {
  110. const char* fileName = "cube.obj"; //"textured_sphere_smooth.obj";
  111. //fileName = "cube.obj";
  112. int shapeId = -1;
  113. b3ImportMeshData meshData;
  114. b3BulletDefaultFileIO fileIO;
  115. if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(fileName, meshData,&fileIO))
  116. {
  117. shapeId = m_data->m_renderer->registerShape(&meshData.m_gfxShape->m_vertices->at(0).xyzw[0],
  118. meshData.m_gfxShape->m_numvertices,
  119. &meshData.m_gfxShape->m_indices->at(0),
  120. meshData.m_gfxShape->m_numIndices,
  121. B3_GL_TRIANGLES,
  122. m_data->m_textureId);
  123. float position[4] = {0, 0, 2, 1};
  124. float orn[4] = {0, 0, 0, 1};
  125. float color[4] = {1, 1, 1, 1};
  126. float scaling[4] = {.1, .1, .1, 1};
  127. m_data->m_gfxObjectId = m_data->m_renderer->registerGraphicsInstance(shapeId, position, orn, color, scaling);
  128. m_data->m_renderer->writeTransforms();
  129. meshData.m_gfxShape->m_scaling[0] = scaling[0];
  130. meshData.m_gfxShape->m_scaling[1] = scaling[1];
  131. meshData.m_gfxShape->m_scaling[2] = scaling[2];
  132. delete meshData.m_gfxShape;
  133. if (!meshData.m_isCached)
  134. {
  135. free(meshData.m_textureImage1);
  136. }
  137. }
  138. }
  139. }
  140. m_data->m_renderer->writeTransforms();
  141. return true;
  142. }
  143. void TinyVRGui::tick(b3Scalar deltaTime, const b3Transform& guiWorldTransform)
  144. {
  145. float time = m_data->m_timeSeries->getCurrentTime();
  146. float v = sinf(time);
  147. m_data->m_timeSeries->insertDataAtCurrentTime(v, 0, true);
  148. v = cosf(time);
  149. m_data->m_timeSeries->insertDataAtCurrentTime(v, 1, true);
  150. v = tanf(time);
  151. m_data->m_timeSeries->insertDataAtCurrentTime(v, 3, true);
  152. m_data->m_timeSeries->insertDataAtCurrentTime(deltaTime * 10, 2, true);
  153. m_data->m_timeSeries->nextTick();
  154. m_data->m_renderer->updateTexture(m_data->m_textureId, &m_data->m_texelsRGB[0]);
  155. m_data->m_renderer->writeSingleInstanceTransformToCPU(guiWorldTransform.getOrigin(), guiWorldTransform.getRotation(), m_data->m_gfxObjectId);
  156. m_data->m_renderer->writeTransforms();
  157. }
  158. void TinyVRGui::clearTextArea()
  159. {
  160. int width = 256;
  161. int height = 50;
  162. for (int i = 0; i < width; i++)
  163. for (int j = 0; j < height; j++)
  164. {
  165. m_data->m_texelsRGB[(i + j * width) * 3 + 0] = 155;
  166. m_data->m_texelsRGB[(i + j * width) * 3 + 1] = 155;
  167. m_data->m_texelsRGB[(i + j * width) * 3 + 2] = 255;
  168. }
  169. }
  170. void TinyVRGui::grapicalPrintf(const char* str, int rasterposx, int rasterposy, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
  171. {
  172. m_data->m_timeSeries->grapicalPrintf(str, sTimeSeriesFontData, rasterposx, rasterposy, red, green, blue, alpha);
  173. }