-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSprite.hpp
More file actions
63 lines (48 loc) · 1.86 KB
/
Sprite.hpp
File metadata and controls
63 lines (48 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Texture2D.hpp"
struct Sprite : DrawableObject
{
std::shared_ptr<Texture2D> Texture;
glm::vec2 Position;
glm::vec2 Size;
float Rotate;
glm::vec3 Color;
Sprite(std::shared_ptr<Texture2D> InTexture, glm::vec2 InPosition, glm::vec2 InSize, float InRotate, glm::vec3 InColor)
{
Vertices = {
{ .Position = { 0.0f, 1.0f, 0.f }, .TextureCoord = { 0.0f, 1.0f } },
{ .Position = { 1.0f, 0.0f, 0.f }, .TextureCoord = { 1.0f, 0.0f } },
{ .Position = { 0.0f, 0.0f, 0.f }, .TextureCoord = { 0.0f, 0.0f } },
{ .Position = { 0.0f, 1.0f, 0.f }, .TextureCoord = { 0.0f, 1.0f } },
{ .Position = { 1.0f, 1.0f, 0.f }, .TextureCoord = { 1.0f, 1.0f } },
{ .Position = { 1.0f, 0.0f, 0.f }, .TextureCoord = { 1.0f, 0.0f } }
};
Texture = InTexture;
Position = InPosition;
Size = InSize;
Rotate = InRotate;
Color = InColor;
VAO.Bind();
VBO.Data(Vertices);
}
virtual void Draw(std::shared_ptr<ShaderProgram> Shader) override
{
VAO.Bind();
Texture->Bind();
glm::mat4 Model = glm::mat4(1.0f);
Model = glm::translate(Model, glm::vec3(Position, 0.0f));
Model = glm::translate(Model, glm::vec3(0.5f * Size.x, 0.5f * Size.y, 0.0f));
Model = glm::rotate(Model, glm::radians(Rotate), glm::vec3(0.0f, 0.0f, 1.0f));
Model = glm::translate(Model, glm::vec3(-0.5f * Size.x, -0.5f * Size.y, 0.0f));
Model = glm::scale(Model, glm::vec3(Size, 1.0f));
Shader->Uniform("model", Model);
Shader->Uniform("spriteColor", Color);
glDrawArrays(GL_TRIANGLES, 0, Vertices.size());
Texture->Unbind();
VAO.Unbind();
}
};