onilink_

gl

May 11th, 2020
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1.  
  2.     // glsl 140 => opengl 3.1
  3.     // https://stackoverflow.com/questions/19021567/
  4.     std::string vertex_shader_scr =
  5.         OD_GLSL(140,
  6.             in  vec2 pos;
  7.             in  vec2 vertex_uv;
  8.             out vec2 uv;
  9.             uniform  mat3 transform; // model view projection
  10.            
  11.             void main()
  12.             {
  13.                 gl_Position = vec4(transform * vec3(pos, 1.0), 1.0);
  14.                 uv = vertex_uv;
  15.             });
  16.    
  17.     std::string fragment_shader_scr =
  18.         OD_GLSL(140,
  19.             uniform sampler2D tex;
  20.             uniform vec4 col;
  21.             in  vec2 uv;
  22.             out vec4 flagcolor;
  23.             void main()
  24.             {
  25.                 vec4 color = texture(tex, uv);
  26.                 flagcolor = color;
  27.             });
  28.    
  29.     enum {
  30.         position_attrib_index,
  31.         texcoord_attrib_index,
  32.     };
  33.     const std::map<unsigned, std::string> attrib_map = {
  34.         { position_attrib_index, "pos" },
  35.         { texcoord_attrib_index, "vertex_uv" }
  36.     };
  37.    
  38.     gl::Program program;
  39.     {
  40.         gl::Shader vertex_shader(gl::Shader::Vertex, vertex_shader_scr);
  41.         gl::Shader fragment_shader(gl::Shader::Fragment, fragment_shader_scr);
  42.        
  43.         program.attach(vertex_shader);
  44.         program.attach(fragment_shader);
  45.         for(const auto& p: attrib_map)
  46.             program.bindAttribLocation(p.first, p.second);
  47.         program.link();
  48.        
  49.        
  50.         program.detach(vertex_shader);
  51.         program.detach(fragment_shader);
  52.     }
  53.    
  54.     gl::Vao vao;
  55.     gl::Vbo vbo_shape;
  56.    
  57.     {
  58.         /*
  59.         std::vector<float> data_pos = {
  60.             // Vertex position
  61.             0, 0,
  62.             16, 0,
  63.             16, 16,
  64.             0, 16,
  65.            
  66.             // Vertex uv
  67.             0, 0,
  68.             1, 0,
  69.             1, 1,
  70.             0, 1
  71.         };
  72.         */
  73.         std::vector<float> data_pos = {
  74.             0, 0, 0, 0,
  75.             16, 0, 1, 0,
  76.             16, 16, 1, 1,
  77.             0, 16, 0, 1
  78.         };
  79.        
  80.         vbo_shape.bind();
  81.         vbo_shape.data(data_pos.size() * sizeof(float), &data_pos[0]);
  82.        
  83.         vao.bind();
  84.         vao.setVertexAttribf(vbo_shape, position_attrib_index, 2, false, 4 * sizeof(float), 0);
  85.         vao.setVertexAttribf(vbo_shape, texcoord_attrib_index, 2, true, 4 * sizeof(float), 2);
  86.         //vao.setVertexAttribf(vbo_shape, position_attrib_index, 2, false, 2 * sizeof(float), 0);
  87.         //vao.setVertexAttribf(vbo_shape, texcoord_attrib_index, 2, false, 2 * sizeof(float), 8);
  88.         vao.unbind();
  89.     }
  90.    
  91.     program.use();
  92.     unsigned uniform_color = gl::getUniformLocation(program.handle(), "col");
  93.     unsigned uniform_transform = gl::getUniformLocation(program.handle(), "transform");
  94.     GLfloat color[4] = {0.2f, 0.7f, 0.7f, 1.f};
  95.     gl::uniform4fv(uniform_color, 1, static_cast<const GLfloat*>(color));
  96.     program.unuse();
  97.    
  98.     od::View view(0, 0, win.width(), win.height());
  99.    
  100.     while(win.open())
  101.     {
  102.         win.ioHandle();
  103.        
  104.         if(od::keyPressed(od::vk_escape))
  105.             break;
  106.        
  107.         if(od::keyCheck(od::vk_left)) { view.translate(1, 0); }
  108.         if(od::keyCheck(od::vk_right)) { view.translate(-1, 0); }
  109.         if(od::keyCheck(od::vk_up)) { view.translate(0, 1); }
  110.         if(od::keyCheck(od::vk_down)) { view.translate(0, -1); }
  111.        
  112.         od::drawClear();
  113.         win.updateView(view);
  114.        
  115.         {
  116.             program.use();
  117.             tex.texture().bind();
  118.            
  119.             od::Mat3 camera_matrix =
  120.                     od::Mat3::ortho(
  121.                         view.x(), view.x() + win.width(),
  122.                         view.y() + win.height(), view.y());
  123.            
  124.             GLfloat color1[4] = {1.f, 1.f, 1.f, 1.f};
  125.             gl::uniform4fv(uniform_color, 1, color1);
  126.            
  127.             for(int i=0; i<4; ++i)
  128.             for(int j=0; j<4; ++j)
  129.             {
  130.                 od::Mat3 model(1.f, 0.f, i*32.f,
  131.                                0.f, 1.f, j*32.f,
  132.                                0.f, 0.f, 1.f);
  133.                
  134.                 gl::uniformMatrix(uniform_transform, camera_matrix * model);
  135.                
  136.                 vao.bind();
  137.                 glDrawArrays(GL_QUADS, 0, 8);
  138.                 vao.unbind();
  139.             }
  140.            
  141.             tex.texture().unbind();
  142.             program.unuse();
  143.         }
  144.        
  145.         tex.draw(tex.spriteIndex("tile"), 0, 16, 16);
  146.        
  147.         win.screenRefresh();
  148.         od::framerateUpdate();
  149.     }
Add Comment
Please, Sign In to add comment