Skip to content

Instantly share code, notes, and snippets.

@matkatmusic matkatmusic/opengl.cpp Secret
Created Mar 17, 2018

Embed
What would you like to do?
void SquareComponent::newOpenGLContextCreated()
{
initialized = true;
shaderProgram = new OpenGLShaderProgram(Settings::getInstance().getOpenGLContext());
// The source files for the shaders are in the directory Resources/Shaders
String vertexShader = "attribute vec4 Position; \r\n"
"attribute vec4 SourceColor; \r\n"
"varying vec4 DestinationColor; \r\n"
"void main(void) { \r\n"
" DestinationColor = SourceColor; \r\n"
" gl_Position = Position; \r\n"
"} \r\n";
String fragmentShader =
#if defined(OS_MACIOS) || defined(OS_ANDROID)
"varying lowp vec4 DestinationColor; \r\n"
#else
"varying vec4 DestinationColor; \r\n"
#endif
"void main(void) \r\n"
" { \r\n"
" gl_FragColor = DestinationColor; \r\n"
"} \r\n";
// Load the vertex/fragment shaders
if( shaderProgram->addVertexShader(vertexShader) &&
shaderProgram->addFragmentShader(fragmentShader) &&
shaderProgram->link() )
{
positionAttribute = new OpenGLShaderProgram::Attribute(*shaderProgram, "Position");
sourceColorAttribute = new OpenGLShaderProgram::Attribute(*shaderProgram, "SourceColor");
}
}
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,
0.5f, -0.5f,
-0.5f, 0.5f,
0.5f, 0.5f,
};
const GLubyte squareColors[] = {
0xFF, 0x00, 0x00, 255,
0xFF, 0x00, 0x00, 255,
0x00, 0x00, 0xFF, 255,
0x00, 0x00, 0xFF, 255,
};
// update attribute values
glVertexAttribPointer(positionAttribute->attributeID, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(positionAttribute->attributeID);
glVertexAttribPointer(sourceColorAttribute->attributeID, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); //enable the normalized flag
glEnableVertexAttribArray(sourceColorAttribute->attributeID);
// draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.