The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

396 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. ==============================================================================
  5. */
  6. #ifndef MAINCOMPONENT_H_INCLUDED
  7. #define MAINCOMPONENT_H_INCLUDED
  8. #include "../JuceLibraryCode/JuceHeader.h"
  9. #include "Resources/WavefrontObjParser.h"
  10. //==============================================================================
  11. /*
  12. This component lives inside our window, and this is where you should put all
  13. your controls and content.
  14. */
  15. class MainContentComponent : public OpenGLAppComponent
  16. {
  17. public:
  18. //==============================================================================
  19. MainContentComponent()
  20. {
  21. setSize (800, 600);
  22. }
  23. ~MainContentComponent()
  24. {
  25. shutdownOpenGL();
  26. }
  27. void initialise() override
  28. {
  29. createShaders();
  30. }
  31. void shutdown() override
  32. {
  33. shader = nullptr;
  34. shape = nullptr;
  35. attributes = nullptr;
  36. uniforms = nullptr;
  37. }
  38. Matrix3D<float> getProjectionMatrix() const
  39. {
  40. float w = 1.0f / (0.5f + 0.1f);
  41. float h = w * getLocalBounds().toFloat().getAspectRatio (false);
  42. return Matrix3D<float>::fromFrustum (-w, w, -h, h, 4.0f, 30.0f);
  43. }
  44. Matrix3D<float> getViewMatrix() const
  45. {
  46. Matrix3D<float> viewMatrix (Vector3D<float> (0.0f, 0.0f, -10.0f));
  47. Matrix3D<float> rotationMatrix
  48. = viewMatrix.rotated (Vector3D<float> (-0.3f, 5.0f * std::sin (getFrameCounter() * 0.01f), 0.0f));
  49. return rotationMatrix * viewMatrix;
  50. }
  51. void render() override
  52. {
  53. jassert (OpenGLHelpers::isContextActive());
  54. const float desktopScale = (float) openGLContext.getRenderingScale();
  55. OpenGLHelpers::clear (Colour::greyLevel (0.1f));
  56. glEnable (GL_BLEND);
  57. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  58. glViewport (0, 0, roundToInt (desktopScale * getWidth()), roundToInt (desktopScale * getHeight()));
  59. shader->use();
  60. if (uniforms->projectionMatrix != nullptr)
  61. uniforms->projectionMatrix->setMatrix4 (getProjectionMatrix().mat, 1, false);
  62. if (uniforms->viewMatrix != nullptr)
  63. uniforms->viewMatrix->setMatrix4 (getViewMatrix().mat, 1, false);
  64. shape->draw (openGLContext, *attributes);
  65. // Reset the element buffers so child Components draw correctly
  66. openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
  67. openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
  68. }
  69. void paint (Graphics& g) override
  70. {
  71. // You can add your component specific drawing code here!
  72. // This will draw over the top of the openGL background.
  73. g.setColour(Colours::white);
  74. g.setFont (20);
  75. g.drawText ("OpenGL Example", 25, 20, 300, 30, Justification::left);
  76. g.drawLine (20, 20, 170, 20);
  77. g.drawLine (20, 50, 170, 50);
  78. }
  79. void resized() override
  80. {
  81. // This is called when the MainContentComponent is resized.
  82. // If you add any child components, this is where you should
  83. // update their positions.
  84. }
  85. void createShaders()
  86. {
  87. vertexShader =
  88. "attribute vec4 position;\n"
  89. "attribute vec4 sourceColour;\n"
  90. "attribute vec2 texureCoordIn;\n"
  91. "\n"
  92. "uniform mat4 projectionMatrix;\n"
  93. "uniform mat4 viewMatrix;\n"
  94. "\n"
  95. "varying vec4 destinationColour;\n"
  96. "varying vec2 textureCoordOut;\n"
  97. "\n"
  98. "void main()\n"
  99. "{\n"
  100. " destinationColour = sourceColour;\n"
  101. " textureCoordOut = texureCoordIn;\n"
  102. " gl_Position = projectionMatrix * viewMatrix * position;\n"
  103. "}\n";
  104. fragmentShader =
  105. #if JUCE_OPENGL_ES
  106. "varying lowp vec4 destinationColour;\n"
  107. "varying lowp vec2 textureCoordOut;\n"
  108. #else
  109. "varying vec4 destinationColour;\n"
  110. "varying vec2 textureCoordOut;\n"
  111. #endif
  112. "\n"
  113. "void main()\n"
  114. "{\n"
  115. #if JUCE_OPENGL_ES
  116. " lowp vec4 colour = vec4(0.95, 0.57, 0.03, 0.7);\n"
  117. #else
  118. " vec4 colour = vec4(0.95, 0.57, 0.03, 0.7);\n"
  119. #endif
  120. " gl_FragColor = colour;\n"
  121. "}\n";
  122. ScopedPointer<OpenGLShaderProgram> newShader (new OpenGLShaderProgram (openGLContext));
  123. String statusText;
  124. if (newShader->addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (vertexShader))
  125. && newShader->addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))
  126. && newShader->link())
  127. {
  128. shape = nullptr;
  129. attributes = nullptr;
  130. uniforms = nullptr;
  131. shader = newShader;
  132. shader->use();
  133. shape = new Shape (openGLContext);
  134. attributes = new Attributes (openGLContext, *shader);
  135. uniforms = new Uniforms (openGLContext, *shader);
  136. statusText = "GLSL: v" + String (OpenGLShaderProgram::getLanguageVersion(), 2);
  137. }
  138. else
  139. {
  140. statusText = newShader->getLastError();
  141. }
  142. }
  143. private:
  144. //==============================================================================
  145. struct Vertex
  146. {
  147. float position[3];
  148. float normal[3];
  149. float colour[4];
  150. float texCoord[2];
  151. };
  152. //==============================================================================
  153. // This class just manages the attributes that the shaders use.
  154. struct Attributes
  155. {
  156. Attributes (OpenGLContext& openGLContext, OpenGLShaderProgram& shaderProgram)
  157. {
  158. position = createAttribute (openGLContext, shaderProgram, "position");
  159. normal = createAttribute (openGLContext, shaderProgram, "normal");
  160. sourceColour = createAttribute (openGLContext, shaderProgram, "sourceColour");
  161. texureCoordIn = createAttribute (openGLContext, shaderProgram, "texureCoordIn");
  162. }
  163. void enable (OpenGLContext& openGLContext)
  164. {
  165. if (position != nullptr)
  166. {
  167. openGLContext.extensions.glVertexAttribPointer (position->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof (Vertex), 0);
  168. openGLContext.extensions.glEnableVertexAttribArray (position->attributeID);
  169. }
  170. if (normal != nullptr)
  171. {
  172. openGLContext.extensions.glVertexAttribPointer (normal->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof (Vertex), (GLvoid*) (sizeof (float) * 3));
  173. openGLContext.extensions.glEnableVertexAttribArray (normal->attributeID);
  174. }
  175. if (sourceColour != nullptr)
  176. {
  177. openGLContext.extensions.glVertexAttribPointer (sourceColour->attributeID, 4, GL_FLOAT, GL_FALSE, sizeof (Vertex), (GLvoid*) (sizeof (float) * 6));
  178. openGLContext.extensions.glEnableVertexAttribArray (sourceColour->attributeID);
  179. }
  180. if (texureCoordIn != nullptr)
  181. {
  182. openGLContext.extensions.glVertexAttribPointer (texureCoordIn->attributeID, 2, GL_FLOAT, GL_FALSE, sizeof (Vertex), (GLvoid*) (sizeof (float) * 10));
  183. openGLContext.extensions.glEnableVertexAttribArray (texureCoordIn->attributeID);
  184. }
  185. }
  186. void disable (OpenGLContext& openGLContext)
  187. {
  188. if (position != nullptr) openGLContext.extensions.glDisableVertexAttribArray (position->attributeID);
  189. if (normal != nullptr) openGLContext.extensions.glDisableVertexAttribArray (normal->attributeID);
  190. if (sourceColour != nullptr) openGLContext.extensions.glDisableVertexAttribArray (sourceColour->attributeID);
  191. if (texureCoordIn != nullptr) openGLContext.extensions.glDisableVertexAttribArray (texureCoordIn->attributeID);
  192. }
  193. ScopedPointer<OpenGLShaderProgram::Attribute> position, normal, sourceColour, texureCoordIn;
  194. private:
  195. static OpenGLShaderProgram::Attribute* createAttribute (OpenGLContext& openGLContext,
  196. OpenGLShaderProgram& shader,
  197. const char* attributeName)
  198. {
  199. if (openGLContext.extensions.glGetAttribLocation (shader.getProgramID(), attributeName) < 0)
  200. return nullptr;
  201. return new OpenGLShaderProgram::Attribute (shader, attributeName);
  202. }
  203. };
  204. //==============================================================================
  205. // This class just manages the uniform values that the demo shaders use.
  206. struct Uniforms
  207. {
  208. Uniforms (OpenGLContext& openGLContext, OpenGLShaderProgram& shaderProgram)
  209. {
  210. projectionMatrix = createUniform (openGLContext, shaderProgram, "projectionMatrix");
  211. viewMatrix = createUniform (openGLContext, shaderProgram, "viewMatrix");
  212. }
  213. ScopedPointer<OpenGLShaderProgram::Uniform> projectionMatrix, viewMatrix;
  214. private:
  215. static OpenGLShaderProgram::Uniform* createUniform (OpenGLContext& openGLContext,
  216. OpenGLShaderProgram& shaderProgram,
  217. const char* uniformName)
  218. {
  219. if (openGLContext.extensions.glGetUniformLocation (shaderProgram.getProgramID(), uniformName) < 0)
  220. return nullptr;
  221. return new OpenGLShaderProgram::Uniform (shaderProgram, uniformName);
  222. }
  223. };
  224. //==============================================================================
  225. /** This loads a 3D model from an OBJ file and converts it into some vertex buffers
  226. that we can draw.
  227. */
  228. struct Shape
  229. {
  230. Shape (OpenGLContext& openGLContext)
  231. {
  232. if (shapeFile.load (BinaryData::teapot_obj).wasOk())
  233. for (int i = 0; i < shapeFile.shapes.size(); ++i)
  234. vertexBuffers.add (new VertexBuffer (openGLContext, *shapeFile.shapes.getUnchecked(i)));
  235. }
  236. void draw (OpenGLContext& openGLContext, Attributes& glAttributes)
  237. {
  238. for (int i = 0; i < vertexBuffers.size(); ++i)
  239. {
  240. VertexBuffer& vertexBuffer = *vertexBuffers.getUnchecked (i);
  241. vertexBuffer.bind();
  242. glAttributes.enable (openGLContext);
  243. glDrawElements (GL_TRIANGLES, vertexBuffer.numIndices, GL_UNSIGNED_INT, 0);
  244. glAttributes.disable (openGLContext);
  245. }
  246. }
  247. private:
  248. struct VertexBuffer
  249. {
  250. VertexBuffer (OpenGLContext& context, WavefrontObjFile::Shape& aShape) : openGLContext (context)
  251. {
  252. numIndices = aShape.mesh.indices.size();
  253. openGLContext.extensions.glGenBuffers (1, &vertexBuffer);
  254. openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
  255. Array<Vertex> vertices;
  256. createVertexListFromMesh (aShape.mesh, vertices, Colours::green);
  257. openGLContext.extensions.glBufferData (GL_ARRAY_BUFFER,
  258. static_cast<GLsizeiptr> (static_cast<size_t> (vertices.size()) * sizeof (Vertex)),
  259. vertices.getRawDataPointer(), GL_STATIC_DRAW);
  260. openGLContext.extensions.glGenBuffers (1, &indexBuffer);
  261. openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  262. openGLContext.extensions.glBufferData (GL_ELEMENT_ARRAY_BUFFER,
  263. static_cast<GLsizeiptr> (static_cast<size_t> (numIndices) * sizeof (juce::uint32)),
  264. aShape.mesh.indices.getRawDataPointer(), GL_STATIC_DRAW);
  265. }
  266. ~VertexBuffer()
  267. {
  268. openGLContext.extensions.glDeleteBuffers (1, &vertexBuffer);
  269. openGLContext.extensions.glDeleteBuffers (1, &indexBuffer);
  270. }
  271. void bind()
  272. {
  273. openGLContext.extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
  274. openGLContext.extensions.glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  275. }
  276. GLuint vertexBuffer, indexBuffer;
  277. int numIndices;
  278. OpenGLContext& openGLContext;
  279. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VertexBuffer)
  280. };
  281. WavefrontObjFile shapeFile;
  282. OwnedArray<VertexBuffer> vertexBuffers;
  283. static void createVertexListFromMesh (const WavefrontObjFile::Mesh& mesh, Array<Vertex>& list, Colour colour)
  284. {
  285. const float scale = 0.2f;
  286. WavefrontObjFile::TextureCoord defaultTexCoord = { 0.5f, 0.5f };
  287. WavefrontObjFile::Vertex defaultNormal = { 0.5f, 0.5f, 0.5f };
  288. for (int i = 0; i < mesh.vertices.size(); ++i)
  289. {
  290. const WavefrontObjFile::Vertex& v = mesh.vertices.getReference (i);
  291. const WavefrontObjFile::Vertex& n
  292. = i < mesh.normals.size() ? mesh.normals.getReference (i) : defaultNormal;
  293. const WavefrontObjFile::TextureCoord& tc
  294. = i < mesh.textureCoords.size() ? mesh.textureCoords.getReference (i) : defaultTexCoord;
  295. Vertex vert =
  296. {
  297. { scale * v.x, scale * v.y, scale * v.z, },
  298. { scale * n.x, scale * n.y, scale * n.z, },
  299. { colour.getFloatRed(), colour.getFloatGreen(), colour.getFloatBlue(), colour.getFloatAlpha() },
  300. { tc.x, tc.y }
  301. };
  302. list.add (vert);
  303. }
  304. }
  305. };
  306. const char* vertexShader;
  307. const char* fragmentShader;
  308. ScopedPointer<OpenGLShaderProgram> shader;
  309. ScopedPointer<Shape> shape;
  310. ScopedPointer<Attributes> attributes;
  311. ScopedPointer<Uniforms> uniforms;
  312. String newVertexShader, newFragmentShader;
  313. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
  314. };
  315. // (This function is called by the app startup code to create our main component)
  316. Component* createMainContentComponent() { return new MainContentComponent(); }
  317. #endif // MAINCOMPONENT_H_INCLUDED