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.

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