Created
August 15, 2012 19:44
-
-
Save sansumbrella/3362997 to your computer and use it in GitHub Desktop.
Cinder ES2 Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attribute vec3 positions; | |
uniform mat4 pMatrix; | |
uniform mat4 mvMatrix; | |
void main() | |
{ | |
gl_Position = pMatrix * mvMatrix * vec4(positions, 1.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main(void) | |
{ | |
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist relies on notlion's es2 branch of Cinder, available here:
https://github.com/notlion/Cinder/tree/es2