2016-09-28 09:50:35 +00:00
|
|
|
#include "renderer_polygon_3d.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-09-28 13:33:35 +00:00
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2016-09-28 13:10:57 +00:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2016-09-28 13:33:35 +00:00
|
|
|
#include <glm/gtx/euler_angles.hpp>
|
2016-09-30 19:24:06 +00:00
|
|
|
#include <glm/gtc/random.hpp>
|
2016-09-28 12:19:05 +00:00
|
|
|
|
2016-09-28 09:50:35 +00:00
|
|
|
namespace endofthejedi {
|
|
|
|
void RendererPolygon3d::setup()
|
|
|
|
{
|
2016-09-29 06:28:56 +00:00
|
|
|
m_lastTime = -1.0;
|
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
std::cout<<"setup polygon 3d" << std::endl;
|
2016-09-28 11:00:40 +00:00
|
|
|
|
2016-09-28 11:36:22 +00:00
|
|
|
m_shader.init();
|
2016-09-28 12:19:05 +00:00
|
|
|
|
|
|
|
#if 0
|
2016-09-29 10:28:35 +00:00
|
|
|
std::string vss_simple = "../data/shader/simple.vert";
|
|
|
|
std::string fss_simple = "../data/shader/simple.frag";
|
|
|
|
m_shader.loadFile(vss_simple, GL_VERTEX_SHADER);
|
|
|
|
m_shader.loadFile(fss_simple, GL_FRAGMENT_SHADER);
|
2016-09-28 12:19:05 +00:00
|
|
|
#else
|
2016-09-29 10:28:35 +00:00
|
|
|
std::string vss_game_objects = "../data/shader/gameobjects.vert";
|
|
|
|
std::string fss_game_objects = "../data/shader/gameobjects.frag";
|
|
|
|
m_shader.loadFile(vss_game_objects, GL_VERTEX_SHADER);
|
|
|
|
m_shader.loadFile(fss_game_objects, GL_FRAGMENT_SHADER);
|
2016-09-28 12:19:05 +00:00
|
|
|
#endif
|
2016-09-29 11:11:01 +00:00
|
|
|
|
|
|
|
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
2016-09-30 18:19:10 +00:00
|
|
|
addModel("../data/mesh/planet_12.stl", &m_planetModel);
|
2016-09-29 11:11:01 +00:00
|
|
|
addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::render(const game::State *state)
|
|
|
|
{
|
2016-09-29 06:28:56 +00:00
|
|
|
if (m_lastTime == -1.0) {
|
|
|
|
m_lastTime = state->timestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
float dt = state->timestamp() - m_lastTime;
|
|
|
|
if (dt < 0.0) {
|
|
|
|
dt = 0.0;
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
m_state = state;
|
2016-09-29 06:28:56 +00:00
|
|
|
advanceGraphicObjects(dt);
|
|
|
|
|
2016-09-29 09:38:26 +00:00
|
|
|
// TODO: add stars (texture)
|
|
|
|
// TODO: add dust particles
|
|
|
|
// TODO: add little rocks flying around
|
|
|
|
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
|
2016-09-28 11:36:22 +00:00
|
|
|
m_shader.bind();
|
|
|
|
|
2016-09-30 19:24:06 +00:00
|
|
|
// TODO: add ONE sun planet
|
|
|
|
// TODO: add lights for explosions
|
|
|
|
|
|
|
|
glm::vec3 c = glm::vec3(1.0, 1.0, 0.8);
|
2016-09-29 09:38:26 +00:00
|
|
|
glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z);
|
|
|
|
|
2016-09-30 19:24:06 +00:00
|
|
|
glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f);
|
2016-09-29 09:38:26 +00:00
|
|
|
glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z);
|
|
|
|
|
2016-09-28 22:52:58 +00:00
|
|
|
renderPlanets();
|
|
|
|
renderShips();
|
|
|
|
renderMissiles();
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-30 18:19:10 +00:00
|
|
|
renderParticles();
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-09-29 07:36:57 +00:00
|
|
|
renderTraces();
|
|
|
|
|
2016-09-28 11:38:31 +00:00
|
|
|
//glColor3f(1.0, 0.0, 0.0);
|
|
|
|
//glBegin(GL_QUADS);
|
|
|
|
//glVertex2f(-1.0f, -1.0f);
|
|
|
|
//glVertex2f(1.0f, -1.0f);
|
|
|
|
//glVertex2f(1.0f, 1.0f);
|
|
|
|
//glVertex2f(-1.0f, 1.0f);
|
|
|
|
//glEnd();
|
2016-09-29 06:28:56 +00:00
|
|
|
|
|
|
|
m_lastTime = state->timestamp();
|
2016-09-28 12:19:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
void RendererPolygon3d::renderParticles()
|
|
|
|
{
|
2016-09-29 06:28:56 +00:00
|
|
|
for (ParticleBatch *batch : m_particles) {
|
|
|
|
batch->bind();
|
|
|
|
batch->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 18:19:10 +00:00
|
|
|
void RendererPolygon3d::addExplosionEffect(size_t id, const glm::vec2 &pos, size_t n, float duration)
|
2016-09-29 06:28:56 +00:00
|
|
|
{
|
2016-09-30 18:19:10 +00:00
|
|
|
//float particleRadius = 0.005;
|
2016-09-30 19:24:06 +00:00
|
|
|
//float particleRadius = 0.003;
|
2016-09-30 20:04:14 +00:00
|
|
|
float particleRadius = 0.007;
|
2016-09-30 19:24:06 +00:00
|
|
|
|
|
|
|
// TODO: use this as shader input too and make the area 2x around this
|
|
|
|
// so that it stays hot/yellow for 2/3 of the time
|
|
|
|
float explSize = 0.01f;
|
|
|
|
|
|
|
|
float maxVelocity = 0.2f;
|
2016-09-30 18:19:10 +00:00
|
|
|
|
|
|
|
ParticleBatch *batch = new ParticleBatch(id, n, particleRadius, duration);
|
2016-09-30 19:24:06 +00:00
|
|
|
batch->setCenter(glm::vec3(pos, 0.0));
|
|
|
|
batch->setMaxVelocity(maxVelocity);
|
2016-09-29 06:28:56 +00:00
|
|
|
|
|
|
|
for (size_t i=0; i<n; i++) {
|
|
|
|
// distribute in a circle
|
2016-09-30 19:24:06 +00:00
|
|
|
//float t = 2.0 * M_PI * i / (float) n;
|
2016-09-30 18:19:10 +00:00
|
|
|
//t += 0.2*util::randf_m1_1();
|
2016-09-29 06:28:56 +00:00
|
|
|
|
|
|
|
// with random velocities
|
2016-09-30 19:24:06 +00:00
|
|
|
// this is 3d because it looks better if some particles leave/enter
|
|
|
|
// the space and they are not all on one plane.
|
|
|
|
// especially in 3d this would look bad without 3d velocity vector.
|
|
|
|
//glm::vec3 v = 0.5f*glm::vec3(sin(t), cos(t), util::randf_m1_1());
|
|
|
|
|
|
|
|
glm::vec3 v = glm::ballRand(maxVelocity);
|
2016-09-30 20:04:14 +00:00
|
|
|
v *= util::randf_0_1() * util::randf_0_1() * util::randf_0_1();
|
2016-09-29 06:28:56 +00:00
|
|
|
|
2016-09-30 19:24:06 +00:00
|
|
|
batch->setParticle(i, glm::vec3(pos, 0.0) + glm::ballRand(explSize), v);
|
2016-09-29 06:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batch->upload();
|
|
|
|
|
|
|
|
m_particles.push_back(batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::advanceGraphicObjects(float dt)
|
|
|
|
{
|
2016-09-29 07:05:35 +00:00
|
|
|
for (const game::Explosion *expl : m_state->explosions) {
|
2016-09-30 18:19:10 +00:00
|
|
|
bool gotIt = false;
|
|
|
|
for (ParticleBatch *batch : m_particles) {
|
|
|
|
if (batch->id() == expl->id) {
|
|
|
|
gotIt = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotIt) {
|
2016-09-30 20:04:14 +00:00
|
|
|
addExplosionEffect(expl->id, expl->position, 1000, 2.0);
|
2016-09-29 07:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 19:24:06 +00:00
|
|
|
if (m_particles.size() == 0) {
|
2016-09-30 20:04:14 +00:00
|
|
|
addExplosionEffect(0, glm::vec2(0.0, 0.0), 1000, 2.0);
|
2016-09-30 19:24:06 +00:00
|
|
|
}
|
2016-09-30 18:19:10 +00:00
|
|
|
|
2016-09-29 07:05:35 +00:00
|
|
|
std::vector<ParticleBatch*> rm;
|
|
|
|
|
2016-09-29 06:28:56 +00:00
|
|
|
for (ParticleBatch *batch : m_particles) {
|
|
|
|
batch->tick(dt);
|
|
|
|
if (batch->done()) {
|
2016-09-29 07:05:35 +00:00
|
|
|
rm.push_back(batch);
|
2016-09-29 06:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 07:05:35 +00:00
|
|
|
|
|
|
|
for (ParticleBatch *batch : rm) {
|
|
|
|
m_particles.remove(batch);
|
|
|
|
delete(batch);
|
|
|
|
}
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderPlanets()
|
2016-09-28 12:19:05 +00:00
|
|
|
{
|
2016-09-29 11:11:01 +00:00
|
|
|
m_planetModel->bind();
|
2016-09-28 12:19:05 +00:00
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Planet *planet : m_state->planets) {
|
|
|
|
glm::mat4 model = computeModelMatrix(planet);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
2016-09-29 00:50:51 +00:00
|
|
|
glm::vec3 c = planet->getColor();
|
2016-09-29 09:38:26 +00:00
|
|
|
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
2016-09-29 00:50:51 +00:00
|
|
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
2016-09-28 12:19:05 +00:00
|
|
|
|
2016-09-29 11:11:01 +00:00
|
|
|
m_planetModel->render();
|
2016-09-28 12:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderMissiles()
|
2016-09-28 12:19:05 +00:00
|
|
|
{
|
2016-09-29 11:11:01 +00:00
|
|
|
m_missileModel->bind();
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Player *player : m_state->players) {
|
2016-09-28 12:41:15 +00:00
|
|
|
for (const game::Missile *missile : player->missiles) {
|
|
|
|
glm::vec3 c = glm::vec3(1.0, 1.0, 0.3);
|
2016-09-29 09:38:26 +00:00
|
|
|
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
2016-09-28 13:10:57 +00:00
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
glm::mat4 model = computeModelMatrix(missile);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-29 11:11:01 +00:00
|
|
|
m_missileModel->render();
|
2016-09-28 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
void RendererPolygon3d::renderShips()
|
2016-09-28 12:41:15 +00:00
|
|
|
{
|
2016-09-29 11:11:01 +00:00
|
|
|
m_shipModel->bind();
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
for (const game::Ship *ship : m_state->ships) {
|
|
|
|
glm::mat4 model = computeModelMatrix(ship);
|
2016-09-28 13:22:16 +00:00
|
|
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
|
|
|
|
2016-09-28 14:12:25 +00:00
|
|
|
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
|
2016-09-29 09:38:26 +00:00
|
|
|
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
2016-09-28 12:41:15 +00:00
|
|
|
|
2016-09-29 11:11:01 +00:00
|
|
|
m_shipModel->render();
|
2016-09-28 12:41:15 +00:00
|
|
|
}
|
2016-09-28 12:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RendererPolygon3d::addModel(const std::string &filename, PolygonModel **dest)
|
|
|
|
{
|
2016-09-28 13:10:57 +00:00
|
|
|
//std::cout<<"adding a model: " << filename << std::endl;
|
2016-09-28 12:19:05 +00:00
|
|
|
|
|
|
|
*dest = new PolygonModel(filename);
|
|
|
|
if (!(*dest)->import()) {
|
|
|
|
std::cout<<"error: failed to load needed model!!!" << std::endl << std::endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:11:01 +00:00
|
|
|
(*dest)->setup(&m_shader);
|
2016-09-28 12:19:05 +00:00
|
|
|
(*dest)->uploadToOpenGl();
|
2016-09-28 11:16:11 +00:00
|
|
|
|
2016-09-28 12:19:05 +00:00
|
|
|
m_models.push_back(*dest);
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|
2016-09-28 14:12:25 +00:00
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Planet *planet)
|
|
|
|
{
|
|
|
|
return computeModelMatrix(planet->position, planet->radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Missile *missile)
|
|
|
|
{
|
|
|
|
glm::vec2 vn = glm::normalize(missile->velocity);
|
|
|
|
float a = std::atan2(vn.y, vn.x);
|
|
|
|
|
|
|
|
// TODO: which visual size has the rocket? in game its just a point with
|
|
|
|
// no size because all others have size.
|
2016-09-29 07:05:35 +00:00
|
|
|
return computeModelMatrix(missile->position, 0.03f, a);
|
2016-09-28 14:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Ship *ship)
|
|
|
|
{
|
|
|
|
// TODO: rotate them before shooting, that looks better
|
2016-09-29 07:05:35 +00:00
|
|
|
glm::mat4 mat = computeModelMatrix(ship->position, m_state->shipRadius());
|
2016-09-29 06:47:32 +00:00
|
|
|
// XXX model is flipped
|
2016-09-29 07:05:35 +00:00
|
|
|
//glm::mat4 mat = computeModelMatrix(ship->position, 0.3);
|
2016-09-29 06:47:32 +00:00
|
|
|
mat = glm::rotate(mat, (float) M_PI, glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
return mat;
|
2016-09-28 14:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 RendererPolygon3d::computeModelMatrix(const glm::vec2 &pos, float scale, float angle)
|
|
|
|
{
|
|
|
|
// init as identity matrix
|
|
|
|
glm::mat4 model;
|
|
|
|
|
|
|
|
model = glm::translate(model, glm::vec3(pos, 0.0));
|
|
|
|
|
|
|
|
if (scale != 1.0) {
|
|
|
|
model = glm::scale(model, glm::vec3(scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (angle != 0.0) {
|
|
|
|
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
2016-09-29 07:16:34 +00:00
|
|
|
|
|
|
|
void RendererPolygon3d::renderTraces()
|
|
|
|
{
|
|
|
|
// revert to default
|
|
|
|
glUseProgram(0);
|
|
|
|
|
2016-09-30 18:19:10 +00:00
|
|
|
// TODO dont use line mode. make that with own quads
|
|
|
|
glPolygonMode(GL_FRONT, GL_LINE);
|
2016-09-29 07:16:34 +00:00
|
|
|
for (const game::Trace *trace : m_state->traces) {
|
2016-09-29 07:32:56 +00:00
|
|
|
float fade_out = 1.0;
|
|
|
|
if (trace->missile == nullptr) {
|
|
|
|
fade_out = 1.0 - (trace->age / trace->maxAge);
|
|
|
|
}
|
|
|
|
|
2016-09-30 20:04:14 +00:00
|
|
|
glColor3f(0.0, 0.5*fade_out, 0.5*fade_out);
|
2016-09-29 07:16:34 +00:00
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
for (const game::Trace::TracePoint &tp : trace->points) {
|
|
|
|
glVertex2f(tp.position.x, tp.position.y);
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
2016-09-30 18:19:10 +00:00
|
|
|
glPolygonMode(GL_FRONT, GL_FILL);
|
2016-09-29 07:16:34 +00:00
|
|
|
}
|
2016-09-28 09:50:35 +00:00
|
|
|
}
|