Skip to content

Instantly share code, notes, and snippets.

@sansumbrella
Created August 15, 2012 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansumbrella/3362997 to your computer and use it in GitHub Desktop.
Save sansumbrella/3362997 to your computer and use it in GitHub Desktop.
Cinder ES2 Sample
#include "cinder/app/AppCocoaTouch.h"
#include "cinder/Camera.h"
#include "cinder/gl/GlslProg.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class ESTwoApp : public AppCocoaTouch {
public:
virtual void setup();
virtual void draw();
Matrix44f mModelView;
Matrix44f mProjection;
private:
gl::GlslProg mPassthrough;
GLuint mTriangleId;
};
void ESTwoApp::setup()
{
try
{
mPassthrough = gl::GlslProg( loadResource("res/passthrough_vert.glsl"), loadResource("res/passthrough_frag.glsl") );
} catch ( gl::GlslProgCompileExc &exc )
{
console() << "Failed to load shader: " << exc.what() << endl;
}
// create a triangle and store it on the GPU
GLfloat positions[] = { 0, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f };
glGenBuffers( 1, &mTriangleId );
glBindBuffer( GL_ARRAY_BUFFER, mTriangleId );
glBufferData( GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), positions, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
// spit out locations of things on GPU to make sure they exist
console() << "Triangle buffer: " << mTriangleId << endl;
console() << "Positions attrib: " << mPassthrough.getAttribLocation("positions") << endl;
console() << "Projection Matrix: " << mPassthrough.getUniformLocation("pMatrix") << endl;
mProjection.setToIdentity();
mModelView.setToIdentity();
}
void ESTwoApp::draw()
{
gl::setViewport( getWindowBounds() );
gl::clear( Color( 0.0f, 0.0f, 1.0f ) );
gl::enableDepthRead();
mPassthrough.bind();
mPassthrough.uniform( "mvMatrix", mModelView );
mPassthrough.uniform( "pMatrix", mProjection );
glEnableVertexAttribArray( mPassthrough.getAttribLocation("positions") );
// bind buffer and attribpointer specify what buffer (bound) goes where (pointer)
glBindBuffer( GL_ARRAY_BUFFER, mTriangleId );
glVertexAttribPointer( mPassthrough.getAttribLocation("positions"), 3, GL_FLOAT, false, 0, 0);
glDrawArrays( GL_TRIANGLES, 0, 3 );
glDisableVertexAttribArray( mTriangleId );
}
CINDER_APP_COCOA_TOUCH( ESTwoApp, RendererGl )
attribute vec3 positions;
uniform mat4 pMatrix;
uniform mat4 mvMatrix;
void main()
{
gl_Position = pMatrix * mvMatrix * vec4(positions, 1.0);
}
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
@sansumbrella
Copy link
Author

This gist relies on notlion's es2 branch of Cinder, available here:
https://github.com/notlion/Cinder/tree/es2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment