Advertisement
Guest User

Untitled

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