Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file project.cpp
- * @brief GLSL project
- *
- * @author H. Q. Bovik (hqbovik)
- * @bug Unimplemented
- */
- #include "glsl/project.hpp"
- #include <fstream>
- // A namespace declaration. All proejct files use this namespace.
- // Add this declration (and its closing) to all source/headers you create.
- // Note that all #includes should be BEFORE the namespace declaration.
- namespace _462 {
- // shader loading code
- /**
- * Load a file as either a vertex shader or a fragment shader, and attach
- * it to a program.
- * @param file The filename to load
- * @param type Either GL_VERTEX_SHADER_ARB, or GL_FRAGMENT_SHADER_ARB
- * @param program The shader program to which to attach the loaded shader.
- * @return True on success.
- */
- static bool load_shader( const char* file, GLint type, GLhandleARB program )
- {
- std::ifstream infile;
- char* buffer;
- char error_msg[2048];
- GLhandleARB shader;
- infile.open( file );
- if( infile.fail() ) {
- std::cout << "ERROR: cannot open file: " << file << std::endl;
- infile.close();
- return false;
- }
- // calculate length
- infile.seekg( 0, std::ios::end );
- int length = infile.tellg();
- infile.seekg( 0, std::ios::beg );
- // allocate space for entire program
- buffer = (char *) malloc( (length + 1) * sizeof *buffer );
- if ( !buffer )
- return false;
- // copy entire program into memory
- infile.getline( buffer, length, '\0' );
- infile.close();
- // create shader object
- shader = glCreateShaderObjectARB( type );
- // link shader source
- const char* src = buffer; // workaround for const correctness
- glShaderSourceARB( shader, 1, &src, NULL );
- // compile shaders
- glCompileShaderARB( shader );
- // check success
- GLint result;
- glGetObjectParameterivARB( shader, GL_OBJECT_COMPILE_STATUS_ARB, &result );
- if ( result != GL_TRUE ) {
- glGetInfoLogARB( shader, sizeof error_msg, NULL, error_msg );
- std::cout << "GLSL COMPILE ERROR in " << file << ": " << error_msg << std::endl;
- return false;
- } else {
- std::cout << "Compiled shaders successfully" << std::endl;
- }
- // attach the shader object to the program object
- glAttachObjectARB( program, shader );
- free( buffer );
- return true;
- }
- /**
- * Loads a vertex and fragment shader from the given files and attaches them to the given
- * shader program object.
- * @param vert_file The filename of the vetex shader.
- * @param frag_file The filename of the fragment shader.
- * @return True on success.
- */
- static bool create_shader( GLhandleARB program, const char* vert_file, const char* frag_file )
- {
- bool rv = true;
- std::cout
- << "Loading vertex shader " << vert_file
- << "and fragment shader " << frag_file << std::endl;
- // Load vertex shader
- rv = rv && load_shader( vert_file, GL_VERTEX_SHADER_ARB, program );
- // Load fragment shader
- rv = rv && load_shader( frag_file, GL_FRAGMENT_SHADER_ARB, program );
- if ( !rv )
- return false;
- // link
- glLinkProgramARB( program );
- // check for success
- GLint result;
- glGetProgramiv( program, GL_LINK_STATUS, &result );
- if ( result == GL_TRUE ) {
- std::cout << "Successfully linked shader" << std::endl;
- return true;
- } else {
- std::cout << "FAILED to link shader" << std::endl;
- return false;
- }
- }
- // definitions of functions for the GlslProject class
- // constructor, invoked when object is created
- GlslProject::GlslProject()
- {
- // TODO any basic construction or initialization of members
- // Warning: Although members' constructors are automatically called,
- // ints, floats, pointers, and classes with empty contructors all
- // will have uninitialized data!
- }
- // destructor, invoked when object is destroyed
- GlslProject::~GlslProject()
- {
- // Warning: Do not throw exceptions or call virtual functions from deconstructors!
- // They will cause undefined behavior (probably a crash, but perhaps worse).
- }
- /**
- * Initialize the project, loading the mesh from the given filename.
- * Also do any necessary opengl initialization.
- * @param renderer Object that renders the scene.
- * @param width The width in pixels of the framebuffer.
- * @param height The height in pixels of the framebuffer.
- * @return true on success, false on error.
- * @see scene/mesh.hpp
- */
- bool GlslProject::initialize( const SceneRenderer* renderer, int width, int height )
- {
- bool rv = true;
- this->width = width;
- this->height = height;
- // copy renderer for later use
- this->renderer = renderer;
- // create a test shader
- program = glCreateProgramObjectARB();
- // compile and link the shader.
- rv = rv && create_shader( program, "shaders/pass1_vert.glsl", "shaders/pass1_frag.glsl" );
- rv = rv && create_shader( program2, "shaders/toon_vert.glsl", "shaders/toon_frag.glsl" );
- // create a framebuffer object
- glGenFramebuffersEXT(1, &fboId);
- // create a texture object
- glGenTextures(1, &textureId);
- glGenTextures(1, &depthId);
- glGenTextures(1, &normalId);
- // bind to the texture id
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
- glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,0);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthId);
- glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT, this->width, this->height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE,0);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, normalId);
- glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,0);
- return rv;
- }
- /**
- * Clean up the project. Free any memory, etc.
- */
- void GlslProject::destroy()
- {
- glDeleteObjectARB( program );
- glDeleteFramebuffersEXT(1, &fboId);
- // TODO any cleanup code
- }
- /**
- * Render the scene with outlines using shaders.
- */
- void GlslProject::render()
- {
- // TODO render code
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // bind the framebuffer object
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
- // create a color attachement for the framebuffer
- glActiveTexture(GL_TEXTURE0);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, textureId, 0);
- // create a depth attachement for the framebuffer
- glActiveTexture(GL_TEXTURE1);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthId, 0);
- // create a color attachment for storing the normal as a texture.
- glActiveTexture(GL_TEXTURE2);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_RECTANGLE_ARB, normalId, 0);
- // then render the scene
- renderer->render_scene();
- // bind the shader proram - to store the normals as a texture.
- glUseProgramObjectARB( program );
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, normalId);
- glUseProgramObjectARB( program2 );
- renderer->render_scene();
- loc = glGetUniformLocation(program2,"colorTexture");
- glUniform1iARB(loc,0);
- loc = glGetUniformLocation(program2, "depthTexture");
- glUniform1iARB(loc,1);
- loc = glGetUniformLocation(program2,"normalTexture");
- glUniform1iARB(loc,2);
- loc = glGetUniformLocation(program2, "near");
- glUniform1f(loc,this->renderer->get_camera()->near_clip);
- loc = glGetUniformLocation(program2, "far");
- glUniform1f(loc,this->renderer->get_camera()->far_clip);
- loc = glGetUniformLocation(program2, "width");
- glUniform1i(loc,this->width);
- loc = glGetUniformLocation(program2, "height");
- glUniform1i(loc,this->height);
- // unbind the framebuffer object.
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthId);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //draw the rectangle
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glTexCoord2f(this->width, 0.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glTexCoord2f(this->width, this->height);
- glVertex3f(1.0f, 1.0f, -1.0f);
- glTexCoord2f(0.0f, this->height);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glEnd();
- // unbind the shader.
- glUseProgramObjectARB( 0 );
- }
- } /* _462 */
Add Comment
Please, Sign In to add comment