Skip to content

Instantly share code, notes, and snippets.

@rgoulter
Created November 18, 2013 06:28
Show Gist options
  • Save rgoulter/7523511 to your computer and use it in GitHub Desktop.
Save rgoulter/7523511 to your computer and use it in GitHub Desktop.
IDK OpenGL RTT 1
// For Linux, NOT CROSS-PLATFORM
#include <GL/glew.h>
#include <GL/glut.h>
#include <SOIL/SOIL.h>
#include <cstdio>
#include <iostream>
using namespace std;
GLuint texImg;
int loadedImageWidth;
int loadedImageHeight;
unsigned char *loadedImageData;
bool CheckFramebufferStatus()
{
GLenum status = (GLenum) glCheckFramebufferStatus( GL_FRAMEBUFFER );
switch( status )
{
case GL_FRAMEBUFFER_COMPLETE:
return true;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
fprintf( stderr, "Framebuffer incomplete, incomplete attachment\n" );
return false;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
fprintf( stderr, "Framebuffer incomplete, missing attachment\n" );
return false;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
fprintf( stderr, "Framebuffer incomplete, missing draw buffer\n" );
return false;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
fprintf( stderr, "Framebuffer incomplete, missing read buffer\n" );
return false;
case GL_FRAMEBUFFER_UNSUPPORTED:
fprintf( stderr, "Unsupported framebuffer format\n" );
return false;
}
return false;
}
void loadTexture() {
GLuint FramebufferName;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texImg);
glBindTexture(GL_TEXTURE_2D, texImg);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
256,
256,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
NULL);
GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 256, 256);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// Set "renderedTexture" as our colour attachment #0
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texImg, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// Always check that our framebuffer is ok
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cout << "FrameBuffer done goofed." << std::endl;
CheckFramebufferStatus();
return;
}
// Render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// Clear mask texture to all black
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up projection.
glViewport(0, 0, 256, 256);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Render and stuff
// Draw a polygon
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
glVertex2d(0.5, 1);
glVertex2d( 0, 0.5);
glColor3f(1, 0, 1);
glVertex2d(0.5, 0);
glVertex2d( 1, 0.5);
glEnd();
}
void display(void) {
// Render to the screen
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Draw a square
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(-0.75, +0.75, 1);
glVertex3f(+0.75, +0.75, 1);
glVertex3f(+0.75, -0.75, 1);
glVertex3f(-0.75, -0.75, 1);
glEnd();
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texImg);
// Draw a square
glBegin(GL_QUADS);
glColor3f(0,1,0);
glTexCoord2f(0, 0);
glVertex2f(-0.5, +0.5);
glTexCoord2f(1, 0);
glVertex2f(+0.5, +0.5);
glTexCoord2f(1, 1);
glVertex2f(+0.5, -0.5);
glTexCoord2f(0, 1);
glVertex2f(-0.5, -0.5);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void reshape(int w, int h) {
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 200);
glutInitWindowSize(512, 512);
glutCreateWindow("Hello OpenGL");
glewInit(); // needed on linux and windows
glutDisplayFunc(display);
glutReshapeFunc(reshape);
loadTexture();
// Set up projection.
glViewport(0, 0, 512, 512);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glutMainLoop();
return 0;
}
@rgoulter
Copy link
Author

Also, there are some redundant things going on here, I believe. Hmm. Indeed this could be tidied quite a bit.

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