Merge particles with master

This commit is contained in:
xkamikoo 2021-02-01 02:47:01 +01:00
commit 0a5ac016c1
7 changed files with 400 additions and 53 deletions

View File

@ -17,6 +17,8 @@
<None Include="shaders\shader_blur.vert" />
<None Include="shaders\shader_color.frag" />
<None Include="shaders\shader_color.vert" />
<None Include="shaders\shader_particle.frag" />
<None Include="shaders\shader_particle.vert" />
<None Include="shaders\shader_skybox.frag" />
<None Include="shaders\shader_skybox.vert" />
<None Include="shaders\shader_sun.frag" />

View File

@ -54,6 +54,12 @@
<None Include="shaders\shader_sun.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_particle.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_particle.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Box.cpp">

14
shader_particle.frag Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
in vec2 UV;
in vec4 ParticleColor;
out vec4 color;
uniform sampler2D sprite;
void main()
{
color = texture( sprite, UV ) * ParticleColor;
}

29
shader_particle.vert Normal file
View File

@ -0,0 +1,29 @@
#version 330 core
layout(location = 0) in vec3 squareVertices;
layout(location = 1) in vec4 xyzs;
layout(location = 2) in vec4 color;
out vec2 UV;
out vec4 ParticleColor;
uniform vec3 CameraRight_worldspace;
uniform vec3 CameraUp_worldspace;
uniform mat4 VP;
void main()
{
float particleSize = xyzs.w; // because we encoded it this way.
vec3 particleCenter_wordspace = xyzs.xyz;
vec3 vertexPosition_worldspace =
particleCenter_wordspace
+ CameraRight_worldspace * squareVertices.x * particleSize
+ CameraUp_worldspace * squareVertices.y * particleSize;
// Output position of the vertex
gl_Position = VP * vec4(vertexPosition_worldspace, 1.0f);
// UV of the vertex. No special space for this one.
UV = squareVertices.xy + vec2(0.5, 0.5);
ParticleColor = color;
}

View File

@ -0,0 +1,14 @@
#version 330 core
in vec2 UV;
in vec4 ParticleColor;
out vec4 color;
uniform sampler2D sprite;
void main()
{
color = texture( sprite, UV ) * ParticleColor;
}

View File

@ -0,0 +1,29 @@
#version 330 core
layout(location = 0) in vec3 squareVertices;
layout(location = 1) in vec4 xyzs;
layout(location = 2) in vec4 color;
out vec2 UV;
out vec4 ParticleColor;
uniform vec3 CameraRight_worldspace;
uniform vec3 CameraUp_worldspace;
uniform mat4 VP;
void main()
{
float particleSize = xyzs.w; // because we encoded it this way.
vec3 particleCenter_wordspace = xyzs.xyz;
vec3 vertexPosition_worldspace =
particleCenter_wordspace
+ CameraRight_worldspace * squareVertices.x * particleSize
+ CameraUp_worldspace * squareVertices.y * particleSize;
// Output position of the vertex
gl_Position = VP * vec4(vertexPosition_worldspace, 1.0f);
// UV of the vertex. No special space for this one.
UV = squareVertices.xy + vec2(0.5, 0.5);
ParticleColor = color;
}

View File

@ -30,18 +30,28 @@ GLuint programSkybox;
GLuint programSun;
GLuint programBlur;
GLuint programBloom;
GLuint programParticle;
GLuint VertexArrayID;
unsigned int pingpongFBO[2];
unsigned int pingpongColorbuffers[2];
unsigned int FBO;
unsigned int colorBuffers[2];
//particlepart
double lastTime;
static GLfloat* g_particule_position_size_data;
static GLubyte* g_particule_color_data;
GLuint particle_vertex_buffer;
GLuint particles_position_buffer;
GLuint particles_color_buffer;
Core::Shader_Loader shaderLoader;
Core::RenderContext armContext;
std::vector<Core::Node> arm;
int ballIndex;
bool bothEngines = true;
GLuint textureShip_normals;
GLuint sunTexture;
@ -49,6 +59,7 @@ GLuint earthTexture;
GLuint moonTexture;
GLuint skyboxTexture;
GLuint shipTexture;
GLuint particleTexture;
obj::Model sphereModel;
obj::Model cubeModel;
obj::Model shipModel;
@ -72,15 +83,54 @@ glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f);
glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f);
//particlepart
struct Particle {
glm::vec3 pos, speed;
unsigned char r, g, b, a; // Color
float size, angle, weight;
float life; // Remaining life of the particle. if <0 : dead and unused.
float cameradistance; // *Squared* distance to the camera. if dead : -1.0f
bool operator<(const Particle& that) const {
return this->cameradistance > that.cameradistance;
}
};
const int MaxParticles = 1000;
Particle ParticlesContainer[MaxParticles];
int LastUsedParticle = 0;
void SortParticles() {
std::sort(&ParticlesContainer[0], &ParticlesContainer[MaxParticles]);
}
int FindUnusedParticle() {
for (int i = LastUsedParticle; i < MaxParticles; i++) {
if (ParticlesContainer[i].life < 0) {
LastUsedParticle = i;
return i;
}
}
for (int i = 0; i < LastUsedParticle; i++) {
if (ParticlesContainer[i].life < 0) {
LastUsedParticle = i;
return i;
}
}
return 0; // All particles are taken, override the first one
}
//Light
struct Light {
glm::vec3 position;
glm::vec3 color;
float intensity;
};
int engineLightTimer;
int engineLightTimer = 50;
//wczytywanie skyboxa (musi byc jpg!)
std::vector<std::string> faces
@ -259,6 +309,63 @@ void drawSkybox(GLuint program, Core::RenderContext context, GLuint texID)
glUseProgram(0);
}
void drawParticles(int ParticlesCount, glm::mat4 &transformation)
{
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, ParticlesCount * sizeof(GLfloat) * 4, g_particule_position_size_data);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLubyte), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, ParticlesCount * sizeof(GLubyte) * 4, g_particule_color_data);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, particleTexture);
glUniform1i(glGetUniformLocation(programParticle, "sprite"), 0);
glUniform3f(glGetUniformLocation(programParticle, "CameraRight_worldspace"), cameraSide.x, cameraSide.y, cameraSide.z);
glUniform3f(glGetUniformLocation(programParticle, "CameraUp_worldspace"), 0, 1, 0);
glUniformMatrix4fv(glGetUniformLocation(programParticle, "VP"), 1, GL_FALSE, &transformation[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, particle_vertex_buffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
4, // size : x + y + z + size => 4
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glVertexAttribPointer(
2, // attribute. No particular reason for 1, but must match the layout in the shader.
4, // size : r + g + b + a => 4
GL_UNSIGNED_BYTE, // type
GL_TRUE, // normalized? *** YES, this means that the unsigned char[4] will be accessible with a vec4 (floats) in the shader ***
0, // stride
(void*)0 // array buffer offset
);
glVertexAttribDivisor(0, 0); // particles vertices : always reuse the same 4 vertices -> 0
glVertexAttribDivisor(1, 1); // positions : one per quad (its center) -> 1
glVertexAttribDivisor(2, 1); // color : one per quad -> 1
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, ParticlesCount);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
}
//Textures
void drawObjectTexture(GLuint program, Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 texture, GLuint texID)
{
@ -335,10 +442,10 @@ void renderScene()
lights[0].position = sunPos;
lights[1].position = sunPos2;
glm::mat4 engineLeft = glm::translate(shipModelMatrix, glm::vec3(700,0,-1500));
glm::mat4 engineLeft = glm::translate(shipModelMatrix, glm::vec3(450, 0, -1500));
lights[2].position = glm::vec3(engineLeft[3][0], engineLeft[3][1], engineLeft[3][2]);
glm::mat4 engineRight = glm::translate(shipModelMatrix, glm::vec3(-700, 0, -1500));
glm::mat4 engineRight = glm::translate(shipModelMatrix, glm::vec3(-450, 0, -1500));
lights[3].position = glm::vec3(engineRight[3][0], engineRight[3][1], engineRight[3][2]);
for (int i = 0; i < lights.size(); i++)
@ -356,6 +463,10 @@ void renderScene()
drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1));
drawFromAssimpModel(programTex, crewmate, crewmateModelMatrix, glm::vec3(1));
//rysowanie Ziemi z ksiezycem
glm::mat4 earth = drawPlanet(time / 5.0f, sunPos * glm::vec3(1.5f, 1, 1), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-10.5f, 0.0f, -10.5f), glm::vec3(0.5f, 0.5f, 0.5f));
glm::mat4 moon = drawMoon(earth, time / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.5f, 1.0f, 1.0f), glm::vec3(0.3f, 0.3f, 0.3f));
earth = glm::rotate(earth, time / 3.0f, glm::vec3(0.0f, 0.0f, 1.0f));
drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture);
drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);
drawObjectTexture(programTex, sphereContext, planet1, glm::vec3(0.4f, 0.2f, 0.9f), moonTexture);
@ -365,6 +476,112 @@ void renderScene()
drawObjectTexture(programSun, sphereContext, sunModelMatrix, glm::vec3(3.5f, 3.8f, 3.8f), sunTexture);
drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture);
//particlepart
glUseProgram(programParticle);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
double delta = time - lastTime;
lastTime = time;
glm::mat4 transformation = perspectiveMatrix * cameraMatrix;
int newparticles = 0;
if (engineLightTimer < 40)
{
engineLightTimer++;
newparticles = (int)(delta * 10000.0);
if (newparticles > (int)(0.016f * 10000.0))
newparticles = (int)(0.016f * 10000.0);
}
else
{
lights[2].intensity = 0.00001;
lights[3].intensity = 0.00001;
}
for (int i = 0; i < newparticles; i++) {
int particleIndex = FindUnusedParticle();
ParticlesContainer[particleIndex].life = 100.0f;
if (lights[2].intensity > 0.001 && lights[3].intensity > 0.001)
{
if (rand() % 2)
ParticlesContainer[particleIndex].pos = lights[2].position;
else
ParticlesContainer[particleIndex].pos = lights[3].position;
}
else if(lights[2].intensity > 0.001)
ParticlesContainer[particleIndex].pos = lights[2].position;
else if (lights[3].intensity > 0.001)
ParticlesContainer[particleIndex].pos = lights[3].position;
float spread = 1.0f;
glm::vec3 maindir = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 randomdir = glm::vec3(
(rand() % 2000 - 1000.0f) / 5000.0f,
(rand() % 2000 - 1000.0f) / 5000.0f,
(rand() % 2000 - 1000.0f) / 5000.0f
);
ParticlesContainer[particleIndex].speed = maindir + randomdir * spread;
// Very bad way to generate a random color
ParticlesContainer[particleIndex].r = rand() % 100 + 100;
ParticlesContainer[particleIndex].g = 0;
ParticlesContainer[particleIndex].b = rand() % 100 + 50;
ParticlesContainer[particleIndex].a = (rand() % 256) / 3;
ParticlesContainer[particleIndex].size = (rand() % 1000) / 50000.0f + 0.01f;
}
// Simulate all particles
int ParticlesCount = 0;
for (int i = 0; i < MaxParticles; i++) {
Particle& p = ParticlesContainer[i]; // shortcut
if (p.life > 0.0f) {
// Decrease life
p.life -= delta;
if (p.life > 0.0f) {
// Simulate simple physics : gravity only, no collisions
p.speed += glm::vec3(0.0f, -9.81f, 0.0f) * (float)delta * 0.5f;
p.pos += p.speed * (float)delta;
p.cameradistance = glm::length2(p.pos - cameraPos);
//ParticlesContainer[i].pos += glm::vec3(0.0f,10.0f, 0.0f) * (float)delta;
// Fill the GPU buffer
g_particule_position_size_data[4 * ParticlesCount + 0] = p.pos.x;
g_particule_position_size_data[4 * ParticlesCount + 1] = p.pos.y;
g_particule_position_size_data[4 * ParticlesCount + 2] = p.pos.z;
g_particule_position_size_data[4 * ParticlesCount + 3] = p.size;
g_particule_color_data[4 * ParticlesCount + 0] = p.r;
g_particule_color_data[4 * ParticlesCount + 1] = p.g;
g_particule_color_data[4 * ParticlesCount + 2] = p.b;
g_particule_color_data[4 * ParticlesCount + 3] = p.a;
}
else {
// Particles that just died will be put at the end of the buffer in SortParticles();
p.cameradistance = -1.0f;
}
ParticlesCount++;
}
}
SortParticles();
drawParticles(ParticlesCount, transformation);
drawSkybox(programSkybox, cubeContext, skyboxTexture);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
@ -391,54 +608,48 @@ void renderScene()
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[!horizontal]);
renderQuad();
if (engineLightTimer < 50) engineLightTimer++;
else
{
lights[2].intensity = 0.00001;
lights[3].intensity = 0.00001;
}
glUseProgram(0);
glutSwapBuffers();
}
void init()
void init_particles()
{
glEnable(GL_DEPTH_TEST);
programTex = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag");
programBlur = shaderLoader.CreateProgram("shaders/shader_blur.vert", "shaders/shader_blur.frag");
programBloom = shaderLoader.CreateProgram("shaders/shader_bloom.vert", "shaders/shader_bloom.frag");
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
g_particule_position_size_data = new GLfloat[MaxParticles * 4];
g_particule_color_data = new GLubyte[MaxParticles * 4];
for (int i = 0; i < MaxParticles; i++) {
ParticlesContainer[i].life = 100.0f;
ParticlesContainer[i].cameradistance = -1.0f;
}
static const GLfloat g_vertex_buffer_data[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
};
glGenBuffers(1, &particle_vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particle_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// The VBO containing the positions and sizes of the particles
glGenBuffers(1, &particles_position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
// Initialize with empty (NULL) buffer : it will be updated later, each frame.
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW);
glUseProgram(programBlur);
glUniform1i(glGetUniformLocation(programBlur, "image"), 0);
glUseProgram(programBloom);
glUniform1i(glGetUniformLocation(programBloom, "scene"), 0);
glUniform1i(glGetUniformLocation(programBloom, "bloomBlur"), 1);
glUseProgram(0);
corvette = std::make_shared<Model>("models/Corvette-F3.obj");
crewmate = std::make_shared<Model>("models/space_humster.obj");
//shipModel = obj::loadModelFromFile("models/spaceship.obj");
sphereModel = obj::loadModelFromFile("models/sphere.obj");
cubeModel = obj::loadModelFromFile("models/cube.obj");
sphereContext.initFromOBJ(sphereModel);
cubeContext.initFromOBJ(cubeModel);
//shipContext.initFromOBJ(shipModel);
shipTexture = Core::LoadTexture("textures/spaceship.png");
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
moonTexture = Core::LoadTexture("textures/moon.png");
skyboxTexture = loadCubemap(faces);
// The VBO containing the colors of the particles
glGenBuffers(1, &particles_color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
// Initialize with empty (NULL) buffer : it will be updated later, each frame.
glBufferData(GL_ARRAY_BUFFER, MaxParticles * 4 * sizeof(GLubyte), NULL, GL_STREAM_DRAW);
lastTime = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
}
void init_bloom()
{
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glGenTextures(2, colorBuffers);
for (unsigned int i = 0; i < 2; i++)
{
@ -481,6 +692,45 @@ void init()
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
}
}
void init()
{
glEnable(GL_DEPTH_TEST);
programTex = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
programSkybox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag");
programBlur = shaderLoader.CreateProgram("shaders/shader_blur.vert", "shaders/shader_blur.frag");
programBloom = shaderLoader.CreateProgram("shaders/shader_bloom.vert", "shaders/shader_bloom.frag");
programParticle = shaderLoader.CreateProgram("shaders/shader_particle.vert", "shaders/shader_particle.frag");
glUseProgram(programBlur);
glUniform1i(glGetUniformLocation(programBlur, "image"), 0);
glUseProgram(programBloom);
glUniform1i(glGetUniformLocation(programBloom, "scene"), 0);
glUniform1i(glGetUniformLocation(programBloom, "bloomBlur"), 1);
glUseProgram(0);
corvette = std::make_shared<Model>("models/Corvette-F3.obj");
crewmate = std::make_shared<Model>("models/space_humster.obj");
//shipModel = obj::loadModelFromFile("models/spaceship.obj");
sphereModel = obj::loadModelFromFile("models/sphere.obj");
cubeModel = obj::loadModelFromFile("models/cube.obj");
sphereContext.initFromOBJ(sphereModel);
cubeContext.initFromOBJ(cubeModel);
//shipContext.initFromOBJ(shipModel);
shipTexture = Core::LoadTexture("textures/spaceship.png");
sunTexture = Core::LoadTexture("textures/sun.png");
earthTexture = Core::LoadTexture("textures/earth2.png");
moonTexture = Core::LoadTexture("textures/moon.png");
particleTexture = Core::LoadTexture("textures/sun.png");
skyboxTexture = loadCubemap(faces);
init_particles();
init_bloom();
Light l1;
l1.position = sunPos;
@ -535,6 +785,8 @@ int main(int argc, char** argv)
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(SCR_WIDTH, SCR_HEIGHT);
//glutCreateWindow("GRK-PROJECT WIP");
@ -542,6 +794,7 @@ int main(int argc, char** argv)
glutFullScreen();
glewInit();
init();
glutKeyboardFunc(keyboard);
//to sprawia, że obiekty ukryte przed kamerą nie są renderowane