Advertisement
Guest User

Untitled

a guest
Jul 14th, 2010
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.05 KB | None | 0 0
  1. #include <boost/noncopyable.hpp>
  2. #include <vector>
  3. #include <SFML/System.hpp>
  4. #include <SFML/Graphics.hpp>
  5. #include <cassert>
  6. #include <iostream>
  7. #include <cmath>
  8. #include <GL/glew.h>
  9.  
  10.  
  11. template<typename T>
  12. class Vector3
  13. {
  14.  
  15. public:
  16.  
  17.     Vector3(const T& x = 0, const T& y = 0, const T& z = 0) : x(x), y(y), z(z)
  18.     {
  19.  
  20.     }
  21.  
  22.     Vector3( const Vector3<T>& v )
  23.         : x( v.x )
  24.         , y( v.y )
  25.         , z( v.z )
  26.     {
  27.     }
  28.  
  29.     union
  30.     {
  31.         T v [3];
  32.         struct
  33.         {
  34.             T x;
  35.             T y;
  36.             T z;
  37.         };
  38.         struct
  39.         {
  40.             T r;
  41.             T g;
  42.             T b;
  43.         };
  44.     };
  45.  
  46. };
  47.  
  48. template<typename T>
  49. Vector3<T>& operator+=( Vector3<T>& self, const Vector3<T>& op )
  50. {
  51.     self.x += op.x;
  52.     self.y += op.y;
  53.     self.z += op.z;
  54.     return self;
  55. }
  56.  
  57. typedef Vector3<float> Vector3f;
  58.  
  59.  
  60. static const unsigned int g_nNumFloatingObjects = 20;
  61.  
  62. static const unsigned int screen_width  = 800;
  63. static const unsigned int screen_height = 600;
  64.  
  65.  
  66. const std::string vshader_src = "void main() \n"
  67.                                 "{ \n"
  68.                                 "   gl_Position = ftransform(); \n"
  69.                                 "   gl_TexCoord[0] = gl_MultiTexCoord0; \n"
  70.                                 "}";
  71.  
  72. const std::string fshader_src = "#version 120 \n"
  73.                                 " \n"
  74.                                 "uniform sampler2D texture0; \n"
  75.                                 "uniform sampler2D depthtex; \n"
  76.                                 " \n"
  77.                                 "void main() \n"
  78.                                 "{ \n"
  79.                                 "   gl_FragColor = vec4( texture2D( texture0, gl_TexCoord[0].xy ).xyz, 1.0 ); \n"
  80.                                 "   gl_FragDepth = texture2D( depthtex, gl_TexCoord[0].xy ).a; \n"
  81.                                 "}";
  82.  
  83.  
  84.  
  85. class FloatingObject : public boost::noncopyable
  86. {
  87.  
  88. private:
  89.  
  90.     std::vector<Vector3f> positions;
  91.  
  92.  
  93.     void Vertex3f( const Vector3f& v )
  94.     {
  95.         glVertex3f( v.x, v.y, v.z );
  96.     }
  97.  
  98.  
  99.     Vector3f position;
  100.  
  101.  
  102. public:
  103.  
  104.     FloatingObject()
  105.     {
  106.         const float fWidth  = 2;
  107.         const float fHeight = 2;
  108.         const float fDepth  = 2;
  109.  
  110.         const float fDeltaX = fWidth  / 2;
  111.         const float fDeltaY = fHeight / 2;
  112.         const float fDeltaZ = fDepth  / 2;
  113.  
  114.         positions.resize( 8 );
  115.  
  116.         for( unsigned int n  = 0;  n < 8; n++ )
  117.         {
  118.                 positions[ n ].x = ( ( n & ( 1 << 2 ) ) ? +fDeltaX : -fDeltaX );
  119.                 positions[ n ].y = ( ( n & ( 1 << 1 ) ) ? +fDeltaY : -fDeltaY );
  120.                 positions[ n ].z = ( ( n & ( 1      ) ) ? +fDeltaZ : -fDeltaZ );
  121.         }
  122.     }
  123.  
  124.  
  125.     void SetPosition( float fDisplacement, float fTotalTime )
  126.     {
  127.         const float fPosition = cos( fTotalTime ) * 5;
  128.         position = Vector3f( fPosition, 0, fDisplacement );
  129.     }
  130.  
  131.  
  132.     void Draw()
  133.     {
  134.         glPushMatrix();
  135.         glTranslatef( position.x, position.y, position.z );
  136.  
  137.     /*     2------------/6
  138.           / |          / |
  139.          3------------7  |
  140.          |  |         |  |
  141.          |  0---------|--4
  142.          | /          | /
  143.          |/           |/
  144.          1------------5      */
  145.  
  146.  
  147.         // front
  148.  
  149.         glColor3f( 1, 0, 0 );
  150.         glBegin( GL_TRIANGLE_FAN );
  151.         Vertex3f( positions[ 1 ] );
  152.         Vertex3f( positions[ 5 ] );
  153.         Vertex3f( positions[ 7 ] );
  154.         Vertex3f( positions[ 3 ] );
  155.         glEnd();
  156.  
  157.         // back
  158.  
  159.         glColor3f( 0, 1, 0 );
  160.         glBegin( GL_TRIANGLE_FAN );
  161.         Vertex3f( positions[ 0 ] );
  162.         Vertex3f( positions[ 2 ] );
  163.         Vertex3f( positions[ 6 ] );
  164.         Vertex3f( positions[ 4 ] );
  165.         glEnd();
  166.  
  167.         // top
  168.  
  169.         glColor3f( 1, 1, 0 );
  170.         glBegin( GL_TRIANGLE_FAN );
  171.         Vertex3f( positions[ 3 ] );
  172.         Vertex3f( positions[ 7 ] );
  173.         Vertex3f( positions[ 6 ] );
  174.         Vertex3f( positions[ 2 ] );
  175.         glEnd();
  176.  
  177.         // bottom
  178.  
  179.         glColor3f( 0, 0, 1 );
  180.         glBegin( GL_TRIANGLE_FAN );
  181.         Vertex3f( positions[ 1 ] );
  182.         Vertex3f( positions[ 0 ] );
  183.         Vertex3f( positions[ 4 ] );
  184.         Vertex3f( positions[ 5 ] );
  185.         glEnd();
  186.  
  187.         // left
  188.  
  189.         glColor3f( 1, 0, 1 );
  190.         glBegin( GL_TRIANGLE_FAN );
  191.         Vertex3f( positions[ 0 ] );
  192.         Vertex3f( positions[ 1 ] );
  193.         Vertex3f( positions[ 3 ] );
  194.         Vertex3f( positions[ 2 ] );
  195.         glEnd();
  196.  
  197.         // right
  198.  
  199.         glColor3f( 0, 1, 1 );
  200.         glBegin( GL_TRIANGLE_FAN );
  201.         Vertex3f( positions[ 5 ] );
  202.         Vertex3f( positions[ 4 ] );
  203.         Vertex3f( positions[ 6 ] );
  204.         Vertex3f( positions[ 7 ] );
  205.         glEnd();
  206.  
  207.         glPopMatrix();
  208.     }
  209.  
  210. };
  211.  
  212.  
  213.  
  214. class RenderTargetDemo
  215. {
  216.  
  217. public:
  218.  
  219.     RenderTargetDemo()
  220.         : ground_y( -3 )
  221.     {
  222.     }
  223.  
  224.     ~RenderTargetDemo()
  225.     {
  226.     }
  227.  
  228.  
  229.     void OnInit()
  230.     {
  231.         // ----------------------------------------------------------------
  232.         // setup scene
  233.         // ----------------------------------------------------------------
  234.  
  235.         world_pos = Vector3f( 0, 0, -10 );
  236.  
  237.         // set color and depth clear value
  238.  
  239.         glClearDepth( 1.f );
  240.         glClearColor( 0.f, 0.f, 0.f, 0.f );
  241.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  242.  
  243.         // setup Z-buffer
  244.  
  245.         glDepthFunc( GL_LEQUAL );
  246.         glDepthMask( GL_TRUE );
  247.         glEnable( GL_DEPTH_TEST );
  248.  
  249.         // setup culling
  250.  
  251.         glFrontFace( GL_CCW );
  252.         glEnable( GL_CULL_FACE );
  253.  
  254.         // setup a perspective projection
  255.  
  256.         glMatrixMode( GL_PROJECTION );
  257.         glLoadIdentity();
  258.         gluPerspective( 90.f, 1.f, 0.1f, 100.f );
  259.         glMatrixMode( GL_MODELVIEW );
  260.  
  261.  
  262.         // ----------------------------------------------------------------
  263.         // configure shaders
  264.         // ----------------------------------------------------------------
  265.  
  266.         assert( glewInit() == GLEW_OK );    //< make sure GLEW is initialized
  267.  
  268.         shader_vert = glCreateShader( GL_VERTEX_SHADER );
  269.         shader_frag = glCreateShader( GL_FRAGMENT_SHADER );
  270.  
  271.         const char* vert_shader_src = vshader_src.c_str();
  272.         const char* frag_shader_src = fshader_src.c_str();
  273.  
  274.         const GLint vert_shader_src_len = vshader_src.length();
  275.         const GLint frag_shader_src_len = fshader_src.length();
  276.  
  277.         glShaderSource( shader_vert, 1, &vert_shader_src, &vert_shader_src_len );
  278.         glShaderSource( shader_frag, 1, &frag_shader_src, &frag_shader_src_len );
  279.  
  280.         glCompileShader( shader_vert );
  281.         glCompileShader( shader_frag );
  282.  
  283.         shader_prog = glCreateProgram();
  284.         glAttachShader( shader_prog, shader_vert );
  285.         glAttachShader( shader_prog, shader_frag );
  286.         glLinkProgram( shader_prog );
  287.  
  288.  
  289.         // ----------------------------------------------------------------
  290.         // init FBO
  291.         // ----------------------------------------------------------------
  292.  
  293.         glGenTextures( 1, &fbo_color_id );
  294.         glGenTextures( 1, &fbo_depth_id );
  295.  
  296.         glBindTexture( GL_TEXTURE_2D, fbo_color_id );
  297.  
  298.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  299.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  300.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  301.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  302.  
  303.         glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, screen_width, screen_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
  304.  
  305.         glBindTexture( GL_TEXTURE_2D, fbo_depth_id );
  306.  
  307.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  308.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  309.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
  310.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
  311.         glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA );
  312.  
  313.         glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, screen_width, screen_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL );
  314.  
  315.         glGenFramebuffersEXT( 1, &fbo_id );
  316.         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );
  317.  
  318.         glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT
  319.                                  , GL_DEPTH_ATTACHMENT_EXT
  320.                                  , GL_TEXTURE_2D
  321.                                  , fbo_depth_id
  322.                                  , 0 ); //< mip-map level (must be 0)
  323.  
  324.         glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  325.                                    GL_TEXTURE_2D, fbo_color_id, 0 );
  326.  
  327.         GLenum error = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
  328.         if( error == GL_FRAMEBUFFER_UNSUPPORTED_EXT )
  329.         {
  330.             std::cerr << "No support for particular FBO" << std::endl;
  331.             assert( false );
  332.         }
  333.         else
  334.         if( error != GL_FRAMEBUFFER_COMPLETE_EXT )
  335.         {
  336.             std::cerr << "Failed to initialize FBO: error code " << error << std::endl;
  337.             assert( false );
  338.         }
  339.  
  340.     }
  341.  
  342.  
  343.     void OnExit()
  344.     {
  345.         glDeleteProgram( shader_prog );
  346.         glDeleteShader ( shader_vert );
  347.         glDeleteShader ( shader_frag );
  348.  
  349.         glDeleteFramebuffersEXT( 1, &fbo_id );
  350.  
  351.         glDeleteTextures( 1, &fbo_color_id );
  352.         glDeleteTextures( 1, &fbo_depth_id );
  353.     }
  354.  
  355.  
  356.     void OnMove( float fTotalTime )
  357.     {
  358.         for( unsigned int i = 0; i < g_nNumFloatingObjects; ++i )
  359.         {
  360.             myFloatingObjects[ i ].SetPosition( ( float( i ) - g_nNumFloatingObjects / 2 ) * 4, fTotalTime + 0.5f * i );
  361.         }
  362.     }
  363.  
  364.  
  365.     void DrawScene()
  366.     {
  367.         glClear( /*GL_COLOR_BUFFER_BIT |*/ GL_DEPTH_BUFFER_BIT );
  368.  
  369.         glLoadIdentity();
  370.         glTranslatef( world_pos.x, world_pos.y, world_pos.z );
  371.         glRotatef( 40, 1, 0, 0 );
  372.  
  373.         // draw the floating objects to an explicit render target
  374.  
  375.         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );
  376.         glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
  377.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  378.  
  379.         for( unsigned int i = 0; i < g_nNumFloatingObjects; ++i )
  380.         {
  381.             myFloatingObjects[ i ].Draw();
  382.         }
  383.  
  384.         glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
  385.         glDrawBuffer( GL_BACK );
  386.  
  387.         // draw the result into the default frame buffer
  388.  
  389.         glUseProgram( shader_prog );                                            //< load shader
  390.         glUniform1i( glGetUniformLocation( shader_prog, "texture0" ), 0 );      //< set texture uniform
  391.         glUniform1i( glGetUniformLocation( shader_prog, "depthtex" ), 1 );      //< set texture uniform
  392.         glActiveTexture( GL_TEXTURE1 );                                         //< activate texture unit 1
  393.         glBindTexture( GL_TEXTURE_2D, fbo_depth_id );                           //< bind depth texture
  394.         glEnable( GL_TEXTURE_2D );                                              //< enable 2D texturing for current unit
  395.  
  396.         glActiveTexture( GL_TEXTURE0 );                                         //< activate texture unit 0
  397.         glBindTexture( GL_TEXTURE_2D, fbo_color_id );                           //< bind color texture
  398.         glEnable( GL_TEXTURE_2D );                                              //< enable 2D texturing for current unit
  399.  
  400.         glPushMatrix();                     //< save current model-view matrix
  401.         glLoadIdentity();                   //< reset model-view matrix
  402.         glMatrixMode( GL_PROJECTION );
  403.         glPushMatrix();                     //< save current projection matrix
  404.         glLoadIdentity();                   //< reset projection matrix
  405.         gluOrtho2D( 0, 1, 1, 0 );           //< create orthognal projection matrix
  406.  
  407.         glBegin( GL_TRIANGLE_FAN );
  408.         glColor4f( 1, 1, 1, 1 );
  409.         glTexCoord2f( 0, 1 );
  410.         glVertex2f( 0, 0 );
  411.         glTexCoord2f( 0, 0 );
  412.         glVertex2f( 0, 1 );
  413.         glTexCoord2f( 1, 0 );
  414.         glVertex2f( 1, 1 );
  415.         glTexCoord2f( 1, 1 );
  416.         glVertex2f( 1, 0 );
  417.         glEnd();
  418.  
  419.         glPopMatrix();                      //< restore previous projection matrix
  420.         glMatrixMode( GL_MODELVIEW );
  421.         glPopMatrix();                      //< restore previous model-view matrix
  422.  
  423.         glActiveTexture( GL_TEXTURE1 );     //< activate texture unit 1
  424.         glDisable( GL_TEXTURE_2D );         //< disable 2D texturing for current unit
  425.         glActiveTexture( GL_TEXTURE0 );     //< activate texture unit 0
  426.         glDisable( GL_TEXTURE_2D );         //< disable 2D texturing for current unit
  427.         glUseProgram( 0 );                  //< restore FFP
  428.  
  429.         // draw the ground
  430.  
  431.         glColor4f( 0.8f, 0.8f, 0.8f, 1 );
  432.         glBegin( GL_TRIANGLE_FAN );
  433.         glVertex3f( -20, ground_y,  20 );
  434.         glVertex3f(  20, ground_y,  20 );
  435.         glVertex3f(  20, ground_y, -20 );
  436.         glVertex3f( -20, ground_y, -20 );
  437.         glEnd();
  438.     }
  439.  
  440.  
  441.     void OnMouseScrolled( int nDelta, unsigned int, unsigned int )
  442.     {
  443.         world_pos += Vector3f( 0, 0, nDelta );
  444.     }
  445.  
  446.  
  447.     void OnKeyPressed( const sf::Event::KeyEvent& ev )
  448.     {
  449.         switch( ev.Code )
  450.         {
  451.  
  452.         case sf::Key::W:
  453.             world_pos.z -= 0.33f;
  454.             break;
  455.  
  456.         case sf::Key::S:
  457.             world_pos.z += 0.33f;
  458.             break;
  459.  
  460.         case sf::Key::A:
  461.             world_pos.x -= 0.33f;
  462.             break;
  463.  
  464.         case sf::Key::D:
  465.             world_pos.x += 0.33f;
  466.             break;
  467.  
  468.         case sf::Key::Up:
  469.             ground_y += 0.33f;
  470.             break;
  471.  
  472.         case sf::Key::Down:
  473.             ground_y -= 0.33f;
  474.             break;
  475.  
  476.         default:
  477.             break;
  478.  
  479.         }
  480.     }
  481.  
  482.  
  483. private:
  484.  
  485.     Vector3f world_pos;
  486.     FloatingObject myFloatingObjects[ g_nNumFloatingObjects ];
  487.  
  488.     float ground_y;
  489.  
  490.     // FBO
  491.  
  492.     GLuint fbo_id;
  493.     GLuint fbo_color_id;
  494.     GLuint fbo_depth_id;
  495.  
  496.     // shaders
  497.  
  498.     GLuint shader_vert;
  499.     GLuint shader_frag;
  500.     GLuint shader_prog;
  501.  
  502. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement