Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // glsl 140 => opengl 3.1
- // https://stackoverflow.com/questions/19021567/
- std::string vertex_shader_scr =
- OD_GLSL(140,
- in vec2 pos;
- in vec2 vertex_uv;
- out vec2 uv;
- uniform mat3 transform; // model view projection
- void main()
- {
- gl_Position = vec4(transform * vec3(pos, 1.0), 1.0);
- uv = vertex_uv;
- });
- std::string fragment_shader_scr =
- OD_GLSL(140,
- uniform sampler2D tex;
- uniform vec4 col;
- in vec2 uv;
- out vec4 flagcolor;
- void main()
- {
- vec4 color = texture(tex, uv);
- flagcolor = color;
- });
- enum {
- position_attrib_index,
- texcoord_attrib_index,
- };
- const std::map<unsigned, std::string> attrib_map = {
- { position_attrib_index, "pos" },
- { texcoord_attrib_index, "vertex_uv" }
- };
- gl::Program program;
- {
- gl::Shader vertex_shader(gl::Shader::Vertex, vertex_shader_scr);
- gl::Shader fragment_shader(gl::Shader::Fragment, fragment_shader_scr);
- program.attach(vertex_shader);
- program.attach(fragment_shader);
- for(const auto& p: attrib_map)
- program.bindAttribLocation(p.first, p.second);
- program.link();
- program.detach(vertex_shader);
- program.detach(fragment_shader);
- }
- gl::Vao vao;
- gl::Vbo vbo_shape;
- {
- /*
- std::vector<float> data_pos = {
- // Vertex position
- 0, 0,
- 16, 0,
- 16, 16,
- 0, 16,
- // Vertex uv
- 0, 0,
- 1, 0,
- 1, 1,
- 0, 1
- };
- */
- std::vector<float> data_pos = {
- 0, 0, 0, 0,
- 16, 0, 1, 0,
- 16, 16, 1, 1,
- 0, 16, 0, 1
- };
- vbo_shape.bind();
- vbo_shape.data(data_pos.size() * sizeof(float), &data_pos[0]);
- vao.bind();
- vao.setVertexAttribf(vbo_shape, position_attrib_index, 2, false, 4 * sizeof(float), 0);
- vao.setVertexAttribf(vbo_shape, texcoord_attrib_index, 2, true, 4 * sizeof(float), 2);
- //vao.setVertexAttribf(vbo_shape, position_attrib_index, 2, false, 2 * sizeof(float), 0);
- //vao.setVertexAttribf(vbo_shape, texcoord_attrib_index, 2, false, 2 * sizeof(float), 8);
- vao.unbind();
- }
- program.use();
- unsigned uniform_color = gl::getUniformLocation(program.handle(), "col");
- unsigned uniform_transform = gl::getUniformLocation(program.handle(), "transform");
- GLfloat color[4] = {0.2f, 0.7f, 0.7f, 1.f};
- gl::uniform4fv(uniform_color, 1, static_cast<const GLfloat*>(color));
- program.unuse();
- od::View view(0, 0, win.width(), win.height());
- while(win.open())
- {
- win.ioHandle();
- if(od::keyPressed(od::vk_escape))
- break;
- if(od::keyCheck(od::vk_left)) { view.translate(1, 0); }
- if(od::keyCheck(od::vk_right)) { view.translate(-1, 0); }
- if(od::keyCheck(od::vk_up)) { view.translate(0, 1); }
- if(od::keyCheck(od::vk_down)) { view.translate(0, -1); }
- od::drawClear();
- win.updateView(view);
- {
- program.use();
- tex.texture().bind();
- od::Mat3 camera_matrix =
- od::Mat3::ortho(
- view.x(), view.x() + win.width(),
- view.y() + win.height(), view.y());
- GLfloat color1[4] = {1.f, 1.f, 1.f, 1.f};
- gl::uniform4fv(uniform_color, 1, color1);
- for(int i=0; i<4; ++i)
- for(int j=0; j<4; ++j)
- {
- od::Mat3 model(1.f, 0.f, i*32.f,
- 0.f, 1.f, j*32.f,
- 0.f, 0.f, 1.f);
- gl::uniformMatrix(uniform_transform, camera_matrix * model);
- vao.bind();
- glDrawArrays(GL_QUADS, 0, 8);
- vao.unbind();
- }
- tex.texture().unbind();
- program.unuse();
- }
- tex.draw(tex.spriteIndex("tile"), 0, 16, 16);
- win.screenRefresh();
- od::framerateUpdate();
- }
Add Comment
Please, Sign In to add comment