added working particle engine. needs improvement.

This commit is contained in:
Andreas Ortmann 2016-09-28 21:26:11 +02:00
parent a5ff1bc20d
commit 61a7f6f9ba
7 changed files with 457 additions and 201 deletions

View file

@ -13,12 +13,15 @@ set(GAME_SRC
renderer.cpp renderer.cpp
renderer_polygon_2d/renderer_polygon_2d.cpp renderer_polygon_2d/renderer_polygon_2d.cpp
renderer_polygon_3d/renderer_polygon_3d.cpp renderer_polygon_3d/renderer_polygon_3d.cpp
renderer_polygon_3d/polygon_model.cpp
renderer_polygon_3d/particle_batch.cpp
renderer_ray_tracer/renderer_ray_tracer.cpp renderer_ray_tracer/renderer_ray_tracer.cpp
network/session.cpp network/session.cpp
util.cpp util.cpp
game.cpp game.cpp
state/object.cpp state/object.cpp
state/explosion.cpp state/explosion.cpp
state/trace.cpp state/trace.cpp

View file

@ -6,7 +6,7 @@
#include <cmath> #include <cmath>
#define QUICK_TEST //#define QUICK_TEST
Game::Game() Game::Game()
{ {
@ -44,12 +44,12 @@ bool Game::cycle(float dt)
acc += dt; acc += dt;
total += dt; total += dt;
float spawnInterval = 1.0; float spawnInterval = 0.1;
while (acc > spawnInterval) { while (acc > spawnInterval) {
acc -= spawnInterval; acc -= spawnInterval;
float angle = 360.0 * util::randf_0_1(); float angle = 360.0 * util::randf_0_1();
float speed = 0.005; float speed = 0.01;
m_state->players.back()->addCommand(new game::SetSpeedCommand(speed)); m_state->players.back()->addCommand(new game::SetSpeedCommand(speed));
m_state->players.back()->addCommand(new game::ShootCommand(angle)); m_state->players.back()->addCommand(new game::ShootCommand(angle));

View file

@ -0,0 +1,181 @@
#include "particle_batch.hpp"
#include <iostream>
namespace endofthejedi {
ParticleBatch::ParticleBatch(size_t numParticles, float particleSize)
: m_numParticles(numParticles)
, m_particleRadius(particleSize)
{
std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl;
size_t s = m_numParticles*4;
m_data_velocity.resize(s);
m_data_position.resize(s);
m_data_kind.resize(s);
m_data_max_age.resize(s);
std::string vss_particles =
"#version 120\n"
"varying vec3 vertex;\n"
"uniform float time;\n"
"uniform float size;\n"
"uniform vec2 velocity;\n"
"void main()\n"
"{\n"
" vec3 p = size*gl_Vertex.xyz;\n"
//" vec3 p = gl_Vertex.xyz;\n"
" p.xy += velocity*time;\n"
" gl_Position = vec4(p, 1.0);\n"
" vertex = p;\n"
"}\n"
;
std::string fss_particles =
"#version 120\n"
"varying vec3 vertex;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
"}\n"
;
m_shader.init();
m_shader.load(vss_particles.c_str(), GL_VERTEX_SHADER);
m_shader.load(fss_particles.c_str(), GL_FRAGMENT_SHADER);
}
ParticleBatch::~ParticleBatch()
{
// TODO: find out if stuff must be deallocated
}
void ParticleBatch::setParticle(size_t index, const glm::vec2 &p, const glm::vec2 &v, float kind, float maxAge)
{
if (index >= m_numParticles) {
return;
}
//std::cout<<"[ParticleBatch] setParticle " << index << std::endl;
// 4 for use as tri / quad
const static glm::vec2 triangles[4] = {
glm::vec2( 1.0f, -1.0f),
glm::vec2( 1.0f, 1.0f),
glm::vec2(-1.0f, 1.0f),
glm::vec2(-1.0f, -1.0f),
};
for (size_t i=0; i<4; i++) {
const size_t data_index = 4*index+i;
m_data_position[data_index] = p + triangles[i];
m_data_velocity[data_index] = v;
m_data_kind[data_index] = kind;
m_data_max_age[data_index] = maxAge;
}
}
void ParticleBatch::bind()
{
//std::cout<<"[ParticleBatch] bind" << std::endl;
for (size_t i=0; i<4; i++) {
//std::cout<<"vbo #" << i << ": " << m_data_vbos[i] << std::endl;
glEnableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
// TODO: i or index? at first argument?
glVertexAttribPointer(
i,
dataSizeForIndex(i),
GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
0, // stride: no
NULL); // No offset
}
}
void ParticleBatch::upload()
{
std::cout<<"[ParticleBatch] upload" << std::endl;
glGenBuffers(4, m_data_vbos); // Generate buffer
for (size_t i=0; i<4; i++) {
glEnableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
// fill buffer with the loaded mesh position data
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
dataSizeForIndex(i) * 4 * m_numParticles * sizeof(float), // Buffer data size
dataSourceForIndex(i), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
}
}
void ParticleBatch::render()
{
//std::cout<<"[ParticleBatch] render " << std::endl;
m_shader.bind();
static float time = 0.0f;
time += 1.0/50.0;
glUniform1f(m_shader.location("time"), time);
glUniform1f(m_shader.location("size"), m_particleRadius);
glUniform2f(m_shader.location("velocity"), m_data_velocity[0].x, m_data_velocity[0].y);
bind();
glDrawArrays(GL_TRIANGLE_FAN, 0, 4*m_numParticles);
#if 0
glBegin(GL_QUADS);
for (size_t index=0; index<m_numParticles; index++) {
for (int i=0; i<4; i++) {
glm::vec2 p = m_data_position[index] + triangles[i];
//glColor3f(1.0, 0.0, 0.0);
glVertex2f(p.x, p.y);
}
}
glEnd();
#endif
}
size_t ParticleBatch::dataSizeForIndex(int i)
{
switch(i) {
case 0:
case 1:
return 2;
case 2:
case 3:
return 1;
default:
std::cerr << "bad" << std::endl;
return 0;
}
}
void *ParticleBatch::dataSourceForIndex(int i)
{
switch(i) {
case 0: return (void *) m_data_position.data();
case 1: return (void *) m_data_velocity.data();
case 2: return (void *) m_data_kind.data();
case 3: return (void *) m_data_max_age.data();
default:
std::cerr << "bad" << std::endl;
return nullptr;
}
}
}

View file

@ -0,0 +1,44 @@
#pragma once
#include <vector>
#include <glm/vec2.hpp>
#include "glclasses.hpp"
namespace endofthejedi {
class ParticleBatch {
public:
ParticleBatch(size_t numParticles, float particleRadius);
// deallocate opengl stuff on destroy
~ParticleBatch();
size_t numParticles() const { return m_numParticles; }
void setParticle(size_t index, const glm::vec2 &p, const glm::vec2 &v, float kind, float maxAge);
void bind();
void upload();
void render();
Shader *shader() { return &m_shader; }
private:
size_t dataSizeForIndex(int i);
void *dataSourceForIndex(int i);
private:
size_t m_numParticles;
GLuint m_data_vbos[4];
std::vector<glm::vec2> m_data_position;
std::vector<glm::vec2> m_data_velocity;
std::vector<float> m_data_kind;
std::vector<float> m_data_max_age;
float m_particleRadius;
Shader m_shader;
};
}

View file

@ -10,220 +10,221 @@
#include <epoxy/gl.h> #include <epoxy/gl.h>
#include <epoxy/glx.h> #include <epoxy/glx.h>
class PolygonModel { namespace endofthejedi {
public: class PolygonModel {
PolygonModel(const std::string &filename) : m_filename(filename) public:
{ PolygonModel(const std::string &filename) : m_filename(filename)
clear(); {
} clear();
void clear()
{
// TODO: delete buffers if there's data
m_loaded_from_file = false;
m_loaded_to_opengl = false;
m_data_position.clear();
m_data_normal.clear();
m_vbo_id_position = 0;
m_vbo_id_normal = 0;
m_numVertices = 0;
}
bool import()
{
clear();
// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(m_filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if (!scene) {
std::cout<<"[polygonmodel] loading file "
<< m_filename << " failed with: "
<< importer.GetErrorString() << std::endl;
return false;
} }
// Now we can access the file's contents. void clear()
copyVertices(scene); {
// TODO: delete buffers if there's data
m_loaded_from_file = true; m_loaded_from_file = false;
m_loaded_to_opengl = false;
// We're done. Everything will be cleaned up by the importer destructor m_data_position.clear();
return true; m_data_normal.clear();
}
bool uploadToOpenGl() m_vbo_id_position = 0;
{ m_vbo_id_normal = 0;
if (!m_loaded_from_file) { m_numVertices = 0;
std::cerr<<"[polygonmodel] warning: try to upload model data "
<< "to OpenGL but no data is loaded!" << std::endl;
exit(-1);
return false;
} }
glEnableVertexAttribArray(0); bool import()
{
clear();
glGenBuffers(1, &m_vbo_id_position); // Generate buffer // Create an instance of the Importer class
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); // Bind buffer Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(m_filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
glVertexAttribPointer(0, // If the import failed, report it
3, // three floats per vertex if (!scene) {
GL_FLOAT, // Data is floating point type std::cout<<"[polygonmodel] loading file "
GL_FALSE, // No fixed point scaling << m_filename << " failed with: "
0, // stride: no << importer.GetErrorString() << std::endl;
NULL); // No offset
// fill buffer with the loaded mesh position data
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
3 * m_numVertices * sizeof(float), // Buffer data size
m_data_position.data(), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
// TODO
#if 0
// 2 holds normal data
glEnableVertexAttribArray(2);
glGenBuffers(1, &(model->vbo_normal)); // Generate buffer
glBindBuffer(GL_ARRAY_BUFFER, model->vbo_normal); // Bind buffer
glVertexAttribPointer(2,
3, // three floats per normal
GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
0, // stride: no
NULL); // No offset
// Fill bound buffer
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
3*model->numVertices*sizeof(float), // Buffer data size
model->normals, // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
#endif
m_loaded_to_opengl = true;
return true;
}
bool bind()
{
// TODO: check whether this vbo is bound!
if (!m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to bind model vbo "
<< "which was not uploaded to OpenGL!" << std::endl;
exit(-1);
return false;
}
// bind position vbo
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
// TODO
#if 0
// bind normal vbo
GLuint normalLoc = glGetAttribLocation(program, "attr_normal");
glEnableVertexAttribArray(normalLoc);
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo_normal);
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
#endif
m_binding_active = true;
return true;
}
bool render()
{
if (!m_binding_active || !m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
exit(-1);
return false;
}
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
return true;
}
private:
bool copyVertices(const aiScene *scene)
{
if (scene->mMeshes == 0) {
std::cout << "[polygonmodel : no meshes loaded for " << m_filename << std::endl;
return false;
}
aiNode *node = scene->mRootNode;
const aiMesh* mesh = scene->mMeshes[node->mMeshes[0]];
// 3 vertices per face, 3 floats per vertex
m_numVertices = mesh->mNumFaces*3;
m_data_position.reserve(m_numVertices);
m_data_normal.reserve(m_numVertices);
size_t t, i;
for (t=0; t<mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
if (face->mNumIndices != 3) {
std::cout << "[polygonmodel] need triangles, got something different with: "
<< face->mNumIndices << " vertices" << std::endl;
return false; return false;
} }
for (i=0; i<face->mNumIndices; i++) { // Now we can access the file's contents.
const size_t index = face->mIndices[i]; copyVertices(scene);
m_data_position.push_back(mesh->mVertices[index].x);
m_data_position.push_back(mesh->mVertices[index].y);
m_data_position.push_back(mesh->mVertices[index].z);
m_data_normal.push_back(mesh->mNormals[index].x); m_loaded_from_file = true;
m_data_normal.push_back(mesh->mNormals[index].y);
m_data_normal.push_back(mesh->mNormals[index].z); // We're done. Everything will be cleaned up by the importer destructor
} return true;
} }
size_t totalBytes = 3*m_numVertices*sizeof(float); bool uploadToOpenGl()
std::cout<<"[polygonmodel] loaded file " << m_filename {
<< " with " << m_numVertices << " vertices (" if (!m_loaded_from_file) {
<< totalBytes << " bytes)" << std::endl; std::cerr<<"[polygonmodel] warning: try to upload model data "
<< "to OpenGL but no data is loaded!" << std::endl;
return true; exit(-1);
} return false;
}
const std::string &filename() const { return m_filename; } glEnableVertexAttribArray(0);
private: glGenBuffers(1, &m_vbo_id_position); // Generate buffer
std::string m_filename; glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); // Bind buffer
bool m_loaded_from_file;
bool m_loaded_to_opengl;
bool m_binding_active;
// both will hold 3 * numVertices floats glVertexAttribPointer(0,
std::vector<float> m_data_position; 3, // three floats per vertex
std::vector<float> m_data_normal; GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
0, // stride: no
NULL); // No offset
size_t m_numVertices; // fill buffer with the loaded mesh position data
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
3 * m_numVertices * sizeof(float), // Buffer data size
m_data_position.data(), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
GLuint m_vbo_id_position; // TODO
GLuint m_vbo_id_normal; #if 0
}; // 2 holds normal data
glEnableVertexAttribArray(2);
glGenBuffers(1, &(model->vbo_normal)); // Generate buffer
glBindBuffer(GL_ARRAY_BUFFER, model->vbo_normal); // Bind buffer
glVertexAttribPointer(2,
3, // three floats per normal
GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
0, // stride: no
NULL); // No offset
// Fill bound buffer
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
3*model->numVertices*sizeof(float), // Buffer data size
model->normals, // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
#endif
m_loaded_to_opengl = true;
return true;
}
bool bind()
{
// TODO: check whether this vbo is bound!
if (!m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to bind model vbo "
<< "which was not uploaded to OpenGL!" << std::endl;
exit(-1);
return false;
}
// bind position vbo
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
// TODO
#if 0
// bind normal vbo
GLuint normalLoc = glGetAttribLocation(program, "attr_normal");
glEnableVertexAttribArray(normalLoc);
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo_normal);
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
#endif
m_binding_active = true;
return true;
}
bool render()
{
if (!m_binding_active || !m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
exit(-1);
return false;
}
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
return true;
}
private:
bool copyVertices(const aiScene *scene)
{
if (scene->mMeshes == 0) {
std::cout << "[polygonmodel : no meshes loaded for " << m_filename << std::endl;
return false;
}
aiNode *node = scene->mRootNode;
const aiMesh* mesh = scene->mMeshes[node->mMeshes[0]];
// 3 vertices per face, 3 floats per vertex
m_numVertices = mesh->mNumFaces*3;
m_data_position.reserve(m_numVertices);
m_data_normal.reserve(m_numVertices);
size_t t, i;
for (t=0; t<mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
if (face->mNumIndices != 3) {
std::cout << "[polygonmodel] need triangles, got something different with: "
<< face->mNumIndices << " vertices" << std::endl;
return false;
}
for (i=0; i<face->mNumIndices; i++) {
const size_t index = face->mIndices[i];
m_data_position.push_back(mesh->mVertices[index].x);
m_data_position.push_back(mesh->mVertices[index].y);
m_data_position.push_back(mesh->mVertices[index].z);
m_data_normal.push_back(mesh->mNormals[index].x);
m_data_normal.push_back(mesh->mNormals[index].y);
m_data_normal.push_back(mesh->mNormals[index].z);
}
}
size_t totalBytes = 3*m_numVertices*sizeof(float);
std::cout<<"[polygonmodel] loaded file " << m_filename
<< " with " << m_numVertices << " vertices ("
<< totalBytes << " bytes)" << std::endl;
return true;
}
const std::string &filename() const { return m_filename; }
private:
std::string m_filename;
bool m_loaded_from_file;
bool m_loaded_to_opengl;
bool m_binding_active;
// both will hold 3 * numVertices floats
std::vector<float> m_data_position;
std::vector<float> m_data_normal;
size_t m_numVertices;
GLuint m_vbo_id_position;
GLuint m_vbo_id_normal;
};
}

View file

@ -6,8 +6,6 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/euler_angles.hpp> #include <glm/gtx/euler_angles.hpp>
#include "polygon_model.hpp"
namespace endofthejedi { namespace endofthejedi {
void RendererPolygon3d::setup() void RendererPolygon3d::setup()
{ {
@ -20,6 +18,21 @@ namespace endofthejedi {
addModel("../data/mesh/planet_12.stl", &m_planetModel); addModel("../data/mesh/planet_12.stl", &m_planetModel);
addModel("../data/mesh/ship.stl", &m_shipModel); addModel("../data/mesh/ship.stl", &m_shipModel);
size_t n = 10;
m_particles = new ParticleBatch(n, 0.01);
for (size_t i=0; i<n; i++) {
m_particles->setParticle(
i,
glm::vec2(i/(float) n, 0.0),
glm::vec2(1.0, 1.0),
0.0,
0.0);
}
m_particles->upload();
std::string vss_simple = std::string vss_simple =
"#version 120\n" "#version 120\n"
"varying vec3 vertex;\n" "varying vec3 vertex;\n"
@ -81,6 +94,8 @@ namespace endofthejedi {
void RendererPolygon3d::render(const game::State *state) void RendererPolygon3d::render(const game::State *state)
{ {
glClearColor(0.0, 0.0, 0.0, 1.0);
m_state = state; m_state = state;
m_shader.bind(); m_shader.bind();
@ -91,6 +106,8 @@ namespace endofthejedi {
renderShips(); renderShips();
renderMissiles(); renderMissiles();
//renderParticles();
//glColor3f(1.0, 0.0, 0.0); //glColor3f(1.0, 0.0, 0.0);
//glBegin(GL_QUADS); //glBegin(GL_QUADS);
//glVertex2f(-1.0f, -1.0f); //glVertex2f(-1.0f, -1.0f);
@ -100,6 +117,12 @@ namespace endofthejedi {
//glEnd(); //glEnd();
} }
void RendererPolygon3d::renderParticles()
{
//m_particles->bind();
m_particles->render();
}
void RendererPolygon3d::renderPlanets() void RendererPolygon3d::renderPlanets()
{ {
m_planetModel->bind(); m_planetModel->bind();

View file

@ -12,9 +12,10 @@
#include "state/ship.hpp" #include "state/ship.hpp"
#include "state/explosion.hpp" #include "state/explosion.hpp"
#include <glm/gtc/matrix_transform.hpp> #include "particle_batch.hpp"
#include "polygon_model.hpp"
class PolygonModel; #include <glm/gtc/matrix_transform.hpp>
namespace endofthejedi { namespace endofthejedi {
@ -27,6 +28,7 @@ namespace endofthejedi {
void renderPlanets(); void renderPlanets();
void renderMissiles(); void renderMissiles();
void renderShips(); void renderShips();
void renderParticles();
void addModel(const std::string &filename, PolygonModel **dest); void addModel(const std::string &filename, PolygonModel **dest);
@ -52,5 +54,7 @@ namespace endofthejedi {
// for accessing // for accessing
const game::State *m_state; const game::State *m_state;
ParticleBatch *m_particles;
}; };
} }