Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/noncopyable.hpp>
- #include <vector>
- #include <SFML/System.hpp>
- #include <SFML/Graphics.hpp>
- #include <cassert>
- #include <iostream>
- #include <cmath>
- #include <GL/glew.h>
- template<typename T>
- class Vector3
- {
- public:
- Vector3(const T& x = 0, const T& y = 0, const T& z = 0) : x(x), y(y), z(z)
- {
- }
- Vector3( const Vector3<T>& v )
- : x( v.x )
- , y( v.y )
- , z( v.z )
- {
- }
- union
- {
- T v [3];
- struct
- {
- T x;
- T y;
- T z;
- };
- struct
- {
- T r;
- T g;
- T b;
- };
- };
- };
- template<typename T>
- Vector3<T>& operator+=( Vector3<T>& self, const Vector3<T>& op )
- {
- self.x += op.x;
- self.y += op.y;
- self.z += op.z;
- return self;
- }
- typedef Vector3<float> Vector3f;
- static const unsigned int g_nNumFloatingObjects = 20;
- static const unsigned int screen_width = 800;
- static const unsigned int screen_height = 600;
- const std::string vshader_src = "void main() \n"
- "{ \n"
- " gl_Position = ftransform(); \n"
- " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
- "}";
- const std::string fshader_src = "#version 120 \n"
- " \n"
- "uniform sampler2D texture0; \n"
- "uniform sampler2D depthtex; \n"
- " \n"
- "void main() \n"
- "{ \n"
- " gl_FragColor = vec4( texture2D( texture0, gl_TexCoord[0].xy ).xyz, 1.0 ); \n"
- " gl_FragDepth = texture2D( depthtex, gl_TexCoord[0].xy ).a; \n"
- "}";
- class FloatingObject : public boost::noncopyable
- {
- private:
- std::vector<Vector3f> positions;
- void Vertex3f( const Vector3f& v )
- {
- glVertex3f( v.x, v.y, v.z );
- }
- Vector3f position;
- public:
- FloatingObject()
- {
- const float fWidth = 2;
- const float fHeight = 2;
- const float fDepth = 2;
- const float fDeltaX = fWidth / 2;
- const float fDeltaY = fHeight / 2;
- const float fDeltaZ = fDepth / 2;
- positions.resize( 8 );
- for( unsigned int n = 0; n < 8; n++ )
- {
- positions[ n ].x = ( ( n & ( 1 << 2 ) ) ? +fDeltaX : -fDeltaX );
- positions[ n ].y = ( ( n & ( 1 << 1 ) ) ? +fDeltaY : -fDeltaY );
- positions[ n ].z = ( ( n & ( 1 ) ) ? +fDeltaZ : -fDeltaZ );
- }
- }
- void SetPosition( float fDisplacement, float fTotalTime )
- {
- const float fPosition = cos( fTotalTime ) * 5;
- position = Vector3f( fPosition, 0, fDisplacement );
- }
- void Draw()
- {
- glPushMatrix();
- glTranslatef( position.x, position.y, position.z );
- /* 2------------/6
- / | / |
- 3------------7 |
- | | | |
- | 0---------|--4
- | / | /
- |/ |/
- 1------------5 */
- // front
- glColor3f( 1, 0, 0 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 1 ] );
- Vertex3f( positions[ 5 ] );
- Vertex3f( positions[ 7 ] );
- Vertex3f( positions[ 3 ] );
- glEnd();
- // back
- glColor3f( 0, 1, 0 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 0 ] );
- Vertex3f( positions[ 2 ] );
- Vertex3f( positions[ 6 ] );
- Vertex3f( positions[ 4 ] );
- glEnd();
- // top
- glColor3f( 1, 1, 0 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 3 ] );
- Vertex3f( positions[ 7 ] );
- Vertex3f( positions[ 6 ] );
- Vertex3f( positions[ 2 ] );
- glEnd();
- // bottom
- glColor3f( 0, 0, 1 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 1 ] );
- Vertex3f( positions[ 0 ] );
- Vertex3f( positions[ 4 ] );
- Vertex3f( positions[ 5 ] );
- glEnd();
- // left
- glColor3f( 1, 0, 1 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 0 ] );
- Vertex3f( positions[ 1 ] );
- Vertex3f( positions[ 3 ] );
- Vertex3f( positions[ 2 ] );
- glEnd();
- // right
- glColor3f( 0, 1, 1 );
- glBegin( GL_TRIANGLE_FAN );
- Vertex3f( positions[ 5 ] );
- Vertex3f( positions[ 4 ] );
- Vertex3f( positions[ 6 ] );
- Vertex3f( positions[ 7 ] );
- glEnd();
- glPopMatrix();
- }
- };
- class RenderTargetDemo
- {
- public:
- RenderTargetDemo()
- : ground_y( -3 )
- {
- }
- ~RenderTargetDemo()
- {
- }
- void OnInit()
- {
- // ----------------------------------------------------------------
- // setup scene
- // ----------------------------------------------------------------
- world_pos = Vector3f( 0, 0, -10 );
- // set color and depth clear value
- glClearDepth( 1.f );
- glClearColor( 0.f, 0.f, 0.f, 0.f );
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- // setup Z-buffer
- glDepthFunc( GL_LEQUAL );
- glDepthMask( GL_TRUE );
- glEnable( GL_DEPTH_TEST );
- // setup culling
- glFrontFace( GL_CCW );
- glEnable( GL_CULL_FACE );
- // setup a perspective projection
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 90.f, 1.f, 0.1f, 100.f );
- glMatrixMode( GL_MODELVIEW );
- // ----------------------------------------------------------------
- // configure shaders
- // ----------------------------------------------------------------
- assert( glewInit() == GLEW_OK ); //< make sure GLEW is initialized
- shader_vert = glCreateShader( GL_VERTEX_SHADER );
- shader_frag = glCreateShader( GL_FRAGMENT_SHADER );
- const char* vert_shader_src = vshader_src.c_str();
- const char* frag_shader_src = fshader_src.c_str();
- const GLint vert_shader_src_len = vshader_src.length();
- const GLint frag_shader_src_len = fshader_src.length();
- glShaderSource( shader_vert, 1, &vert_shader_src, &vert_shader_src_len );
- glShaderSource( shader_frag, 1, &frag_shader_src, &frag_shader_src_len );
- glCompileShader( shader_vert );
- glCompileShader( shader_frag );
- shader_prog = glCreateProgram();
- glAttachShader( shader_prog, shader_vert );
- glAttachShader( shader_prog, shader_frag );
- glLinkProgram( shader_prog );
- // ----------------------------------------------------------------
- // init FBO
- // ----------------------------------------------------------------
- glGenTextures( 1, &fbo_color_id );
- glGenTextures( 1, &fbo_depth_id );
- glBindTexture( GL_TEXTURE_2D, fbo_color_id );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, screen_width, screen_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
- glBindTexture( GL_TEXTURE_2D, fbo_depth_id );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
- glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, screen_width, screen_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL );
- glGenFramebuffersEXT( 1, &fbo_id );
- glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );
- glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT
- , GL_DEPTH_ATTACHMENT_EXT
- , GL_TEXTURE_2D
- , fbo_depth_id
- , 0 ); //< mip-map level (must be 0)
- glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
- GL_TEXTURE_2D, fbo_color_id, 0 );
- GLenum error = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
- if( error == GL_FRAMEBUFFER_UNSUPPORTED_EXT )
- {
- std::cerr << "No support for particular FBO" << std::endl;
- assert( false );
- }
- else
- if( error != GL_FRAMEBUFFER_COMPLETE_EXT )
- {
- std::cerr << "Failed to initialize FBO: error code " << error << std::endl;
- assert( false );
- }
- }
- void OnExit()
- {
- glDeleteProgram( shader_prog );
- glDeleteShader ( shader_vert );
- glDeleteShader ( shader_frag );
- glDeleteFramebuffersEXT( 1, &fbo_id );
- glDeleteTextures( 1, &fbo_color_id );
- glDeleteTextures( 1, &fbo_depth_id );
- }
- void OnMove( float fTotalTime )
- {
- for( unsigned int i = 0; i < g_nNumFloatingObjects; ++i )
- {
- myFloatingObjects[ i ].SetPosition( ( float( i ) - g_nNumFloatingObjects / 2 ) * 4, fTotalTime + 0.5f * i );
- }
- }
- void DrawScene()
- {
- glClear( /*GL_COLOR_BUFFER_BIT |*/ GL_DEPTH_BUFFER_BIT );
- glLoadIdentity();
- glTranslatef( world_pos.x, world_pos.y, world_pos.z );
- glRotatef( 40, 1, 0, 0 );
- // draw the floating objects to an explicit render target
- glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );
- glDrawBuffer( GL_COLOR_ATTACHMENT0_EXT );
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- for( unsigned int i = 0; i < g_nNumFloatingObjects; ++i )
- {
- myFloatingObjects[ i ].Draw();
- }
- glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
- glDrawBuffer( GL_BACK );
- // draw the result into the default frame buffer
- glUseProgram( shader_prog ); //< load shader
- glUniform1i( glGetUniformLocation( shader_prog, "texture0" ), 0 ); //< set texture uniform
- glUniform1i( glGetUniformLocation( shader_prog, "depthtex" ), 1 ); //< set texture uniform
- glActiveTexture( GL_TEXTURE1 ); //< activate texture unit 1
- glBindTexture( GL_TEXTURE_2D, fbo_depth_id ); //< bind depth texture
- glEnable( GL_TEXTURE_2D ); //< enable 2D texturing for current unit
- glActiveTexture( GL_TEXTURE0 ); //< activate texture unit 0
- glBindTexture( GL_TEXTURE_2D, fbo_color_id ); //< bind color texture
- glEnable( GL_TEXTURE_2D ); //< enable 2D texturing for current unit
- glPushMatrix(); //< save current model-view matrix
- glLoadIdentity(); //< reset model-view matrix
- glMatrixMode( GL_PROJECTION );
- glPushMatrix(); //< save current projection matrix
- glLoadIdentity(); //< reset projection matrix
- gluOrtho2D( 0, 1, 1, 0 ); //< create orthognal projection matrix
- glBegin( GL_TRIANGLE_FAN );
- glColor4f( 1, 1, 1, 1 );
- glTexCoord2f( 0, 1 );
- glVertex2f( 0, 0 );
- glTexCoord2f( 0, 0 );
- glVertex2f( 0, 1 );
- glTexCoord2f( 1, 0 );
- glVertex2f( 1, 1 );
- glTexCoord2f( 1, 1 );
- glVertex2f( 1, 0 );
- glEnd();
- glPopMatrix(); //< restore previous projection matrix
- glMatrixMode( GL_MODELVIEW );
- glPopMatrix(); //< restore previous model-view matrix
- glActiveTexture( GL_TEXTURE1 ); //< activate texture unit 1
- glDisable( GL_TEXTURE_2D ); //< disable 2D texturing for current unit
- glActiveTexture( GL_TEXTURE0 ); //< activate texture unit 0
- glDisable( GL_TEXTURE_2D ); //< disable 2D texturing for current unit
- glUseProgram( 0 ); //< restore FFP
- // draw the ground
- glColor4f( 0.8f, 0.8f, 0.8f, 1 );
- glBegin( GL_TRIANGLE_FAN );
- glVertex3f( -20, ground_y, 20 );
- glVertex3f( 20, ground_y, 20 );
- glVertex3f( 20, ground_y, -20 );
- glVertex3f( -20, ground_y, -20 );
- glEnd();
- }
- void OnMouseScrolled( int nDelta, unsigned int, unsigned int )
- {
- world_pos += Vector3f( 0, 0, nDelta );
- }
- void OnKeyPressed( const sf::Event::KeyEvent& ev )
- {
- switch( ev.Code )
- {
- case sf::Key::W:
- world_pos.z -= 0.33f;
- break;
- case sf::Key::S:
- world_pos.z += 0.33f;
- break;
- case sf::Key::A:
- world_pos.x -= 0.33f;
- break;
- case sf::Key::D:
- world_pos.x += 0.33f;
- break;
- case sf::Key::Up:
- ground_y += 0.33f;
- break;
- case sf::Key::Down:
- ground_y -= 0.33f;
- break;
- default:
- break;
- }
- }
- private:
- Vector3f world_pos;
- FloatingObject myFloatingObjects[ g_nNumFloatingObjects ];
- float ground_y;
- // FBO
- GLuint fbo_id;
- GLuint fbo_color_id;
- GLuint fbo_depth_id;
- // shaders
- GLuint shader_vert;
- GLuint shader_frag;
- GLuint shader_prog;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement