Skip to content

Instantly share code, notes, and snippets.

@bcatcho
Created October 1, 2014 21:31
Show Gist options
  • Save bcatcho/52bc75357ea4a3c3bda7 to your computer and use it in GitHub Desktop.
Save bcatcho/52bc75357ea4a3c3bda7 to your computer and use it in GitHub Desktop.
// ---------------------------------
// 1. SETUP
// We need:
// - a frame buffer to do anything
// - a color render buffer to draw on off screen
// - a texture to paint with
// - and a mesh that describes where it is drawn
// THE FRAME BUFFER
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer); // register a frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); // set it to the current FB we are operating on
// COLOR BUFFER
// Greate all we have is a pointer to nothing. Let's make a Render Buffer that stores colors.
GLuint colorRenderbuffer;
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); // set it to the current Render Buffer we are operating on
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); // define it's essential properties
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); // attach it to the frame buffer.
// TEXTURE
// At this point we have a bucket of bits that we can shit colors to. and a framebuffer that knows where that is. Honestly it's just a fancy texture. At any point here we could blit to the screen. We would see nothing but junk
GLuint texture;
glGenTextures(1, &texture); // allocate a texture
glBindTexture(GL_TEXTURE_2D, texture); // make it the current texture we are operating on
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // we may or may not need to use filtering on the image, I assume our frame buffer will be as big as the texture and so we can use nearest neighbor filtering
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // define the properties of a texture, should be clear
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); // Bind the texture to the frame buffer via the color buffer
// THE MESH
// we are going to need a quad to paint our texture. OpenGLes may not work with quads so we need 2 polygons.
// A polygon (in this case) requires 3 lists of things
// 1. Verticies (world position)
// 2. UVs (texture position)
// 3. vertex indicies
// The quad is made up of 2 polygons. So therefore we will need a list of verticies stored in a way that open gl understands.
// The order of the verticies in the array needs to be clockwise or the polygon will face the wrong direction.
// The UV's are easy. 0,0 = bottom left, 1,1 = top right
// The indicies array is how opengl knows what order to draw verties in
// ----------------------------------------
// 2. DRAW loop. All of this is done each frame
// Bind. Configure. Clear. Set up transforms. Draw. Blit?
// Bind. We are operating on this frame buffer now
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
// we need to bind the render buffer. forgot how to do that
// Clear. Write the clear color (set with some openGL command) to every pixel
glClearColor(0, 0, 0, 0);
glClear( GL_COLOR_BUFFER_BIT);
// Configure opengl features
glEnable(GL_TEXTURE_2D); // enable the use of textures
glActiveTexture(0); // set the active texture
// Setup our camera. This affects everything. We will be using an orthogrpahic (2d) pixel perfect camera I assume
// We need some things:
// An orthographic matrix
// A mvpMatrix (Model-view-Projection matrix) that will be multiplied by each vertex in our shader program
// an openGL viewport (this is the pixel algined rect that the drawing commands will be clipped to)
// a vertex and fragment program (Vertex = transform verts, Frag = drawing pixels)
// Set the shader here
// set some properties on the shader -> the mvpMatrix, texture, pointer to our array of verticies, pointer to our array of uvs
// Draw
glDrawArrays(GL_TRIANGLES, 0, numVertices);
// Blit? I'm not sure if you need to do this part so I left it out.
// unbind our shader and texture? I usually do this but not sure if it is necessary
//-----------------------------------------
// 3. CLEAN UP
// Discard things. I forgot how to do it but you'll need to deallocate your buffers. It's the same old Bind->Do Something->Profit pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment