| void SquareComponent::newOpenGLContextCreated() | |
| { | |
| initialized = true; | |
| shaderProgram = new OpenGLShaderProgram(Settings::getInstance().getOpenGLContext()); | |
| // The source files for the shaders are in the directory Resources/Shaders | |
| #define NEW_LINE "\n" | |
| String vertexShader = "attribute vec4 Position;" NEW_LINE | |
| //"uniform vec4 Color1;" NEW_LINE | |
| //"uniform vec4 Color2;" NEW_LINE | |
| "void main(void)" NEW_LINE | |
| "{" NEW_LINE | |
| " gl_Position = Position * vec4(2.0, 2.0, 0.0, 1.0);" NEW_LINE | |
| "}" NEW_LINE; | |
| String fragmentShader = "uniform vec4 Color1;" NEW_LINE | |
| //"uniform vec4 Color2;" NEW_LINE | |
| "void main(void)" NEW_LINE | |
| "{" NEW_LINE | |
| " " //juce is ARGB, GL is RGBA. Destination is xyzw | |
| " gl_FragColor = vec4(Color1.r, Color1.g, Color1.b, Color1.a);" NEW_LINE | |
| "}" NEW_LINE; | |
| // Load the vertex/fragment shaders | |
| if( shaderProgram->addVertexShader(vertexShader) && | |
| shaderProgram->addFragmentShader(fragmentShader) && | |
| shaderProgram->link() ) | |
| { | |
| positionAttribute = new OpenGLShaderProgram::Attribute(*shaderProgram, "Position"); | |
| if( (shaderProgram->getUniformIDFromName("Color1") < 0) | |
| //|| (shaderProgram->getUniformIDFromName("Color2") < 0) | |
| ) | |
| { | |
| jassertfalse; //uniforms weren't created | |
| } | |
| color1Uniform = new OpenGLShaderProgram::Uniform(*shaderProgram, "Color1"); | |
| //color2Uniform = new OpenGLShaderProgram::Uniform(*shaderProgram, "Color2"); | |
| } | |
| } | |
| //render callback: | |
| void SquareComponent::renderOpenGL() | |
| { | |
| if (!initialized) | |
| { | |
| newOpenGLContextCreated(); | |
| } | |
| OpenGLHelpers::clear(Colours::white); | |
| glEnableClientState(GL_VERTEX_ARRAY); | |
| glEnable (GL_DEPTH_TEST); | |
| glDepthFunc (GL_LESS); | |
| glEnable (GL_BLEND); | |
| glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| glEnable (GL_TEXTURE_2D); | |
| // Draw the gradient for the background fill | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| shaderProgram->use(); | |
| const GLfloat squareVertices[] = { | |
| -0.5f, -0.5f, //LL | |
| 0.5f, -0.5f, //LR | |
| -0.5f, 0.5f, //UL | |
| 0.5f, 0.5f, //UR | |
| }; | |
| Colour inputColor1 = Colours::green; | |
| Colour inputColor2 = Colours::blue; | |
| // update attribute values | |
| glVertexAttribPointer(positionAttribute->attributeID, //ID of attribute | |
| 2, //number of components per generic vertex attribute | |
| GL_FLOAT, //data type of each component in the array | |
| GL_FALSE, //data should not be normalized, because data type is float | |
| 0, //the byte offset between consecutive generic vertex attributes. 0 means bytes are tightly packed | |
| squareVertices); //the data source for the attribute | |
| glEnableVertexAttribArray(positionAttribute->attributeID); | |
| shaderProgram->setUniform("Color1", | |
| inputColor1.getRed(), | |
| inputColor1.getGreen(), | |
| inputColor1.getBlue(), | |
| inputColor1.getAlpha()); | |
| // draw | |
| glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| } | |
| //shaderProgram->setUniform calls this: | |
| void OpenGLShaderProgram::setUniform (const char* name, GLint n1, GLint n2, GLint n3, GLint n4) noexcept | |
| { | |
| context.extensions.glUniform4i (getUniformIDFromName (name), n1, n2, n3, n4); | |
| } | |
| GLint OpenGLShaderProgram::getUniformIDFromName (const char* uniformName) const noexcept | |
| { | |
| // The shader program must be active when this method is called! | |
| jassert (programID != 0); | |
| return (GLint) context.extensions.glGetUniformLocation (programID, uniformName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment