Advertisement
Guest User

Untitled

a guest
Nov 17th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Quad.h"
  3. #include "Camera2D.h"
  4.  
  5. namespace df2d
  6. {
  7. //------------------------------------------------------------------
  8.  
  9.     static const char quad_shader_vs[] =
  10.         "#version 330 core\n"
  11.         "\n"
  12.         "uniform mat4 PVW;\n"
  13.         "uniform vec4 color;\n"
  14.         "uniform vec2 sizequad;\n"
  15.         "uniform float depth;\n"
  16.         "uniform float scale;\n"
  17.         "\n"
  18.         "layout (location = 0) in vec2 in_position;\n"
  19.         "layout (location = 1) in vec2 in_tex_coord;\n"
  20.         "\n"
  21.         "out vec2 tex_coord;\n"
  22.         "out vec4 v_color;\n"
  23.         "\n"
  24.         "void main(void)\n"
  25.         "{\n"
  26.         "   vec2 npos = in_position*mat2( sizequad.x*scale, 0.0,\n"
  27.         "                                 0.0, sizequad.y*scale);\n"
  28.         "   gl_Position = PVW * (vec4(npos, depth, 1.0));\n"
  29.         "   tex_coord = in_tex_coord;\n"
  30.         "   v_color = color;\n"
  31.         "}\n"
  32.         ;
  33.  
  34.     static const char quad_shader_fs[] =
  35.         "#version 330 core\n"
  36.         "\n"
  37.         "uniform sampler2D tex;\n"
  38.         "\n"
  39.         "in vec2 tex_coord;\n"
  40.         "in vec4 v_color;\n"
  41.         "\n"
  42.         "out vec4 outColor;\n"
  43.         "\n"
  44.         "void main(void)\n"
  45.         "{\n"
  46.         "   vec4 texel = texture(tex, tex_coord);\n"
  47.         "   if(texel.a < 0.01) discard;\n"
  48.         "// if (texel.r>=0.99 && texel.g>=0.99 && texel.b>=0.99) discard;\n"
  49.         "   outColor = texture(tex, tex_coord);\n"
  50.         "}\n"
  51.         ;
  52.  
  53.     VAO Quad::m_vao;
  54.     ShaderProgram Quad::m_shaders;
  55.  
  56.     bool Quad::Create(const glm::vec2 &size)
  57.     {
  58.         m_size = size;
  59.  
  60.         if ( !m_vao.Create() )
  61.             return false;
  62.         m_vao.Bind();      
  63.  
  64.         m_vbo.Create();
  65.         m_vbo.Bind();
  66.         const GLfloat quad_data[] =
  67.         {
  68.             0.0f, 0.0f,     0.0f, 0.0f,
  69.             1.0f, 0.0f,     1.0f, 0.0f,
  70.             1.0f, 1.0f,     1.0f, 1.0f,
  71.             0.0f, 1.0f,     0.0f, 1.0f
  72.         };
  73.         m_vbo.SetData(sizeof(quad_data), quad_data);
  74.         m_vbo.SetVertexAtribs(2, 2, 4);
  75.  
  76.         if ( !m_shaders.Create(quad_shader_vs, quad_shader_fs) )
  77.             return false;
  78.                
  79.         m_uniform_pvw = m_shaders.SetUniform("PVW", Camera2D::Get().GetProjViewMatrix());
  80.         m_uniform_color = m_shaders.SetUniform("color", m_color);
  81.         m_uniform_size = m_shaders.SetUniform("sizequad", m_size);
  82.         m_uniform_depth = m_shaders.SetUniform("depth", m_depth);
  83.         m_uniform_scale = m_shaders.SetUniform("scale", m_scale);
  84.        
  85.         return true;
  86.     }
  87.  
  88.  
  89.     void Quad::SetDepth(float d)
  90.     {
  91.         if ( m_depth == d )
  92.             return;
  93.         m_depth = d;
  94.         m_shaders.UpdateUniform(m_uniform_depth, m_depth);
  95.     }
  96.  
  97.  
  98.     void Quad::SetSize(const glm::vec2 &size)
  99.     {
  100.         if ( m_size == size )
  101.             return;
  102.         m_size = size;
  103.         m_shaders.UpdateUniform(m_uniform_size, m_size);
  104.     }
  105.  
  106.     void Quad::SetTexRect(const glm::vec4 &rect)
  107.     {
  108.         const glm::vec4 trect(  ((float)rect.x + 0.5f) / m_size.x,
  109.                                 ((float)rect.y + 0.5f) / m_size.y,
  110.                                 ((float)rect.z + 0.5f) / m_size.x,
  111.                                 ((float)rect.w + 0.5f) / m_size.y );
  112.  
  113.         if ( m_rect == trect )
  114.             return;
  115.         m_rect = trect;
  116.         SetFlip(false, false);
  117.     }
  118.  
  119.     void Quad::SetFlip(bool hor, bool ver)
  120.     {
  121.         float x1, x2, y1, y2;
  122.  
  123.  
  124.         if ( !hor && !ver )
  125.         {
  126.             x1 = m_rect.x;
  127.             y1 = m_rect.y;
  128.             x2 = m_rect.z;
  129.             y2 = m_rect.w;
  130.         }
  131.         else if ( hor && !ver )
  132.         {
  133.             x1 = m_rect.z;
  134.             y1 = m_rect.y;
  135.             x2 = m_rect.x;
  136.             y2 = m_rect.w;
  137.         }
  138.         else if ( !hor && ver )
  139.         {
  140.             x1 = m_rect.x;
  141.             y1 = m_rect.w;
  142.             x2 = m_rect.z;
  143.             y2 = m_rect.y;
  144.         }
  145.         else
  146.         {
  147.             x1 = m_rect.z;
  148.             y1 = m_rect.w;
  149.             x2 = m_rect.x;
  150.             y2 = m_rect.y;
  151.         }
  152.  
  153.         const GLfloat quad_data[] =
  154.         {
  155.             0.0f, 0.0f, x1, y1,
  156.             1.0f, 0.0f, x2, y1,
  157.             1.0f, 1.0f, x2, y2,
  158.             0.0f, 1.0f, x1, y2
  159.         };
  160.         m_vbo.SetData(sizeof(quad_data), quad_data);
  161.  
  162.     }
  163.  
  164.     void Quad::Draw(const glm::vec2 &pos)
  165.     {
  166.         m_vao.Bind();
  167.         m_shaders.Bind();
  168.        
  169.         if ( (m_pos != pos) || Camera2D::Get().IsUpdate() )
  170.         {
  171.             m_pos = pos;
  172.            
  173.             m_shaders.UpdateUniform(m_uniform_pvw, Camera2D::Get().GetProjViewMatrix()*glm::translate(glm::vec3(m_pos.x, m_pos.y, 0.0f)));
  174.         }
  175.        
  176.         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  177.     }
  178.  
  179.     void Quad::Destroy()
  180.     {
  181.         m_vbo.Destroy();
  182.         m_shaders.Destroy();
  183.         m_vao.Destroy();
  184.     }
  185.  
  186. //------------------------------------------------------------------
  187. } // namespace df2d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement