Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////// VERTEX SHADER///////////////////////////////////////////
- #version 330 core
- layout (location = 0) in vec2 aPos;
- layout (location = 1) in vec2 aTexCoord;
- out vec2 TexCoord;
- uniform mat4 model;
- uniform mat4 projection;
- void main()
- {
- gl_Position = vec4((vec4(aPos, 1.0, 1.0) * model * projection));
- TexCoord = vec2(aTexCoord.x, aTexCoord.y);
- }
- /////////////////// GRAPHIC MATH FUNCTIONS ///////////////////////////////////////
- matrix4 matrix4::ortho(const float width, const float height,
- const float near_plane, const float far_plane)
- {
- float zoom_x = 2/width;
- float zoom_y = 2/height;
- float n1 = -2 / (far_plane - near_plane);
- float n2 = -(far_plane + near_plane) / (far_plane - near_plane);
- return matrix4{{ zoom_x, 0, 0, 0,
- 0, zoom_y, 0, 0,
- 0, 0, n1, n2,
- 0, 0, 0, 1,}};
- }
- matrix4 matrix4::translation(const float x, const float y, const float z)
- {
- return matrix4{{ 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- x, y, z, 1,}};
- }
- matrix4 matrix4::from_martrix2(const matrix2x2 a)
- {
- return matrix4{{ a.n[0], a.n[1], 0, 0,
- a.n[2], a.n[3], 0, 0,
- 0 , 1, 0, 0,
- 0, 0, 0, 1,}};
- }
- ///////////////////////////////////////// RENDER FUNCTION //////////////////////////////////////////////////////
- void Sprite::draw_sprite(uint64_t tex, PF location, PF scale, float rotation, float layer)
- {
- auto scale_2d = matrix2x2::scale(scale.first, scale.second);
- auto scaled = matrix4::from_martrix2(scale_2d.multiply(matrix2x2::rotation(rotation)));
- auto model = scaled.multiply(matrix4::translation(location.first, location.second, layer));
- shader.set_mat4("model", model);
- shader.set_mat4("projection", projection);
- shader.set_int("texture1", 0);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex);
- shader.use();
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement