Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.70 KB | None | 0 0
  1. /**
  2.  * @file project.cpp
  3.  * @brief GLSL project
  4.  *
  5.  * @author H. Q. Bovik (hqbovik)
  6.  * @bug Unimplemented
  7.  */
  8.  
  9. #include "glsl/project.hpp"
  10. #include <fstream>
  11.  
  12. // A namespace declaration. All proejct files use this namespace.
  13. // Add this declration (and its closing) to all source/headers you create.
  14. // Note that all #includes should be BEFORE the namespace declaration.
  15. namespace _462 {
  16. // shader loading code
  17.  
  18. /**
  19.  * Load a file as either a vertex shader or a fragment shader, and attach
  20.  * it to a program.
  21.  * @param file The filename to load
  22.  * @param type Either GL_VERTEX_SHADER_ARB, or GL_FRAGMENT_SHADER_ARB
  23.  * @param program The shader program to which to attach the loaded shader.
  24.  * @return True on success.
  25.  */
  26. static bool load_shader( const char* file, GLint type, GLhandleARB program )
  27. {
  28.     std::ifstream infile;
  29.     char* buffer;
  30.     char error_msg[2048];
  31.     GLhandleARB shader;
  32.  
  33.     infile.open( file );
  34.  
  35.     if( infile.fail() ) {
  36.         std::cout << "ERROR: cannot open file: " << file << std::endl;
  37.         infile.close();
  38.         return false;
  39.     }
  40.  
  41.     // calculate length
  42.     infile.seekg( 0, std::ios::end );
  43.     int length = infile.tellg();
  44.     infile.seekg( 0, std::ios::beg );
  45.     // allocate space for entire program
  46.     buffer = (char *) malloc( (length + 1) * sizeof *buffer );
  47.     if ( !buffer )
  48.         return false;
  49.     // copy entire program into memory
  50.     infile.getline( buffer, length, '\0' );
  51.     infile.close();
  52.  
  53.     // create shader object
  54.     shader = glCreateShaderObjectARB( type );
  55.     // link shader source
  56.     const char* src = buffer; // workaround for const correctness
  57.     glShaderSourceARB( shader, 1, &src, NULL );
  58.     // compile shaders
  59.     glCompileShaderARB( shader );
  60.     // check success
  61.     GLint result;
  62.     glGetObjectParameterivARB( shader, GL_OBJECT_COMPILE_STATUS_ARB, &result );
  63.     if ( result != GL_TRUE ) {
  64.         glGetInfoLogARB( shader, sizeof error_msg, NULL, error_msg );
  65.         std::cout << "GLSL COMPILE ERROR in " << file << ": " << error_msg << std::endl;
  66.         return false;
  67.     } else {
  68.         std::cout << "Compiled shaders successfully" << std::endl;
  69.     }
  70.  
  71.     // attach the shader object to the program object
  72.     glAttachObjectARB( program, shader );
  73.  
  74.     free( buffer );
  75.     return true;
  76. }
  77.  
  78. /**
  79.  * Loads a vertex and fragment shader from the given files and attaches them to the given
  80.  * shader program object.
  81.  * @param vert_file The filename of the vetex shader.
  82.  * @param frag_file The filename of the fragment shader.
  83.  * @return True on success.
  84.  */
  85. static bool create_shader( GLhandleARB program, const char* vert_file, const char* frag_file )
  86. {
  87.     bool rv = true;
  88.  
  89.     std::cout
  90.         << "Loading vertex shader " << vert_file
  91.         << "and fragment shader " << frag_file << std::endl;
  92.  
  93.     // Load vertex shader
  94.     rv = rv && load_shader( vert_file, GL_VERTEX_SHADER_ARB, program );
  95.     // Load fragment shader
  96.     rv = rv && load_shader( frag_file, GL_FRAGMENT_SHADER_ARB, program );
  97.  
  98.     if ( !rv )
  99.         return false;
  100.  
  101.     // link
  102.     glLinkProgramARB( program );
  103.  
  104.     // check for success
  105.     GLint result;
  106.     glGetProgramiv( program, GL_LINK_STATUS, &result );
  107.     if ( result == GL_TRUE ) {
  108.         std::cout << "Successfully linked shader" << std::endl;
  109.         return true;
  110.     } else {
  111.         std::cout << "FAILED to link shader" << std::endl;
  112.         return false;
  113.     }
  114. }
  115.  
  116.  
  117. // definitions of functions for the GlslProject class
  118.  
  119. // constructor, invoked when object is created
  120. GlslProject::GlslProject()
  121. {
  122.     // TODO any basic construction or initialization of members
  123.     // Warning: Although members' constructors are automatically called,
  124.     // ints, floats, pointers, and classes with empty contructors all
  125.     // will have uninitialized data!
  126. }
  127.  
  128. // destructor, invoked when object is destroyed
  129. GlslProject::~GlslProject()
  130. {
  131.     // Warning: Do not throw exceptions or call virtual functions from deconstructors!
  132.     // They will cause undefined behavior (probably a crash, but perhaps worse).
  133. }
  134.  
  135. /**
  136.  * Initialize the project, loading the mesh from the given filename.
  137.  * Also do any necessary opengl initialization.
  138.  * @param renderer Object that renders the scene.
  139.  * @param width The width in pixels of the framebuffer.
  140.  * @param height The height in pixels of the framebuffer.
  141.  * @return true on success, false on error.
  142.  * @see scene/mesh.hpp
  143.  */
  144. bool GlslProject::initialize( const SceneRenderer* renderer, int width, int height )
  145. {
  146.     bool rv = true;
  147.     this->width = width;
  148.     this->height = height;
  149.     // copy renderer for later use
  150.     this->renderer = renderer;
  151.     // create a test shader
  152.     program  = glCreateProgramObjectARB();
  153.    
  154.     // compile and link the shader.
  155.     rv = rv && create_shader( program, "shaders/pass1_vert.glsl", "shaders/pass1_frag.glsl" );
  156.     rv = rv && create_shader( program2, "shaders/toon_vert.glsl", "shaders/toon_frag.glsl" );
  157.    
  158.     // create a framebuffer object
  159.     glGenFramebuffersEXT(1, &fboId);
  160.    
  161.     // create a texture object
  162.     glGenTextures(1, &textureId);
  163.     glGenTextures(1, &depthId);
  164.     glGenTextures(1, &normalId);
  165.    
  166.     // bind to the texture id
  167.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
  168.     glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,0);
  169.    
  170.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthId);
  171.     glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT, this->width, this->height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE,0);
  172.    
  173.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, normalId);
  174.     glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,0);
  175.    
  176.     return rv;
  177. }
  178.  
  179. /**
  180.  * Clean up the project. Free any memory, etc.
  181.  */
  182. void GlslProject::destroy()
  183. {
  184.     glDeleteObjectARB( program );
  185.     glDeleteFramebuffersEXT(1, &fboId);
  186.     // TODO any cleanup code
  187. }
  188.  
  189. /**
  190.  * Render the scene with outlines using shaders.
  191.  */
  192. void GlslProject::render()
  193. {
  194.     // TODO render code
  195.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  196.    
  197.     // bind the framebuffer object
  198.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
  199.    
  200.     // create a color attachement for the framebuffer
  201.     glActiveTexture(GL_TEXTURE0);
  202.     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, textureId, 0);
  203.    
  204.     // create a depth attachement for the framebuffer
  205.     glActiveTexture(GL_TEXTURE1);
  206.     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthId, 0);
  207.    
  208.     // create a color attachment for storing the normal as a texture.
  209.     glActiveTexture(GL_TEXTURE2);
  210.     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_RECTANGLE_ARB, normalId, 0);
  211.    
  212.     // then render the scene
  213.     renderer->render_scene();
  214.    
  215.     // bind the shader proram - to store the normals as a texture.
  216.     glUseProgramObjectARB( program );
  217.    
  218.     glActiveTexture(GL_TEXTURE2);
  219.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, normalId);
  220.    
  221.     glUseProgramObjectARB( program2 );
  222.     renderer->render_scene();
  223.     loc = glGetUniformLocation(program2,"colorTexture");
  224.     glUniform1iARB(loc,0);
  225.     loc = glGetUniformLocation(program2, "depthTexture");
  226.     glUniform1iARB(loc,1);
  227.     loc = glGetUniformLocation(program2,"normalTexture");
  228.     glUniform1iARB(loc,2);
  229.     loc = glGetUniformLocation(program2, "near");
  230.     glUniform1f(loc,this->renderer->get_camera()->near_clip);
  231.     loc = glGetUniformLocation(program2, "far");
  232.     glUniform1f(loc,this->renderer->get_camera()->far_clip);
  233.     loc = glGetUniformLocation(program2, "width");
  234.     glUniform1i(loc,this->width);
  235.     loc = glGetUniformLocation(program2, "height");
  236.     glUniform1i(loc,this->height);
  237.  
  238.    
  239.     // unbind the framebuffer object.
  240.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  241.    
  242.     glActiveTexture(GL_TEXTURE0);
  243.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
  244.     glActiveTexture(GL_TEXTURE1);
  245.     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthId);
  246.    
  247.  
  248.    
  249.     glMatrixMode(GL_PROJECTION);
  250.     glLoadIdentity();
  251.     glMatrixMode(GL_MODELVIEW);
  252.     glLoadIdentity();
  253.    
  254.     //draw the rectangle
  255.     glBegin(GL_QUADS);
  256.     glTexCoord2f(0.0f, 0.0f);
  257.     glVertex3f(-1.0f, -1.0f, -1.0f);
  258.     glTexCoord2f(this->width, 0.0f);
  259.     glVertex3f(1.0f, -1.0f, -1.0f);
  260.     glTexCoord2f(this->width, this->height);
  261.     glVertex3f(1.0f, 1.0f, -1.0f);
  262.     glTexCoord2f(0.0f, this->height);
  263.     glVertex3f(-1.0f, 1.0f, -1.0f);
  264.     glEnd();
  265.    
  266.     // unbind the shader.
  267.     glUseProgramObjectARB( 0 );
  268.  
  269. }
  270.  
  271. } /* _462 */
Add Comment
Please, Sign In to add comment