Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ==============================================================================
- This is an automatically generated GUI class created by the Introjucer!
- Be careful when adding custom code to these files, as only the code within
- the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
- and re-saved.
- Created with Introjucer version: 3.1.1
- ------------------------------------------------------------------------------
- The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions"
- Copyright 2004-13 by Raw Material Software Ltd.
- ==============================================================================
- */
- //[Headers] You can add your own extra header files here...
- //[/Headers]
- #include "HeadAnimation.h"
- //[MiscUserDefs] You can add your own user definitions and misc code here...
- //[/MiscUserDefs]
- //==============================================================================
- HeadAnimation::HeadAnimation ()
- {
- //[UserPreSize]
- //[/UserPreSize]
- setSize (600, 400);
- //[Constructor] You can add your own custom stuff here..
- setOpaque(true);
- openGLContext.setRenderer(this);
- openGLContext.attachTo(*this);
- openGLContext.setContinuousRepainting(true);
- //[/Constructor]
- }
- HeadAnimation::~HeadAnimation()
- {
- //[Destructor_pre]. You can add your own custom destruction code here..
- //[/Destructor_pre]
- //[Destructor]. You can add your own custom destruction code here..
- //[/Destructor]
- }
- //==============================================================================
- void HeadAnimation::paint (Graphics& g)
- {
- //[UserPrePaint] Add your own custom painting code here..
- //[/UserPrePaint]
- g.fillAll (Colour (0xffaaaaaa));
- //[UserPaint] Add your own custom painting code here..
- //[/UserPaint]
- }
- void HeadAnimation::resized()
- {
- //[UserPreResize] Add your own custom resize code here..
- //[/UserPreResize]
- //[UserResized] Add your own custom resize handling here..
- //[/UserResized]
- }
- //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
- struct Vertex
- {
- float position[3];
- float normal[3];
- float colour[4];
- float texCoord[2];
- };
- struct Attributes
- {
- Attributes(OpenGLContext& openGLContext, OpenGLShaderProgram& shader)
- {
- position = createAttribute(openGLContext, shader, "position");
- normal = createAttribute(openGLContext, shader, "normal");
- sourceColour = createAttribute(openGLContext, shader, "sourceColour");
- texureCoordIn = createAttribute(openGLContext, shader, "texureCoordIn");
- }
- void enable(OpenGLContext& openGLContext)
- {
- if (position != nullptr)
- {
- openGLContext.extensions.glVertexAttribPointer(position->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
- openGLContext.extensions.glEnableVertexAttribArray(position->attributeID);
- }
- if (normal != nullptr)
- {
- openGLContext.extensions.glVertexAttribPointer(normal->attributeID, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 3));
- openGLContext.extensions.glEnableVertexAttribArray(normal->attributeID);
- }
- if (sourceColour != nullptr)
- {
- openGLContext.extensions.glVertexAttribPointer(sourceColour->attributeID, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 6));
- openGLContext.extensions.glEnableVertexAttribArray(sourceColour->attributeID);
- }
- if (texureCoordIn != nullptr)
- {
- openGLContext.extensions.glVertexAttribPointer(texureCoordIn->attributeID, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 10));
- openGLContext.extensions.glEnableVertexAttribArray(texureCoordIn->attributeID);
- }
- }
- void disable(OpenGLContext& openGLContext)
- {
- if (position != nullptr) openGLContext.extensions.glDisableVertexAttribArray(position->attributeID);
- if (normal != nullptr) openGLContext.extensions.glDisableVertexAttribArray(normal->attributeID);
- if (sourceColour != nullptr) openGLContext.extensions.glDisableVertexAttribArray(sourceColour->attributeID);
- if (texureCoordIn != nullptr) openGLContext.extensions.glDisableVertexAttribArray(texureCoordIn->attributeID);
- }
- ScopedPointer<OpenGLShaderProgram::Attribute> position, normal, sourceColour, texureCoordIn;
- private:
- static OpenGLShaderProgram::Attribute* createAttribute(OpenGLContext& openGLContext,
- OpenGLShaderProgram& shader,
- const char* attributeName)
- {
- if (openGLContext.extensions.glGetAttribLocation(shader.getProgramID(), attributeName) < 0)
- return nullptr;
- return new OpenGLShaderProgram::Attribute(shader, attributeName);
- }
- };
- struct Uniforms
- {
- Uniforms(OpenGLContext& openGLContext, OpenGLShaderProgram& shader)
- {
- projectionMatrix = createUniform(openGLContext, shader, "projectionMatrix");
- viewMatrix = createUniform(openGLContext, shader, "viewMatrix");
- texture = createUniform(openGLContext, shader, "demoTexture");
- lightPosition = createUniform(openGLContext, shader, "lightPosition");
- bouncingNumber = createUniform(openGLContext, shader, "bouncingNumber");
- }
- ScopedPointer<OpenGLShaderProgram::Uniform> projectionMatrix, viewMatrix, texture, lightPosition, bouncingNumber;
- private:
- static OpenGLShaderProgram::Uniform* createUniform(OpenGLContext& openGLContext,
- OpenGLShaderProgram& shader,
- const char* uniformName)
- {
- if (openGLContext.extensions.glGetUniformLocation(shader.getProgramID(), uniformName) < 0)
- return nullptr;
- return new OpenGLShaderProgram::Uniform(shader, uniformName);
- }
- };
- struct Shape
- {
- Shape(OpenGLContext& openGLContext)
- {
- if (shapeFile.load(BinaryData::teapot_obj).wasOk())
- for (int i = 0; i < shapeFile.shapes.size(); ++i)
- vertexBuffers.add(new VertexBuffer(openGLContext, *shapeFile.shapes.getUnchecked(i)));
- }
- void draw(OpenGLContext& openGLContext, Attributes& attributes)
- {
- for (int i = 0; i < vertexBuffers.size(); ++i)
- {
- VertexBuffer& vertexBuffer = *vertexBuffers.getUnchecked(i);
- vertexBuffer.bind();
- attributes.enable(openGLContext);
- glDrawElements(GL_TRIANGLES, vertexBuffer.numIndices, GL_UNSIGNED_INT, 0);
- attributes.disable(openGLContext);
- }
- }
- private:
- struct VertexBuffer
- {
- VertexBuffer(OpenGLContext& context, WavefrontObjFile::Shape& shape) : openGLContext(context)
- {
- numIndices = shape.mesh.indices.size();
- openGLContext.extensions.glGenBuffers(1, &vertexBuffer);
- openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- Array<Vertex> vertices;
- createVertexListFromMesh(shape.mesh, vertices, Colours::green);
- openGLContext.extensions.glBufferData(GL_ARRAY_BUFFER, vertices.size() * (int) sizeof(Vertex),
- vertices.getRawDataPointer(), GL_STATIC_DRAW);
- openGLContext.extensions.glGenBuffers(1, &indexBuffer);
- openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- openGLContext.extensions.glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * (int) sizeof(juce::uint32),
- shape.mesh.indices.getRawDataPointer(), GL_STATIC_DRAW);
- }
- ~VertexBuffer()
- {
- openGLContext.extensions.glDeleteBuffers(1, &vertexBuffer);
- openGLContext.extensions.glDeleteBuffers(1, &indexBuffer);
- }
- void bind()
- {
- openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- }
- GLuint vertexBuffer, indexBuffer;
- int numIndices;
- OpenGLContext& openGLContext;
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VertexBuffer)
- };
- WavefrontObjFile shapeFile;
- OwnedArray<VertexBuffer> vertexBuffers;
- static void createVertexListFromMesh(const WavefrontObjFile::Mesh& mesh, Array<Vertex>& list, Colour colour)
- {
- const float scale = 0.2f;
- WavefrontObjFile::TextureCoord defaultTexCoord = { 0.5f, 0.5f };
- WavefrontObjFile::Vertex defaultNormal = { 0.5f, 0.5f, 0.5f };
- for (int i = 0; i < mesh.vertices.size(); ++i)
- {
- const WavefrontObjFile::Vertex& v = mesh.vertices.getReference(i);
- const WavefrontObjFile::Vertex& n
- = i < mesh.normals.size() ? mesh.normals.getReference(i) : defaultNormal;
- const WavefrontObjFile::TextureCoord& tc
- = i < mesh.textureCoords.size() ? mesh.textureCoords.getReference(i) : defaultTexCoord;
- Vertex vert =
- {
- { scale * v.x, scale * v.y, scale * v.z, },
- { scale * n.x, scale * n.y, scale * n.z, },
- { colour.getFloatRed(), colour.getFloatGreen(), colour.getFloatBlue(), colour.getFloatAlpha() },
- { tc.x, tc.y }
- };
- list.add(vert);
- }
- }
- };
- void HeadAnimation::renderOpenGL()
- {
- jassert(OpenGLHelpers::isContextActive());
- const float desktopScale = (float)openGLContext.getRenderingScale();
- OpenGLHelpers::clear(Colours::lightblue);
- //if (textureToUse != nullptr)
- // if (!textureToUse->applyTo(texture))
- // textureToUse = nullptr;
- // First draw our background graphics to demonstrate the OpenGLGraphicsContext class
- //if (doBackgroundDrawing)
- // drawBackground2DStuff(desktopScale);
- //updateShader(); // Check whether we need to compile a new shader
- if (shader == nullptr)
- return;
- // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
- // we need to initialise some important settings before doing our normal GL 3D drawing..
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- openGLContext.extensions.glActiveTexture(GL_TEXTURE0);
- glEnable(GL_TEXTURE_2D);
- glViewport(0, 0, roundToInt(desktopScale * getWidth()), roundToInt(desktopScale * getHeight()));
- texture.bind();
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- shader->use();
- if (uniforms->projectionMatrix != nullptr)
- uniforms->projectionMatrix->setMatrix4(getProjectionMatrix().mat, 1, false);
- if (uniforms->viewMatrix != nullptr)
- uniforms->viewMatrix->setMatrix4(getViewMatrix().mat, 1, false);
- if (uniforms->texture != nullptr)
- uniforms->texture->set((GLint)0);
- if (uniforms->lightPosition != nullptr)
- uniforms->lightPosition->set(-15.0f, 10.0f, 15.0f, 0.0f);
- //if (uniforms->bouncingNumber != nullptr)
- // uniforms->bouncingNumber->set(bouncingNumber.getValue());
- shape->draw(openGLContext, *attributes);
- // Reset the element buffers so child Components draw correctly
- openGLContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
- openGLContext.extensions.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- /*if (!controlsOverlay->isMouseButtonDown())
- rotation += (float)rotationSpeed;*/
- }
- void HeadAnimation::newOpenGLContextCreated()
- {
- // nothing to do in this case - we'll initialise our shaders + textures
- // on demand, during the render callback.
- freeAllContextObjects();
- //if (controlsOverlay != nullptr) controlsOverlay->updateShader();
- }
- void HeadAnimation::openGLContextClosing()
- {
- // When the context is about to close, you must use this callback to delete
- // any GPU resources while the context is still current.
- freeAllContextObjects();
- //if (lastTexture != nullptr) setTexture(lastTexture);
- }
- void HeadAnimation::freeAllContextObjects()
- {
- shape = nullptr;
- shader = nullptr;
- attributes = nullptr;
- uniforms = nullptr;
- texture.release();
- }
- Matrix3D<float> HeadAnimation::getProjectionMatrix() const
- {
- float w = 1.0f / (scale + 0.1f);
- float h = w * getLocalBounds().toFloat().getAspectRatio(false);
- return Matrix3D<float>::fromFrustum(-w, w, -h, h, 4.0f, 30.0f);
- }
- Matrix3D<float> HeadAnimation::getViewMatrix() const
- {
- Matrix3D<float> viewMatrix = draggableOrientation.getRotationMatrix()
- * Vector3D<float>(0.0f, 1.0f, -10.0f);
- Matrix3D<float> rotationMatrix = viewMatrix.rotated(Vector3D<float>(rotation, rotation, -0.3f));
- return rotationMatrix * viewMatrix;
- }
- //[/MiscUserCode]
- //==============================================================================
- #if 0
- /* -- Introjucer information section --
- This is where the Introjucer stores the metadata that describe this GUI layout, so
- make changes in here at your peril!
- BEGIN_JUCER_METADATA
- <JUCER_COMPONENT documentType="Component" className="HeadAnimation" componentName=""
- parentClasses="public CPIComponent" constructorParams="" variableInitialisers=""
- snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
- fixedSize="0" initialWidth="600" initialHeight="400">
- <BACKGROUND backgroundColour="ffaaaaaa"/>
- </JUCER_COMPONENT>
- END_JUCER_METADATA
- */
- #endif
- //[EndFile] You can add extra defines here...
- //[/EndFile]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement