Advertisement
Guest User

cpp file

a guest
Aug 23rd, 2016
83
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ==============================================================================
  3.  
  4. This is an automatically generated GUI class created by the Introjucer!
  5.  
  6. Be careful when adding custom code to these files, as only the code within
  7. the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  8. and re-saved.
  9.  
  10. Created with Introjucer version: 3.1.1
  11.  
  12. ------------------------------------------------------------------------------
  13.  
  14. The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions"
  15. Copyright 2004-13 by Raw Material Software Ltd.
  16.  
  17. ==============================================================================
  18. */
  19.  
  20. //[Headers] You can add your own extra header files here...
  21. //[/Headers]
  22.  
  23. #include "HeadAnimation.h"
  24.  
  25.  
  26. //[MiscUserDefs] You can add your own user definitions and misc code here...
  27. //[/MiscUserDefs]
  28.  
  29. //==============================================================================
  30. HeadAnimation::HeadAnimation ()
  31. {
  32.  
  33. //[UserPreSize]
  34. //[/UserPreSize]
  35.  
  36. setSize (600, 400);
  37.  
  38.  
  39. //[Constructor] You can add your own custom stuff here..
  40.  
  41. setOpaque(true);
  42. openGLContext.setRenderer(this);
  43. openGLContext.attachTo(*this);
  44. openGLContext.setContinuousRepainting(true);
  45.  
  46. //[/Constructor]
  47. }
  48.  
  49. HeadAnimation::~HeadAnimation()
  50. {
  51. //[Destructor_pre]. You can add your own custom destruction code here..
  52. //[/Destructor_pre]
  53.  
  54.  
  55.  
  56. //[Destructor]. You can add your own custom destruction code here..
  57. //[/Destructor]
  58. }
  59.  
  60. //==============================================================================
  61. void HeadAnimation::paint (Graphics& g)
  62. {
  63. //[UserPrePaint] Add your own custom painting code here..
  64. //[/UserPrePaint]
  65.  
  66. g.fillAll (Colour (0xffaaaaaa));
  67.  
  68. //[UserPaint] Add your own custom painting code here..
  69. //[/UserPaint]
  70. }
  71.  
  72. void HeadAnimation::resized()
  73. {
  74. //[UserPreResize] Add your own custom resize code here..
  75. //[/UserPreResize]
  76.  
  77. //[UserResized] Add your own custom resize handling here..
  78. //[/UserResized]
  79. }
  80.  
  81.  
  82.  
  83. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  84.  
  85. struct Vertex
  86. {
  87. float position[3];
  88. float normal[3];
  89. float colour[4];
  90. float texCoord[2];
  91. };
  92.  
  93. struct Attributes
  94. {
  95. Attributes(OpenGLContext& openGLContext, OpenGLShaderProgram& shader)
  96. {
  97. position = createAttribute(openGLContext, shader, "position");
  98. normal = createAttribute(openGLContext, shader, "normal");
  99. sourceColour = createAttribute(openGLContext, shader, "sourceColour");
  100. texureCoordIn = createAttribute(openGLContext, shader, "texureCoordIn");
  101. }
  102.  
  103. void enable(OpenGLContext& openGLContext)
  104. {
  105. if (position != nullptr)
  106. {
  107. openGLContext.extensions.glVertexAttribPointer(position->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
  108. openGLContext.extensions.glEnableVertexAttribArray(position->attributeID);
  109. }
  110.  
  111. if (normal != nullptr)
  112. {
  113. openGLContext.extensions.glVertexAttribPointer(normal->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 3));
  114. openGLContext.extensions.glEnableVertexAttribArray(normal->attributeID);
  115. }
  116.  
  117. if (sourceColour != nullptr)
  118. {
  119. openGLContext.extensions.glVertexAttribPointer(sourceColour->attributeID, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 6));
  120. openGLContext.extensions.glEnableVertexAttribArray(sourceColour->attributeID);
  121. }
  122.  
  123. if (texureCoordIn != nullptr)
  124. {
  125. openGLContext.extensions.glVertexAttribPointer(texureCoordIn->attributeID, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 10));
  126. openGLContext.extensions.glEnableVertexAttribArray(texureCoordIn->attributeID);
  127. }
  128. }
  129.  
  130. void disable(OpenGLContext& openGLContext)
  131. {
  132. if (position != nullptr) openGLContext.extensions.glDisableVertexAttribArray(position->attributeID);
  133. if (normal != nullptr) openGLContext.extensions.glDisableVertexAttribArray(normal->attributeID);
  134. if (sourceColour != nullptr) openGLContext.extensions.glDisableVertexAttribArray(sourceColour->attributeID);
  135. if (texureCoordIn != nullptr) openGLContext.extensions.glDisableVertexAttribArray(texureCoordIn->attributeID);
  136. }
  137.  
  138. ScopedPointer<OpenGLShaderProgram::Attribute> position, normal, sourceColour, texureCoordIn;
  139.  
  140. private:
  141. static OpenGLShaderProgram::Attribute* createAttribute(OpenGLContext& openGLContext,
  142. OpenGLShaderProgram& shader,
  143. const char* attributeName)
  144. {
  145. if (openGLContext.extensions.glGetAttribLocation(shader.getProgramID(), attributeName) < 0)
  146. return nullptr;
  147.  
  148. return new OpenGLShaderProgram::Attribute(shader, attributeName);
  149. }
  150. };
  151.  
  152. struct Uniforms
  153. {
  154. Uniforms(OpenGLContext& openGLContext, OpenGLShaderProgram& shader)
  155. {
  156. projectionMatrix = createUniform(openGLContext, shader, "projectionMatrix");
  157. viewMatrix = createUniform(openGLContext, shader, "viewMatrix");
  158. texture = createUniform(openGLContext, shader, "demoTexture");
  159. lightPosition = createUniform(openGLContext, shader, "lightPosition");
  160. bouncingNumber = createUniform(openGLContext, shader, "bouncingNumber");
  161. }
  162.  
  163. ScopedPointer<OpenGLShaderProgram::Uniform> projectionMatrix, viewMatrix, texture, lightPosition, bouncingNumber;
  164.  
  165. private:
  166. static OpenGLShaderProgram::Uniform* createUniform(OpenGLContext& openGLContext,
  167. OpenGLShaderProgram& shader,
  168. const char* uniformName)
  169. {
  170. if (openGLContext.extensions.glGetUniformLocation(shader.getProgramID(), uniformName) < 0)
  171. return nullptr;
  172.  
  173. return new OpenGLShaderProgram::Uniform(shader, uniformName);
  174. }
  175. };
  176.  
  177. struct Shape
  178. {
  179. Shape(OpenGLContext& openGLContext)
  180. {
  181. if (shapeFile.load(BinaryData::teapot_obj).wasOk())
  182. for (int i = 0; i < shapeFile.shapes.size(); ++i)
  183. vertexBuffers.add(new VertexBuffer(openGLContext, *shapeFile.shapes.getUnchecked(i)));
  184.  
  185. }
  186.  
  187. void draw(OpenGLContext& openGLContext, Attributes& attributes)
  188. {
  189. for (int i = 0; i < vertexBuffers.size(); ++i)
  190. {
  191. VertexBuffer& vertexBuffer = *vertexBuffers.getUnchecked(i);
  192. vertexBuffer.bind();
  193.  
  194. attributes.enable(openGLContext);
  195. glDrawElements(GL_TRIANGLES, vertexBuffer.numIndices, GL_UNSIGNED_INT, 0);
  196. attributes.disable(openGLContext);
  197. }
  198. }
  199.  
  200. private:
  201. struct VertexBuffer
  202. {
  203. VertexBuffer(OpenGLContext& context, WavefrontObjFile::Shape& shape) : openGLContext(context)
  204. {
  205. numIndices = shape.mesh.indices.size();
  206.  
  207. openGLContext.extensions.glGenBuffers(1, &vertexBuffer);
  208. openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  209.  
  210. Array<Vertex> vertices;
  211. createVertexListFromMesh(shape.mesh, vertices, Colours::green);
  212.  
  213. openGLContext.extensions.glBufferData(GL_ARRAY_BUFFER, vertices.size() * (int) sizeof(Vertex),
  214. vertices.getRawDataPointer(), GL_STATIC_DRAW);
  215.  
  216. openGLContext.extensions.glGenBuffers(1, &indexBuffer);
  217. openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  218. openGLContext.extensions.glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * (int) sizeof(juce::uint32),
  219. shape.mesh.indices.getRawDataPointer(), GL_STATIC_DRAW);
  220. }
  221.  
  222. ~VertexBuffer()
  223. {
  224. openGLContext.extensions.glDeleteBuffers(1, &vertexBuffer);
  225. openGLContext.extensions.glDeleteBuffers(1, &indexBuffer);
  226. }
  227.  
  228. void bind()
  229. {
  230. openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  231. openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  232. }
  233.  
  234. GLuint vertexBuffer, indexBuffer;
  235. int numIndices;
  236. OpenGLContext& openGLContext;
  237.  
  238. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VertexBuffer)
  239. };
  240.  
  241. WavefrontObjFile shapeFile;
  242. OwnedArray<VertexBuffer> vertexBuffers;
  243.  
  244. static void createVertexListFromMesh(const WavefrontObjFile::Mesh& mesh, Array<Vertex>& list, Colour colour)
  245. {
  246. const float scale = 0.2f;
  247. WavefrontObjFile::TextureCoord defaultTexCoord = { 0.5f, 0.5f };
  248. WavefrontObjFile::Vertex defaultNormal = { 0.5f, 0.5f, 0.5f };
  249.  
  250. for (int i = 0; i < mesh.vertices.size(); ++i)
  251. {
  252. const WavefrontObjFile::Vertex& v = mesh.vertices.getReference(i);
  253.  
  254. const WavefrontObjFile::Vertex& n
  255. = i < mesh.normals.size() ? mesh.normals.getReference(i) : defaultNormal;
  256.  
  257. const WavefrontObjFile::TextureCoord& tc
  258. = i < mesh.textureCoords.size() ? mesh.textureCoords.getReference(i) : defaultTexCoord;
  259.  
  260. Vertex vert =
  261. {
  262. { scale * v.x, scale * v.y, scale * v.z, },
  263. { scale * n.x, scale * n.y, scale * n.z, },
  264. { colour.getFloatRed(), colour.getFloatGreen(), colour.getFloatBlue(), colour.getFloatAlpha() },
  265. { tc.x, tc.y }
  266. };
  267.  
  268. list.add(vert);
  269. }
  270. }
  271. };
  272.  
  273. void HeadAnimation::renderOpenGL()
  274. {
  275. jassert(OpenGLHelpers::isContextActive());
  276.  
  277. const float desktopScale = (float)openGLContext.getRenderingScale();
  278. OpenGLHelpers::clear(Colours::lightblue);
  279.  
  280. //if (textureToUse != nullptr)
  281. // if (!textureToUse->applyTo(texture))
  282. // textureToUse = nullptr;
  283.  
  284. // First draw our background graphics to demonstrate the OpenGLGraphicsContext class
  285. //if (doBackgroundDrawing)
  286. // drawBackground2DStuff(desktopScale);
  287.  
  288. //updateShader(); // Check whether we need to compile a new shader
  289.  
  290. if (shader == nullptr)
  291. return;
  292.  
  293. // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
  294. // we need to initialise some important settings before doing our normal GL 3D drawing..
  295. glEnable(GL_DEPTH_TEST);
  296. glDepthFunc(GL_LESS);
  297. glEnable(GL_BLEND);
  298. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  299. openGLContext.extensions.glActiveTexture(GL_TEXTURE0);
  300. glEnable(GL_TEXTURE_2D);
  301.  
  302. glViewport(0, 0, roundToInt(desktopScale * getWidth()), roundToInt(desktopScale * getHeight()));
  303.  
  304. texture.bind();
  305.  
  306. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  307. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  308.  
  309. shader->use();
  310.  
  311. if (uniforms->projectionMatrix != nullptr)
  312. uniforms->projectionMatrix->setMatrix4(getProjectionMatrix().mat, 1, false);
  313.  
  314. if (uniforms->viewMatrix != nullptr)
  315. uniforms->viewMatrix->setMatrix4(getViewMatrix().mat, 1, false);
  316.  
  317. if (uniforms->texture != nullptr)
  318. uniforms->texture->set((GLint)0);
  319.  
  320. if (uniforms->lightPosition != nullptr)
  321. uniforms->lightPosition->set(-15.0f, 10.0f, 15.0f, 0.0f);
  322.  
  323. //if (uniforms->bouncingNumber != nullptr)
  324. // uniforms->bouncingNumber->set(bouncingNumber.getValue());
  325.  
  326. shape->draw(openGLContext, *attributes);
  327.  
  328. // Reset the element buffers so child Components draw correctly
  329. openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
  330. openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  331.  
  332. /*if (!controlsOverlay->isMouseButtonDown())
  333. rotation += (float)rotationSpeed;*/
  334. }
  335.  
  336. void HeadAnimation::newOpenGLContextCreated()
  337. {
  338. // nothing to do in this case - we'll initialise our shaders + textures
  339. // on demand, during the render callback.
  340. freeAllContextObjects();
  341.  
  342. //if (controlsOverlay != nullptr) controlsOverlay->updateShader();
  343. }
  344.  
  345. void HeadAnimation::openGLContextClosing()
  346. {
  347. // When the context is about to close, you must use this callback to delete
  348. // any GPU resources while the context is still current.
  349. freeAllContextObjects();
  350.  
  351. //if (lastTexture != nullptr) setTexture(lastTexture);
  352. }
  353.  
  354. void HeadAnimation::freeAllContextObjects()
  355. {
  356. shape = nullptr;
  357. shader = nullptr;
  358. attributes = nullptr;
  359. uniforms = nullptr;
  360. texture.release();
  361. }
  362.  
  363. Matrix3D<float> HeadAnimation::getProjectionMatrix() const
  364. {
  365. float w = 1.0f / (scale + 0.1f);
  366. float h = w * getLocalBounds().toFloat().getAspectRatio(false);
  367. return Matrix3D<float>::fromFrustum(-w, w, -h, h, 4.0f, 30.0f);
  368. }
  369.  
  370. Matrix3D<float> HeadAnimation::getViewMatrix() const
  371. {
  372. Matrix3D<float> viewMatrix = draggableOrientation.getRotationMatrix()
  373. * Vector3D<float>(0.0f, 1.0f, -10.0f);
  374.  
  375. Matrix3D<float> rotationMatrix = viewMatrix.rotated(Vector3D<float>(rotation, rotation, -0.3f));
  376.  
  377. return rotationMatrix * viewMatrix;
  378. }
  379.  
  380. //[/MiscUserCode]
  381.  
  382.  
  383. //==============================================================================
  384. #if 0
  385. /* -- Introjucer information section --
  386.  
  387. This is where the Introjucer stores the metadata that describe this GUI layout, so
  388. make changes in here at your peril!
  389.  
  390. BEGIN_JUCER_METADATA
  391.  
  392. <JUCER_COMPONENT documentType="Component" className="HeadAnimation" componentName=""
  393. parentClasses="public CPIComponent" constructorParams="" variableInitialisers=""
  394. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
  395. fixedSize="0" initialWidth="600" initialHeight="400">
  396. <BACKGROUND backgroundColour="ffaaaaaa"/>
  397. </JUCER_COMPONENT>
  398.  
  399. END_JUCER_METADATA
  400. */
  401. #endif
  402.  
  403.  
  404. //[EndFile] You can add extra defines here...
  405. //[/EndFile]
Advertisement
RAW Paste Data Copied
Advertisement