forked from mortennobel/SimpleRenderEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparticle-sprite.cpp
More file actions
142 lines (120 loc) · 4.17 KB
/
Copy pathparticle-sprite.cpp
File metadata and controls
142 lines (120 loc) · 4.17 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <vector>
#include <fstream>
#include "sre/Texture.hpp"
#include "sre/Renderer.hpp"
#include "sre/Camera.hpp"
#include "sre/Mesh.hpp"
#include "sre/Material.hpp"
#include "sre/Shader.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/random.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <imgui.h>
#include <sre/SDLRenderer.hpp>
#include "sre/imgui_sre.hpp"
using namespace sre;
class ParticleSpriteExample{
public:
ParticleSpriteExample(){
r.init();
camera.lookAt({0,0,3},{0,0,0},{0,1,0});
camera.setPerspectiveProjection(60,0.1,100);
material = Shader::getStandardParticles()->createMaterial();
material->setTexture(Texture::create().withFile("test_data/t_explosionsheet.png").build());
particleMesh = createParticles();
r.frameUpdate = [&](float deltaTime){
update(deltaTime);
};
r.frameRender = [&](){
render();
};
r.startEventLoop();
}
void update(float deltaTime){
timeF += deltaTime;
if (ortho) {
camera.setOrthographicProjection(4,0,100);
} else {
camera.setPerspectiveProjection(60,0.1,10);
}
updateParticles(particleMesh, spriteUV, uvSize, uvRotation, size);
}
void render(){
auto rp = RenderPass::create()
.withCamera(camera)
.withClearColor(true,{1,0,0.0,1})
.build();
rp.draw(particleMesh, glm::mat4(1), material);
// 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
{
ImGui::Text("Particle sprite");
ImGui::Checkbox("Orthographic proj",&ortho);
ImGui::Checkbox("Sprite animation",&spriteAnimation);
if (spriteAnimation) {
updateParticlesAnimation(timeF, spriteUV,uvSize, uvRotation);
}
ImGui::SliderFloat("size", &size, 0.0f, 200.0f);
ImGui::DragFloat2("uv", &(spriteUV.x), 0.1f);
ImGui::SliderFloat("uv", &uvSize, 0.0f, 1.0f);
ImGui::SliderFloat("uvRotation", &uvRotation, 0.0f, 2*3.1415f);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
}
}
std::shared_ptr<Mesh> createParticles(){
std::vector<glm::vec3> positions;
std::vector<glm::vec4> colors;
std::vector<glm::vec4> uvs;
std::vector<float> sizes;
positions.push_back({0,0,0});
colors.push_back({1,1,1,1});
sizes.push_back(10.0f);
return Mesh::create()
.withPositions(positions)
.withParticleSizes(sizes)
.withColors(colors)
.withUVs(uvs)
.withMeshTopology(MeshTopology::Points)
.build();
}
void updateParticlesAnimation(float time, glm::vec2& pos,float& size, float& rotation){
int frame = ((int)(time*10))%16;
int frameX = 3-frame%4;
int frameY = frame/4;
pos = glm::vec2(frameX * 0.25f, frameY * 0.25f);
size = 0.25f;
rotation = 0;
}
void updateParticles(std::shared_ptr<Mesh> particleMesh, glm::vec2 uv, float uvSize, float rotation, float size){
std::vector<glm::vec3> positions;
std::vector<glm::vec4> uvs;
std::vector<float> sizes;
positions.push_back({0,0,0});
uvs.push_back({uv,uvSize,rotation});
sizes.push_back(size);
particleMesh->update()
.withPositions(positions)
.withUVs(uvs)
.withParticleSizes(sizes)
.build();
}
private:
SDLRenderer r;
glm::vec2 spriteUV = glm::vec2(0, 0);
bool spriteAnimation = false;
bool ortho = false;
float uvSize = 1.0;
float uvRotation = 0.0;
float size = 200.0f;
float timeF = 0;
std::shared_ptr<Mesh> particleMesh;
std::shared_ptr<Material> material;
Camera camera;
};
int main() {
std::make_unique<ParticleSpriteExample>();
return 0;
}